1. 程式人生 > 其它 >四、實戰:部署Redis叢集

四、實戰:部署Redis叢集

實戰:部署Redis叢集 1.建立網絡卡 [root@docker ~]# docker network create redis --subnet 192.168.2.0/24 --gateway 192.168.2.1 97304262c0e2e2a98f9635e60fb5d80e2c9c73aaae849d4866487e181f917fcf [root@docker ~]# docker network ls NETWORK IDNAMEDRIVERSCOPE 219769a74201bridgebridgelocal f6b9d84b1f81hosthostlocal 2bb4bf4c0539mynetbridgelocal 47132fb890acnonenulllocal 97304262c0e2redisbridgelocal 2.通過指令碼建立6個redis配置檔案 for port in {1..6}; do mkdir -p /mydata/redis/node-${port}/conf touch /mydata/redis/node-${port}/conf/redis.conf cat >>/mydata/redis/node-${port}/conf/redis.conf << EOF port 6379 bind 0.0.0.0 cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 cluster-announce-ip 192.168.2.1${port} cluster-announce-port 6379 cluster-announce-bus-port 16379 appendonly yes EOF done 3.啟動redis容器 容器-1 [root@docker ~]# 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 --network redis --ip 192.168.2.11 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf 容器-2 [root@docker ~]# 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 --network redis --ip 192.168.2.12 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf 容器-3 [root@docker ~]# 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 --network redis --ip 192.168.2.13 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf 容器-4 [root@docker ~]# 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 --network redis --ip 192.168.2.14 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf c881a22475f3ca563ed33e46eaad4522e6e7f02581e6dd708bd7ca819fdda23a 容器-5 [root@docker ~]# 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 --network redis --ip 192.168.2.15 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf 3272fae0293aef7674823375b9f0640d172bbac0054ea26a1d9bc70ba7e52974 容器-6 [root@docker ~]# 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 --network redis --ip 192.168.2.16 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf 4.建立叢集 [root@docker ~]# docker exec -it redis-1 /bin/sh /data # redis-cli --cluster create 192.168.2.11:6379 192.168.2.12:6379 192.168.2.13:6379 192.168.2.14:6379 192.168.2.15:6379 192.168.2.16:6379 --cluster-replicas1 /data # 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:115 cluster_stats_messages_pong_sent:105 cluster_stats_messages_sent:220 cluster_stats_messages_ping_received:100 cluster_stats_messages_pong_received:115 cluster_stats_messages_meet_received:5 cluster_stats_messages_received:220 127.0.0.1:6379> cluster nodes c9a1d39bc42bdb9a4b69ba21f5e6219249228d7f 192.168.2.11:6379@16379 myself,master - 0 1632712331000 1 connected 0-5460 69e6de33d8109bdd6502ded5236b6934009e86de 192.168.2.13:6379@16379 master - 0 1632712331000 3 connected 10923-16383 fa3e39d75667267f3de00344ed026a068174f70d 192.168.2.16:6379@16379 slave 2deddc69cc4423ee3bb3468282786aada2aaece3 0 1632712332000 6 conn 2deddc69cc4423ee3bb3468282786aada2aaece3 192.168.2.12:6379@16379 master - 0 1632712330235 2 connected 5461-10922 94036c138b2ea711a1e286ee9a5ff24e88267326 192.168.2.14:6379@16379 slave 69e6de33d8109bdd6502ded5236b6934009e86de 0 1632712332250 4 conn 91c7263eebc80758d4a270263ef45a1649b4f5f6 192.168.2.15:6379@16379 slave c9a1d39bc42bdb9a4b69ba21f5e6219249228d7f 0 1632712331242 5 conn 127.0.0.1:6379> set a b -> Redirected to slot [15495] located at 192.168.2.13:6379 OK 測試高可用: [root@docker ~]# docker stop redis-3 redis-3 [root@docker ~]# docker exec -it redis-1 /bin/sh /data # redis-cli -c 127.0.0.1:6379> get a -> Redirected to slot [15495] located at 192.168.2.14:6379 "b" 192.168.2.14:6379> cluster nodes 94036c138b2ea711a1e286ee9a5ff24e88267326 192.168.2.14:6379@16379 myself,master - 0 1632712512000 7 connected 10923-16383 c9a1d39bc42bdb9a4b69ba21f5e6219249228d7f 192.168.2.11:6379@16379 master - 0 1632712512501 1 connected 0-5460 2deddc69cc4423ee3bb3468282786aada2aaece3 192.168.2.12:6379@16379 master - 0 1632712513709 2 connected 5461-10922 fa3e39d75667267f3de00344ed026a068174f70d 192.168.2.16:6379@16379 slave 2deddc69cc4423ee3bb3468282786aada2aaece3 0 1632712512199 6 connected 69e6de33d8109bdd6502ded5236b6934009e86de 192.168.2.13:6379@16379 master,fail - 1632712430316 1632712428105 3 connected 91c7263eebc80758d4a270263ef45a1649b4f5f6 192.168.2.15:6379@16379 slave c9a1d39bc42bdb9a4b69ba21f5e6219249228d7f 0 1632712513205 1 connected [root@docker ~]# docker start redis-3 redis-3 [root@docker ~]# docker exec -it redis-1 /bin/sh /data # redis-cli -c 127.0.0.1:6379> cluster nodes c9a1d39bc42bdb9a4b69ba21f5e6219249228d7f 192.168.2.11:6379@16379 myself,master - 0 1632712776000 1 connected 0-5460 69e6de33d8109bdd6502ded5236b6934009e86de 192.168.2.13:6379@16379 slave 94036c138b2ea711a1e286ee9a5ff24e88267326 0 1632712776000 7 connected fa3e39d75667267f3de00344ed026a068174f70d 192.168.2.16:6379@16379 slave 2deddc69cc4423ee3bb3468282786aada2aaece3 0 1632712775599 6 connected 2deddc69cc4423ee3bb3468282786aada2aaece3 192.168.2.12:6379@16379 master - 0 1632712774592 2 connected 5461-10922 94036c138b2ea711a1e286ee9a5ff24e88267326 192.168.2.14:6379@16379 master - 0 1632712776506 7 connected 10923-16383 91c7263eebc80758d4a270263ef45a1649b4f5f6 192.168.2.15:6379@16379 slave c9a1d39bc42bdb9a4b69ba21f5e6219249228d7f 0 1632712776000 5 connected