如何使用docker搭建redis集群
最近跟着狂神学习了Docker技术,这里对如何使用docker搭建redis集群做个总结
一、创建redis的docker网卡
sudo docker network create redis --subnet 192.161.0.0/16
二、通过脚本创建六个redis配置
for port in $(seq 1 6); \
do \
mkdir -p /mydata/redis/node-${port}/conf
touch /mydata/redis/node-${port}/conf/redis.conf
cat << EOF >/mydata/redis/node-${port}/conf/redis.conf
port 6379
bind 0.0.0.0
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 192.161.0.1${port}
cluster-announce-port 6379
cluster-announce-bus-port 16379
appendonly yes
EOF
done
三、创建redis容器并启动
for port in $(seq 1 6); \
do \
docker run -p 637${port}:6379 -p 1637${port}:16379 --name redis-${port} \
-v /mydata/reids/node-${port}/data:/data \
-v /mydata/redis/node-${port}/conf/redis.conf:/etc/redis/redis.conf \
-d --net redisnet --ip 192.161.0.1${port} redis redis-server /etc/redis/redis.conf
EOF
done
四、创建集群
- 进入到刚刚创建的容器中
docker exec -it redis-1 /bin/sh
- 创建集群
redis-cli --cluster create 192.161.0.11:6379 192.161.0.12:6379 192.161.0.13:6379 192.161.0.14:6379 192.161.0.15:6379 192.161.0.16: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 192.161.0.15:6379 to 192.161.0.11:6379
Adding replica 192.161.0.16:6379 to 192.161.0.12:6379
Adding replica 192.161.0.14:6379 to 192.161.0.13:6379
M: 114e98251e35456e08665965e09d76ee685c9312 192.161.0.11:6379
slots:[0-5460] (5461 slots) master
M: e052bc2a9dcdf59356e37e6dcfedaacdf32df158 192.161.0.12:6379
slots:[5461-10922] (5462 slots) master
M: 79ea1d72060fd0ed4b238b7b04eba6a7a9985880 192.161.0.13:6379
slots:[10923-16383] (5461 slots) master
S: b5e91d65bf50c19172e7e36881e627cff1123f19 192.161.0.14:6379
replicates 79ea1d72060fd0ed4b238b7b04eba6a7a9985880
S: c53a787a2d8d9bea0d1de6eb8b2978d5e54eb608 192.161.0.15:6379
replicates 114e98251e35456e08665965e09d76ee685c9312
S: 0aecc3e25024a60db1871271166a1d5798f962f6 192.161.0.16:6379
replicates e052bc2a9dcdf59356e37e6dcfedaacdf32df158
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.161.0.11:6379)
M: 114e98251e35456e08665965e09d76ee685c9312 192.161.0.11:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 79ea1d72060fd0ed4b238b7b04eba6a7a9985880 192.161.0.13:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: c53a787a2d8d9bea0d1de6eb8b2978d5e54eb608 192.161.0.15:6379
slots: (0 slots) slave
replicates 114e98251e35456e08665965e09d76ee685c9312
S: 0aecc3e25024a60db1871271166a1d5798f962f6 192.161.0.16:6379
slots: (0 slots) slave
replicates e052bc2a9dcdf59356e37e6dcfedaacdf32df158
M: e052bc2a9dcdf59356e37e6dcfedaacdf32df158 192.161.0.12:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: b5e91d65bf50c19172e7e36881e627cff1123f19 192.161.0.14:6379
slots: (0 slots) slave
replicates 79ea1d72060fd0ed4b238b7b04eba6a7a9985880
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
五、进入集群
redis-cli -c
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:145
cluster_stats_messages_pong_sent:153
cluster_stats_messages_sent:298
cluster_stats_messages_ping_received:148
cluster_stats_messages_pong_received:145
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:298
cluster nodes
集群节点信息
79ea1d72060fd0ed4b238b7b04eba6a7a9985880 192.161.0.13:6379@16379 master - 0 1646645048000 3 connected 10923-16383
114e98251e35456e08665965e09d76ee685c9312 192.161.0.11:6379@16379 myself,master - 0 1646645047000 1 connected 0-5460
c53a787a2d8d9bea0d1de6eb8b2978d5e54eb608 192.161.0.15:6379@16379 slave 114e98251e35456e08665965e09d76ee685c9312 0 1646645049528 1 connected
0aecc3e25024a60db1871271166a1d5798f962f6 192.161.0.16:6379@16379 slave e052bc2a9dcdf59356e37e6dcfedaacdf32df158 0 1646645048324 2 connected
e052bc2a9dcdf59356e37e6dcfedaacdf32df158 192.161.0.12:6379@16379 master - 0 1646645048000 2 connected 5461-10922
b5e91d65bf50c19172e7e36881e627cff1123f19 192.161.0.14:6379@16379 slave 79ea1d72060fd0ed4b238b7b04eba6a7a9985880 0 1646645048825 3 connected
六、测试
127.0.0.1:6379> set a b
-> Redirected to slot [15495] located at 192.161.0.13:6379
OK
docker stop redis-3
redis-cli -c
127.0.0.1:6379> get a
-> Redirected to slot [15495] located at 192.161.0.14:6379
"b"
cluster nodes
0aecc3e25024a60db1871271166a1d5798f962f6 192.161.0.16:6379@16379 slave e052bc2a9dcdf59356e37e6dcfedaacdf32df158 0 1646651913232 2 connected
e052bc2a9dcdf59356e37e6dcfedaacdf32df158 192.161.0.12:6379@16379 master - 0 1646651913733 2 connected 5461-10922
b5e91d65bf50c19172e7e36881e627cff1123f19 192.161.0.14:6379@16379 myself,master - 0 1646651913000 9 connected 10923-16383
114e98251e35456e08665965e09d76ee685c9312 192.161.0.11:6379@16379 master - 0 1646651913000 1 connected 0-5460
79ea1d72060fd0ed4b238b7b04eba6a7a9985880 192.161.0.13:6379@16379 master,fail - 1646651680915 1646651678405 8 connected
c53a787a2d8d9bea0d1de6eb8b2978d5e54eb608 192.161.0.15:6379@16379 slave 114e98251e35456e08665965e09d76ee685c9312 0 1646651914234 1 connected
|