单节点部署

  1. version: '3.1'
  2. networks:
  3. base:
  4. services:
  5. #redis
  6. redis:
  7. image: redis:latest
  8. container_name: redis
  9. restart: always
  10. command:
  11. --appendonly yes
  12. --requirepass "123456"
  13. --notify-keyspace-events Ex
  14. ports:
  15. - 6379:6379
  16. networks:
  17. - base
  18. environment:
  19. - TZ=Asia/Shanghai
  20. volumes:
  21. - /etc/localtime:/etc/localtime:ro # 设置容器时区与宿主机保持一致
  22. - ./redis/data:/data
  23. - ./redis/redis.conf:/etc/redis.conf

Cluster集群部署

第一步:启动所有节点

要注意修改每个实例的端口以及宿主机ip

  1. version: "3.8"
  2. # 定义服务,可以多个
  3. services:
  4. redis-6371: # 服务名称
  5. image: redis:6.2.4 # 创建容器时所需的镜像
  6. container_name: redis-6371 # 容器名称
  7. restart: always # 容器总是重新启动
  8. volumes: # 数据卷,目录挂载
  9. - ./redis-6371/data:/data
  10. ports:
  11. - 6371:6371
  12. - 16371:16371
  13. environment:
  14. - TZ=Asia/Shanghai
  15. command:
  16. # protected-mode no 设置外部网络连接redis服务,no:外部网络可以直接访问
  17. # masterauth "123456" 当master服务设置了密码保护时,slav服务连接master的密码
  18. # cluster-node-timeout 5000 节点超时时间
  19. # cluster-announce-ip 122.112.141.95 这里是宿主机IP
  20. # notify-keyspace-events Ex 开启key过期事件监听功能
  21. --port 6371
  22. --protected-mode no
  23. --appendonly yes
  24. --requirepass "123456"
  25. --masterauth "123456"
  26. --notify-keyspace-events Ex
  27. --cluster-enabled yes
  28. --cluster-node-timeout 5000
  29. --cluster-announce-ip 122.112.141.95
  30. --cluster-config-file nodes-6371.conf
  31. --cluster-announce-port 6371
  32. --cluster-announce-bus-port 16371
  33. redis-6372:
  34. image: redis:6.2.4
  35. container_name: redis-6372
  36. restart: always # 容器总是重新启动
  37. volumes: # 数据卷,目录挂载
  38. - ./redis-6372/data:/data
  39. ports:
  40. - 6372:6372
  41. - 16372:16372
  42. environment:
  43. - TZ=Asia/Shanghai
  44. command:
  45. --port 6372
  46. --protected-mode no
  47. --appendonly yes
  48. --requirepass "123456"
  49. --masterauth "123456"
  50. --notify-keyspace-events Ex
  51. --cluster-enabled yes
  52. --cluster-node-timeout 5000
  53. --cluster-announce-ip 122.112.141.95
  54. --cluster-config-file nodes-6372.conf
  55. --cluster-announce-port 6372
  56. --cluster-announce-bus-port 16372
  57. redis-6373:
  58. image: redis:6.2.4
  59. container_name: redis-6373
  60. restart: always # 容器总是重新启动
  61. volumes: # 数据卷,目录挂载
  62. - ./redis-6373/data:/data
  63. ports:
  64. - 6373:6373
  65. - 16373:16373
  66. environment:
  67. - TZ=Asia/Shanghai
  68. command:
  69. --port 6373
  70. --protected-mode no
  71. --appendonly yes
  72. --requirepass "123456"
  73. --masterauth "123456"
  74. --notify-keyspace-events Ex
  75. --cluster-enabled yes
  76. --cluster-node-timeout 5000
  77. --cluster-announce-ip 122.112.141.95
  78. --cluster-config-file nodes-6373.conf
  79. --cluster-announce-port 6373
  80. --cluster-announce-bus-port 16373
  81. redis-6374:
  82. image: redis:6.2.4
  83. container_name: redis-6374
  84. restart: always # 容器总是重新启动
  85. volumes: # 数据卷,目录挂载
  86. - ./redis-6374/data:/data
  87. ports:
  88. - 6374:6374
  89. - 16374:16374
  90. environment:
  91. - TZ=Asia/Shanghai
  92. command:
  93. --port 6374
  94. --protected-mode no
  95. --appendonly yes
  96. --requirepass "123456"
  97. --masterauth "123456"
  98. --notify-keyspace-events Ex
  99. --cluster-enabled yes
  100. --cluster-node-timeout 5000
  101. --cluster-announce-ip 122.112.141.95
  102. --cluster-config-file nodes-6374.conf
  103. --cluster-announce-port 6374
  104. --cluster-announce-bus-port 16374
  105. redis-6375:
  106. image: redis:6.2.4
  107. container_name: redis-6375
  108. restart: always # 容器总是重新启动
  109. volumes: # 数据卷,目录挂载
  110. - ./redis-6375/data:/data
  111. ports:
  112. - 6375:6375
  113. - 16375:16375
  114. environment:
  115. - TZ=Asia/Shanghai
  116. command:
  117. --port 6375
  118. --protected-mode no
  119. --appendonly yes
  120. --requirepass "123456"
  121. --masterauth "123456"
  122. --notify-keyspace-events Ex
  123. --cluster-enabled yes
  124. --cluster-node-timeout 5000
  125. --cluster-announce-ip 122.112.141.95
  126. --cluster-config-file nodes-6375.conf
  127. --cluster-announce-port 6375
  128. --cluster-announce-bus-port 16375
  129. redis-6376:
  130. image: redis:6.2.4
  131. container_name: redis-6376
  132. restart: always # 容器总是重新启动
  133. volumes: # 数据卷,目录挂载
  134. - ./redis-6376/data:/data
  135. ports:
  136. - 6376:6376
  137. - 16376:16376
  138. environment:
  139. - TZ=Asia/Shanghai
  140. command:
  141. --port 6376
  142. --protected-mode no
  143. --appendonly yes
  144. --requirepass "123456"
  145. --masterauth "123456"
  146. --notify-keyspace-events Ex
  147. --cluster-enabled yes
  148. --cluster-node-timeout 5000
  149. --cluster-announce-ip 122.112.141.95
  150. --cluster-config-file nodes-6376.conf
  151. --cluster-announce-port 6376
  152. --cluster-announce-bus-port 16376

