0%

redis集群模式部署

一些重要参数

  • --cluster-enabled:是否启动集群,选值:yes 、no
  • --cluster-config-file 配置文件.conf :指定节点信息,自动生成
  • --cluster-node-timeout 毫秒值: 配置节点连接超时时间
  • --appendonly:是否开启持久化,选值:yes、no

最简单3主模式

方便测试,使用host网络模式启动docker,且并没有将持久化映射出来.

持久化启动参数-v /data/redis-data/node1:/data.

# 节点1
docker run -d --name redis-node1 --net host redis:6.2.5 --cluster-enabled yes --cluster-config-file node-1.conf --port 6379
# 节点2
docker run -d --name redis-node2 --net host redis:6.2.5 --cluster-enabled yes --cluster-config-file node-2.conf --port 6380
# 节点3
docker run -d --name redis-node3 --net host redis:6.2.5 --cluster-enabled yes --cluster-config-file node-3.conf --port 6381

# 进入一个容器执行创建集群的命令
docker exec -it redis-node1 /bin/bash
# 创建集群,只有三个节点,所有指定副本数为0
redis-cli --cluster create 192.168.41.128:6379  192.168.41.128:6380  192.168.41.128:6381 --cluster-replicas 0
>>> Performing hash slots allocation on 3 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
M: 699e2ff7c0098cc1cee1ddae640b4bbb66747c72 192.168.41.128:6379
   slots:[0-5460] (5461 slots) master
M: ee2f07166809df6a56a57454470d65944ab8051f 192.168.41.128:6380
   slots:[5461-10922] (5462 slots) master
M: 043f76013aec2600396c4e92c50d0861f01a0dde 192.168.41.128:6381
   slots:[10923-16383] (5461 slots) master
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.41.128:6379)
M: 699e2ff7c0098cc1cee1ddae640b4bbb66747c72 192.168.41.128:6379
   slots:[0-5460] (5461 slots) master
M: ee2f07166809df6a56a57454470d65944ab8051f 192.168.41.128:6380
   slots:[5461-10922] (5462 slots) master
M: 043f76013aec2600396c4e92c50d0861f01a0dde 192.168.41.128:6381
   slots:[10923-16383] (5461 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

# 查看集群状态
root@node1:/data# redis-cli 
127.0.0.1:6379> cluster nodes
ee2f07166809df6a56a57454470d65944ab8051f 192.168.41.128:6380@16380 master - 0 1629358092049 2 connected 5461-10922
043f76013aec2600396c4e92c50d0861f01a0dde 192.168.41.128:6381@16381 master - 0 1629358091025 3 connected 10923-16383
699e2ff7c0098cc1cee1ddae640b4bbb66747c72 192.168.41.128:6379@16379 myself,master - 0 1629358091000 1 connected 0-5460

# 测试,连接集群,set数据,别的节点可以get到数据
root@node1:/data# redis-cli -c
127.0.0.1:6379> set hello world
OK

3主3从模式

# 主备6个redis节点
docker run -d --name redis-node1 --net host redis:6.2.5 --cluster-enabled yes --cluster-config-file node-1.conf --port 6379
docker run -d --name redis-node2 --net host redis:6.2.5 --cluster-enabled yes --cluster-config-file node-2.conf --port 6380
docker run -d --name redis-node3 --net host redis:6.2.5 --cluster-enabled yes --cluster-config-file node-3.conf --port 6381
docker run -d --name redis-node4 --net host redis:6.2.5 --cluster-enabled yes --cluster-config-file node-4.conf --port 6382
docker run -d --name redis-node5 --net host redis:6.2.5 --cluster-enabled yes --cluster-config-file node-5.conf --port 6383
docker run -d --name redis-node6 --net host redis:6.2.5 --cluster-enabled yes --cluster-config-file node-6.conf --port 6384

# 进入任一节点
docker exec -it redis-node1 /bin/bash

# 创建集群
redis-cli --cluster create 192.168.41.128:6379 192.168.41.128:6380 192.168.41.128:6381 192.168.41.128:6382 192.168.41.128:6383 192.168.41.128:6384 --cluster-replicas 1

# 查看集群状态
root@node1:/data# redis-cli 
127.0.0.1:6379> cluster nodes
95b197193ee57fb0f331ee03df671b0e50d293c2 192.168.41.128:6380@16380 master - 0 1629359633234 2 connected 5461-10922
10678b7ed61dbcd923472a272b6d365222fdad95 192.168.41.128:6384@16384 slave 345dd13e0bbf74a09ebe0011302f0ba8a011c726 0 1629359633000 3 connected
34e4d85bbdebc619ed4e9a183a0939db0ad82d9a 192.168.41.128:6382@16382 slave 75325c54ff09715263c514bde71d6582fde2b8a8 0 1629359632000 1 connected
345dd13e0bbf74a09ebe0011302f0ba8a011c726 192.168.41.128:6381@16381 master - 0 1629359635272 3 connected 10923-16383
75325c54ff09715263c514bde71d6582fde2b8a8 192.168.41.128:6379@16379 myself,master - 0 1629359634000 1 connected 0-5460
98b18508d3248973c3d28930b7a571d4564c8e2a 192.168.41.128:6383@16383 slave 95b197193ee57fb0f331ee03df671b0e50d293c2 0 1629359635000 2 connected