This article was first published on the wechat public account “My Little bowl of soup”. You can follow it by scanning the QR code at the end of the article. Welcome to communicate with us!

During the containerization process of Redis, the POD instance CPU, memory and maxmemory value of Redis instance are lengthwise expanded, and the pod managed by StatefulSet needs to be restarted. So put the redis cluster status check into the health check, relying on statefulSet’s native capabilities (restart the next one when the POD instance is ready, When ready, the EndPoints controller updates the POD information to the EndPoints resource object, without writing logic in the Redis operator.

You need to run the redis-cli -h {REDis instance IP} ping command to check whether Redis is normal. Run the redis-cli -c -h {redis instance IP} -a {redis password} cluster info command to check whether the ClusterState value is OK and the ClusterKnown_nodes value is 1. Determine whether the Redis cluster is normal;

If the Redis cluster is newly created, ClusterKnownNodes is 1 and Cluster_state is fail.

If the Redis cluster is restarted for vertical expansion (CPU and memory expansion), the cluster is normal only when the Value of ClusterKnownNodes is not 1 and the value of Cluster_state is OK.

Because it involves string equality judgment, we use the following judgment:

if [ "$cluster_known_nodes"x = "1"x  ]; then
.....
fiCopy the code

If you add x to the end of $a, you will add a to the beginning of the overwriting formula. The result is that the judgment result is not equal.Redirects the result of the redis-cli -c -h {redis instance IP} -a {redis password} cluster info execution to a file.

Vi 1.txt File, in vi use the set ff command to check the file format Unix, but each line of the file has a special character of ^M, that is the problem.

The most important thing is that you can’t tell by cat that special characters exist.

Manually delete the ^M special characters.

^M is a newline rn for windows-formatted text files, which can be converted to Unix using the dos2UNIX command. But executing cluster info is all done in Linux, and redirecting the set ff command to a file is also seen in Unix format. Again, this is hard to understand.

Sed ‘/^M//g’ did not work, so sed’? r?? G ‘substitution, the final script is as follows.

The [[]] of the if statement needs to be executed using bash. The [[: not found

#! Redis -cli -h {redis instance IP} -a {redis password} cluster info Check whether the value of cluster_state is OK and the value of Cluster_known_nodes is 1 to determine whether the Redis cluster is normal. If the Redis cluster is newly created, Cluster_Known_nodes # is 1 and cluster_state is fail. If the redis cluster is being upgraded, the cluster can be restarted only when the value of cluster_known_nodes is not 1 and the value of Cluster_state is OK. Pingres =$(redis-cli -h $(hostname) ping) # Cluster_state: OK # cluster_slots_assigned:16384 # cluster_slots_ok:16384 # cluster_slots_pfail:0 # cluster_slots_fail:0 # cluster_known_nodes:6 # cluster_size:3 # cluster_current_epoch:15 # cluster_my_epoch:12 # cluster_stats_messages_sent:270782059 # cluster_stats_messages_received:270732696 pingres=$(echo "${pingres}" | sed 's? \r?? g') if [[ "$pingres"x = "PONG"x ]]; then clusterinfo=$(redis-cli -c -h ${PODIP} cluster info) # redis-cli -c -h ${PODIP} cluster info output info include ^M(win \n\r) char lead to error, so use sed 's? \r?? g' clusterknownnodes=$(echo "${clusterinfo}" | grep cluster_known_nodes | sed 's? \r?? g' | awk -F ':' '{print $2}') clusterstate=$(echo "${clusterinfo}" | grep cluster_state | sed 's? \r?? g' | awk -F ':' '{print $2}') echo "clusterknownnodes: ${clusterknownnodes} --- clusterstate: ${clusterState}" # [[need run this script use /bin/bash instead of /bin/sh # if [[]] not found if [[ "${clusterknownnodes}"x = "1"x && "${clusterstate}"x = "ok"x ]];  then echo "--1--" exit 0 elif [[ "${clusterknownnodes}"x ! = "1"x && "${clusterstate}"x = "ok"x ]];  then echo "--2--" exit 0 # create redis cluster elif [[ "${clusterknownnodes}"x = "1"x && "${clusterstate}"x ! = "ok"x ]]; then echo "--3--" exit 0 elif [[ "${clusterknownnodes}"x != "1"x && "${clusterstate}"x ! = "ok"x ]]; then echo "--4--" exit 1 else echo "--5--" exit 1 fi else exit 1 fiCopy the code

This kind of weird problem is usually caused by special characters in the script, which can be displayed in the script set list. Of course Windows edited scripts run on Linux in general dos2UNIX test.sh conversion is best, avoid trouble.

Reference:

Specific USES Linux shell bracket of the if condition judgment more https://www.cnblogs.com/jjzd/p/6397495.html

Run a shell script times wrong [[: “not found” solution to https://www.cnblogs.com/han-1034683568/p/7211392.html

This public number provides CSDN download service for free, massive IT learning resources, if you are ready to enter the IT pit, inspirational to become an excellent program monkey, then these resources are suitable for you, including but not limited to Java, GO, Python, SpringCloud, ELK, embedded, big data, interview materials, front-end resources. At the same time, we have set up a technical exchange group, in which there are many big wigs who will share technical articles from time to time. If you want to learn and improve together, you can reply [2] on the background of the public account. We invite the technical exchange group to learn and improve each other for free, and we will share programming IT related resources from time to time.

Scan code attention, wonderful content for the first time to push to you