1. #创建redis配置文件
    2. for port in $(seq 1 6); \
    3. do \
    4. mkdir -p /mydata/redis/node-${port}/conf
    5. touch /mydata/redis/node-${port}/conf/redis.conf
    6. cat << EOF >>/mydata/redis/node-${port}/conf/redis.conf
    7. port 6379
    8. bind 0.0.0.0
    9. cluster-enabled yes
    10. cluster-config-file nodes.conf
    11. cluster-node-timeout 5000
    12. cluster-announce-ip 172.38.0.1${port}
    13. cluster-announce-port 6379
    14. cluster-announce-bus-port 16379
    15. appendonly yes
    16. EOF
    17. done
    18. #编写docker-compose.yml文件
    19. version: '2'
    20. services:
    21. redis-cluster-6371:
    22. image: redis:5.0.7
    23. container_name: node-1 #节点名称
    24. ports:
    25. - 6371:6379
    26. - 16371:16379
    27. volumes:
    28. - /mydata/redis/node-1/conf/redis.conf:/etc/redis/redis.conf
    29. - /mydata/redis/node-1/log:/var/log/redis
    30. - /mydata/redis/node-1/data:/data
    31. command: sh -c "redis-server /etc/redis/redis.conf"
    32. environment:
    33. # 设置时区为上海,否则时间会有问题
    34. - TZ=Asia/Shanghai
    35. networks:
    36. redis:
    37. ipv4_address: 172.38.0.11
    38. redis-cluster-6372:
    39. image: redis:5.0.7
    40. container_name: node-2 #节点名称
    41. ports:
    42. - 6372:6379
    43. - 16372:16379
    44. volumes:
    45. - /mydata/redis/node-2/conf/redis.conf:/etc/redis/redis.conf
    46. - /mydata/redis/node-2/log:/var/log/redis
    47. - /mydata/redis/node-2/data:/data
    48. command: sh -c "redis-server /etc/redis/redis.conf"
    49. environment:
    50. # 设置时区为上海,否则时间会有问题
    51. - TZ=Asia/Shanghai
    52. networks:
    53. redis:
    54. ipv4_address: 172.38.0.12
    55. redis-cluster-6373:
    56. image: redis:5.0.7
    57. container_name: node-3 #节点名称
    58. ports:
    59. - 6373:6379
    60. - 16373:16379
    61. volumes:
    62. - /mydata/redis/node-3/conf/redis.conf:/etc/redis/redis.conf
    63. - /mydata/redis/node-3/log:/var/log/redis
    64. - /mydata/redis/node-3/data:/data
    65. command: sh -c "redis-server /etc/redis/redis.conf"
    66. environment:
    67. # 设置时区为上海,否则时间会有问题
    68. - TZ=Asia/Shanghai
    69. networks:
    70. redis:
    71. ipv4_address: 172.38.0.13
    72. redis-cluster-6374:
    73. image: redis:5.0.7
    74. container_name: node-4 #节点名称
    75. ports:
    76. - 6374:6379
    77. - 16374:16379
    78. volumes:
    79. - /mydata/redis/node-4/conf/redis.conf:/etc/redis/redis.conf
    80. - /mydata/redis/node-4/log:/var/log/redis
    81. - /mydata/redis/node-4/data:/data
    82. command: sh -c "redis-server /etc/redis/redis.conf"
    83. environment:
    84. # 设置时区为上海,否则时间会有问题
    85. - TZ=Asia/Shanghai
    86. networks:
    87. redis:
    88. ipv4_address: 172.38.0.14
    89. redis-cluster-6375:
    90. image: redis:5.0.7
    91. container_name: node-5 #节点名称
    92. ports:
    93. - 6375:6379
    94. - 16375:16379
    95. volumes:
    96. - /mydata/redis/node-5/conf/redis.conf:/etc/redis/redis.conf
    97. - /mydata/redis/node-5/log:/var/log/redis
    98. - /mydata/redis/node-5/data:/data
    99. command: sh -c "redis-server /etc/redis/redis.conf"
    100. environment:
    101. # 设置时区为上海,否则时间会有问题
    102. - TZ=Asia/Shanghai
    103. networks:
    104. redis:
    105. ipv4_address: 172.38.0.15
    106. redis-cluster-6376:
    107. image: redis:5.0.7
    108. container_name: node-6 #节点名称
    109. ports:
    110. - 6376:6379
    111. - 16376:16379
    112. volumes:
    113. - /mydata/redis/node-6/conf/redis.conf:/etc/redis/redis.conf
    114. - /mydata/redis/node-6/log:/var/log/redis
    115. - /mydata/redis/node-6/data:/data
    116. command: sh -c "redis-server /etc/redis/redis.conf"
    117. environment:
    118. # 设置时区为上海,否则时间会有问题
    119. - TZ=Asia/Shanghai
    120. networks:
    121. redis:
    122. ipv4_address: 172.38.0.16
    123. networks:
    124. redis:
    125. ipam:
    126. config:
    127. - subnet: 172.38.0.0/16
    128. gateway: 172.38.0.1
    129. #将docker-compose文件移动到linux服务器上,使用
    130. docker-compose up
    131. #命令运行,会自动的下载并运行六个Redis。
    132. #配置redis集群
    133. docker run --rm -it --network redis_redis zvelo/redis-trib create --replicas 1 172.38.0.11:6379 172.38.0.12:6379 172.38.0.13:6379 172.38.0.14:6379 172.38.0.15:6379 172.38.0.16:6379