创建redis配置文件模板(自定义路径:/data1/redis-cluster)

redis-cluster.tmpl (我的IP地址为192.168.32.4,若是想公网访问,此处修改为公网IP。若是在云服务器,注意端口设置或实例设置)

  1. port ${PORT}
  2. protected-mode no
  3. cluster-enabled yes
  4. cluster-config-file nodes.conf
  5. cluster-node-timeout 5000
  6. cluster-announce-ip 192.168.32.4
  7. cluster-announce-port ${PORT}
  8. cluster-announce-bus-port 1${PORT}
  9. appendonly yes
  10. requirepass boya2019
  11. masterauth boya2019

创建各个容器配置文件和数据存放文件夹

  1. for port in {7000..7005}; do
  2. mkdir -p ./${port}/conf \
  3. && PORT=${port} envsubst < ./redis-cluster.tmpl > ./${port}/conf/redis.conf \
  4. && mkdir -p ./${port}/data; \
  5. done

创建 docker-compose.yml 文件

此处自定义文件(redis-cluster.yml)

  1. version: '3.4'
  2. services:
  3. redis-7000:
  4. image: redis:5.0.5
  5. container_name: redis-7000
  6. restart: always
  7. sysctls:
  8. - net.core.somaxconn=1024
  9. ports:
  10. - 7000:7000
  11. - 17000:17000
  12. volumes:
  13. - /data1/redis-cluster/7000/data:/data
  14. - /data1/redis-cluster/7000/conf/redis.conf:/usr/local/etc/redis/redis.conf
  15. command:
  16. redis-server /usr/local/etc/redis/redis.conf
  17. networks:
  18. - redisNet
  19. redis-7001:
  20. image: redis:5.0.5
  21. container_name: redis-7001
  22. restart: always
  23. sysctls:
  24. - net.core.somaxconn=1024
  25. ports:
  26. - 7001:7001
  27. - 17001:17001
  28. volumes:
  29. - /data1/redis-cluster/7001/data:/data
  30. - /data1/redis-cluster/7001/conf/redis.conf:/usr/local/etc/redis/redis.conf
  31. command:
  32. redis-server /usr/local/etc/redis/redis.conf
  33. networks:
  34. - redisNet
  35. redis-7002:
  36. image: redis:5.0.5
  37. container_name: redis-7002
  38. restart: always
  39. sysctls:
  40. - net.core.somaxconn=1024
  41. ports:
  42. - 7002:7002
  43. - 17002:17002
  44. volumes:
  45. - /data1/redis-cluster/7002/data:/data
  46. - /data1/redis-cluster/7002/conf/redis.conf:/usr/local/etc/redis/redis.conf
  47. command:
  48. redis-server /usr/local/etc/redis/redis.conf
  49. networks:
  50. - redisNet
  51. redis-7003:
  52. image: redis:5.0.5
  53. container_name: redis-7003
  54. restart: always
  55. sysctls:
  56. - net.core.somaxconn=1024
  57. ports:
  58. - 7003:7003
  59. - 17003:17003
  60. volumes:
  61. - /data1/redis-cluster/7003/data:/data
  62. - /data1/redis-cluster/7003/conf/redis.conf:/usr/local/etc/redis/redis.conf
  63. command:
  64. redis-server /usr/local/etc/redis/redis.conf
  65. networks:
  66. - redisNet
  67. redis-7004:
  68. image: redis:5.0.5
  69. container_name: redis-7004
  70. restart: always
  71. sysctls:
  72. - net.core.somaxconn=1024
  73. ports:
  74. - 7004:7004
  75. - 17004:17004
  76. volumes:
  77. - /data1/redis-cluster/7004/data:/data
  78. - /data1/redis-cluster/7004/conf/redis.conf:/usr/local/etc/redis/redis.conf
  79. command:
  80. redis-server /usr/local/etc/redis/redis.conf
  81. networks:
  82. - redisNet
  83. redis-7005:
  84. image: redis:5.0.5
  85. container_name: redis-7005
  86. restart: always
  87. sysctls:
  88. - net.core.somaxconn=1024
  89. ports:
  90. - 7005:7005
  91. - 17005:17005
  92. volumes:
  93. - /data1/redis-cluster/7005/data:/data
  94. - /data1/redis-cluster/7005/conf/redis.conf:/usr/local/etc/redis/redis.conf
  95. command:
  96. redis-server /usr/local/etc/redis/redis.conf
  97. networks:
  98. - redisNet
  99. networks:
  100. redisNet:

