一 拉取镜像

  1. docker pull redis:6.0.7

二 创建模板文件

  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.64.2
  7. #cluster-announce-port ${PORT}
  8. #cluster-announce-bus-port 1${PORT}
  9. bind 0.0.0.0
  10. appendonly yes
  11. logfile "redis.log"

三 生成配置文件

生成配置文件和文件夹

  1. cd /Users/hezhaoming/Documents/docker/redis
  2. for port in `seq 7010 7015`; do \
  3. mkdir -p ./${port}/conf \
  4. && PORT=${port} envsubst < ./redis-cluster.tmpl > ./${port}/conf/redis.conf \
  5. && mkdir -p ./${port}/data; \
  6. done

注意mac需要安装命令工具

  1. brew install gettext
  2. brew link --force gettext

四 编写编排文件

  1. version: '3'
  2. services:
  3. redis1:
  4. image: redis:6.0.7
  5. hostname: redis1
  6. container_name: redis1
  7. networks:
  8. redis-net:
  9. ipv4_address: 172.35.0.10
  10. restart: always
  11. privileged: true
  12. ports:
  13. - 7010:7010
  14. - 17010:17010
  15. volumes:
  16. - /Users/hezhaoming/Documents/docker/redis/7010/conf/redis.conf:/usr/local/etc/redis/redis.conf
  17. - /Users/hezhaoming/Documents/docker/redis/7010/data:/data
  18. command: redis-server /usr/local/etc/redis/redis.conf
  19. redis2:
  20. image: redis:6.0.7
  21. hostname: redis2
  22. container_name: redis2
  23. networks:
  24. redis-net:
  25. ipv4_address: 172.35.0.20
  26. restart: always
  27. privileged: true
  28. ports:
  29. - 7011:7011
  30. - 17011:17011
  31. volumes:
  32. - /Users/hezhaoming/Documents/docker/redis/7011/conf/redis.conf:/usr/local/etc/redis/redis.conf
  33. - /Users/hezhaoming/Documents/docker/redis/7011/data:/data
  34. command: redis-server /usr/local/etc/redis/redis.conf
  35. redis3:
  36. image: redis:6.0.7
  37. hostname: redis3
  38. container_name: redis3
  39. networks:
  40. redis-net:
  41. ipv4_address: 172.35.0.30
  42. restart: always
  43. privileged: true
  44. ports:
  45. - 7012:7012
  46. - 17012:17012
  47. volumes:
  48. - /Users/hezhaoming/Documents/docker/redis/7012/conf/redis.conf:/usr/local/etc/redis/redis.conf
  49. - /Users/hezhaoming/Documents/docker/redis/7012/data:/data
  50. command: redis-server /usr/local/etc/redis/redis.conf
  51. redis4:
  52. image: redis:6.0.7
  53. hostname: redis4
  54. container_name: redis4
  55. networks:
  56. redis-net:
  57. ipv4_address: 172.35.0.40
  58. restart: always
  59. privileged: true
  60. ports:
  61. - 7013:7013
  62. - 17013:17013
  63. volumes:
  64. - /Users/hezhaoming/Documents/docker/redis/7013/conf/redis.conf:/usr/local/etc/redis/redis.conf
  65. - /Users/hezhaoming/Documents/docker/redis/7013/data:/data
  66. command: redis-server /usr/local/etc/redis/redis.conf
  67. redis5:
  68. image: redis:6.0.7
  69. hostname: redis5
  70. container_name: redis5
  71. networks:
  72. redis-net:
  73. ipv4_address: 172.35.0.50
  74. restart: always
  75. privileged: true
  76. ports:
  77. - 7014:7014
  78. - 17014:17014
  79. volumes:
  80. - /Users/hezhaoming/Documents/docker/redis/7014/conf/redis.conf:/usr/local/etc/redis/redis.conf
  81. - /Users/hezhaoming/Documents/docker/redis/7014/data:/data
  82. command: redis-server /usr/local/etc/redis/redis.conf
  83. redis6:
  84. image: redis:6.0.7
  85. hostname: redis6
  86. container_name: redis6
  87. networks:
  88. redis-net:
  89. ipv4_address: 172.35.0.60
  90. restart: always
  91. privileged: true
  92. ports:
  93. - 7015:7015
  94. - 17015:17015
  95. volumes:
  96. - /Users/hezhaoming/Documents/docker/redis/7015/conf/redis.conf:/usr/local/etc/redis/redis.conf
  97. - /Users/hezhaoming/Documents/docker/redis/7015/data:/data
  98. command: redis-server /usr/local/etc/redis/redis.conf
  99. networks:
  100. redis-net:
  101. driver: bridge
  102. ipam:
  103. driver: default
  104. config:
  105. - subnet: 172.35.0.1/24

五 执行集群命令

  1. docker-compose up -d
  2. //随机进入一个节点 执行集群命令
  3. redis-cli --cluster create 172.35.0.10:7010 172.35.0.20:7011 172.35.0.30:7012 172.35.0.40:7013 172.35.0.50:7014 172.35.0.60:7015 --cluster-replicas 1
  4. // 交互提示是否创建,输入yes 回车
  5. //创建成功
  6. //随便进入一个节点 比如最后一个节点
  7. redis-cli -c -h 172.35.0.60 -p 7015
  8. //输入命令,查看节点信息
  9. info
  10. cluster nodes

六 测试使用

image.png

工具查看,也是OK的
image.png