Detailed explanation of master-slave configuration tutorial of redis under Docker
1. Pull redis image
Docker pull redis
2. Start 3 redis container services, using ports 6379, 6380 and 6381, respectively.
Docker run-- name redis-6379-p 6379 d redisdocker run-- name redis-6380-p 6380 name redis-6380-d redisdocker run-- name redis-6381-p 6381 name redis-6380-dredis
3. View the container
[tcy@tcy1 ~] $docker ps-aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESa9fa77adc598 daocloud.io/library/redis "docker-entrypoint.s 2 hours ago Up 2 hours 0.0.0.0 docker-entrypoint.s 6381-> 6379/tcp redis-6381 6ee2f2f007e6 daocloud.io/library/redis" docker-entrypoint.s 2 hours ago Up 2 hours 0.0.0.0 docker-entrypoint.s 6380-> 6379/tcp redis-6380 ab54741166e1 daocloud.io/library/redis "docker-entrypoint.s 3 hours ago Up 3 hours 0.0.0.06379/tcp redis-6379 6379-> 6379/tcp redis-6379
4. Test container, successful
Docker exec-it ab54741166e1 redis-cli: enter the container [root@tcy1 tcy] # docker exec-it ab54741166e1 redis-cli127.0.0.1:6379 > set b tcyOK127.0.0.1:6379 > get b "tcy" 127.0.0.1 root@tcy1 tcy 6379 > quit [root@tcy1 tcy] #
5. Start redis cluster configuration
5.1. see the ip address of the private network of the container
[root@tcy1 tcy] # docker inspect a9fa77adc598
The private network ip addresses of the three redis are:
Redis-6379:172.17.0.1:6379redis-6380:172.17.0.2:6379redis-6381:172.17.0.3:6379
5.2. enter the docker container to view the current redis role (master or slave)
[root@tcy1 tcy] # docker exec-it ab54741166e1 / bin/bashroot@ab54741166e1:/data# redis-cli127.0.0.1:6379 > info replication# Replicationrole:masterconnected_slaves:0master_replid:d43d1ae8cde6cb084220e18b926aba79e0bb2504master_replid2:0000000000000000000000000000000000000000master_repl_offset:0second_repl_offset:-1repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:0repl_backlog_histlen:0127.0.0.1:6379 > quitroot@ab54741166e1:/data# exit exit
Currently, all three are in master status.
5.3.Use the redis-cli command to modify the host of redis-6380 and redis-6381 to 172.17.0.1
[root@tcy1 tcy] # docker exec-it a9fa77adc598 / bin/bash / / redis-6380root@a9fa77adc598:/data# redis-cli127.0.0.1:6379 > SLAVEOF 172.17.0.1 6379OK127.0.0.1:6379 > quitroot@a9fa77adc598:/data# exitexit [root@tcy1 tcy] # docker exec-it 6ee2f2f007e6 / bin/bash / / redis-6381root@6ee2f2f007e6:/data# redis-cli127.0.0.1:6379 > SLAVEOF 172.17.0.1 6379OK127.0.0.1:6379 > quit
Check whether redis-6379 already has 2 slaves, connected_slaves:2, yes
[root@tcy1 tcy] # docker exec-it ab54741166e1 / bin/bashroot@ab54741166e1:/data# redis-cli127.0.0.1:6379 > info replication# Replicationrole:masterconnected_slaves:2slave0:ip=172.17.0.3,port=6379,state=online,offset=378,lag=1slave1:ip=172.17.0.2,port=6379,state=online,offset=378 Lag=0master_replid:ce193b15cfd57f7dc3ccfbf2a4aef6156b131e6dmaster_replid2:0000000000000000000000000000000000000000master_repl_offset:378second_repl_offset:-1repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:1repl_backlog_histlen:378127.0.0.1:6379 > quitroot@ab54741166e1:/data# exitexit
5.5. configure Sentinel Sentinel
Go inside the three redis containers for configuration, and create sentinel.conf files in the container root directory
The content of the file is: sentinel monitor mymaster 172.17.0.1 6379 1
[root@tcy1 tcy] # docker exec-it a9fa77adc598 / bin/bashroot@a9fa77adc598:/data# cd / & & touch sentinel.conf root@a9fa77adc598:/# vim / sentinel.conf
If: bash: vim: command not found appears
Solution: 1. Apt-get update 2, apt-get install vim
Finally, activate the Redis Sentinel:
Root@a9fa77adc598:/# redis-sentinel / sentinel.conf342:X 24 Jun 11:37:58.934 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo342:X 24 Jun 11:37:58.957 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=342 Just started342:X 24 Jun 11:37:58.958 # Configuration loaded342:X 24 Jun 11:37:58.959 # You requested maxclients of 10000 requiring at least 10032 max file descriptors.342:X 24 Jun 11:37:58.959 # Server can't set maximum open files to 10032 because of OS error: Operation not permitted.342:X 24 Jun 11:37:58.960 # Current maximum open files is 4096. Maxclients has been reduced to 4064 to compensate for low ulimit. If you need higher maxclients increase 'ulimit-nasty. _. _.-`. `_. '' -. _ Redis 4.0.10 (000000Universe 0) 64 bit.-``.- ```. ```\ / _., _'-. _ (.-`|` ) Running in sentinel mode | `-. _`...-.`` -. _ |'`_. -'| Port: 26379 |` -. _ `. _ / _. -'| PID: 342` -. _` -. _ `. / _. -'_ -'|` -. _ `-. _`. -'| | `-. _` -. -'_. -'| http://redis.io `-. _ _. -'. -'|` -. _ `-. _. -'_. | |` -. . _. -'_. -'| `-. _. -'` -. _ `-. -'_. -'. -` -. _. -'. 342cannot be enforced because / proc/sys/net/core/somaxconn is set to the lower value of 128.342 cannot be enforced because / proc/sys/net/core/somaxconn is set to the lower value of 128.342 The TCP backlog setting of X 24 Jun 11 The TCP backlog setting of 37RV 59.089 # Sentinel ID is dfd5a5bfe1036b1df3395c4ba858329034fc5b7e342:X 24 Jun 11V 37RV 59.091 # + monitor master mymaster 172.17.0.1 6379 quorum 134The TCP backlog setting of X 24 Jun 1115 * + slave slave 172.17.0.3: 6379 172.17.0.3 6379 @ mymaster 172.17.0.1 6379342 slave slave 172.17.0.2 6379 @ mymaster 172.17.0.1 6379342 Jun 24 Jun 1139 Jun 27.601 * + sentinel sentinel ba9b0d0539d8273edfcbd922fe138f50daa78bbb 172.17.0.2 26379 @ mymaster 172.17.0.1 6379342 QR 24 Jun 111V 59.144 * + sentinel sentinel f0510f8582b72c056531f219397ed8826683e665 172.17.0.1 26379 @ mymaster 172.17.0.1 26379 @ mymaster
Easy to observe and open multiple windows.
Sentinel Sentinel configuration complete
5.6. Testing
Close Master
[tcy@tcy1 ~] $docker stop ab54741166e1ab54741166e1
At this time, the remaining two slaves will automatically elect a new host, here 172.17.0.2 will be elected as the host.
Look at 172.17.0.2 and become the host.
[root@tcy1 /] # docker exec-it 6ee2f2f007e6 / bin/bashroot@6ee2f2f007e6:/data# redis-cli127.0.0.1:6379 > info replication# Replicationrole:masterconnected_slaves:1slave0:ip=172.17.0.3,port=6379,state=online,offset=66906,lag=1master_replid:5a7489c8181ddf0d73d418d30d6a4c8e039198bamaster_replid2:ce193b15cfd57f7dc3ccfbf2a4aef6156b131e6dmaster_repl_offset:67041second_repl_offset:65534repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:85repl_backlog_histlen:66957127.0.0.1:6379 >
Summary
The above is the master and slave configuration of redis under Docker introduced by the editor. I hope it will be helpful to you. If you have any questions, you are welcome to leave a message. The editor will reply you in time!