Docker安装redis集群(3主3从)

2022-07-13  本文已影响0人  ssttIsme

查看IP

如192.168.100.102

[server@hadoop102 ~]$ ifconfig
docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:f3:a0:b7:28  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.100.102  netmask 255.255.255.0  broadcast 192.168.100.255
        inet6 fe80::fdd2:477b:3f5e:2ef9  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:e8:a6:3f  txqueuelen 1000  (Ethernet)
        RX packets 28129  bytes 39930383 (38.0 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4907  bytes 398400 (389.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

启动docker

[server@hadoop102 ~]$ sudo systemctl start docker

新建6个docker容器实例

[server@hadoop102 ~]$ sudo docker run -d --name redis-node1 --net host --privileged=true -v /data/redis/share/redis-node1:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6381

[server@hadoop102 ~]$ sudo docker run -d --name redis-node2 --net host --privileged=true -v /data/redis/share/redis-node2:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6382

[server@hadoop102 ~]$ sudo docker run -d --name redis-node3 --net host --privileged=true -v /data/redis/share/redis-node3:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6383

[server@hadoop102 ~]$ sudo docker run -d --name redis-node4 --net host --privileged=true -v /data/redis/share/redis-node4:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6384

[server@hadoop102 ~]$ sudo docker run -d --name redis-node5 --net host --privileged=true -v /data/redis/share/redis-node5:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6385

[server@hadoop102 ~]$ sudo docker run -d --name redis-node6 --net host --privileged=true -v /data/redis/share/redis-node6:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6386

查看容器实例

[server@hadoop102 ~]$ sudo docker ps
CONTAINER ID   IMAGE         COMMAND                  CREATED              STATUS              PORTS     NAMES
8e075eb2fa3c   redis:6.0.8   "docker-entrypoint.s…"   46 seconds ago       Up 45 seconds                 redis-node6
4af949a12a3a   redis:6.0.8   "docker-entrypoint.s…"   About a minute ago   Up About a minute             redis-node5
583335d7b8dc   redis:6.0.8   "docker-entrypoint.s…"   About a minute ago   Up About a minute             redis-node4
2ec34f15d4e4   redis:6.0.8   "docker-entrypoint.s…"   About a minute ago   Up About a minute             redis-node3
762c0b0da5bd   redis:6.0.8   "docker-entrypoint.s…"   2 minutes ago        Up 2 minutes                  redis-node2
41d80a88e15d   redis:6.0.8   "docker-entrypoint.s…"   2 minutes ago        Up 2 minutes                  redis-node1

解释
docker run 创建并运行docker容器实例
--name redis-node6 容器名字
--net host 使用宿主机的IP和端口,默认
--privileged=true 使用宿主机root权限
-v /data/redis/share/redis-node6:/data 容器卷,宿主机地址:docker内部地址
redis:6.0.8 redis镜像版本和编号
--cluster-enabled yes 开启redis集群
--appendonly yes 开启持久化
--port 6381 redis端口号

构建主从关系

[server@hadoop102 ~]$ sudo docker exec -it redis-node1 /bin/bash

本例子ip为宿主机ip 192.168.100.102

root@hadoop102:/data# redis-cli --cluster create 192.168.100.102:6381 192.168.100.102:6382 192.168.100.102:6383 192.168.100.102:6384 192.168.100.102:6385 192.168.100.102:6386 --cluster-replicas 1
>>> 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.100.102:6385 to 192.168.100.102:6381
Adding replica 192.168.100.102:6386 to 192.168.100.102:6382
Adding replica 192.168.100.102:6384 to 192.168.100.102:6383
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 0a63e6f79aa367f1f2c9e43e42d74845fd162675 192.168.100.102:6381
   slots:[0-5460] (5461 slots) master
M: a90a10dcc8ab210f0063f3bd952b4e3048d3cdbb 192.168.100.102:6382
   slots:[5461-10922] (5462 slots) master
M: 57252b27ec04e907651a6870b083548c5978d990 192.168.100.102:6383
   slots:[10923-16383] (5461 slots) master
S: 92f880a35cac4e1d40f1fdeaf92a8ed06fb8062e 192.168.100.102:6384
   replicates a90a10dcc8ab210f0063f3bd952b4e3048d3cdbb
S: 09e4cf87c3a5bc1acd564a14d2b77e4e29e5d7d7 192.168.100.102:6385
   replicates 57252b27ec04e907651a6870b083548c5978d990
S: 17dad6dafdc55a54f046df9bff8caeb479b37d4f 192.168.100.102:6386
   replicates 0a63e6f79aa367f1f2c9e43e42d74845fd162675
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.100.102:6381)
M: 0a63e6f79aa367f1f2c9e43e42d74845fd162675 192.168.100.102:6381
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 57252b27ec04e907651a6870b083548c5978d990 192.168.100.102:6383
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 17dad6dafdc55a54f046df9bff8caeb479b37d4f 192.168.100.102:6386
   slots: (0 slots) slave
   replicates 0a63e6f79aa367f1f2c9e43e42d74845fd162675
S: 92f880a35cac4e1d40f1fdeaf92a8ed06fb8062e 192.168.100.102:6384
   slots: (0 slots) slave
   replicates a90a10dcc8ab210f0063f3bd952b4e3048d3cdbb
S: 09e4cf87c3a5bc1acd564a14d2b77e4e29e5d7d7 192.168.100.102:6385
   slots: (0 slots) slave
   replicates 57252b27ec04e907651a6870b083548c5978d990
M: a90a10dcc8ab210f0063f3bd952b4e3048d3cdbb 192.168.100.102:6382
   slots:[5461-10922] (5462 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.
root@hadoop102:/data# 

查看集群状态

6个节点
16384个槽位

[server@hadoop102 ~]$ sudo docker exec -it redis-node1 /bin/bash
root@hadoop102:/data# redis-cli -p 6381 
127.0.0.1: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:1
cluster_stats_messages_ping_sent:1305
cluster_stats_messages_pong_sent:1335
cluster_stats_messages_sent:2640
cluster_stats_messages_ping_received:1330
cluster_stats_messages_pong_received:1305
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:2640

可看到
M1 挂了 S6
M2 挂了 S4
M3 挂了 S5

127.0.0.1:6381> cluster nodes
57252b27ec04e907651a6870b083548c5978d990 192.168.100.102:6383@16383 master - 0 1657637064718 3 connected 10923-16383
17dad6dafdc55a54f046df9bff8caeb479b37d4f 192.168.100.102:6386@16386 slave 0a63e6f79aa367f1f2c9e43e42d74845fd162675 0 1657637066000 1 connected
92f880a35cac4e1d40f1fdeaf92a8ed06fb8062e 192.168.100.102:6384@16384 slave a90a10dcc8ab210f0063f3bd952b4e3048d3cdbb 0 1657637065000 2 connected
09e4cf87c3a5bc1acd564a14d2b77e4e29e5d7d7 192.168.100.102:6385@16385 slave 57252b27ec04e907651a6870b083548c5978d990 0 1657637066749 3 connected
a90a10dcc8ab210f0063f3bd952b4e3048d3cdbb 192.168.100.102:6382@16382 master - 0 1657637064000 2 connected 5461-10922
0a63e6f79aa367f1f2c9e43e42d74845fd162675 192.168.100.102:6381@16381 myself,master - 0 1657637064000 1 connected 0-5460
上一篇下一篇

猜你喜欢

热点阅读