docker redis cluster 集群

随笔

VM 启动三个虚拟机ip分别为
192.168.247.133 

192.168.247.134

192.168.247.135

每台虚拟机分别部署两个redis容器实例 分别使用 6381 6382 端口

# 三台机器分别配置配置文件
# 6381 端口配置文件
vim /home/redis-cluster/6381/redis.conf
port 6381
protected-mode no
pidfile /var/run/redis_6381.pid
appendonly yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
requirepass 123456
masterauth 123456
# 6382 端口配置文件
vim /home/redis-cluster/6382/redis.conf
port 6382
protected-mode no
pidfile /var/run/redis_6382.pid
appendonly yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
requirepass 123456
masterauth 123456

开启防火墙端口

# 三台机器都需要开发端口
# 防火墙放行 6381 16381 6382 16382 端口
firewall-cmd --add-port 6381/tcp
firewall-cmd --add-port 16381/tcp
firewall-cmd --add-port 6382/tcp
firewall-cmd --add-port 16382/tcp

启动实例

# --net host 官网有提到
# 使用docker分别启动容器实例 三台机器都需要启动容器
# 启动 6381 实例
docker run -v /home/redis-cluster/6381/redis.conf:/etc/redis/redis.conf --privileged=true --name redis-6381 --net host -d -p 6381:6381 -p 16831:16831 redis:6.2.6 redis-server /etc/redis/redis.conf
# 启动 6382 实例
docker run -v /home/redis-cluster/6382/redis.conf:/etc/redis/redis.conf --privileged=true --name redis-6382 --net host -d -p 6382:6382 -p 16832:16832 redis:6.2.6 redis-server /etc/redis/redis.conf
# 确保都执行成功
[root@localhost134 6382]# docker run -v /home/redis-cluster/6381/redis.conf:/etc/redis/redis.conf --privileged=true --name redis-6381 --net host -d -p 6381:6381 -p 16831:16831 redis:6.2.6 redis-server /etc/redis/redis.conf
WARNING: Published ports are discarded when using host network mode
5edb7ef9cbebc7d81861296bf4b507377b96c0c51da81acd30303fc55bcd0bb3
[root@localhost134 6382]# docker run -v /home/redis-cluster/6382/redis.conf:/etc/redis/redis.conf --privileged=true --name redis-6382 --net host -d -p 6382:6382 -p 16832:16832 redis:6.2.6 redis-server /etc/redis/redis.conf
WARNING: Published ports are discarded when using host network mode
b7dcbd0c64f35e8db1cfe92fcbf0cc1b476499a5fcb75d3c0fdcf69fdfd64c71
[root@localhost134 6382]# docker ps
CONTAINER ID   IMAGE         COMMAND                  CREATED          STATUS          PORTS     NAMES
b7dcbd0c64f3   redis:6.2.6   "docker-entrypoint.s…"   10 seconds ago   Up 9 seconds              redis-6382
5edb7ef9cbeb   redis:6.2.6   "docker-entrypoint.s…"   19 seconds ago   Up 18 seconds             redis-6381

加入集群

# 在任意节点执行
docker exec -it redis-6381 redis-cli --cluster create 192.168.247.133:6381 192.168.247.133:6382 192.168.247.134:6381 192.168.247.134:6382 192.168.247.135:6381 192.168.247.135:6382 --cluster-replicas 1
# 集群成功
[root@localhost133 6381]# docker exec -it redis-6381 redis-cli -a 123456 --cluster create 192.168.247.133:6381 192.168.247.133:6382 192.168.247.134:6381 192.168.247.134:6382 192.168.247.135:6381 192.168.247.135:6382 --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 192.168.247.134:6382 to 192.168.247.133:6381
Adding replica 192.168.247.135:6382 to 192.168.247.134:6381
Adding replica 192.168.247.133:6382 to 192.168.247.135:6381
M: fe493cfabfad9ab289d52480b9e0c436942a04f0 192.168.247.133:6381
   slots:[0-5460] (5461 slots) master
S: d975ede34715f85461cca179bdcff42eca9d074a 192.168.247.133:6382
   replicates 6938a230c1253423b5f8775da66918e1de605d62
M: d01c20ee23393081e849abfd9b204cd7884f8b42 192.168.247.134:6381
   slots:[5461-10922] (5462 slots) master
S: 13a1860ac3caca5a7bdf75bc79b52243b2e43347 192.168.247.134:6382
   replicates fe493cfabfad9ab289d52480b9e0c436942a04f0
M: 6938a230c1253423b5f8775da66918e1de605d62 192.168.247.135:6381
   slots:[10923-16383] (5461 slots) master
S: 10ebc3d017bb001839b5fd175aea61df7790108b 192.168.247.135:6382
   replicates d01c20ee23393081e849abfd9b204cd7884f8b42
Can I set the above configuration? (type 'yes' to accept): yes
>>> 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 node 192.168.247.133:6381)
M: fe493cfabfad9ab289d52480b9e0c436942a04f0 192.168.247.133:6381
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 13a1860ac3caca5a7bdf75bc79b52243b2e43347 192.168.247.134:6382
   slots: (0 slots) slave
   replicates fe493cfabfad9ab289d52480b9e0c436942a04f0
S: 10ebc3d017bb001839b5fd175aea61df7790108b 192.168.247.135:6382
   slots: (0 slots) slave
   replicates d01c20ee23393081e849abfd9b204cd7884f8b42
