1. 两台机器,将下边的脚本分别在两台机器上执行;sh redis.sh
  2. 进入任意一个docker容器内并启动集群服务:```

    docker exec -it $(docker ps | grep 600 |head -n 1| awk ‘{print $1}’) bash

    redis-cli —cluster create IP1:6001 IP1:6002 IP1:6003 IP2:6004 IP2:6005 IP2:6006 —cluster-replicas 1

    输入完上条命令后,需要输入yes完成集群创建 ```

注意:脚本是已6001-6006为集群的监听端口,如需其他端口,修改对应的脚本和命令。

机器1,redis.sh:

  1. #!/bin/bash
  2. #
  3. mkdir $HOME/redis-cluster && cd $HOME/redis-cluster
  4. cat > redis-cluster.tmpl << EOF
  5. bind 0.0.0.0
  6. protected-mode yes
  7. port \$PORT
  8. tcp-backlog 511
  9. timeout 0
  10. tcp-keepalive 300
  11. #daemonize yes
  12. supervised no
  13. pidfile /var/run/redis_\$PORT.pid
  14. loglevel notice
  15. logfile ""
  16. databases 16
  17. always-show-logo yes
  18. save 900 1
  19. save 300 10
  20. save 60 10000
  21. stop-writes-on-bgsave-error yes
  22. rdbcompression yes
  23. rdbchecksum yes
  24. dbfilename dump.rdb
  25. dir /data
  26. replica-serve-stale-data yes
  27. replica-read-only yes
  28. repl-diskless-sync no
  29. repl-diskless-sync-delay 5
  30. repl-disable-tcp-nodelay no
  31. replica-priority 100
  32. lazyfree-lazy-eviction no
  33. lazyfree-lazy-expire no
  34. lazyfree-lazy-server-del no
  35. replica-lazy-flush no
  36. appendonly no
  37. appendfilename "appendonly.aof"
  38. appendfsync everysec
  39. no-appendfsync-on-rewrite no
  40. auto-aof-rewrite-percentage 100
  41. auto-aof-rewrite-min-size 64mb
  42. aof-load-truncated yes
  43. aof-use-rdb-preamble yes
  44. lua-time-limit 5000
  45. slowlog-log-slower-than 10000
  46. slowlog-max-len 128
  47. latency-monitor-threshold 0
  48. notify-keyspace-events ""
  49. hash-max-ziplist-entries 512
  50. hash-max-ziplist-value 64
  51. list-max-ziplist-size -2
  52. list-compress-depth 0
  53. set-max-intset-entries 512
  54. zset-max-ziplist-entries 128
  55. zset-max-ziplist-value 64
  56. hll-sparse-max-bytes 3000
  57. stream-node-max-bytes 4096
  58. stream-node-max-entries 100
  59. activerehashing yes
  60. client-output-buffer-limit normal 0 0 0
  61. client-output-buffer-limit replica 256mb 64mb 60
  62. client-output-buffer-limit pubsub 32mb 8mb 60
  63. hz 10
  64. dynamic-hz yes
  65. aof-rewrite-incremental-fsync yes
  66. rdb-save-incremental-fsync yes
  67. cluster-enabled yes
  68. cluster-config-file nodes_\$PORT.conf
  69. cluster-node-timeout 15000
  70. EOF
  71. for port in `seq 6001 6003`; do \
  72. mkdir -p redis-${port} \
  73. && PORT=${port} envsubst < redis-cluster.tmpl > redis-${port}/redis.conf \
  74. && mkdir -p redis-${port}/data;\
  75. done
  76. cat > docker-compose.yaml << EOF
  77. version: "3.6"
  78. services:
  79. redis-6001:
  80. image: redis:5.0.4
  81. container_name: redis-6001
  82. restart: always
  83. network_mode: "host"
  84. volumes:
  85. - $HOME/redis-cluster/redis-6001/redis.conf:/etc/redis/redis.conf
  86. - $HOME/redis-cluster/redis-6001/data:/data
  87. command: redis-server /etc/redis/redis.conf
  88. redis-6002:
  89. image: redis:5.0.4
  90. container_name: redis-6002
  91. restart: always
  92. network_mode: "host"
  93. volumes:
  94. - $HOME/redis-cluster/redis-6002/redis.conf:/etc/redis/redis.conf
  95. - $HOME/redis-cluster/redis-6002/data:/data
  96. command: redis-server /etc/redis/redis.conf
  97. redis-6003:
  98. image: redis:5.0.4
  99. container_name: redis-6003
  100. restart: always
  101. network_mode: "host"
  102. volumes:
  103. - $HOME/redis-cluster/redis-6003/redis.conf:/etc/redis/redis.conf
  104. - $HOME/redis-cluster/redis-6003/data:/data
  105. command: redis-server /etc/redis/redis.conf
  106. EOF
  107. docker-compose up -d

机器2,redis.sh:

#!/bin/bash
#
mkdir $HOME/redis-cluster && cd $HOME/redis-cluster

cat > redis-cluster.tmpl << EOF
bind 0.0.0.0
protected-mode yes
port \$PORT
tcp-backlog 511
timeout 0
tcp-keepalive 300
#daemonize yes
supervised no
pidfile /var/run/redis_\$PORT.pid
loglevel notice
logfile ""
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /data
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes

cluster-enabled yes
cluster-config-file nodes_\$PORT.conf
cluster-node-timeout 15000
EOF

for port in `seq 6004 6006`; do \
mkdir -p redis-${port} \
&& PORT=${port} envsubst < redis-cluster.tmpl > redis-${port}/redis.conf \
&& mkdir -p redis-${port}/data;\
done

cat > docker-compose.yaml << EOF
version: "3.6"

services:
redis-6004:
    image: redis:5.0.4
    container_name: redis-6004
    restart: always
    network_mode: "host"
    volumes:
    - $HOME/redis-cluster/redis-6004/redis.conf:/etc/redis/redis.conf
    - $HOME/redis-cluster/redis-6004/data:/data
    command: redis-server /etc/redis/redis.conf

redis-6005:
    image: redis:5.0.4
    container_name: redis-6005
    restart: always
    network_mode: "host"
    volumes:
    - $HOME/redis-cluster/redis-6005/redis.conf:/etc/redis/redis.conf
    - $HOME/redis-cluster/redis-6005/data:/data
    command: redis-server /etc/redis/redis.conf

redis-6006:
    image: redis:5.0.4
    container_name: redis-6006
    restart: always
    network_mode: "host"
    volumes:
    - $HOME/redis-cluster/redis-6006/redis.conf:/etc/redis/redis.conf
    - $HOME/redis-cluster/redis-6006/data:/data
    command: redis-server /etc/redis/redis.conf
EOF

docker-compose up -d