If you were asked to build sentinel and cluster redis for your development and test environments, how fast would it take you? Maybe a day? 2 hours? The truth is it can be shorter. Yes, you guessed it, with Docker deployment, it really only takes a couple of minutes.
One. Preparation
Pull the Redis image
Run the following command:
docker pull redis
Copy the code
The image pulled by this command is the official image, of course you can search for other images, not to go into depth here
View the mirror status:
Deploy redis Sentry master-slave mode
What is Sentinel mode? — Please do your own search
1, What is Docker compose?
Docker Compose can be understood as a way to solidify the operation and configuration of multiple containers!
To take the simplest example, if we want to prepare a MySQL container and a Redis container for our application container, we need to start MySQL container and Redis container before running the application container at each startup. Don’t forget to connect the container network to the MySQL container and Redis container when creating the application container so that the application can connect to them and exchange data.
This is not enough. If we do a lot of configuration on the container, we’d better save the container creation and configuration commands so we can use them next time.
In this case, we have to introduce Docker Compose, which is the most commonly used multi-container definition and runtime software in our development.
2. Compile reids master-slave docker-comemage.yml
version: '3.7'
services:
master:
image: redis
container_name: redis-master
restart: always
command: redis-server --requirepass redispwd --appendonly yes
ports:
- 6379: 6379
volumes:
- ./data1:/data
slave1:
image: redis
container_name: redis-slave-1
restart: always
command: redis-server --slaveof redis-master 6379 --requirepass redispwd --masterauth redispwd --appendonly yes
ports:
- 6380: 6379
volumes:
- ./data2:/data
slave2:
image: redis
container_name: redis-slave-2
restart: always
command: redis-server --slaveof redis-master 6379 --requirepass redispwd --masterauth redispwd --appendonly yes
ports:
- 6381: 6379
volumes:
- ./data3:/data
Copy the code
Noun explanation:
3. Start primary/secondary Redis
Go to the docker-comemess. yml directory corresponding to redis and run the following command:
docker-compose up -d
Copy the code
-d indicates background running
Run the docker ps command to view the startup result:If a screenshot is displayed, the command is successfully executed
4. Write sentry docker-comemage.yml
version: '3.7'
services:
sentinel1:
image: redis
container_name: redis-sentinel-1
restart: always
ports:
- 26379: 26379
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
volumes:
- ./sentinel1.conf:/usr/local/etc/redis/sentinel.conf
sentinel2:
image: redis
container_name: redis-sentinel-2
restart: always
ports:
- 26380: 26379
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
volumes:
- ./sentinel2.conf:/usr/local/etc/redis/sentinel.conf
sentinel3:
image: redis
container_name: redis-sentinel-3
ports:
- 26381: 26379
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
volumes:
- ./sentinel3.conf:/usr/local/etc/redis/sentinel.conf
networks:
default:
external:
name: redis_default
Copy the code
5. Write sentinel sentinel.conf
172.19.0.3 = redis-master IP address, 6379 = redis-master port Port 26379 dir/TMP Sentinel Monitor myMaster 172.19.0.3 6379 2 Sentinel down-after-milliseconds mymaster 30000 sentinel parallel-syncs mymaster 1 sentinel auth-pass mymaster redispwd sentinel failover-timeout mymaster 180000 sentinel deny-scripts-reconfig yesCopy the code
Make three copies of the above files, and name them sentinel1.conf, Sentinel2.conf, and Sentinel3. conf to match the configuration files in docker-comemess. yml, and place them in the same directory as the sentinels docker-comemess. yml
6. Activate sentry
Go to the directory where the sentry docker-comemage. yml is located and execute the following command:
docker-compose up -d
Copy the code
Looking at the container, you can see that both the sentry and the master/slave Redis are up
6.1 Sentinel Startup Logs
As you can see from the above log, the sentry listens on the master and slave nodes
6.2 Shutting down the Master Node
Stop the master node of Redis by running the command
docker stop redis-master
Copy the code
From the above log, we can see sdown, odown, what do they mean?
Sdown is a subjective downtimeIf a sentry thinks a master is down, it’s subjective down
Odown is an objective breakdownIf the sentinels of the quorum quantity feel that a master is down, then it is an objective outage
Then the election starts. From the log, it can be seen that the two sentinels choose the same slave node. At this time, the minimum number of votes is met, and the slave is elected as the new master.
6.3 Restarting the Master Node
The above log indicates that the sentry has detected a reboot of the old master and has turned the old master into a slave of the new master
Deploy the Redis cluster mode
1. Create directories and files
├ ─ ─ docker - compose. Yml ├ ─ ─ redis - 6371 │ ├ ─ ─ the conf │ │ └ ─ ─ redis. Conf │ └ ─ ─ data ├ ─ ─ redis - 6372 │ ├ ─ ─ the conf │ │ └ ─ ─ Redis. Conf │ └ ─ ─ data ├ ─ ─ redis - 6373 │ ├ ─ ─ the conf │ │ └ ─ ─ redis. Conf │ └ ─ ─ data ├ ─ ─ redis - 6374 │ ├ ─ ─ the conf │ │ └ ─ ─ Redis. Conf │ └ ─ ─ data ├ ─ ─ redis - 6375 │ ├ ─ ─ the conf │ │ └ ─ ─ redis. Conf │ └ ─ ─ data └ ─ ─ redis - 6376 ├ ─ ─ the conf │ └ ─ ─ redis. Conf └ ─ ─ the dataCopy the code
Conf configuration file
port 6371 cluster-enabled yes cluster-config-file nodes-6371.conf cluster-node-timeout 5000 appendonly yes Protected-mode no requirepass 1234 masterauth 1234 cluster-announce-ip 10.12.12.10 # cluster-announce-bus-port 16371Copy the code
The configuration of each node requires only a change of port.
Docker-compose configuration file
Redis-6371: image: redis-6371: container_name: Redis-6371 # redis-6371 # redis-6371 # data volume, directory mount - / redis - 6371 / conf/redis. Conf: / usr/local/etc/redis/redis conf -. / data/redis - 6371: / data ports: - 6371:6371 - 16371:16371 command: redis-server /usr/local/etc/redis/redis.conf redis-6372: image: redis container_name: redis-6372 volumes: - ./redis-6372/conf/redis.conf:/usr/local/etc/redis/redis.conf - ./redis-6372/data:/data ports: - 6372:6372 - 16372:16372 command: redis-server /usr/local/etc/redis/redis.conf redis-6373: image: redis container_name: redis-6373 volumes: - ./redis-6373/conf/redis.conf:/usr/local/etc/redis/redis.conf - ./redis-6373/data:/data ports: - 6373:6373 - 16373:16373 command: redis-server /usr/local/etc/redis/redis.conf redis-6374: image: redis container_name: redis-6374 restart: always volumes: - ./redis-6374/conf/redis.conf:/usr/local/etc/redis/redis.conf - ./redis-6374/data:/data ports: - 6374:6374 - 16374:16374 command: redis-server /usr/local/etc/redis/redis.conf redis-6375: image: redis container_name: redis-6375 volumes: - ./redis-6375/conf/redis.conf:/usr/local/etc/redis/redis.conf - ./redis-6375/data:/data ports: - 6375:6375 - 16375:16375 command: redis-server /usr/local/etc/redis/redis.conf redis-6376: image: redis container_name: redis-6376 volumes: - ./redis-6376/conf/redis.conf:/usr/local/etc/redis/redis.conf - ./redis-6376/data:/data ports: - 6376:6376 - 16376:16376 command: redis-server /usr/local/etc/redis/redis.confCopy the code
Docker-compose up -d: docker-compose up -d: docker-compose up -d: docker-compose up -d: docker-compose up
4. Go to the container and create a cluster
Only six instances of Redis were started, not clustered. Run docker exec it redis-6371 bash to enter a Redis node container, any of which will do. Proceed with the following command to create the cluster:
Redis -cli -a 1234 --cluster create 10.35.30.39:6371 10.35.30.39:6372 10.35.30.39:6373 10.35.30.39:6374 10.35.30.39:6375 10.35.30.39:6376 --cluster-replicas 1 # Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. >>> Performing hash slots allocation on 6 nodes... Master[0] -> Slots 0 - 5460 Master[1] -> Slots 5461 - 10922 Master[2] -> Slots 10923 - 16383 Adding replica 10.35.30.39:6375 to 10.35.30.39:6371 Adding Replica 10.35.30.39:6376 to 10.35.30.39:6372 Adding Replica 10.35.30.39:6374 To 10.35.30.39:6373 >>> Trying to optimize Slaves allocation for anti-affinity [WARNING] Some slaves are in the same The host as their master M: e9a35d6a9d203830556de89f06a3be2e2ab4eee1 10.35.30.39:6371 slots: [0-5460] (5461 slots) master M: 0 c8755144fe6a200a46716371495b04f8ab9d4c8 10.35.30.39:6372 slots: [5461-10922] (5462 slots) master M: Fcb83b0097d2a0a87a76c0d782de12147bc86291 10.35.30.39:6373 slots: [10923-16383] (5461 slots) master S: B9819797e98fcd49f263cec1f77563537709bcb8 10.35.30.39:6374 replicates fcb83b0097d2a0a87a76c0d782de12147bc86291 S: F4660f264f12786d81bcf0b18bc7287947ec8a1b 10.35.30.39:6375 replicates e9a35d6a9d203830556de89f06a3be2e2ab4eee1 S: D2b9f265ef7dbb4a612275def57a9cc24eb2fd5d 10.35.30.39:6376 replicates 0 c8755144fe6a200a46716371495b04f8ab9d4c8 Can I set the above configuration? (type 'yes' to accept): >>> Nodes Configuration updated >>> Assign a different config epoch to each node >>> Sending CLUSTER MEET messages to join the cluster Waiting for the cluster to join . >>> Performing Cluster Check (using the node 10.35.30.39:6371) M: E9a35d6a9d203830556de89f06a3be2e2ab4eee1 10.35.30.39:6371 slots: [0-5460] (5461 slots) master 1 additional up (s) M: 0 c8755144fe6a200a46716371495b04f8ab9d4c8 10.35.30.39:6372 slots: [5461-10922] (5462 slots) master 1 additional up (s) S: b9819797e98fcd49f263cec1f77563537709bcb8 10.35.30.39:6374 slots: (0 slots) slave replicates fcb83b0097d2a0a87a76c0d782de12147bc86291 M: Fcb83b0097d2a0a87a76c0d782de12147bc86291 10.35.30.39:6373 slots: [10923-16383] (5461 slots) 1 additional master Up (s) s: f4660f264f12786d81bcf0b18bc7287947ec8a1b 10.35.30.39:6375 slots: (0 slots) slave replicates e9a35d6a9d203830556de89f06a3be2e2ab4eee1 S: D2b9f265ef7dbb4a612275def57a9cc24eb2fd5d 10.35.30.39:6376 slots: (0 slots) slave replicates 0c8755144fe6a200a46716371495b04f8ab9d4c8 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.Copy the code
If the preceding output is displayed, the Cluster configuration is complete. There are 3 master and 3 slave.
Conclusion:
The above is the whole process of deploying sentry mode and cluster mode through Docker compose mode. Redis is deployed in Docker, applicable to local, development, testing and other environments. Please be careful to use it in production environment, unless you have strong control over Docker.