The target

Before I install redis cluster through Docker, according to the online tutorial step by step to install, I have also written my successful installation case, if necessary, please see below, but although Docker simplifies some tedious steps, but inevitably there are still some steps need to write their own manual command, So I wrote a shell script, self-help through a command to complete the deployment of redis cluster

Docker Compose deploys a Redis Cluster

One-click Installation of the Redis cluster

1 Write shell scripts

  • Create the install.sh script file
#! /bin/bash

#Create the Redis mount directoryEcho "step 1 - > create redis installation position -- -- -- -- -- -" the mkdir -p/usr/local/docker/volumes/redis - cluster CD / usr/local/docker/volumes/redis - cluster echo "step 2 - > create redis - cluster. TMPL template -- -- -- -- -- -" cat < < > 'EOF' redis - cluster. TMPL port ${PORT} protected-mode no cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 cluster-announce-ip ${CLUSTER_ANNOUNCE_IP} cluster-announce-port ${PORT} cluster-announce-bus-port 1${PORT} EOF
#for port in `seq 6391 6396`; do \
#firewall-cmd --zone=public --add-port=${port}/tcp --permanent
#doneEcho "step 3 -> Create the redis data configuration mount directory ------" CLUSTER_ANNOUNCE_IP=127.0.0.1 echo ${CLUSTER_ANNOUNCE_IP} for port in 'seq 6391 6396 `; do \ mkdir -p ./${port}/conf \ && PORT=${port} CLUSTER_ANNOUNCE_IP=${CLUSTER_ANNOUNCE_IP} envsubst < ./redis-cluster.tmpl > ./${port}/conf/redis.conf \ && mkdir -p ./${port}/data; ------" cat <<EOF > docker-compose. Yaml version: '3' services: redis-6391: image: redis:latest container_name: redis-6391 command: redis-server /usr/local/etc/redis/redis.conf --appendonly yes privileged: true restart: always volumes: - /usr/local/docker/volumes/redis-cluster/6391/data:/data - /usr/local/docker/volumes/redis-cluster/6391/conf/redis.conf:/usr/local/etc/redis/redis.conf ports: - 6391:6391 - 16391:16391 redis-6392: image: redis:latest container_name: redis-6392 command: redis-server /usr/local/etc/redis/redis.conf --appendonly yes privileged: true restart: always volumes: - /usr/local/docker/volumes/redis-cluster/6392/data:/data - /usr/local/docker/volumes/redis-cluster/6392/conf/redis.conf:/usr/local/etc/redis/redis.conf ports: - 6392:6392 - 16392:16392 redis-6393: image: redis:latest container_name: redis-6393 command: redis-server /usr/local/etc/redis/redis.conf --appendonly yes privileged: true restart: always volumes: - /usr/local/docker/volumes/redis-cluster/6393/data:/data - /usr/local/docker/volumes/redis-cluster/6393/conf/redis.conf:/usr/local/etc/redis/redis.conf ports: - 6393:6393 - 16393:16393 redis-6394: image: redis:latest container_name: redis-6394 command: redis-server /usr/local/etc/redis/redis.conf --appendonly yes privileged: true restart: always volumes: - /usr/local/docker/volumes/redis-cluster/6394/data:/data - /usr/local/docker/volumes/redis-cluster/6394/conf/redis.conf:/usr/local/etc/redis/redis.conf ports: - 6394:6394 - 16394:16394 redis-6395: image: redis:latest container_name: redis-6395 command: redis-server /usr/local/etc/redis/redis.conf --appendonly yes privileged: true restart: always volumes: - /usr/local/docker/volumes/redis-cluster/6395/data:/data - /usr/local/docker/volumes/redis-cluster/6395/conf/redis.conf:/usr/local/etc/redis/redis.conf ports: - 6395:6395 - 16395:16395 redis-6396: image: redis:latest container_name: redis-6396 command: redis-server /usr/local/etc/redis/redis.conf --appendonly yes privileged: true restart: always volumes: - /usr/local/docker/volumes/redis-cluster/6396/data:/data - /usr/local/docker/volumes/redis-cluster/6396/conf/redis.conf:/usr/local/etc/redis/redis.conf ports: -6396:6396:16396 EOF echo "docker-compose redis generated successfully." Docker-compose = docker-compose = docker-compose >#Run Docker-compose to start the Redis containerdocker-compose -f docker-compose.yaml up -d exist=$(docker inspect --format '{{.State.Running}}' redis-6391) if [[${exist}!='true']]; Then sleep 3000 else echo 'redis' ' IP_RESULT="" CONTAINER_IP="" for port in `seq 6391 6396`; do #CONTAINER_IP=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' redis-${port}) IP_RESULT=${IP_RESULT}${CLUSTER_ANNOUNCE_IP}":"${port}" "done fi echo" Obtain the IP address and port number of the Redis container: "${IP_RESULT} echo "step 6 -> redis execute cluster command ------" docker run --rm it inem0o/redis-trib create --replicas 1 ${IP_RESULT}Copy the code

CLUSTER_ANNOUNCE_IP: Enables ports 6391 to 6396 and 16391 to 16396 to access your public IP address before executing the script

2 The following scripts are for cluster testing and redeployment

  • New delete_redis_cluster. Sh
#! /bin/bash
for port in `seq 6391 6396`; do
docker stop "redis-"${port}
sleep 1
docker rm "redis-"${port}
done
Copy the code
  • show_cluster_info.sh
#! /bin/bash
docker exec -it redis-6391 redis-cli -p 6391 -a 123456
Copy the code

After you run show_cluster_info.sh, type Cluster Nodes or cluster info

The lazy mode

If you find the above steps a bit cumbersome you can download the code I uploaded to Github

redis-cluster

conclusion

Programmer nature is lazy, my goal is to free hands, click on the installation of success, or not happy ~