基于Docker搭建Redis Cluster ,三主三从的配置

基于Docker搭建Redis Cluster

1. 规划网络配置

首先创建redis集群的网络

docker network create --subnet=192.168.200.0/24 redisclusternet

ip port remark
192.168.200.11 7001
192.168.200.12 7002
192.168.200.13 7003
192.168.200.14 7004
192.168.200.15 7005
192.168.200.16 7006

2. 运行redis

采用docker-compose运行集群

docker-compose的安装: https://docs.docker.com/compose/install/#install-compose

version: '2.3'

x-image:
  &default-image
  redis:4.0.11

services:
  redis7001:
    image: *default-image
    container_name: redis7001
    command:
      ["redis-server", "/home/redis/cluster/7001/redis.conf"]
    networks:
      redisclusternet:
        ipv4_address: 192.168.200.11
    volumes:
      - /home/redis/cluster/7001/redis.conf:/home/redis/cluster/7001/redis.conf
      - /home/redis-cluster/7001/data:/data
    ports:
      - 7001:7001
      - 17001:17001
  redis7002:
    image: *default-image
    container_name: redis7002
    command:
      ["redis-server", "/home/redis/cluster/7002/redis.conf"]
    networks:
      redisclusternet:
        ipv4_address: 192.168.200.12
    volumes:
      - /home/redis/cluster/7002/redis.conf:/home/redis/cluster/7002/redis.conf
      - /home/redis-cluster/7002/data:/data
    ports:
      - 7002:7002
      - 17002:17002
  redis7003:
    image: *default-image
    container_name: redis7003
    command:
      ["redis-server", "/home/redis/cluster/7003/redis.conf"]
    networks:
      redisclusternet:
        ipv4_address: 192.168.200.13
    volumes:
      - /home/redis/cluster/7003/redis.conf:/home/redis/cluster/7003/redis.conf
      - /home/redis-cluster/7003/data:/data
    ports:
      - 7003:7003
      - 17003:17003
  redis7004:
      image: *default-image
      container_name: redis7004
      command:
        ["redis-server", "/home/redis/cluster/7004/redis.conf"]
      networks:
          redisclusternet:
            ipv4_address: 192.168.200.14
      volumes:
          - /home/redis/cluster/7004/redis.conf:/home/redis/cluster/7004/redis.conf
          - /home/redis-cluster/7004/data:/data
      ports:
          - 7004:7004
          - 17004:17004
  redis7005:
      image: *default-image
      container_name: redis7005
      command:
        ["redis-server", "/home/redis/cluster/7005/redis.conf"]
      networks:
          redisclusternet:
            ipv4_address: 192.168.200.15
      volumes:
          - /home/redis/cluster/7005/redis.conf:/home/redis/cluster/7005/redis.conf
          - /home/redis-cluster/7005/data:/data
      ports:
          - 7005:7005
          - 17005:17005
  redis7006:
      image: *default-image
      container_name: redis7006
      command:
        ["redis-server", "/home/redis/cluster/7006/redis.conf"]
      networks:
          redisclusternet:
            ipv4_address: 192.168.200.16
      volumes:
          - /home/redis/cluster/7006/redis.conf:/home/redis/cluster/7006/redis.conf
          - /home/redis-cluster/7006/data:/data
      ports:
          - 7006:7006
          - 17006:17006
networks:
   redisclusternet:
    external:
      name: redisclusternet

运行docker-compose up -d

注: -d 是后台运行

docker ps 查看一下,都正常跑起来了

3. 使用redis.trib建立集群

docker run --rm -it --net=redisclusternet --ip=192.168.200.21 inem0o/redis-trib create --replicas 1 192.168.200.11:7001 192.168.200.12:7002 192.168.200.13:7003 192.168.200.14:7004 192.168.200.15:7005 192.168.200.16:7006

这样集群就建立好了

4. 进入redis查看集群信息

可以看到已经完成3主3从的配置

[root@izwz90o0w65t1ogj9cpmiqz cluster]# docker exec -it redis7001 bash
root@02ee84fd8e91:/data# redis-cli  -p 7001
127.0.0.1:7001> CLUSTER NODES
b226a5721b6f2745cfd4a5c09772225a9149f291 39.xxx.xxx.xxx:7001@17001 myself,master - 0 1542676938000 1 connected 0-5460
48deb6fb7347d4ceddb72880f941572bbb79ed0f 39.xxx.xxx.xxx:7004@17004 slave b226a5721b6f2745cfd4a5c09772225a9149f291 0 1542676938533 4 connected
135e87086dd9482ee5b4ffb3f82d952e26b8a448 39.xxx.xxx.xxx:7005@17005 slave e9bd4328e0b130d06984de8e0cb8478d099c27f4 0 1542676939234 5 connected
e9bd4328e0b130d06984de8e0cb8478d099c27f4 39.xxx.xxx.xxx:7002@17002 master - 0 1542676938533 2 connected 5461-10922
3755f9977426c893a5ad1197d2ee92cecafb28a1 39.xxx.xxx.xxx:7006@17006 slave 7e217c32570648155a37fa1c7dacaec5ecdc8c1d 0 1542676938233 6 connected
7e217c32570648155a37fa1c7dacaec5ecdc8c1d 39.xxx.xxx.xxx:7003@17003 master - 0 1542676938000 3 connected 10923-16383

你可能感兴趣的:(redis)