前言Docker下redis的主从配置_lzy_yuan的博客-CSDN博客_docker redis 主从配置
本节主要实践redis集群分类重点如下两个
- 主从复制 Replication
- 高可用 Sentinel
理论知识讲解在:Redis集群配置
Docker redis 集群搭建 1、拉取redis镜像[tcy@tcy1 ~]$ docker pull latest2、启动3个redis容器服务,分别使用到6379、6380、6381端口
docker run --name redis-6379 -p 6379:6379 -d redis docker run --name redis-6380 -p 6380:6379 -d redis docker run --name redis-6381 -p 6381:6379 -d redis3、查看容器
[root@iZuf64res1ona9ad2ddsejZ /]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b2cd780565ea redis "docker-entrypoint.s…" 6 minutes ago Up 6 minutes 0.0.0.0:6381->6379/tcp, :::6381->6379/tcp redis-6381 c77bb120d46b redis "docker-entrypoint.s…" 6 minutes ago Up 6 minutes 0.0.0.0:6380->6379/tcp, :::6380->6379/tcp redis-6380 e686c631435d redis "docker-entrypoint.s…" 6 minutes ago Up 6 minutes 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp redis-63794、测试容器,成功
docker exec -it redis-6379 redis-cli:进入容器
[root@tcy1 tcy]# docker exec -it redis-6379 redis-cli 127.0.0.1:6379> set my 123 OK 127.0.0.1:6379> get my "123" 127.0.0.1:6379> hset demo my 123 OK 127.0.0.1:6379> hget demo my "123" 127.0.0.1:6379> quit [root@tcy1 tcy]#5、开始redis集群配置 5.1、看容器内网的ip地址
[root@tcy1 tcy]# docker inspect redis-6379 | grep IPAddress "SecondaryIPAddresses": null, "IPAddress": "172.17.0.2", "IPAddress": "172.17.0.2",
3个redis的内网ip地址为:
redis-6379:172.17.0.2:6379 redis-6380:172.17.0.3:6379 redis-6381:172.17.0.4:63795.2、进入docker容器内部,查看当前redis角色(主还是从)
docker exec -it redis-6379 redis-cli info replication [root@iZuf64res1ona9ad2ddsejZ /]# docker exec -it redis-6379 redis-cli 127.0.0.1:6379> info replication # Replication role:master connected_slaves:0 master_failover_state:no-failover master_replid:6344cee02a6be76f645801653e90e37f6a3e707b master_replid2:0000000000000000000000000000000000000000 master_repl_offset:0 second_repl_offset:-1 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0 127.0.0.1:6379> 127.0.0.1:6379> exit exit
目前三个都是master状态(即主人状态)
5.3、使用redis-cli命令修改redis-6380、redis-6381的主机为172.17.0.1:6379[root@iZuf64res1ona9ad2ddsejZ /]# docker exec -it redis-6380 redis-cli 127.0.0.1:6379> SLAVEOF 172.17.0.2 6379 OK 127.0.0.1:6379> exit exit [root@iZuf64res1ona9ad2ddsejZ /]# docker exec -it redis-6381 redis-cli 127.0.0.1:6379> slaveof 172.17.0.2 6379 OK 127.0.0.1:6379> exit exit5.4、查看主从配置是否成功
查看redis-6379是否已经拥有2个从机,connected_slaves:2,是的
[root@iZuf64res1ona9ad2ddsejZ /]# docker exec -it redis-6379 redis-cli 127.0.0.1:6379> info replication # Replication role:master connected_slaves:2 slave0:ip=172.17.0.3,port=6379,state=online,offset=154,lag=1 slave1:ip=172.17.0.4,port=6379,state=online,offset=154,lag=0 master_failover_state:no-failover master_replid:97cf8fd7f54da1653a173900b032ae3becfdceac master_replid2:0000000000000000000000000000000000000000 master_repl_offset:154 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:154 127.0.0.1:6379> exit exitSentinel哨兵集群配置
这里将在三个docker上各配置一个sentinel哨兵,以实现哨兵的高可用。当然也可以只实现一个或者两个哨兵。
1.哨兵配置文件进入3台redis容器内部进行配置,在容器根目录里面创建sentinel.conf文件(注意,这里我三台都配建立一个sentinel集群)
文件内容为如下,表示监听master 172.17.0.1 6379端口,当master出问题时,只需要一个sentinel发现就可以切换master(mymaster为命名,可任意)。
sentinel monitor mymaster 172.17.0.1 6379 1 [root@iZuf64res1ona9ad2ddsejZ /]# docker exec -it redis-6379 bash root@e686c631435d:/data# cd / && touch sentinel.conf root@e686c631435d:# vim /sentinel.conf
如果出现:bash: vim: command not found
解决:
apt-get update apt-get install vim
解决之后请记得写入内容
2.启动Redis哨兵三个都需要启动,启动后自动构建为三个集群
root@a9fa77adc598:/# redis-sentinel /sentinel.conf 485:X 01 Jan 2022 11:11:04.231 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 485:X 01 Jan 2022 11:11:04.231 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=485, just started 485:X 01 Jan 2022 11:11:04.231 # Configuration loaded 485:X 01 Jan 2022 11:11:04.231 * monotonic clock: POSIX clock_gettime _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 6.2.6 (00000000/0) 64 bit .-`` .-```. ```/ _.,_ ''-._ ( ' , .-` | `, ) Running in sentinel mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 26379 | `-._ `._ / _.-' | PID: 485 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | https://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 485:X 01 Jan 2022 11:11:04.232 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 485:X 01 Jan 2022 11:11:04.238 # Sentinel ID is e11bad57828dbb4dcbd2864d9fe3cb1c98660cc1 485:X 01 Jan 2022 11:11:04.238 # +monitor master mymaster 172.17.0.1 6379 quorum 1 485:X 01 Jan 2022 11:11:04.239 * +slave slave 172.17.0.3:6379 172.17.0.3 6379 @ mymaster 172.17.0.1 6379 485:X 01 Jan 2022 11:11:04.245 * +slave slave 172.17.0.4:6379 172.17.0.4 6379 @ mymaster 172.17.0.1 6379
Sentinel哨兵配置完毕。
3.测试关闭Master
[tcy@tcy1 ~]$ docker stop redis-6379 redis-6379
这时,剩余的2个从机,会自动选举产生新的主机,这里选举172.17.0.2为主机。
+switch-master mymaster 172.17.0.1 6379 172.17.0.3 6379 662:X 01 Jan 2022 11:15:14.992 # +new-epoch 1 662:X 01 Jan 2022 11:15:14.995 # +vote-for-leader e11bad57828dbb4dcbd2864d9fe3cb1c98660cc1 1 662:X 01 Jan 2022 11:15:15.833 # +config-update-from sentinel e11bad57828dbb4dcbd2864d9fe3cb1c98660cc1 172.17.0.2 26379 @ mymaster 172.17.0.1 6379 662:X 01 Jan 2022 11:15:15.833 # +switch-master mymaster 172.17.0.1 6379 172.17.0.3 6379 662:X 01 Jan 2022 11:15:15.833 * +slave slave 172.17.0.1:6379 172.17.0.1 6379 @ mymaster 172.17.0.3 6379 662:X 01 Jan 2022 11:15:21.961 * +slave slave 172.17.0.2:6379 172.17.0.2 6379 @ mymaster 172.17.0.3 6379 662:X 01 Jan 2022 11:15:21.965 * +slave slave 172.17.0.4:6379 172.17.0.4 6379 @ mymaster 172.17.0.3 6379 662:X 01 Jan 2022 11:16:02.505 # +sdown slave 172.17.0.1:6379 172.17.0.1 6379 @ mymaster 172.17.0.3 6379 662:X 01 Jan 2022 11:16:02.505 # +sdown slave 172.17.0.2:6379 172.17.0.2 6379 @ mymaster 172.17.0.3 6379 662:X 01 Jan 2022 11:16:02.596 # +sdown sentinel e11bad57828dbb4dcbd2864d9fe3cb1c98660cc1 172.17.0.2 26379 @ mymaster 172.17.0.3 6379
查看172.17.0.2,变成了主机。
[root@iZuf64res1ona9ad2ddsejZ ~]# docker exec -it redis-6380 redis-cli 127.0.0.1:6379> info replication # Replication role:master connected_slaves:1 slave0:ip=172.17.0.4,port=6379,state=online,offset=55609,lag=1 master_failover_state:no-failover master_replid:61f98261fd6306c390ebe5c9c5d3e2fee4fbb66d master_replid2:97cf8fd7f54da1653a173900b032ae3becfdceac master_repl_offset:55758 second_repl_offset:23959 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:55758 127.0.0.1:6379>
重启redis-6379查看发现redis-6379为redis-6380的从
[root@iZuf64res1ona9ad2ddsejZ ~]# docker start redis-6379 redis-6379 [root@iZuf64res1ona9ad2ddsejZ ~]# docker exec -it redis-6379 redis-cli 127.0.0.1:6379> info replication # Replication role:slave master_host:172.17.0.3 master_port:6379 master_link_status:up master_last_io_seconds_ago:1 master_sync_in_progress:0 slave_read_repl_offset:63006 slave_repl_offset:63006 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:61f98261fd6306c390ebe5c9c5d3e2fee4fbb66d master_replid2:0000000000000000000000000000000000000000 master_repl_offset:63006 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:62025 repl_backlog_histlen:982 127.0.0.1:6379>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)