"If you want to do a good job, you must first sharpen your tools." If you want to get the docker+redis sentinel mode (one master, two slaves and three sentries) within one minute, the following method is fast, fast, and time-saving:
Note: This article mainly explains one master, two slaves and three sentries, which are specially deployed on one machine and distinguished by ports (the production environment should be deployed on three or more machines).
1. Preparatory work requires the following environment or corresponding adapted version environment:
※Centos version 7.9
※GNU Make 3.82 version
※gcc 4.8.5 version
※Docker 20.10.18 version
※sentinel.conf file
download method:
wget https://download.redis.io/redis-stable/sentinel.conf
※The docker-compose version 1.29.2
technology update is faster, and it is faster to use docker-compose.
Installation method:
Command 1: curl -L https://get.daocloud.io/docker/compose/releases/download/1.29.2/docker-compose- uname -s
- uname -m
> /usr/local/bin/docker-compose
Command 2: chmod +x /usr/local/bin/docker-compose
2. Deployment (one minute)
1. Obtain redis image
installation command: docker pull redis:5.0.14
2 Create a directory
Command 1: mkdir /usr/local/etc/redis
Command 2: mkdir /usr/local/etc/redis/sentinel
3 In the /usr/local/etc/redis directory, create a new docker-compose.yml file
version: '3'
services:
master:
image: redis
container_name: redis-master
command: redis-server
ports:
- 6379:6379
slave1:
image: redis
container_name: redis-slave1
ports:
- 6380:6380
command: redis-server --slaveof redis-master 6379
slave2:
image: redis
container_name: redis-slave2
ports:
- 6381:6381
command: redis-server --slaveof redis-master 6379
4 Start the redis cluster
Run the command in the ./redis directory:
Command: docker-compose up -d
5 Check the docker-ip and network name of the redis-master node, see the figure below, and remember to use it when configuring the docker-compose file of sentinel.
Command: docker inspect redis-master
6 In the /usr/local/etc/redis/sentinel directory, create a new docker-compose.yml file
version: '3'
services:
sentinel1:
image: redis
container_name: redis-sentinel1
ports:
- 26379:26379
command: redis-sentinel /usr/local/etc/redis/sentinel/sentinel1.conf
volumes:
- ./sentinel1.conf:/usr/local/etc/redis/sentinel/sentinel1.conf
sentinel2:
image: redis
container_name: redis-sentinel2
ports:
- 26380:26380
command: redis-sentinel /usr/local/etc/redis/sentinel/sentinel2.conf
volumes:
- ./sentinel2.conf:/usr/local/etc/redis/sentinel/sentinel2.conf
sentinel3:
image: redis
container_name: redis-sentinel3
ports:
- 26381:26381
command: redis-sentinel /usr/local/etc/redis/sentinel/sentinel3.conf
volumes:
- ./sentinel3.conf:/usr/local/etc/redis/sentinel/sentinel3.conf
networks:
default:
external:
name: redis_default
7 Copy the sentinel.conf file to /usr/local/etc/redis/sentinel, and make three copies, named sentinel1.conf, sentinel2.conf, sentinel3.conf, and modify the three files respectively:
The modified content is as follows:
Modify the sentinel1.conf
file and modify the content as follows:
protected-mode no
daemonize yes
port 26379 #sentinel port
sentinel monitor mymaster 172.18.0.3 6379 2 #Note: 172.18.0.3 is the dockerIP of redis-master
Then modify the sentinel2.conf information as follows:
protected-mode no
daemonize yes
port 26380 # Sentinel port, because we are on a virtual machine, so the port should be different
sentinel monitor mymaster 172.18.0.3 6379 2 #Note: 172.18.0.3 is redis -master's dockerIP
Then modify sentinel3.conf information as follows:
protected-mode no
daemonize yes
port 26381 # Sentinel port, because we are on a virtual machine, so the port should be different
dir "/var/llib/redis"
sentinel monitor mymaster 172.18.0.3 6379 2 #Note: 172.18.0.3 is the dockerIP of redis-master
8 Start the sentinel cluster in the ./sentinel directory
Command: docker-compose up -d
After these 8 steps, the redis one-master, two-slave and three-sentry mode under docker is completed.
3. Verification method
1 Verify whether it is installed
*Detect container command: docker ps
to see if there are the following 6 containers.
*Login host command:
docker exec -it redis-master /bin/bash
redis-cli
info Replication
The following screen appears, which proves that the redis host is installed successfully.
*Login slave redis-slave1 and redis-slave2 commands:
*Login Sentinel command:
docker exec -it redis-sentinel1 /bin/bash
redis-cli -p 26379
info sentinel
as shown in the figure below, the Sentinel installation is successful.
*View sentinel sentinel log:
command: docker logs -f redis-sentinel1
as shown in the figure below, which also proves that the sentinel is installed successfully
2 1Verify master-slave switching
*Stop the host redis-master service, you can directly shut down the container.
Command: docker stop redis-master
*Login to the slave 1 (slave1)
command:
docker exec -it redis-slave1 /bin/bash
redis-cli
info Replication
finds that the slave has become the master, as shown below: