Docker部署Redis集群
第一步創(chuàng)建Redis網(wǎng)卡
?docker network create redis --subnet 192.168.1.0/24
通過腳本創(chuàng)建6個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.168.1.1${port}
?cluster-announce-port 6379
?cluster-announce-bus-port 16379
?appendonly yes
?EOF
?done
創(chuàng)建完畢后檢查是否成功
?[root@D mydata]# cd /mydata/
?[root@D mydata]# ls
?redis
?[root@D mydata]# cd redis/
?[root@D redis]# ls
?node-1 ?node-2 ?node-3 ?node-4 ?node-5 ?node-6
?[root@D redis]# cd node-1
?[root@D node-1]# ls
?conf
?[root@D node-1]# cd conf/
?[root@D conf]# ls
?redis.conf
?[root@D conf]# cat 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.168.1.11
?cluster-announce-port 6379
?cluster-announce-bus-port 16379
?appendonly yes
?[root@D conf]# ?docker run -p 637${port}:6379 -p 1637${port}:16379 --name redis-${port} \
?-v /mydata/redis/node-${port}/data:/data \
?-v /mydata/redis/node-${port}/conf/redis.conf:/etc/redis/redis.conf \
?-d --net redis --ip 192.168.1.1${port} redis:5.0 redis-server /etc/redis/redis.conf
創(chuàng)建六臺redis
?docker run -p 6371:6379 -p 16371:16379 --name redis-1 \
?-v /mydata/redis/node-1/data:/data \
?-v /mydata/redis/node-1/conf/redis.conf:/etc/redis/redis.conf \
?-d --net redis --ip 192.168.1.11 redis:5.0 redis-server /etc/redis/redis.conf ?docker run -p 6372:6379 -p 16372:16379 --name redis-2 \
?-v /mydata/redis/node-2/data:/data \
?-v /mydata/redis/node-2/conf/redis.conf:/etc/redis/redis.conf \
?-d --net redis --ip 192.168.1.12 redis:5.0 redis-server /etc/redis/redis.conf ?docker run -p 6373:6379 -p 16373:16379 --name redis-3 \
?-v /mydata/redis/node-3/data:/data \
?-v /mydata/redis/node-3/conf/redis.conf:/etc/redis/redis.conf \
?-d --net redis --ip 192.168.1.13 redis:5.0 redis-server /etc/redis/redis.conf ?docker run -p 6374:6379 -p 16374:16379 --name redis-4 \
?-v /mydata/redis/node-4/data:/data \
?-v /mydata/redis/node-4/conf/redis.conf:/etc/redis/redis.conf \
?-d --net redis --ip 192.168.1.14 redis:5.0 redis-server /etc/redis/redis.conf ?docker run -p 6375:6379 -p 16375:16379 --name redis-5 \
?-v /mydata/redis/node-5/data:/data \
?-v /mydata/redis/node-5/conf/redis.conf:/etc/redis/redis.conf \
?-d --net redis --ip 192.168.1.15 redis:5.0 redis-server /etc/redis/redis.conf ?docker run -p 6376:6379 -p 16376:16379 --name redis-6 \
?-v /mydata/redis/node-6/data:/data \
?-v /mydata/redis/node-6/conf/redis.conf:/etc/redis/redis.conf \
?-d --net redis --ip 192.168.1.16 redis:5.0 redis-server /etc/redis/redis.conf
然后進入redis創(chuàng)建集群
?docker exec -it redis-1 /bin/sh?redis-cli --cluster create 192.168.1.11:6379 192.168.1.12:6379 192.168.1.13:6379 192.168.1.14:6379 192.168.1.15:6379 192.168.1.16:6379 --cluster-replicas 1?# redis-cli --cluster create 192.168.1.11:6379 192.168.1.12:6379 192.168.1.13:6379 192.168.1.14:6379 192.168.1.15:6379 192.168.1.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.168.1.15:6379 to 192.168.1.11:6379
?Adding replica 192.168.1.16:6379 to 192.168.1.12:6379
?Adding replica 192.168.1.14:6379 to 192.168.1.13:6379
?M: a7f6ab5c0d726d45099b63593b12676172b3f86b 192.168.1.11:6379
? ? slots:[0-5460] (5461 slots) master
?M: 38d476004e54dcb651471fb46f40567f0ee72c89 192.168.1.12:6379
? ? slots:[5461-10922] (5462 slots) master
?M: 4a260ed09fdc1eaee4896c6acf8185a5ff408faa 192.168.1.13:6379
? ? slots:[10923-16383] (5461 slots) master
?S: 7cc614aa214debdef74984e5e42becfbc60f4885 192.168.1.14:6379
? ? replicates 4a260ed09fdc1eaee4896c6acf8185a5ff408faa
?S: b2ec6c8a0ba9c7e8a2773d8f50fd99ef2d6a1de7 192.168.1.15:6379
? ? replicates a7f6ab5c0d726d45099b63593b12676172b3f86b
?S: 08accdc50545573fd75262bf1d304a0056fd2c6d 192.168.1.16:6379
? ? replicates 38d476004e54dcb651471fb46f40567f0ee72c89
?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.1.11:6379)
?M: a7f6ab5c0d726d45099b63593b12676172b3f86b 192.168.1.11:6379
? ? slots:[0-5460] (5461 slots) master
? ? 1 additional replica(s)
?M: 38d476004e54dcb651471fb46f40567f0ee72c89 192.168.1.12:6379
? ? slots:[5461-10922] (5462 slots) master
? ? 1 additional replica(s)
?S: b2ec6c8a0ba9c7e8a2773d8f50fd99ef2d6a1de7 192.168.1.15:6379
? ? slots: (0 slots) slave
? ? replicates a7f6ab5c0d726d45099b63593b12676172b3f86b
?M: 4a260ed09fdc1eaee4896c6acf8185a5ff408faa 192.168.1.13:6379
? ? slots:[10923-16383] (5461 slots) master
? ? 1 additional replica(s)
?S: 7cc614aa214debdef74984e5e42becfbc60f4885 192.168.1.14:6379
? ? slots: (0 slots) slave
? ? replicates 4a260ed09fdc1eaee4896c6acf8185a5ff408faa
?S: 08accdc50545573fd75262bf1d304a0056fd2c6d 192.168.1.16:6379
? ? slots: (0 slots) slave
? ? replicates 38d476004e54dcb651471fb46f40567f0ee72c89
?[OK] All nodes agree about slots configuration.
?>>> Check for open slots...
?>>> Check slots coverage...
?[OK] All 16384 slots covered.
?# ?# redis-cli -c
?127.0.0.1:6379> 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:167
?cluster_stats_messages_pong_sent:187
?cluster_stats_messages_sent:354
?cluster_stats_messages_ping_received:182
?cluster_stats_messages_pong_received:167
?cluster_stats_messages_meet_received:5
?cluster_stats_messages_received:354
?127.0.0.1:6379> cluster nodes
?38d476004e54dcb651471fb46f40567f0ee72c89 192.168.1.12:6379@16379 master - 0 1647498718381 2 connected 5461-10922
?a7f6ab5c0d726d45099b63593b12676172b3f86b 192.168.1.11:6379@16379 myself,master - 0 1647498716000 1 connected 0-5460
?b2ec6c8a0ba9c7e8a2773d8f50fd99ef2d6a1de7 192.168.1.15:6379@16379 slave a7f6ab5c0d726d45099b63593b12676172b3f86b 0 1647498717378 5 connected
?4a260ed09fdc1eaee4896c6acf8185a5ff408faa 192.168.1.13:6379@16379 master - 0 1647498718080 3 connected 10923-16383
?7cc614aa214debdef74984e5e42becfbc60f4885 192.168.1.14:6379@16379 slave 4a260ed09fdc1eaee4896c6acf8185a5ff408faa 0 1647498718000 4 connected
?08accdc50545573fd75262bf1d304a0056fd2c6d 192.168.1.16:6379@16379 slave 38d476004e54dcb651471fb46f40567f0ee72c89 0 1647498717879 6 connected
?127.0.0.1:6379> set a b
?-> Redirected to slot [15495] located at 192.168.1.13:6379
?OK
?127.0.0.1:6379> get a
?-> Redirected to slot [15495] located at 192.168.1.14:6379
?"b"
?192.168.1.14:6379> get a b
?(error) ERR wrong number of arguments for 'get' command
?192.168.1.14:6379> get ?b
?-> Redirected to slot [3300] located at 192.168.1.11:6379
?(nil)
?192.168.1.11:6379> get ?a
?-> Redirected to slot [15495] located at 192.168.1.14:6379
?"b"
?192.168.1.14:6379> cluster nodes
?38d476004e54dcb651471fb46f40567f0ee72c89 192.168.1.12:6379@16379 master - 0 1647499631792 2 connected 5461-10922
?7cc614aa214debdef74984e5e42becfbc60f4885 192.168.1.14:6379@16379 myself,master - 0 1647499631000 7 connected 10923-16383
?4a260ed09fdc1eaee4896c6acf8185a5ff408faa 192.168.1.13:6379@16379 master,fail - 1647498826767 1647498825665 3 connected
?b2ec6c8a0ba9c7e8a2773d8f50fd99ef2d6a1de7 192.168.1.15:6379@16379 slave a7f6ab5c0d726d45099b63593b12676172b3f86b 0 1647499630000 5 connected
?a7f6ab5c0d726d45099b63593b12676172b3f86b 192.168.1.11:6379@16379 master - 0 1647499631000 1 connected 0-5460
?08accdc50545573fd75262bf1d304a0056fd2c6d 192.168.1.16:6379@16379 slave 38d476004e54dcb651471fb46f40567f0ee72c89 0 1647499631592 6 connected
?192.168.1.14:6379>