第二步:创建cluster集群

进入任一实例容器,执行命令:
redis-cli -a 123456 —cluster create 122.112.141.95:6371 122.112.141.95:6372 122.112.141.95:6373 122.112.141.95:6374 122.112.141.95:6375 122.112.141.95:6376 —cluster-replicas 1
—cluster-replicas 1 表示每个主节点的从节点数量
输出如下:

  1. root@99b5f39b9982:/data# redis-cli -a 123456 --cluster create 122.112.141.95:6371 122.112.141.95:6372 122.112.141.95:6373 122.112.141.95:6374 122.112.141.95:6375 122.112.141.95:6376 --cluster-replicas 1
  2. Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
  3. >>> Performing hash slots allocation on 6 nodes...
  4. Master[0] -> Slots 0 - 5460
  5. Master[1] -> Slots 5461 - 10922
  6. Master[2] -> Slots 10923 - 16383
  7. Adding replica 122.112.141.95:6375 to 122.112.141.95:6371
  8. Adding replica 122.112.141.95:6376 to 122.112.141.95:6372
  9. Adding replica 122.112.141.95:6374 to 122.112.141.95:6373
  10. >>> Trying to optimize slaves allocation for anti-affinity
  11. [WARNING] Some slaves are in the same host as their master
  12. M: e4c4ea75206125357df7124c781327415fd7f84f 122.112.141.95:6371
  13. slots:[0-5460] (5461 slots) master
  14. M: cbc82977fe3b07a6e676d47be5264f2c26d6e707 122.112.141.95:6372
  15. slots:[5461-10922] (5462 slots) master
  16. M: f915bab39755559facb2e8856a671b610295d278 122.112.141.95:6373
  17. slots:[10923-16383] (5461 slots) master
  18. S: 0fd3ab8031426ff08590b3121ba0899b43b5d15e 122.112.141.95:6374
  19. replicates e4c4ea75206125357df7124c781327415fd7f84f
  20. S: 4819bfadead695deb86cb67238f85d3905124d21 122.112.141.95:6375
  21. replicates cbc82977fe3b07a6e676d47be5264f2c26d6e707
  22. S: bf2472dd2271dd28f4aa9d24f7b85956493ff6af 122.112.141.95:6376
  23. replicates f915bab39755559facb2e8856a671b610295d278
  24. Can I set the above configuration? (type 'yes' to accept): yes # 输入yes 并回车 确认节点 主从身份 以及 哈希槽的分配
  25. >>> Nodes configuration updated
  26. >>> Assign a different config epoch to each node
  27. >>> Sending CLUSTER MEET messages to join the cluster
  28. Waiting for the cluster to join
  29. >>> Performing Cluster Check (using node 122.112.141.95:6371)
  30. M: e4c4ea75206125357df7124c781327415fd7f84f 122.112.141.95:6371
  31. slots:[0-5460] (5461 slots) master
  32. 1 additional replica(s)
  33. S: 4819bfadead695deb86cb67238f85d3905124d21 122.112.141.95:6375
  34. slots: (0 slots) slave
  35. replicates cbc82977fe3b07a6e676d47be5264f2c26d6e707
  36. S: 0fd3ab8031426ff08590b3121ba0899b43b5d15e 122.112.141.95:6374
  37. slots: (0 slots) slave
  38. replicates e4c4ea75206125357df7124c781327415fd7f84f
  39. S: bf2472dd2271dd28f4aa9d24f7b85956493ff6af 122.112.141.95:6376
  40. slots: (0 slots) slave
  41. replicates f915bab39755559facb2e8856a671b610295d278
  42. M: cbc82977fe3b07a6e676d47be5264f2c26d6e707 122.112.141.95:6372
  43. slots:[5461-10922] (5462 slots) master
  44. 1 additional replica(s)
  45. M: f915bab39755559facb2e8856a671b610295d278 122.112.141.95:6373
  46. slots:[10923-16383] (5461 slots) master
  47. 1 additional replica(s)
  48. [OK] All nodes agree about slots configuration.
  49. >>> Check for open slots...
  50. >>> Check slots coverage...
  51. [OK] All 16384 slots covered.

从输出内容可见各个节点的主从配置