创建redis配置文件模板(自定义路径:/data1/redis-cluster)
redis-cluster.tmpl (我的IP地址为192.168.32.4,若是想公网访问,此处修改为公网IP。若是在云服务器,注意端口设置或实例设置)
port ${PORT}
protected-mode no
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 192.168.32.4
cluster-announce-port ${PORT}
cluster-announce-bus-port 1${PORT}
appendonly yes
requirepass boya2019
masterauth boya2019
创建各个容器配置文件和数据存放文件夹
for port in {7000..7005}; do
mkdir -p ./${port}/conf \
&& PORT=${port} envsubst < ./redis-cluster.tmpl > ./${port}/conf/redis.conf \
&& mkdir -p ./${port}/data; \
done
创建 docker-compose.yml 文件
此处自定义文件(redis-cluster.yml)
version: '3.4'
services:
redis-7000:
image: redis:5.0.5
container_name: redis-7000
restart: always
sysctls:
- net.core.somaxconn=1024
ports:
- 7000:7000
- 17000:17000
volumes:
- /data1/redis-cluster/7000/data:/data
- /data1/redis-cluster/7000/conf/redis.conf:/usr/local/etc/redis/redis.conf
command:
redis-server /usr/local/etc/redis/redis.conf
networks:
- redisNet
redis-7001:
image: redis:5.0.5
container_name: redis-7001
restart: always
sysctls:
- net.core.somaxconn=1024
ports:
- 7001:7001
- 17001:17001
volumes:
- /data1/redis-cluster/7001/data:/data
- /data1/redis-cluster/7001/conf/redis.conf:/usr/local/etc/redis/redis.conf
command:
redis-server /usr/local/etc/redis/redis.conf
networks:
- redisNet
redis-7002:
image: redis:5.0.5
container_name: redis-7002
restart: always
sysctls:
- net.core.somaxconn=1024
ports:
- 7002:7002
- 17002:17002
volumes:
- /data1/redis-cluster/7002/data:/data
- /data1/redis-cluster/7002/conf/redis.conf:/usr/local/etc/redis/redis.conf
command:
redis-server /usr/local/etc/redis/redis.conf
networks:
- redisNet
redis-7003:
image: redis:5.0.5
container_name: redis-7003
restart: always
sysctls:
- net.core.somaxconn=1024
ports:
- 7003:7003
- 17003:17003
volumes:
- /data1/redis-cluster/7003/data:/data
- /data1/redis-cluster/7003/conf/redis.conf:/usr/local/etc/redis/redis.conf
command:
redis-server /usr/local/etc/redis/redis.conf
networks:
- redisNet
redis-7004:
image: redis:5.0.5
container_name: redis-7004
restart: always
sysctls:
- net.core.somaxconn=1024
ports:
- 7004:7004
- 17004:17004
volumes:
- /data1/redis-cluster/7004/data:/data
- /data1/redis-cluster/7004/conf/redis.conf:/usr/local/etc/redis/redis.conf
command:
redis-server /usr/local/etc/redis/redis.conf
networks:
- redisNet
redis-7005:
image: redis:5.0.5
container_name: redis-7005
restart: always
sysctls:
- net.core.somaxconn=1024
ports:
- 7005:7005
- 17005:17005
volumes:
- /data1/redis-cluster/7005/data:/data
- /data1/redis-cluster/7005/conf/redis.conf:/usr/local/etc/redis/redis.conf
command:
redis-server /usr/local/etc/redis/redis.conf
networks:
- redisNet
networks:
redisNet:
创建集群
启动所有的redis服务
[root@instance-2jubc0t5 redis-cluster]# docker-compose -f redis-cluster.yml up -d
Creating network "redis-cluster_redisNet" with the default driver
Creating redis-7002 ... done
Creating redis-7003 ... done
Creating redis-7000 ... done
Creating redis-7001 ... done
Creating redis-7005 ... done
Creating redis-7004 ... done
创建集群
[root@instance-2jubc0t5 ~]# docker exec -it redis-7000 bash
root@63a09bfb9fcb:/data# redis-cli --cluster create 192.168.32.4:7000 192.168.32.4:7001 192.168.32.4:7002 192.168.32.4:7003 192.168.32.4:7004 192.168.32.4:7005 --cluster-replicas 1 -a boya2019
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.32.4:7004 to 192.168.32.4:7000
Adding replica 192.168.32.4:7005 to 192.168.32.4:7001
Adding replica 192.168.32.4:7003 to 192.168.32.4:7002
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: a99d5ca1d87328fdc1d6c81fd78e262abb1be88f 192.168.32.4:7000
slots:[0-5460] (5461 slots) master
M: ec289f8318512513ac4858bbd69dde2de66fb5cf 192.168.32.4:7001
slots:[5461-10922] (5462 slots) master
M: 727b193790c06c77038f00c090ff9bc1e1047f0b 192.168.32.4:7002
slots:[10923-16383] (5461 slots) master
S: dd18555a286d82471fcc19f31e244edc4e80cb43 192.168.32.4:7003
replicates ec289f8318512513ac4858bbd69dde2de66fb5cf
S: 059915db65a6911538f394fae334a9ebd88f3c90 192.168.32.4:7004
replicates 727b193790c06c77038f00c090ff9bc1e1047f0b
S: f57ebb8dcad17597674d01e3418c0b66b974555a 192.168.32.4:7005
replicates a99d5ca1d87328fdc1d6c81fd78e262abb1be88f
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
...
>>> Performing Cluster Check (using node 192.168.32.4:7000)
M: a99d5ca1d87328fdc1d6c81fd78e262abb1be88f 192.168.32.4:7000
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 727b193790c06c77038f00c090ff9bc1e1047f0b 192.168.32.4:7002
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
M: ec289f8318512513ac4858bbd69dde2de66fb5cf 192.168.32.4:7001
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: f57ebb8dcad17597674d01e3418c0b66b974555a 192.168.32.4:7005
slots: (0 slots) slave
replicates a99d5ca1d87328fdc1d6c81fd78e262abb1be88f
S: dd18555a286d82471fcc19f31e244edc4e80cb43 192.168.32.4:7003
slots: (0 slots) slave
replicates ec289f8318512513ac4858bbd69dde2de66fb5cf
S: 059915db65a6911538f394fae334a9ebd88f3c90 192.168.32.4:7004
slots: (0 slots) slave
replicates 727b193790c06c77038f00c090ff9bc1e1047f0b
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
root@63a09bfb9fcb:/data# exit
exit
[root@instance-2jubc0t5 ~]# docker exec -it redis-7000 bash
root@63a09bfb9fcb:/data# redis-cli -p 7000
127.0.0.1:7000> set k1 v1
(error) NOAUTH Authentication required.
127.0.0.1:7000> exit
root@63a09bfb9fcb:/data# redis-cli -p 7000 -c -a boya2019
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:7000> set k1 v1
-> Redirected to slot [12706] located at 192.168.32.4:7002
OK
192.168.32.4:7002> set k2 v2
-> Redirected to slot [449] located at 192.168.32.4:7000
OK
192.168.32.4:7000> [root@instance-2jubc0t5 ~]#