集群配置
总共六个redis,其中3个作为master,另外3个作为replica
docker-compose.yml
version: '2'
services:
redis-6379:
image: redis
container_name: redis-6379
ports:
- 6379:6379
- 16379:16379
volumes:
- ./6379/data:/data/redis
- ./6379/redis.conf:/etc/redis/redis.conf
command: redis-server /etc/redis/redis.conf
redis-6380:
image: redis
container_name: redis-6380
ports:
- 6380:6380
- 16380:16380
volumes:
- ./6380/data:/data/redis
- ./6380/redis.conf:/etc/redis/redis.conf
command: redis-server /etc/redis/redis.conf
redis-6381:
image: redis
container_name: redis-6381
ports:
- 6381:6381
- 16381:16381
volumes:
- ./6381/data:/data/redis
- ./6381/redis.conf:/etc/redis/redis.conf
command: redis-server /etc/redis/redis.conf
redis-6382:
image: redis
container_name: redis-6382
ports:
- 6382:6382
- 16382:16382
volumes:
- ./6382/data:/data/redis
- ./6382/redis.conf:/etc/redis/redis.conf
command: redis-server /etc/redis/redis.conf
redis-6383:
image: redis
container_name: redis-6383
ports:
- 6383:6383
- 16383:16383
volumes:
- ./6383/data:/data/redis
- ./6383/redis.conf:/etc/redis/redis.conf
command: redis-server /etc/redis/redis.conf
redis-6384:
image: redis
container_name: redis-6384
ports:
- 6384:6384
- 16384:16384
volumes:
- ./6384/data:/data/redis
- ./6384/redis.conf:/etc/redis/redis.conf
command: redis-server /etc/redis/redis.conf
redis.conf
bind 0.0.0.0
port 容器端口 # 这里对应docker-compose.yml对应的ports端口
dir /data/redis # redis的数据目录
appendonly yes # aof持久化机制
cluster-enabled yes # 开启cluster
cluster-config-file nodes-6379.conf # 这里的文件名可以不做更改,用于记录集群的信息
cluster-node-timeout 15000 # 集群节点的超时毫秒时间,超过此时间则判断为该node不可达
运行、集群创建
启动容器后,随便进入一个容器,然后使用redis-cli创建
[root@iZwz9669229pzjci7wt131Z redis] docker-compose up -d
[root@iZwz9669229pzjci7wt131Z redis] docker exec -it redis-6380 bash
root@bcd3b2cb4d58: redis-cli --cluster create --cluster-replicas 1 172.18.248.36:6379 172.18.248.36:6380 172.18.248.36:6381 172.18.248.36:6382 172.18.248.36:6383 172.18.248.36:6384
>>> 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.18.248.36:6383 to 172.18.248.36:6379
Adding replica 172.18.248.36:6384 to 172.18.248.36:6380
Adding replica 172.18.248.36:6382 to 172.18.248.36:6381
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 98fcf444b67eb4c75620d63d80fa09f89815e8ea 172.18.248.36:6379
slots:[0-5460] (5461 slots) master
M: c6a50ead8930dc3d21da4b2c260b436597906dc3 172.18.248.36:6380
slots:[5461-10922] (5462 slots) master
M: c50984e8743cf2704725cdfff8f84b9ccd51d11a 172.18.248.36:6381
slots:[10923-16383] (5461 slots) master
S: a7234465fcb3e1c9bbfb83447ff682fa4b1614bd 172.18.248.36:6382
replicates 98fcf444b67eb4c75620d63d80fa09f89815e8ea
S: 1d037fe427f1fe4c292f8198b70782fd435827e4 172.18.248.36:6383
replicates c6a50ead8930dc3d21da4b2c260b436597906dc3
S: 827da77a5a902f9b7c5b8f300496f3af9de73d76 172.18.248.36:6384
replicates c50984e8743cf2704725cdfff8f84b9ccd51d11a
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.18.248.36:6379)
M: 98fcf444b67eb4c75620d63d80fa09f89815e8ea 172.18.248.36:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 1d037fe427f1fe4c292f8198b70782fd435827e4 172.30.0.1:6383
slots: (0 slots) slave
replicates c6a50ead8930dc3d21da4b2c260b436597906dc3
S: 827da77a5a902f9b7c5b8f300496f3af9de73d76 172.30.0.1:6384
slots: (0 slots) slave
replicates c50984e8743cf2704725cdfff8f84b9ccd51d11a
M: c6a50ead8930dc3d21da4b2c260b436597906dc3 172.30.0.1:6380
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: a7234465fcb3e1c9bbfb83447ff682fa4b1614bd 172.30.0.1:6382
slots: (0 slots) slave
replicates 98fcf444b67eb4c75620d63d80fa09f89815e8ea
M: c50984e8743cf2704725cdfff8f84b9ccd51d11a 172.30.0.1: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.
root@bcd3b2cb4d58:/data# redis-cli -p 6380
127.0.0.1:6380> 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:2
cluster_stats_messages_ping_sent:1088
cluster_stats_messages_pong_sent:1105
cluster_stats_messages_meet_sent:1
cluster_stats_messages_sent:2194
cluster_stats_messages_ping_received:1105
cluster_stats_messages_pong_received:1089
cluster_stats_messages_received:2194
redis-cli参数:
--cluster # 集群管理命令
create host1:port1 ... hostN:portN # 指定要加入集群的redis节点
--cluster-replicas # 指定需要多少个副本,设置为1时,每个master会有1个replica