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

你可能感兴趣的:(docker和微服务踩坑之路)