单节点部署
version: '3.1'networks:base:services:#redisredis:image: redis:latestcontainer_name: redisrestart: alwayscommand:--appendonly yes--requirepass "123456"--notify-keyspace-events Exports:- 6379:6379networks:- baseenvironment:- TZ=Asia/Shanghaivolumes:- /etc/localtime:/etc/localtime:ro # 设置容器时区与宿主机保持一致- ./redis/data:/data- ./redis/redis.conf:/etc/redis.conf
Cluster集群部署
第一步:启动所有节点
要注意修改每个实例的端口以及宿主机ip
version: "3.8"# 定义服务,可以多个services:redis-6371: # 服务名称image: redis:6.2.4 # 创建容器时所需的镜像container_name: redis-6371 # 容器名称restart: always # 容器总是重新启动volumes: # 数据卷,目录挂载- ./redis-6371/data:/dataports:- 6371:6371- 16371:16371environment:- TZ=Asia/Shanghaicommand:# protected-mode no 设置外部网络连接redis服务,no:外部网络可以直接访问# masterauth "123456" 当master服务设置了密码保护时,slav服务连接master的密码# cluster-node-timeout 5000 节点超时时间# cluster-announce-ip 122.112.141.95 这里是宿主机IP# notify-keyspace-events Ex 开启key过期事件监听功能--port 6371--protected-mode no--appendonly yes--requirepass "123456"--masterauth "123456"--notify-keyspace-events Ex--cluster-enabled yes--cluster-node-timeout 5000--cluster-announce-ip 122.112.141.95--cluster-config-file nodes-6371.conf--cluster-announce-port 6371--cluster-announce-bus-port 16371redis-6372:image: redis:6.2.4container_name: redis-6372restart: always # 容器总是重新启动volumes: # 数据卷,目录挂载- ./redis-6372/data:/dataports:- 6372:6372- 16372:16372environment:- TZ=Asia/Shanghaicommand:--port 6372--protected-mode no--appendonly yes--requirepass "123456"--masterauth "123456"--notify-keyspace-events Ex--cluster-enabled yes--cluster-node-timeout 5000--cluster-announce-ip 122.112.141.95--cluster-config-file nodes-6372.conf--cluster-announce-port 6372--cluster-announce-bus-port 16372redis-6373:image: redis:6.2.4container_name: redis-6373restart: always # 容器总是重新启动volumes: # 数据卷,目录挂载- ./redis-6373/data:/dataports:- 6373:6373- 16373:16373environment:- TZ=Asia/Shanghaicommand:--port 6373--protected-mode no--appendonly yes--requirepass "123456"--masterauth "123456"--notify-keyspace-events Ex--cluster-enabled yes--cluster-node-timeout 5000--cluster-announce-ip 122.112.141.95--cluster-config-file nodes-6373.conf--cluster-announce-port 6373--cluster-announce-bus-port 16373redis-6374:image: redis:6.2.4container_name: redis-6374restart: always # 容器总是重新启动volumes: # 数据卷,目录挂载- ./redis-6374/data:/dataports:- 6374:6374- 16374:16374environment:- TZ=Asia/Shanghaicommand:--port 6374--protected-mode no--appendonly yes--requirepass "123456"--masterauth "123456"--notify-keyspace-events Ex--cluster-enabled yes--cluster-node-timeout 5000--cluster-announce-ip 122.112.141.95--cluster-config-file nodes-6374.conf--cluster-announce-port 6374--cluster-announce-bus-port 16374redis-6375:image: redis:6.2.4container_name: redis-6375restart: always # 容器总是重新启动volumes: # 数据卷,目录挂载- ./redis-6375/data:/dataports:- 6375:6375- 16375:16375environment:- TZ=Asia/Shanghaicommand:--port 6375--protected-mode no--appendonly yes--requirepass "123456"--masterauth "123456"--notify-keyspace-events Ex--cluster-enabled yes--cluster-node-timeout 5000--cluster-announce-ip 122.112.141.95--cluster-config-file nodes-6375.conf--cluster-announce-port 6375--cluster-announce-bus-port 16375redis-6376:image: redis:6.2.4container_name: redis-6376restart: always # 容器总是重新启动volumes: # 数据卷,目录挂载- ./redis-6376/data:/dataports:- 6376:6376- 16376:16376environment:- TZ=Asia/Shanghaicommand:--port 6376--protected-mode no--appendonly yes--requirepass "123456"--masterauth "123456"--notify-keyspace-events Ex--cluster-enabled yes--cluster-node-timeout 5000--cluster-announce-ip 122.112.141.95--cluster-config-file nodes-6376.conf--cluster-announce-port 6376--cluster-announce-bus-port 16376
第二步:创建cluster集群
进入任一实例容器,执行命令:
redis-cli -a 123456 —cluster create 122.112.141.95:6371 122.112.141.95:6372 122.112.141.95:6373 122.112.141.95:6374 122.112.141.95:6375 122.112.141.95:6376 —cluster-replicas 1
—cluster-replicas 1 表示每个主节点的从节点数量
输出如下:
root@99b5f39b9982:/data# redis-cli -a 123456 --cluster create 122.112.141.95:6371 122.112.141.95:6372 122.112.141.95:6373 122.112.141.95:6374 122.112.141.95:6375 122.112.141.95:6376 --cluster-replicas 1Warning: 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 - 5460Master[1] -> Slots 5461 - 10922Master[2] -> Slots 10923 - 16383Adding replica 122.112.141.95:6375 to 122.112.141.95:6371Adding replica 122.112.141.95:6376 to 122.112.141.95:6372Adding replica 122.112.141.95:6374 to 122.112.141.95:6373>>> Trying to optimize slaves allocation for anti-affinity[WARNING] Some slaves are in the same host as their masterM: e4c4ea75206125357df7124c781327415fd7f84f 122.112.141.95:6371slots:[0-5460] (5461 slots) masterM: cbc82977fe3b07a6e676d47be5264f2c26d6e707 122.112.141.95:6372slots:[5461-10922] (5462 slots) masterM: f915bab39755559facb2e8856a671b610295d278 122.112.141.95:6373slots:[10923-16383] (5461 slots) masterS: 0fd3ab8031426ff08590b3121ba0899b43b5d15e 122.112.141.95:6374replicates e4c4ea75206125357df7124c781327415fd7f84fS: 4819bfadead695deb86cb67238f85d3905124d21 122.112.141.95:6375replicates cbc82977fe3b07a6e676d47be5264f2c26d6e707S: bf2472dd2271dd28f4aa9d24f7b85956493ff6af 122.112.141.95:6376replicates f915bab39755559facb2e8856a671b610295d278Can 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 clusterWaiting for the cluster to join>>> Performing Cluster Check (using node 122.112.141.95:6371)M: e4c4ea75206125357df7124c781327415fd7f84f 122.112.141.95:6371slots:[0-5460] (5461 slots) master1 additional replica(s)S: 4819bfadead695deb86cb67238f85d3905124d21 122.112.141.95:6375slots: (0 slots) slavereplicates cbc82977fe3b07a6e676d47be5264f2c26d6e707S: 0fd3ab8031426ff08590b3121ba0899b43b5d15e 122.112.141.95:6374slots: (0 slots) slavereplicates e4c4ea75206125357df7124c781327415fd7f84fS: bf2472dd2271dd28f4aa9d24f7b85956493ff6af 122.112.141.95:6376slots: (0 slots) slavereplicates f915bab39755559facb2e8856a671b610295d278M: cbc82977fe3b07a6e676d47be5264f2c26d6e707 122.112.141.95:6372slots:[5461-10922] (5462 slots) master1 additional replica(s)M: f915bab39755559facb2e8856a671b610295d278 122.112.141.95:6373slots:[10923-16383] (5461 slots) master1 additional replica(s)[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.
从输出内容可见各个节点的主从配置
