docker install redis7-sentinel mode

illustrate

System: CentOS7.9

redis:7.0.5

Due to resource issues, this deployment is all completed on one host by starting different docker containers.

Before setting up the sentinel mode, first set up the master-slave mode, 1 master and 2 slaves. You can refer to the previous article: docker installation redis7-master-slave mode . After setting up the master-slave mode, we started to deploy sentinels. Three sentinels were deployed here, with ports 26379, 26380, and 26381. You can also refer to the instructions on the official website: Redis achieves high availability through sentry

Modify the master-slave configuration

The passwords of the three nodes must be consistent

Master node configuration (the subsequent master node may become a slave node)

vim /root/data/redis-master/conf/redis.conf

Add the following content

masterauth <配置的requirepass的值>

Configuration from node

Copy the configuration of the master node to the mount path of the slave node

cp /root/data/redis-master/conf/redis.conf /root/data/redis-slave1/conf/redis.conf
cp /root/data/redis-master/conf/redis.conf /root/data/redis-slave2/conf/redis.conf

 Modify slave node 1 configuration

vim /root/data/redis-slave1/conf/redis.conf

Add the following configuration

replicaof 192.168.66.114 6379
# 对外宣称自己的ip
replica-announce-ip 192.168.66.114
# 对外宣称自己的端口
replica-announce-port 6380

Modify slave node 2 configuration

vim /root/data/redis-slave2/conf/redis.conf

Add the following configuration

replicaof 192.168.66.114 6379
# 对外宣称自己的ip
replica-announce-ip 192.168.66.114
# 对外宣称自己的端口
replica-announce-port 6381

Restart the master and slave nodes

docker restart redis-master
docker restart redis-slave1
docker restart redis-slave2

  Check the master node information to ensure that there is no problem with the master and slave nodes.

docker exec -it redis-master redis-cli
auth <your password>
info replication

Create mount directory 

The mounting paths of the three sentinel nodes use ports to distinguish different configuration files and log files.

Create folder

# 哨兵1
mkdir -p /root/data/redis-sentinel/26379/conf
mkdir -p /root/data/redis-sentinel/26379/data
# 哨兵2
mkdir -p /root/data/redis-sentinel/26380/conf
mkdir -p /root/data/redis-sentinel/26380/data
# 哨兵3
mkdir -p /root/data/redis-sentinel/26381/conf
mkdir -p /root/data/redis-sentinel/26381/data

The created structure is as follows

 The configuration files and data file mounting paths of the three Sentinels are as follows:

# 哨兵1
/root/data/redis-sentinel/26379/data
/root/data/redis-sentinel/26379/data/sentinel.log
/root/data/redis-sentinel/26379/conf/sentinel.conf

# 哨兵2                        
/root/data/redis-sentinel/26380/data/sentinel
/root/data/redis-sentinel/26380/data/sentinel.log
/root/data/redis-sentinel/26380/conf/sentinel.conf

# 哨兵3                        
/root/data/redis-sentinel/26381/data/sentinel
/root/data/redis-sentinel/26381/data/sentinel.log
/root/data/redis-sentinel/26381/conf/sentinel.conf

 Sentinel node configuration

common arrangement

bind 0.0.0.0
#端口
port 26379
#关闭保护模式
protected-mode no
#使用容器启动时,关闭后台启动,否则容器启动后就会关闭
daemonize no
#日志文件
logfile /data/sentinel.log
#守护进程pid存储文件
pidfile /var/run/redis-sentinel26379.pid
#指定要监控的主机IP地址和端口,redis-master:主节点的名称,名称可以自定义
#最后的值代表最少几个哨兵客观认可主节点下线才同意故障迁移
sentinel monitor redis-master 192.168.66.114 6379 2
#如果redis主节点设了requirepass,需要提供访问密码
sentinel auth-pass redis-master <主节点的密码>
#主机多少秒无响应,则认为挂了,为了测试暂时设置为30秒
sentinel down-after-milliseconds redis-master 30000
#主备切换时,最多有多少个slave同时对新的master进行同步,这里设置为默认的1
sentinel parallel-syncs redis-master 1
#故障转移的超时时间,这里设置为三分钟
sentinel failover-timeout redis-master 180000
#sentinel工作目录
dir /data

 Sentinel 1 configuration

vim /root/data/redis-sentinel/26379/conf/sentinel.conf

Add the following content on the basis of general configuration

# 添加通用配置内容以及追加以下内容
#如果使用docker进行了端口映射,需要配置如下内容
# 哨兵节点向其他节点宣布自己的 IP 地址
sentinel announce-ip 192.168.66.114
# 哨兵节点向其他节点宣布自己的端口号
sentinel announce-port 26379

Sentinel 2 configuration

vim /root/data/redis-sentinel/26380/conf/sentinel.conf

