- 两台机器,将下边的脚本分别在两台机器上执行;sh redis.sh
- 进入任意一个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:
#!/bin/bash#mkdir $HOME/redis-cluster && cd $HOME/redis-clustercat > redis-cluster.tmpl << EOFbind 0.0.0.0protected-mode yesport \$PORTtcp-backlog 511timeout 0tcp-keepalive 300#daemonize yessupervised nopidfile /var/run/redis_\$PORT.pidloglevel noticelogfile ""databases 16always-show-logo yessave 900 1save 300 10save 60 10000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename dump.rdbdir /datareplica-serve-stale-data yesreplica-read-only yesrepl-diskless-sync norepl-diskless-sync-delay 5repl-disable-tcp-nodelay noreplica-priority 100lazyfree-lazy-eviction nolazyfree-lazy-expire nolazyfree-lazy-server-del noreplica-lazy-flush noappendonly noappendfilename "appendonly.aof"appendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mbaof-load-truncated yesaof-use-rdb-preamble yeslua-time-limit 5000slowlog-log-slower-than 10000slowlog-max-len 128latency-monitor-threshold 0notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-size -2list-compress-depth 0set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000stream-node-max-bytes 4096stream-node-max-entries 100activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit replica 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10dynamic-hz yesaof-rewrite-incremental-fsync yesrdb-save-incremental-fsync yescluster-enabled yescluster-config-file nodes_\$PORT.confcluster-node-timeout 15000EOFfor port in `seq 6001 6003`; do \mkdir -p redis-${port} \&& PORT=${port} envsubst < redis-cluster.tmpl > redis-${port}/redis.conf \&& mkdir -p redis-${port}/data;\donecat > docker-compose.yaml << EOFversion: "3.6"services:redis-6001:image: redis:5.0.4container_name: redis-6001restart: alwaysnetwork_mode: "host"volumes:- $HOME/redis-cluster/redis-6001/redis.conf:/etc/redis/redis.conf- $HOME/redis-cluster/redis-6001/data:/datacommand: redis-server /etc/redis/redis.confredis-6002:image: redis:5.0.4container_name: redis-6002restart: alwaysnetwork_mode: "host"volumes:- $HOME/redis-cluster/redis-6002/redis.conf:/etc/redis/redis.conf- $HOME/redis-cluster/redis-6002/data:/datacommand: redis-server /etc/redis/redis.confredis-6003:image: redis:5.0.4container_name: redis-6003restart: alwaysnetwork_mode: "host"volumes:- $HOME/redis-cluster/redis-6003/redis.conf:/etc/redis/redis.conf- $HOME/redis-cluster/redis-6003/data:/datacommand: redis-server /etc/redis/redis.confEOFdocker-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
