# 1.创建文件夹 >> mkdir redis-cluster && cd redis-cluster # 2.拷贝一份配置文件 >> cp /usr/local/redis/conf/redis.conf ./ # 3.修改配置 >> vim redis.conf dir . bind 0.0.0.0 requirepass 123456 protected-mode no port 8400 pidfile redis-8400.pid logfile redis-8400.log cluster-enabled yes cluster-config-file nodes-8400.conf cluster-node-timeout 5000 appendonly yes daemonize yes maxmemory 10g
(2) bash批量生成对应配置
1 2 3 4 5 6 7 8 9
for port in 8400 8401 8402 8403 8404 8405; do cp redis.conf redis-${port}.conf; if [ $port -ne 8400 ]; then sed -i "s/8400/${port}/g" redis-${port}.conf; fi; /usr/local/redis/bin/redis-server redis-${port}.conf; done
Can I set the above configuration? (type'yes' to accept): yes [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
(3) 测试一下
1 2 3 4 5 6 7 8 9 10 11
# 1.登录8400节点 >> redis-cli -c -h 192.168.89.64 -p 8400 -a '123456' 192.168.89.64:8400> set mirror 123 -> Redirected to slot [15439] located at 127.0.0.1:8402 OK # 2.退出连接后,更换一个节点来获取结果 127.0.0.1:8402> exit >> redis-cli -c -h 192.168.89.64 -p 8401 -a '123456' 192.168.89.64:8401> get mirror -> Redirected to slot [15439] located at 127.0.0.1:8402 "123"
(4) 停止服务
1 2 3 4 5 6 7 8
for port in 8400 8401 8402 8403 8404 8405; do redis-cli -c -h 192.168.89.64 -p $port -a '123456' shutdown done rm *.log rm *.pid rm nodes-*.conf rm *.rdb rm -rf appendonlydir
# 1.进入容器 >> docker exec -it redis-6379 bash # 2.创建集群 >> redis-cli -a 1234 --cluster create 10.124.132.23:6379 10.124.132.23:6380 10.124.132.23:6381 10.124.132.16:6379 10.124.132.16:6380 10.124.132.16:6381 --cluster-replicas 1 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. >>> Performing hash slots allocation on 6 nodes... Master[0] -> Slots 0 - 5460 Master[1] -> Slots 5461 - 10922 Master[2] -> Slots 10923 - 16383 Adding replica 10.124.132.16:6381 to 10.124.132.23:6379 Adding replica 10.124.132.23:6381 to 10.124.132.16:6379 Adding replica 10.124.132.16:6380 to 10.124.132.23:6380 M: a79eeb4564c564b93043e6a2b3187f931bf1f11f 10.124.132.23:6379 slots:[0-5460] (5461 slots) master M: 32945c041c174632c5d448143fc440da91a67e5a 10.124.132.23:6380 slots:[10923-16383] (5461 slots) master S: 345e489c0aa0e84644ffb853b0bb6381a92872c4 10.124.132.23:6381 replicates 6cbf25f8f3de92b6a135bc0caf2daf5192858979 M: 6cbf25f8f3de92b6a135bc0caf2daf5192858979 10.124.132.16:6379 slots:[5461-10922] (5462 slots) master S: 1c834fb401e8a1fe4fd5273731224088c915e9c7 10.124.132.16:6380 replicates 32945c041c174632c5d448143fc440da91a67e5a S: 1c295466bf89a355b6f9455600e04e15ea9a651e 10.124.132.16:6381 replicates a79eeb4564c564b93043e6a2b3187f931bf1f11f 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 10.124.132.23:6379) M: a79eeb4564c564b93043e6a2b3187f931bf1f11f 10.124.132.23:6379 slots:[0-5460] (5461 slots) master 1 additional replica(s) S: 1c834fb401e8a1fe4fd5273731224088c915e9c7 10.124.132.16:6380 slots: (0 slots) slave replicates 32945c041c174632c5d448143fc440da91a67e5a M: 6cbf25f8f3de92b6a135bc0caf2daf5192858979 10.124.132.16:6379 slots:[5461-10922] (5462 slots) master 1 additional replica(s) S: 1c295466bf89a355b6f9455600e04e15ea9a651e 10.124.132.16:6381 slots: (0 slots) slave replicates a79eeb4564c564b93043e6a2b3187f931bf1f11f M: 32945c041c174632c5d448143fc440da91a67e5a 10.124.132.23:6380 slots:[10923-16383] (5461 slots) master 1 additional replica(s) S: 345e489c0aa0e84644ffb853b0bb6381a92872c4 10.124.132.23:6381 slots: (0 slots) slave replicates 6cbf25f8f3de92b6a135bc0caf2daf5192858979 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
这样就成功了。
(5) 验证集群
1 2 3 4 5 6 7 8
>> redis-cli -c -h 10.124.132.23 -p 6379 -a 1234 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 10.124.132.23:6379> set mirror cjy -> Redirected to slot [15439] located at 10.124.132.23:6380 OK 10.124.132.23:6379> get mirror -> Redirected to slot [15439] located at 10.124.132.23:6380 "cjy"