创建集群

启动所有的redis服务

[root@instance-2jubc0t5 redis-cluster]# docker-compose -f redis-cluster.yml up -d
Creating network "redis-cluster_redisNet" with the default driver
Creating redis-7002 ... done
Creating redis-7003 ... done
Creating redis-7000 ... done
Creating redis-7001 ... done
Creating redis-7005 ... done
Creating redis-7004 ... done

创建集群

[root@instance-2jubc0t5 ~]# docker exec -it redis-7000 bash
root@63a09bfb9fcb:/data# redis-cli --cluster create  192.168.32.4:7000  192.168.32.4:7001  192.168.32.4:7002  192.168.32.4:7003  192.168.32.4:7004  192.168.32.4:7005  --cluster-replicas 1 -a boya2019
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 192.168.32.4:7004 to 192.168.32.4:7000
Adding replica 192.168.32.4:7005 to 192.168.32.4:7001
Adding replica 192.168.32.4:7003 to 192.168.32.4:7002
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: a99d5ca1d87328fdc1d6c81fd78e262abb1be88f 192.168.32.4:7000
   slots:[0-5460] (5461 slots) master
M: ec289f8318512513ac4858bbd69dde2de66fb5cf 192.168.32.4:7001
   slots:[5461-10922] (5462 slots) master
M: 727b193790c06c77038f00c090ff9bc1e1047f0b 192.168.32.4:7002
   slots:[10923-16383] (5461 slots) master
S: dd18555a286d82471fcc19f31e244edc4e80cb43 192.168.32.4:7003
   replicates ec289f8318512513ac4858bbd69dde2de66fb5cf
S: 059915db65a6911538f394fae334a9ebd88f3c90 192.168.32.4:7004
   replicates 727b193790c06c77038f00c090ff9bc1e1047f0b
S: f57ebb8dcad17597674d01e3418c0b66b974555a 192.168.32.4:7005
   replicates a99d5ca1d87328fdc1d6c81fd78e262abb1be88f
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.32.4:7000)
M: a99d5ca1d87328fdc1d6c81fd78e262abb1be88f 192.168.32.4:7000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 727b193790c06c77038f00c090ff9bc1e1047f0b 192.168.32.4:7002
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: ec289f8318512513ac4858bbd69dde2de66fb5cf 192.168.32.4:7001
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: f57ebb8dcad17597674d01e3418c0b66b974555a 192.168.32.4:7005
   slots: (0 slots) slave
   replicates a99d5ca1d87328fdc1d6c81fd78e262abb1be88f
S: dd18555a286d82471fcc19f31e244edc4e80cb43 192.168.32.4:7003
   slots: (0 slots) slave
   replicates ec289f8318512513ac4858bbd69dde2de66fb5cf
S: 059915db65a6911538f394fae334a9ebd88f3c90 192.168.32.4:7004
   slots: (0 slots) slave
   replicates 727b193790c06c77038f00c090ff9bc1e1047f0b
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
root@63a09bfb9fcb:/data# exit
exit

[root@instance-2jubc0t5 ~]# docker exec -it redis-7000 bash
root@63a09bfb9fcb:/data# redis-cli -p 7000
127.0.0.1:7000> set k1 v1
(error) NOAUTH Authentication required.
127.0.0.1:7000> exit
root@63a09bfb9fcb:/data# redis-cli -p 7000 -c -a boya2019
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:7000> set k1 v1
-> Redirected to slot [12706] located at 192.168.32.4:7002
OK
192.168.32.4:7002> set k2 v2
-> Redirected to slot [449] located at 192.168.32.4:7000
OK
192.168.32.4:7000> [root@instance-2jubc0t5 ~]#