redis集群(docker)

创建容器

[root@localhost /]# docker run -d --name redis8001 -p 8001:6379 redis --cluster-enabled yes
de841af29485d9ade4c1b1dbd4ff54b6e22cb5443161d7043b47b73e0aaf9095
[root@localhost /]# docker run -d --name redis8002 -p 8002:6379 redis --cluster-enabled yes
bc5440e29f928afc7fb31fcbbe2f6e8242a5ee0bd897329c3f68bf243c845f13
[root@localhost /]# docker run -d --name redis8003 -p 8003:6379 redis --cluster-enabled yes
da590ac5047c8f330705eb7ba0217b6202a87ed696fe563e8b7d550cdcb4a3aa
[root@localhost /]# docker run -d --name redis8004 -p 8004:6379 redis --cluster-enabled yes
cd2ddb4c6066a041134664e02bcdc2702b732088cd9b35aae631ccbd3bec7cf1
[root@localhost /]# docker run -d --name redis8005 -p 8005:6379 redis --cluster-enabled yes
a40878593d150724301345773a77c34fbf06b419d5124d76bce56dc6249aa138
[root@localhost /]# docker run -d --name redis8006 -p 8006:6379 redis --cluster-enabled yes
c5c4c7eed8ee40daaddd43d45c0a3513c1640a7b61e54d024f5adb9419dec6cf
[root@localhost /]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                                       NAMES
c5c4c7eed8ee   redis     "docker-entrypoint.s…"   4 seconds ago    Up 3 seconds    0.0.0.0:8006->6379/tcp, :::8006->6379/tcp   redis8006
a40878593d15   redis     "docker-entrypoint.s…"   16 seconds ago   Up 15 seconds   0.0.0.0:8005->6379/tcp, :::8005->6379/tcp   redis8005
cd2ddb4c6066   redis     "docker-entrypoint.s…"   24 seconds ago   Up 23 seconds   0.0.0.0:8004->6379/tcp, :::8004->6379/tcp   redis8004
da590ac5047c   redis     "docker-entrypoint.s…"   33 seconds ago   Up 32 seconds   0.0.0.0:8003->6379/tcp, :::8003->6379/tcp   redis8003
bc5440e29f92   redis     "docker-entrypoint.s…"   42 seconds ago   Up 41 seconds   0.0.0.0:8002->6379/tcp, :::8002->6379/tcp   redis8002
de841af29485   redis     "docker-entrypoint.s…"   52 seconds ago   Up 51 seconds   0.0.0.0:8001->6379/tcp, :::8001->6379/tcp   redis8001

查看容器ip(以redis8001为例)

[root@localhost /]# docker inspect redis8001|grep IPAddress
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.2",
                    "IPAddress": "172.17.0.2",

进入容器并创建集群

[root@localhost /]# docker exec -it redis8001 /bin/bash
root@de841af29485:/data# redis-cli --cluster create \
172.17.0.2:6379 \
172.17.0.3:6379 \
172.17.0.4:6379 \
172.17.0.5:6379 \
172.17.0.6:6379 \
172.17.0.7:6379  --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 172.17.0.6:6379 to 172.17.0.2:6379
Adding replica 172.17.0.7:6379 to 172.17.0.3:6379
Adding replica 172.17.0.5:6379 to 172.17.0.4:6379
M: 7508de5966cd30dbc87d7884d5edc4e890f96222 172.17.0.2:6379
   slots:[0-5460] (5461 slots) master
M: 4b282db91824f8217f007bb31aad5a4395339a98 172.17.0.3:6379
   slots:[5461-10922] (5462 slots) master
M: 1bab51ec48214a56f8245da2667df9dfcfc9c899 172.17.0.4:6379
   slots:[10923-16383] (5461 slots) master
S: e9ef8e3f78b38df6f62d1deaf0bbcd18c372d6e0 172.17.0.5:6379
   replicates 1bab51ec48214a56f8245da2667df9dfcfc9c899
S: 90f34d9cb858b6bc2b807965dfc421c481a969d8 172.17.0.6:6379
   replicates 7508de5966cd30dbc87d7884d5edc4e890f96222
S: 66bdb7dea061c433a44bfd27be66939ee7455086 172.17.0.7:6379
   replicates 4b282db91824f8217f007bb31aad5a4395339a98
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 172.17.0.2:6379)
M: 7508de5966cd30dbc87d7884d5edc4e890f96222 172.17.0.2:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 1bab51ec48214a56f8245da2667df9dfcfc9c899 172.17.0.4:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 90f34d9cb858b6bc2b807965dfc421c481a969d8 172.17.0.6:6379
   slots: (0 slots) slave
   replicates 7508de5966cd30dbc87d7884d5edc4e890f96222
S: 66bdb7dea061c433a44bfd27be66939ee7455086 172.17.0.7:6379
   slots: (0 slots) slave
   replicates 4b282db91824f8217f007bb31aad5a4395339a98
M: 4b282db91824f8217f007bb31aad5a4395339a98 172.17.0.3:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: e9ef8e3f78b38df6f62d1deaf0bbcd18c372d6e0 172.17.0.5:6379
   slots: (0 slots) slave
   replicates 1bab51ec48214a56f8245da2667df9dfcfc9c899
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

测试

root@de841af29485:/data# redis-cli -c
127.0.0.1:6379> set name 11
-> Redirected to slot [5798] located at 172.17.0.3:6379
OK
172.17.0.3:6379> get name
"11"
172.17.0.3:6379> set age 21
-> Redirected to slot [741] located at 172.17.0.2:6379
OK
172.17.0.2:6379> get age
"21"
172.17.0.2:6379>

查看集群槽位分布

172.17.0.2:6379> cluster nodes
7508de5966cd30dbc87d7884d5edc4e890f96222 172.17.0.2:6379@16379 myself,master - 0 1692188307000 1 connected 0-5460
1bab51ec48214a56f8245da2667df9dfcfc9c899 172.17.0.4:6379@16379 master - 0 1692188308000 3 connected 10923-16383
90f34d9cb858b6bc2b807965dfc421c481a969d8 172.17.0.6:6379@16379 slave 7508de5966cd30dbc87d7884d5edc4e890f96222 0 1692188309000 1 connected
66bdb7dea061c433a44bfd27be66939ee7455086 172.17.0.7:6379@16379 slave 4b282db91824f8217f007bb31aad5a4395339a98 0 1692188309189 2 connected
4b282db91824f8217f007bb31aad5a4395339a98 172.17.0.3:6379@16379 master - 0 1692188308177 2 connected 5461-10922
e9ef8e3f78b38df6f62d1deaf0bbcd18c372d6e0 172.17.0.5:6379@16379 slave 1bab51ec48214a56f8245da2667df9dfcfc9c899 0 1692188310194 3 connected

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值