docker上部署redis cluster集群

1 篇文章 0 订阅
1 篇文章 0 订阅
集群配置

总共六个redis,其中3个作为master,另外3个作为replica

docker-compose.yml

version: '2'
services:
  redis-6379:
    image: redis
    container_name: redis-6379
    ports:
     - 6379:6379
     - 16379:16379
    volumes:
     - ./6379/data:/data/redis
     - ./6379/redis.conf:/etc/redis/redis.conf
    command: redis-server /etc/redis/redis.conf
  redis-6380:
    image: redis
    container_name: redis-6380
    ports:
     - 6380:6380
     - 16380:16380
    volumes:
     - ./6380/data:/data/redis
     - ./6380/redis.conf:/etc/redis/redis.conf
    command: redis-server /etc/redis/redis.conf
  redis-6381:
    image: redis
    container_name: redis-6381
    ports:
     - 6381:6381
     - 16381:16381
    volumes:
     - ./6381/data:/data/redis
     - ./6381/redis.conf:/etc/redis/redis.conf
    command: redis-server /etc/redis/redis.conf
  redis-6382:
    image: redis
    container_name: redis-6382
    ports:
     - 6382:6382
     - 16382:16382
    volumes:
     - ./6382/data:/data/redis
     - ./6382/redis.conf:/etc/redis/redis.conf
    command: redis-server /etc/redis/redis.conf
  redis-6383:
    image: redis
    container_name: redis-6383
    ports:
     - 6383:6383
     - 16383:16383
    volumes:
     - ./6383/data:/data/redis
     - ./6383/redis.conf:/etc/redis/redis.conf
    command: redis-server /etc/redis/redis.conf
  redis-6384:
    image: redis
    container_name: redis-6384
    ports:
     - 6384:6384
     - 16384:16384
    volumes:
     - ./6384/data:/data/redis
     - ./6384/redis.conf:/etc/redis/redis.conf
    command: redis-server /etc/redis/redis.conf

redis.conf

bind 0.0.0.0
port 容器端口 									# 这里对应docker-compose.yml对应的ports端口
dir /data/redis 									# redis的数据目录
appendonly yes     							# aof持久化机制
cluster-enabled yes 						# 开启cluster
cluster-config-file nodes-6379.conf # 这里的文件名可以不做更改,用于记录集群的信息
cluster-node-timeout 15000            # 集群节点的超时毫秒时间,超过此时间则判断为该node不可达

运行、集群创建

启动容器后,随便进入一个容器,然后使用redis-cli创建

[root@iZwz9669229pzjci7wt131Z redis] docker-compose up -d
[root@iZwz9669229pzjci7wt131Z redis] docker exec -it redis-6380 bash
root@bcd3b2cb4d58: redis-cli --cluster create --cluster-replicas 1 172.18.248.36:6379 172.18.248.36:6380 172.18.248.36:6381 172.18.248.36:6382 172.18.248.36:6383 172.18.248.36:6384
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 172.18.248.36:6383 to 172.18.248.36:6379
Adding replica 172.18.248.36:6384 to 172.18.248.36:6380
Adding replica 172.18.248.36:6382 to 172.18.248.36:6381
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 98fcf444b67eb4c75620d63d80fa09f89815e8ea 172.18.248.36:6379
   slots:[0-5460] (5461 slots) master
M: c6a50ead8930dc3d21da4b2c260b436597906dc3 172.18.248.36:6380
   slots:[5461-10922] (5462 slots) master
M: c50984e8743cf2704725cdfff8f84b9ccd51d11a 172.18.248.36:6381
   slots:[10923-16383] (5461 slots) master
S: a7234465fcb3e1c9bbfb83447ff682fa4b1614bd 172.18.248.36:6382
   replicates 98fcf444b67eb4c75620d63d80fa09f89815e8ea
S: 1d037fe427f1fe4c292f8198b70782fd435827e4 172.18.248.36:6383
   replicates c6a50ead8930dc3d21da4b2c260b436597906dc3
S: 827da77a5a902f9b7c5b8f300496f3af9de73d76 172.18.248.36:6384
   replicates c50984e8743cf2704725cdfff8f84b9ccd51d11a
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 172.18.248.36:6379)
M: 98fcf444b67eb4c75620d63d80fa09f89815e8ea 172.18.248.36:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 1d037fe427f1fe4c292f8198b70782fd435827e4 172.30.0.1:6383
   slots: (0 slots) slave
   replicates c6a50ead8930dc3d21da4b2c260b436597906dc3
