Docker搭建Redis集群
注意:Redis搭建集群最少需要6个Redis节点,其中3个作为主节点,3个作为从节点;
为了方便,这里用Docker在一台机器上启动6个容器来作为集群,生产上建议用多台服务器搭建;
#!/bin/bash
if [ ! $1 ]; then
echo "请输入Redis配置文件存放路径,例如:/home"
exit 1
fi
for port in $(seq 1 6);
do
mkdir -p $1/redis/node-${port}/conf
mkdir -p $1/redis/node-${port}/data
touch $1/redis/node-${port}/conf/redis.conf
cat << EOF > $1/redis/node-${port}/conf/redis.conf
#port(端口号)
port 6379
bind 0.0.0.0
#masterauth(设置集群节点间访问密码,跟下面一致)
masterauth 2022
#requirepass(设置Redis访问密码)
requirepass 2022
#cluster-enabled yes(启动集群模式)
cluster-enabled yes
#cluster-config-file nodes.conf(集群节点信息文件)
cluster-config-file nodes.conf
#cluster-node-timeout 5000(Redis节点宕机被发现的时间)
cluster-node-timeout 5000
#cluster-announce-ip(集群节点的汇报ip,防止nat,预先填写为网关ip后续需要手动修改配置文件)
cluster-announce-ip 172.18.0.1${port}
#cluster-announce-port(集群节点的汇报port,防止nat)
cluster-announce-port 6379
#cluster-announce-bus-port(集群节点的汇报bus-port,防止nat)
cluster-announce-bus-port 16379
#appendonly yes(开启aof)
appendonly yes
EOF
done
运行脚本,在home目录下可以看到创建好的6个Redis配置文件
(base) ➜ ~ chmod +x redis_conf.sh
(base) ➜ ~ ./redis_conf.sh /home
自行修改IP和端口,需要和上面的Redis配置文件保持一致
version: '3'
services:
# redis-1
redis-1:
image: redis:latest
container_name: redis-1
hostname: redis-1
ports:
- "6371:6379"
- "16371:16379"
command: "redis-server /etc/redis/redis.conf"
environment:
- TZ=Asia/Shanghai
volumes:
- ./redis/node-1/data:/data
- ./redis/node-1/conf/redis.conf:/etc/redis/redis.conf
networks:
tools_net:
ipv4_address: 172.18.0.11
# redis-2
redis-2:
image: redis:latest
container_name: redis-2
hostname: redis-2
ports:
- "6372:6379"
- "16372:16379"
command: "redis-server /etc/redis/redis.conf"
environment:
- TZ=Asia/Shanghai
volumes:
- ./redis/node-2/data:/data
- ./redis/node-2/conf/redis.conf:/etc/redis/redis.conf
networks:
tools_net:
ipv4_address: 172.18.0.12
# redis-3
redis-3:
image: redis:latest
container_name: redis-3
hostname: redis-3
ports:
- "6373:6379"
- "16373:16379"
command: "redis-server /etc/redis/redis.conf"
environment:
- TZ=Asia/Shanghai
volumes:
- ./redis/node-3/data:/data
- ./redis/node-3/conf/redis.conf:/etc/redis/redis.conf
networks:
tools_net:
ipv4_address: 172.18.0.13
# redis-4
redis-4:
image: redis:latest
container_name: redis-4
hostname: redis-4
ports:
- "6374:6379"
- "16374:16379"
command: "redis-server /etc/redis/redis.conf"
environment:
- TZ=Asia/Shanghai
volumes:
- ./redis/node-4/data:/data
- ./redis/node-4/conf/redis.conf:/etc/redis/redis.conf
networks:
tools_net:
ipv4_address: 172.18.0.14
# redis-5
redis-5:
image: redis:latest
container_name: redis-5
hostname: redis-5
ports:
- "6375:6379"
- "16375:16379"
command: "redis-server /etc/redis/redis.conf"
environment:
- TZ=Asia/Shanghai
volumes:
- ./redis/node-5/data:/data
- ./redis/node-5/conf/redis.conf:/etc/redis/redis.conf
networks:
tools_net:
ipv4_address: 172.18.0.15
# redis-6
redis-6:
image: redis:latest
container_name: redis-6
hostname: redis-6
ports:
- "6376:6379"
- "16376:16379"
command: "redis-server /etc/redis/redis.conf"
environment:
- TZ=Asia/Shanghai
volumes:
- ./redis/node-6/data:/data
- ./redis/node-6/conf/redis.conf:/etc/redis/redis.conf
networks:
tools_net:
ipv4_address: 172.18.0.16
networks:
tools_net:
ipam:
config:
- subnet: 172.18.0.0/24
启动Redis
(base) ➜ ~ docker-compose up -d
(base) ➜ ~ docker ps
进入其中一台容器,进行Redis集群配置
redis-cli -a 2022 --cluster create 172.18.0.11:6379 172.18.0.12:6379 172.18.0.13:6379 172.18.0.14:6379 172.18.0.15:6379 172.18.0.16:6379 --cluster-replicas 1
(base) ➜ ~ docker exec -it redis-1 bash
# -a 为上面设置的密码
root@redis-1:/data# redis-cli -a 2022 --cluster create 172.18.0.11:6379 172.18.0.12:6379 172.18.0.13:6379 172.18.0.14:6379 172.18.0.15:6379 172.18.0.16:6379 --cluster-replicas 1
# 回车后,会有提示,是否同意设置上面的配置,输入yes,然后集群就算搭建成功了;
新增一个key,可以看到,数据成功的存入了redis-2这台容器
# 进入其中一台容器,执行如下命令,以集群模式进入
root@redis-1:/data# redis-cli -c -a 2022
127.0.0.1:6379> set name yid
-> Redirected to slot [5798] located at 172.18.0.12:6379
OK
172.18.0.12:6379> get name
"yid"
# 查看节点
172.18.0.12:6379> CLUSTER NODES
转载:https://blog.csdn.net/weixin_43577131/article/details/127942067
查看评论