Docker 搭建 Redis Cluster 集群
2021/11/27 19:14:07
本文主要是介绍Docker 搭建 Redis Cluster 集群,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Redis 配置文件
根据需要自己可以定制
# bind 127.0.0.1 port ${PORT} protected-mode no cluster-enabled yes cluster-config-file nodes-${PORT}.conf cluster-node-timeout 5000 cluster-announce-ip 10.0.18.8 cluster-announce-port ${PORT} cluster-announce-bus-port 1${PORT} appendonly yes appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb
bind
:IP地址需要注释;port
:节点端口;requirepass
:添加访问认证;masterauth
:如果主节点开启了访问认证,从节点访问主节点需要认证;protected-mode
:保护模式,默认值 yes,即开启。开启保护模式以后,需配置bind ip
或者设置访问密码;关闭保护模式,外部网络可以直接访问;daemonize
:是否以守护线程的方式启动(后台启动),默认 noappendonly
:是否开启 AOF 持久化模式,默认 no;cluster-config-file
:集群节点信息文件;cluster-node-timeout
:集群节点连接超时时间;cluster-announce-ip
:集群节点 IP,填写宿主机的 IP;cluster-announce-port
:集群节点映射端口;cluster-announce-bus-port
:集群节点总线端口。
每个 Redis 集群节点都需要打开两个 TCP 连接。
一个用于为客户端提供服务的正常 Redis TCP 端口,例如 6379。还有一个基于 6379 端口加 10000 的端口,比如 16379。
第二个端口用于集群总线,这是一个使用二进制协议的节点到节点通信通道。
节点使用集群总线进行故障检测、配置更新、故障转移授权等等。
客户端永远不要尝试与集群总线端口通信,与正常的 Redis 命令端口通信即可
但是请确保防火墙中的这两个端口都已经打开,否则 Redis 集群节点将无法通信
创建可执行文件,执行下面的命令,创建 9001~9006 相关目录和文件
for port in `seq 9001 9006`; do \ mkdir -p ./redis-cluster/${port}/conf \ && PORT=${port} envsubst < ./redis.conf > ./redis-cluster/${port}/conf/redis.conf \ && mkdir -p ./redis-cluster/${port}/data; \ done
执行tree 命令,查看结果
再查看节点配置文件
ubuntu@VM-16-6-ubuntu:/usr/local/redis/cluster$ vim /usr/local/redis/cluster/redis-cluster/900{1...6}/conf/redis.conf
运行 Redis 容器
第一种方式
docker-compose
下表显示了哪些Compose文件版本支持特定的Docker版本。
配置 docker-compose.yml 文件
version: '3.7' services: redis9001: image: 'redis' restart: always container_name: redis9001 command: ["redis-server", "/usr/local/etc/redis/redis.conf"] volumes: - ./redis-cluster/9001/conf/redis.conf:/usr/local/etc/redis/redis.conf - ./redis-cluster/9001/data:/data ports: - "9001:9001" - "19001:19001" environment: # 设置时区为上海,否则时间会有问题 - TZ=Asia/Shanghai privileged: true redis9002: image: 'redis' restart: always container_name: redis9002 command: ["redis-server", "/usr/local/etc/redis/redis.conf"] volumes: - ./redis-cluster/9002/conf/redis.conf:/usr/local/etc/redis/redis.conf - ./redis-cluster/9002/data:/data ports: - "9002:9002" - "19002:19002" environment: # 设置时区为上海,否则时间会有问题 - TZ=Asia/Shanghai privileged: true redis9003: image: 'redis' restart: always container_name: redis9003 command: ["redis-server", "/usr/local/etc/redis/redis.conf"] volumes: - ./redis-cluster/9003/conf/redis.conf:/usr/local/etc/redis/redis.conf - ./redis-cluster/9003/data:/data ports: - "9003:9003" - "19003:19003" environment: # 设置时区为上海,否则时间会有问题 - TZ=Asia/Shanghai privileged: true redis9004: image: 'redis' restart: always container_name: redis9004 command: ["redis-server", "/usr/local/etc/redis/redis.conf"] volumes: - ./redis-cluster/9004/conf/redis.conf:/usr/local/etc/redis/redis.conf - ./redis-cluster/9004/data:/data ports: - "9004:9004" - "19004:19004" environment: # 设置时区为上海,否则时间会有问题 - TZ=Asia/Shanghai privileged: true redis9005: image: 'redis' restart: always container_name: redis9005 command: ["redis-server", "/usr/local/etc/redis/redis.conf"] volumes: - ./redis-cluster/9005/conf/redis.conf:/usr/local/etc/redis/redis.conf - ./redis-cluster/9005/data:/data ports: - "9005:9005" - "19005:19005" environment: # 设置时区为上海,否则时间会有问题 - TZ=Asia/Shanghai privileged: true redis9006: image: 'redis' restart: always container_name: redis9006 command: ["redis-server", "/usr/local/etc/redis/redis.conf"] volumes: - ./redis-cluster/9006/conf/redis.conf:/usr/local/etc/redis/redis.conf - ./redis-cluster/9006/data:/data ports: - "9006:9006" - "19006:19006" environment: # 设置时区为上海,否则时间会有问题 - TZ=Asia/Shanghai privileged: true
启动
# 后台启动 ubuntu@VM-16-6-ubuntu:/usr/local/redis/cluster$ sudo docker-compose up -d # 查看容器列表 ubuntu@VM-16-6-ubuntu:/usr/local/redis/cluster$ sudo docker-compose ps
第二种方式
创建可执行文件,命令行的方式启动
for port in $(seq 9001 9006); do \ docker run -di --restart always --name redis${port} --net host \ -v /usr/local/redis/cluster/redis-cluster/${port}/conf/redis.conf:/usr/local/etc/redis/redis.conf \ -v /usr/local/redis/cluster/redis-cluster/${port}/data:/data \ redis redis-server /usr/local/etc/redis/redis.conf; \ done
使用命令查看容器是否启动
ubuntu@VM-16-6-ubuntu:/usr/local/redis/cluster/redis-cluster$ sudo docker ps -a
创建Redis Cluster 集群
进入容器的指定目录下,通过命令创建集群
ubuntu@VM-16-6-ubuntu:/usr/local/redis/cluster/redis-cluster$ sudo docker exec -it redis9001 bash root@VM-16-6-ubuntu:/data# cd /usr/local/bin/ root@VM-16-6-ubuntu:/usr/local/bin# ./redis-cli --cluster create 176.25.153.251:9001 176.25.153.251:9002 176.25.153.251:9003 176.25.153.251:9004 176.25.153.251:9005 176.25.153.251:9006 --cluster-replicas 1 >>> Performing hash slots allocation on 6 nodes... Master[0] -> Slots 0 - 5460 Master[1] -> Slots 5461 - 10922 Master[2] -> Slots 10923 - 16383 Adding replica 176.25.153.251:9005 to 176.25.153.251:9001 Adding replica 176.25.153.251:9006 to 176.25.153.251:9002 Adding replica 176.25.153.251:9004 to 176.25.153.251:9003 >>> Trying to optimize slaves allocation for anti-affinity [WARNING] Some slaves are in the same host as their master M: 7955c3772b747eedd9449e2ad0051ccd7c8cfaee 176.25.153.251:9001 slots:[0-5460] (5461 slots) master M: 1cc6412fd24eb0cc11ea8021bebb5ae86af5cd19 176.25.153.251:9002 slots:[5461-10922] (5462 slots) master M: 87bb09ba6faa22f8bc953c02d8405d687bf2132e 176.25.153.251:9003 slots:[10923-16383] (5461 slots) master S: 56430b6383100b63088735cf7d62a58e57dc5c7f 176.25.153.251:9004 replicates 7955c3772b747eedd9449e2ad0051ccd7c8cfaee S: dca7cdc669b000847d57b50903bea4abe0ee9792 176.25.153.251:9005 replicates 1cc6412fd24eb0cc11ea8021bebb5ae86af5cd19 S: 28b82a3788ea3af89fd3a447f94d93b284b6de0b 176.25.153.251:9006 replicates 87bb09ba6faa22f8bc953c02d8405d687bf2132e Can I set the above configuration? (type 'yes' to accept): yes # 出现提示,输入 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 176.25.153.251:9001) M: 7955c3772b747eedd9449e2ad0051ccd7c8cfaee 176.25.153.251:9001 slots:[0-5460] (5461 slots) master 1 additional replica(s) M: 87bb09ba6faa22f8bc953c02d8405d687bf2132e 10.0.18.8:9003 slots:[10923-16383] (5461 slots) master 1 additional replica(s) S: 28b82a3788ea3af89fd3a447f94d93b284b6de0b 10.0.18.8:9006 slots: (0 slots) slave replicates 87bb09ba6faa22f8bc953c02d8405d687bf2132e S: 56430b6383100b63088735cf7d62a58e57dc5c7f 10.0.18.8:9004 slots: (0 slots) slave replicates 7955c3772b747eedd9449e2ad0051ccd7c8cfaee S: dca7cdc669b000847d57b50903bea4abe0ee9792 10.0.18.8:9005 slots: (0 slots) slave replicates 1cc6412fd24eb0cc11ea8021bebb5ae86af5cd19 M: 1cc6412fd24eb0cc11ea8021bebb5ae86af5cd19 10.0.18.8:9002 slots:[5461-10922] (5462 slots) master 1 additional replica(s) [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. 175.24.153.255:9001> cluster info cluster_state:ok cluster_slots_assigned:16384 cluster_slots_ok:16384 cluster_slots_pfail:0 cluster_slots_fail:0 cluster_known_nodes:6 cluster_size:3 cluster_current_epoch:6 cluster_my_epoch:1 cluster_stats_messages_ping_sent:184 cluster_stats_messages_pong_sent:192 cluster_stats_messages_sent:376 cluster_stats_messages_ping_received:187 cluster_stats_messages_pong_received:184 cluster_stats_messages_meet_received:5 cluster_stats_messages_received:376 176.25.153.251:9001> cluster nodes 87bb09ba6faa22f8bc953c02d8405d687bf2132e 10.0.18.8:9003@19003 master - 0 1636013178000 3 connected 10923-16383 28b82a3788ea3af89fd3a447f94d93b284b6de0b 10.0.18.8:9006@19006 slave 87bb09ba6faa22f8bc953c02d8405d687bf2132e 0 1636013179125 3 connected 56430b6383100b63088735cf7d62a58e57dc5c7f 10.0.18.8:9004@19004 slave 7955c3772b747eedd9449e2ad0051ccd7c8cfaee 0 1636013179000 1 connected dca7cdc669b000847d57b50903bea4abe0ee9792 10.0.18.8:9005@19005 slave 1cc6412fd24eb0cc11ea8021bebb5ae86af5cd19 0 1636013181132 2 connected 7955c3772b747eedd9449e2ad0051ccd7c8cfaee 10.0.18.8:9001@19001 myself,master - 0 1636013178000 1 connected 0-5460 1cc6412fd24eb0cc11ea8021bebb5ae86af5cd19 10.0.18.8:9002@19002 master - 0 1636013180128 2 connected 5461-10922
还可以使用这种方式 创建集群
ubuntu@VM-16-6-ubuntu:/usr/local/redis/cluster/redis-cluster$ sudo docker exec -it redis9001 redis-cli -p 9001 --cluster create 176.25.153.251:9001 176.25.153.251:9002 176.25.153.251:9003 176.25.153.251:9004 176.25.153.251:9005 176.25.153.251:9006 --cluster-replicas 1
了解更多可扫码关注公众号
这篇关于Docker 搭建 Redis Cluster 集群的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-24Redis资料:新手入门快速指南
- 2024-12-24Redis资料:新手入门教程与实践指南
- 2024-12-24Redis资料:新手入门教程与实践指南
- 2024-12-07Redis高并发入门详解
- 2024-12-07Redis缓存入门:新手必读指南
- 2024-12-07Redis缓存入门:新手必读教程
- 2024-12-07Redis入门:新手必备的简单教程
- 2024-12-07Redis入门:新手必读的简单教程
- 2024-12-06Redis入门教程:从安装到基本操作
- 2024-12-06Redis缓存入门教程:轻松掌握缓存技巧