需要在服务器上提前安装 Docker 与 Docker-Compose
此处在一台服务器上以 Docker-Compose 方式部署了 6 个 Redis 节点,组建 3 主 3 从的 Redis Cluster 集群。
1 准备相关环境及文件 {#1-%E5%87%86%E5%A4%87%E7%9B%B8%E5%85%B3%E7%8E%AF%E5%A2%83%E5%8F%8A%E6%96%87%E4%BB%B6}
1.1 创建 Redis 文件目录 {#1.1-%E5%88%9B%E5%BB%BA-redis-%E6%96%87%E4%BB%B6%E7%9B%AE%E5%BD%95}
mkdir /opt/redis
准备 Redis 配置文件 redis.conf,放置于 /opt/redis 目录下,内容如下:
示例设置密码为 admin123456。
port 6379
daemonize no
protected-mode no
maxmemory-policy allkeys-lru
requirepass admin123456
masterauth admin123456
cluster-enabled yes
cluster-config-file nodes-c.conf
准备 docker-compose 运行时所需的 docker-compose.yml 文件,放置于 /opt/redis 目录下,内容如下:
version: '3'
services:
node1:
image: redis:6.2.6-alpine3.15
container_name: redis-node1
command: /bin/sh -c "redis-server /usr/local/etc/redis/redis.conf"
volumes:
-
/opt/redis/node1:/usr/local/etc/redis
-
/opt/redis/node1:/data
-
/opt/redis/node1:/log
network_mode: "host"
node2:
image: redis:6.2.6-alpine3.15
container_name: redis-node2
command: /bin/sh -c "redis-server /usr/local/etc/redis/redis.conf"
volumes:
-
/opt/redis/node2:/usr/local/etc/redis
-
/opt/redis/node2:/data
-
/opt/redis/node2:/log
network_mode: "host"
node3:
image: redis:6.2.6-alpine3.15
container_name: redis-node3
command: /bin/sh -c "redis-server /usr/local/etc/redis/redis.conf"
volumes:
-
/opt/redis/node3:/usr/local/etc/redis
-
/opt/redis/node3:/data
-
/opt/redis/node3:/log
network_mode: "host"
node4:
image: redis:6.2.6-alpine3.15
container_name: redis-node4
command: /bin/sh -c "redis-server /usr/local/etc/redis/redis.conf"
volumes:
-
/opt/redis/node4:/usr/local/etc/redis
-
/opt/redis/node4:/data
-
/opt/redis/node4:/log
network_mode: "host"
node5:
image: redis:6.2.6-alpine3.15
container_name: redis-node5
command: /bin/sh -c "redis-server /usr/local/etc/redis/redis.conf"
volumes:
-
/opt/redis/node5:/usr/local/etc/redis
-
/opt/redis/node5:/data
-
/opt/redis/node5:/log
network_mode: "host"
node6:
image: redis:6.2.6-alpine3.15
container_name: redis-node6
command: /bin/sh -c "redis-server /usr/local/etc/redis/redis.conf"
volumes:
-
/opt/redis/node6:/usr/local/etc/redis
-
/opt/redis/node6:/data
-
/opt/redis/node6:/log network_mode: "host"
1.2 创建 Redis 节点目录及配置文件 {#1.2-%E5%88%9B%E5%BB%BA-redis-%E8%8A%82%E7%82%B9%E7%9B%AE%E5%BD%95%E5%8F%8A%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6}
cd /opt/redis
mkdir node{1..6}
for i in {1..6};do \cp redis.conf node$i;done
# 修改各个 node 目录下的 redis.conf,将 port 依次改为 6379 6380 6381 6382 6383 6384
2 启动 Redis Cluster {#2-%E5%90%AF%E5%8A%A8-redis-cluster}
docker-compose up -d
3 配置集群 {#3-%E9%85%8D%E7%BD%AE%E9%9B%86%E7%BE%A4}
服务启动后,登录其中任意一个节点,进行主从集群的配置:
docker exec -it redis-node1 sh
将 6 个节点设置为 3 主 3 从模式
redis-cli -a admin123456 --cluster create 10.1.11.197:6379 10.1.11.197:6380 10.1.11.197:6381 10.1.11.197:6382 10.1.11.197:6383 10.1.11.197:6384 --cluster-replicas 1
4 验证集群 {#4-%E9%AA%8C%E8%AF%81%E9%9B%86%E7%BE%A4}
登录到任意一个 Redis 节点上:
docker exec -it redis-node1 sh
查看集群节点,可以看到 3 主 3 从的节点信息
redis-cli -a admin123456 -c cluster nodes
如果存在防火墙问题,可以将 Redis 的端口打开
firewall-cmd --add-port=6379-6384/tcp --zone=public --permanent firewall-cmd --reload