S: 827da77a5a902f9b7c5b8f300496f3af9de73d76 172.30.0.1:6384
   slots: (0 slots) slave
   replicates c50984e8743cf2704725cdfff8f84b9ccd51d11a
M: c6a50ead8930dc3d21da4b2c260b436597906dc3 172.30.0.1:6380
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: a7234465fcb3e1c9bbfb83447ff682fa4b1614bd 172.30.0.1:6382
   slots: (0 slots) slave
   replicates 98fcf444b67eb4c75620d63d80fa09f89815e8ea
M: c50984e8743cf2704725cdfff8f84b9ccd51d11a 172.30.0.1:6381
   slots:[10923-16383] (5461 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.
root@bcd3b2cb4d58:/data# redis-cli -p 6380
127.0.0.1:6380> 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:2
cluster_stats_messages_ping_sent:1088
cluster_stats_messages_pong_sent:1105
cluster_stats_messages_meet_sent:1
cluster_stats_messages_sent:2194
cluster_stats_messages_ping_received:1105
cluster_stats_messages_pong_received:1089
cluster_stats_messages_received:2194

redis-cli参数:

--cluster # 集群管理命令
create host1:port1 ... hostN:portN # 指定要加入集群的redis节点
--cluster-replicas # 指定需要多少个副本,设置为1时,每个master会有1个replica
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Docker中集成Redis Cluster集群,可以按照以下步骤操作: 1. 创建一个Docker Compose文件,定义Redis Cluster集群的服务和配置。例如: ``` version: '3' services: redis-1: image: redis:latest command: redis-server /redis-conf/redis-1.conf ports: - 6381:6381 volumes: - ./conf/redis-1.conf:/redis-conf/redis-1.conf - ./data/redis-1:/data redis-2: image: redis:latest command: redis-server /redis-conf/redis-2.conf ports: - 6382:6382 volumes: - ./conf/redis-2.conf:/redis-conf/redis-2.conf - ./data/redis-2:/data redis-3: image: redis:latest command: redis-server /redis-conf/redis-3.conf ports: - 6383:6383 volumes: - ./conf/redis-3.conf:/redis-conf/redis-3.conf - ./data/redis-3:/data redis-4: image: redis:latest command: redis-server /redis-conf/redis-4.conf ports: - 6384:6384 volumes: - ./conf/redis-4.conf:/redis-conf/redis-4.conf - ./data/redis-4:/data redis-5: image: redis:latest command: redis-server /redis-conf/redis-5.conf ports: - 6385:6385 volumes: - ./conf/redis-5.conf:/redis-conf/redis-5.conf - ./data/redis-5:/data redis-6: image: redis:latest command: redis-server /redis-conf/redis-6.conf ports: - 6386:6386 volumes: - ./conf/redis-6.conf:/redis-conf/redis-6.conf - ./data/redis-6:/data networks: redis-cluster: driver: bridge ``` 2. 在同一目录下创建一个conf文件夹,用于存储每个Redis节点的配置文件。例如,创建一个redis-1.conf文件: ``` port 6381 cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 appendonly yes ``` 3. 启动Redis Cluster集群服务: ``` docker-compose up -d ``` 4. 将节点加入Redis Cluster集群。可以使用redis-cli命令行工具连接到每个节点,并使用CLUSTER MEET命令将节点加入集群。例如: ``` redis-cli -h 127.0.0.1 -p 6381 > CLUSTER MEET 127.0.0.1 6382 > CLUSTER MEET 127.0.0.1 6383 > CLUSTER MEET 127.0.0.1 6384 > CLUSTER MEET 127.0.0.1 6385 > CLUSTER MEET 127.0.0.1 6386 ``` 5. 创建一个Redis Cluster集群。可以使用redis-cli命令行工具连接到任何一个节点,并使用CLUSTER CREATE命令来创建集群。例如: ``` redis-cli --cluster create 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 127.0.0.1:6385 127.0.0.1:6386 --cluster-replicas 1 ``` 6. 现在,您已经成功地在Docker中集成了Redis Cluster集群。可以使用redis-cli命令行工具连接到任何一个节点,并开始使用集群
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值