Add the following content on the basis of general configuration

# 添加通用配置内容,并追加以下内容
#如果使用docker进行了端口映射,需要配置如下内容
# 哨兵节点向其他节点宣布自己的 IP 地址
sentinel announce-ip 192.168.66.114
# 哨兵节点向其他节点宣布自己的端口号
sentinel announce-port 26380

Sentinel 3 configuration

vim /root/data/redis-sentinel/26381/conf/sentinel.conf

Add the following content on the basis of general configuration

# 添加通用配置内容以及追加以下内容
#如果使用docker进行了端口映射,需要配置如下内容
# 哨兵节点向其他节点宣布自己的 IP 地址
sentinel announce-ip 192.168.66.114
# 哨兵节点向其他节点宣布自己的端口号
sentinel announce-port 26381

Complete directory structure

Start all sentinel nodes

Start Sentinel 1

docker run -p 26379:26379 \
--privileged=true \
-v /root/data/redis-sentinel/26379/data:/data \
-v /root/data/redis-sentinel/26379/conf:/usr/local/etc/redis/ \
--name redis-sentinel1 \
--restart=always \
-d redis:7.0.5 \
redis-sentinel /usr/local/etc/redis/sentinel.conf

View log

tail -1000f /root/data/redis-sentinel/26379/data/sentinel.log

 Start Sentinel 2

docker run -p 26380:26379 \
--privileged=true \
-v /root/data/redis-sentinel/26380/data:/data \
-v /root/data/redis-sentinel/26380/conf:/usr/local/etc/redis/ \
--name redis-sentinel2 \
--restart=always \
-d redis:7.0.5 \
redis-sentinel /usr/local/etc/redis/sentinel.conf

View log

tail -1000f /root/data/redis-sentinel/26380/data/sentinel.log

 Start Sentinel 3

docker run -p 26381:26379 \
--privileged=true \
-v /root/data/redis-sentinel/26381/data:/data \
-v /root/data/redis-sentinel/26381/conf:/usr/local/etc/redis/ \
--name redis-sentinel3 \
--restart=always \
-d redis:7.0.5 \
redis-sentinel /usr/local/etc/redis/sentinel.conf

View log

tail -1000f /root/data/redis-sentinel/26381/data/sentinel.log

 View sentinel node information

The container of all nodes has port 26379 inside, so you can enter the corresponding container to view information only by modifying the container name

# docker exec -it <container-name> redis-cli -p 26379
docker exec -it redis-sentinel1 redis-cli -p 26379
# docker exec -it redis-sentinel2 redis-cli -p 26379
# docker exec -it redis-sentinel3 redis-cli -p 26379
# 查看容器信息
sentinel master redis-master

Disaster recovery verification

 Close host point

Simulate the scene where the master hangs up, and wait for 30 seconds after executing the following command

docker stop redis-master
# 以上命令执行后等待30秒

After 30 seconds, Sentinel monitors and detects that the master node has died, and starts a new election to elect the slave node with port 6380 as the master node (each election is random and may not necessarily be the node with port 6380).

 Restart the redis-master node

docker start redis-master
# 金融容器内部当前节点信息
docker exec -it redis-master redis-cli
auth 123123
info replication

The role of the current node becomes slave

 View the information of the redis-slave1 node, the role has changed to master

 View redis-slave2 node information

 data verification

After the master node hangs up and restarts, you can verify whether the data is lost. Only one node is verified here, and other nodes verify by themselves

 final effect

Start 6 docker containers on a host, 1 master, 2 slaves and 3 sentinels

 Problems encountered in deployment

WARNING: Sentinel was not able to save the new configuration on disk!!!: Device or resource busy

solution:

挂载配置文件的时候使用目录挂载映射,不要直接挂载文件

WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128

solution:

# 在所有从节点的配置文件中增加以下配置
tcp-backlog 511

sentinel-invalid-addr sentinel 8ca6429840821d38651cb8bb64268dd1856541eb 192.168.66.114 6379 @ redis-master 192.168.66.114 6379

Solution:
1. When using docker to start a container, if the port is exposed to the outside world, you need to declare your own IP and port in the sentinel configuration file and add the following configuration:

# 哨兵节点向其他节点宣布自己的 IP 地址
sentinel announce-ip 192.168.66.114
# 哨兵节点向其他节点宣布自己的端口号
sentinel announce-port 26381

2. After each sentry is started, the following content will be written in its own configuration file. It is possible that the myid of a certain node is duplicated. Stop the corresponding sentinel node, delete the myid, and restart the sentinel node.

 Conclusion

The above is the process for deploying Sentinel mode. If you have any questions, please comment.

Guess you like

Origin blog.csdn.net/LSW_JAVADP/article/details/132360308
Recommended