docker搭建redis cluster集群模式

一、创建redis容器

使用docker-compose创建redis容器
启动命令

docker-compose -f docker-compose-redis.yaml up -d

其中docker-compose-redis.yaml的配置内容如下

version: "3.4"
services:
  redis:
    image: redis
    container_name: redis-test-6383
    command: redis-server /etc/redis/redis.conf
    ports:
      - "6383:6379"
    volumes:
      - ./redis-test/data:/data
      - ./redis-test/conf/redis.conf:/etc/redis/redis.conf

二、搭建redis集群
以6个redis容器,搭建三组一主一从集群模式,
创建redis-cluster.yaml配置文件,内容如下

version: "3.4"

x-image:
  &default-image
  redis
x-command:
  &default-command
  redis-server /etc/redis/redis.conf
  
services:
  redis1:
    image: *default-image
    container_name: redis-m1-1001
    command: *default-command
    ports:
      - "1001:6379"
    volumes:
      - ./redis-m1-1001/data:/data
      - ./redis-m1-1001/conf/redis.conf:/etc/redis/redis.conf
      
  redis2:
    image: *default-image
    container_name: redis-m2-1002
    command: *default-command
    ports:
      - "1002:6379"
    volumes:
      - ./redis-m2-1002/data:/data
      - ./redis-m2-1002/conf/redis.conf:/etc/redis/redis.conf
      
  redis3:
    image: *default-image
    container_name: redis-m3-1003
    command: *default-command
    ports:
      - "1003:6379"
    volumes:
      - ./redis-m3-1003/data:/data
      - ./redis-m3-1003/conf/redis.conf:/etc/redis/redis.conf
      
  redis4:
    image: *default-image
    container_name: redis-s1-1004
    command: *default-command
    ports:
      - "1004:6379"
    volumes:
      - ./redis-s1-1004/data:/data
      - ./redis-s1-1004/conf/redis.conf:/etc/redis/redis.conf
      
  redis5:
    image: *default-image
    container_name: redis-s2-1005
    command: *default-command
    ports:
      - "1005:6379"
    volumes:
      - ./redis-s2-1005/data:/data
      - ./redis-s2-1005/conf/redis.conf:/etc/redis/redis.conf
      
  redis6:
    image: *default-image
    container_name: redis-s3-1006
    command: *default-command
    ports:
      - "1006:6379"
    volumes:
      - ./redis-s3-1006/data:/data
      - ./redis-s3-1006/conf/redis.conf:/etc/redis/redis.conf

创建好了6个redis容器后,他们的ip和端口如下

redis-m1-1001 172.18.0.6:6379
redis-m2-1002 172.18.0.8:6379
redis-m3-1003 172.18.0.4:6379
redis-s1-1004 172.18.0.5:6379
redis-s2-1005 172.18.0.3:6379
redis-s3-1006 172.18.0.7:6379

redis cluster 在5.0之后取消了ruby脚本 redis-trib.rb的支持(手动命令行添加集群的方式不变),集合到redis-cli里
1.创建集群主从节点

redis-cli --cluster create 172.18.0.6:6379 172.18.0.8:6379 172.18.0.4:6379

2.依次添加集群从节点

redis-cli --cluster add-node 172.18.0.7:6379 172.18.0.4:6379 --cluster-slave --cluster-master-id d8f1423a00fd79d9b72ebe1f7481626b54f0c508 

172.18.0.7节点加入到172.18.0.4节点的集群中,并且当做node_id为 d8f1423a00fd79d9b72ebe1f7481626b54f0c508 的从节点。如果不指定 --cluster-master-id 会随机分配到任意一个主节点。
3.任意连接一个集群节点,检查集群

cluster info

4.删除节点
指定IP、端口和node_id 来删除一个节点,从节点可以直接删除,主节点不能直接删除,删除之后,该节点会被shutdown。

redis-cli --cluster del-node 172.18.0.5:6379 f6a6957421b80409106cb36be3c7ba41f3b603ff

参考链接 https://www.cnblogs.com/zhoujinyi/p/11606935.html

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值