M: d01c20ee23393081e849abfd9b204cd7884f8b42 192.168.247.134:6381
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: d975ede34715f85461cca179bdcff42eca9d074a 192.168.247.133:6382
   slots: (0 slots) slave
   replicates 6938a230c1253423b5f8775da66918e1de605d62
M: 6938a230c1253423b5f8775da66918e1de605d62 192.168.247.135:6381
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

操作

RDM Redis Console
Connecting...
Connected to cluster.
192.168.247.133.6381(192.168.247.135:6381)>get name
"NOAUTH Authentication required."
192.168.247.133.6381(192.168.247.134:6381)>
Connected to cluster.
192.168.247.133.6381(192.168.247.134:6381)>auth 123456
"OK"
192.168.247.133.6381(192.168.247.134:6381)>get name
"hzq"
192.168.247.133.6381(192.168.247.134:6381)>set name hj
"OK"
192.168.247.133.6381(192.168.247.134:6381)>get name
"hj"
192.168.247.133.6381(192.168.247.134:6381)>cluster info
"cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:3
cluster_stats_messages_ping_sent:949
cluster_stats_messages_pong_sent:952
cluster_stats_messages_meet_sent:1
cluster_stats_messages_sent:1902
cluster_stats_messages_ping_received:952
cluster_stats_messages_pong_received:950
cluster_stats_messages_received:1902
"
192.168.247.133.6381(192.168.247.134:6381)>cluster nodes
"10ebc3d017bb001839b5fd175aea61df7790108b 192.168.247.135:6382@16382 slave d01c20ee23393081e849abfd9b204cd7884f8b42 0 1646459894166 3 connected
d975ede34715f85461cca179bdcff42eca9d074a 192.168.247.133:6382@16382 slave 6938a230c1253423b5f8775da66918e1de605d62 0 1646459896099 5 connected
d01c20ee23393081e849abfd9b204cd7884f8b42 192.168.247.134:6381@16381 myself,master - 0 1646459894000 3 connected 5461-10922
fe493cfabfad9ab289d52480b9e0c436942a04f0 192.168.247.133:6381@16381 master - 0 1646459896201 1 connected 0-5460
6938a230c1253423b5f8775da66918e1de605d62 192.168.247.135:6381@16381 master - 0 1646459894000 5 connected 10923-16383
13a1860ac3caca5a7bdf75bc79b52243b2e43347 192.168.247.134:6382@16382 slave fe493cfabfad9ab289d52480b9e0c436942a04f0 0 1646459895181 1 connected
"
192.168.247.133.6381(192.168.247.134:6381)>keys *
 1)  "name"
# 停掉 192.168.247.135 6381 节点 docker stop redis-6381
192.168.247.133.6381(192.168.247.134:6381)>cluster nodes
"10ebc3d017bb001839b5fd175aea61df7790108b 192.168.247.135:6382@16382 slave d01c20ee23393081e849abfd9b204cd7884f8b42 0 1646460058482 3 connected
d975ede34715f85461cca179bdcff42eca9d074a 192.168.247.133:6382@16382 master - 0 1646460058071 7 connected 10923-16383
d01c20ee23393081e849abfd9b204cd7884f8b42 192.168.247.134:6381@16381 myself,master - 0 1646460056000 3 connected 5461-10922
fe493cfabfad9ab289d52480b9e0c436942a04f0 192.168.247.133:6381@16381 master - 0 1646460058585 1 connected 0-5460
6938a230c1253423b5f8775da66918e1de605d62 192.168.247.135:6381@16381 master,fail - 1646460043171 1646460041039 5 disconnected
13a1860ac3caca5a7bdf75bc79b52243b2e43347 192.168.247.134:6382@16382 slave fe493cfabfad9ab289d52480b9e0c436942a04f0 0 1646460058000 1 connected
"
# 此时 192.168.247.133 主机上 两个 master 节点
# 不影响使用
192.168.247.133.6381(192.168.247.134:6381)>get name
"hj"
# 恢复 192.168.247.135 6381 节点 docker start redis-6381
192.168.247.133.6381(192.168.247.134:6381)>cluster nodes
"10ebc3d017bb001839b5fd175aea61df7790108b 192.168.247.135:6382@16382 slave d01c20ee23393081e849abfd9b204cd7884f8b42 0 1646460243890 3 connected
d975ede34715f85461cca179bdcff42eca9d074a 192.168.247.133:6382@16382 master - 0 1646460242574 7 connected 10923-16383
d01c20ee23393081e849abfd9b204cd7884f8b42 192.168.247.134:6381@16381 myself,master - 0 1646460240000 3 connected 5461-10922
fe493cfabfad9ab289d52480b9e0c436942a04f0 192.168.247.133:6381@16381 master - 0 1646460242000 1 connected 0-5460
6938a230c1253423b5f8775da66918e1de605d62 192.168.247.135:6381@16381 slave d975ede34715f85461cca179bdcff42eca9d074a 0 1646460242000 7 connected
13a1860ac3caca5a7bdf75bc79b52243b2e43347 192.168.247.134:6382@16382 slave fe493cfabfad9ab289d52480b9e0c436942a04f0 0 1646460242877 1 connected
"
# 此时 192.168.247.135 主机上 两个 slave 节点

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值