1. The docker – Compose

1.1 Details on preparing the Docker-Redis-cluster folder

The serial number Directories or files role
1 ./docker-redis-cluster General Configuration Directory
2 ./docker-redis-cluster/docker-compose.yaml Deployment file
3 ./docker-redis-cluster/redis/Dockerfile Image configuration file
4 ./docker-redis-cluster/redis/redis.conf Redis configuration file
5 ./docker-redis-cluster/redis/start.sh Redis startup script

Special attention: the last three files file encoding format -UTF-8, newline encoding -LF(UNIX)

1.2 docker – compose. Yaml

version: '3'

networks:

  redisnet:
    driver: bridge
    ipam:
      config:
        - subnet: 10.0. 0. 0/ 24

services:

  redis-cluster:
    image: Redis: 6.0.9
    command: redis-cli -p 6379 --cluster create 10.0. 010.: 6379 10.0. 011.: 6380 10.0. 012.: 6381 10.0. 013.: 6382 10.0. 014.: 6383 10.0. 015.: 6384 --cluster-replicas 1 --cluster-yes
    depends_on:
      - redis-1
      - redis-2
      - redis-3
      - redis-4
      - redis-5
      - redis-6
    networks:
      redisnet:
        ipv4_address: 10.0. 02.

  redis-1:
    build: ./redis
    image: redis-cluster-node
    environment:
      X_REDIS_PORT: 6379
    networks:
      redisnet:
        ipv4_address: 10.0. 010.
    ports:
      - 6379: 6379

  redis-2:
    image: redis-cluster-node
    environment:
      X_REDIS_PORT: 6380
    networks:
      redisnet:
        ipv4_address: 10.0. 011.
    ports:
      - 6380: 6379

  redis-3:
    image: redis-cluster-node
    environment:
      X_REDIS_PORT: 6381
    networks:
      redisnet:
        ipv4_address: 10.0. 012.
    ports:
      - 6381: 6379

  redis-4:
    image: redis-cluster-node
    environment:
      X_REDIS_PORT: 6382
    networks:
      redisnet:
        ipv4_address: 10.0. 013.
    ports:
      - 6382: 6379

  redis-5:
    image: redis-cluster-node
    environment:
      X_REDIS_PORT: 6383
    networks:
      redisnet:
        ipv4_address: 10.0. 014.
    ports:
      - 6383: 6379

  redis-6:
    image: redis-cluster-node
    environment:
      X_REDIS_PORT: 6384
    networks:
      redisnet:
        ipv4_address: 10.0. 015.
    ports:
      - 6384: 6379
Copy the code

1.3 Supporting File Dockerfile

FROM redis:6.0.9

MAINTAINER Lu Dongsheng <[email protected]>

RUN ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime
RUN echo 'Asia/Tokyo' >/etc/timezone

# Dependent tool installation
RUN apt-get update
RUN apt-get install -y gettext-base && apt-get install -y apt-utils

COPY redis.conf /etc/redis/redis.conf.template
COPY start.sh /start.sh

ENTRYPOINT [ "/bin/bash"."/start.sh" ]
Copy the code

1.4 Supporting file redis.conf

port $X_REDIS_PORT
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
Copy the code

1.5 Supporting file start.sh

envsubst '$X_REDIS_PORT:$X_REDIS_PORT' < /etc/redis/redis.conf.template > /etc/redis/redis.conf
redis-server /etc/redis/redis.conf
Copy the code

Deployment of 2.

2.1 Build and start all containers

$ docker-compose up --build -d
Creating network "docker-redis-cluster_redisnet" with driver "bridge"Building redis-1 Step 1/9: FROM redis:6.0.9 --> 6060DF96cef3 Step 2/9: Building redis-1 Step 1/9: FROM Redis :6.0.9 --> 6060df96cef3 Step 2/9: MAINTAINER Lu Dongsheng <[email protected]> ---> Using cache ---> e20c87bd3cef Step 3/9 : RUN ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime ---> Using cache ---> f7e6083e6fb3 Step 4/9 : RUNecho 'Asia/Tokyo' >/etc/timezone
 ---> Using cache
 ---> ef5aa8bb6c22
Step 5/9 : RUN apt-get update
 ---> Using cache
 ---> 362e0274a91c
Step 6/9 : RUN apt-get install -y gettext-base && apt-get install -y apt-utils
 ---> Using cache
 ---> 5fbf8d44d5bd
Step 7/9 : COPY redis.conf /etc/redis/redis.conf.template
 ---> Using cache
 ---> ed05230e6323
Step 8/9 : COPY start.sh /start.sh
 ---> Using cache
 ---> 5e253f787309
Step 9/9 : ENTRYPOINT [ "/bin/bash"."/start.sh" ]
 ---> Using cache
 ---> 51f8529def29

Successfully built 51f8529def29
Successfully tagged redis-cluster-node:latest
Creating docker-redis-cluster_redis-4_1 ... done
Creating docker-redis-cluster_redis-1_1 ... done
Creating docker-redis-cluster_redis-2_1 ... done
Creating docker-redis-cluster_redis-6_1 ... done
Creating docker-redis-cluster_redis-3_1 ... done
Creating docker-redis-cluster_redis-5_1 ... done
Creating docker-redis-cluster_redis-cluster_1 ... done
Copy the code

2.2 Viewing Containers

$ docker ps
CONTAINER ID   IMAGE                COMMAND                  CREATED          STATUS          PORTS                    NAMES
d638dc1a8109   redis-cluster-node   "/bin/bash /start.sh"37 seconds ago Up 34 seconds 0.0.0.0:6384->6379/ TCP docker-redis-cluster_redis-6_1 32188654EEDf Redis-cluster-node"/bin/bash /start.sh"37 seconds ago Up 34 seconds 0.0.0.0:6380->6379/ TCP docker-redis-cluster_redis-2_1 7050bae336F1 Redis-cluster-node"/bin/bash /start.sh"37 seconds ago Up 34 seconds 0.0.0.0:6379->6379/ TCP docker-redis-cluster_redis-1_1 41bbf8d74f6c Redis-cluster-node"/bin/bash /start.sh"37 seconds ago Up 34 seconds 0.0.0.0:6382->6379/ TCP docker-redis-cluster_redis-4_1 f55577e67af7 Redis-cluster-node"/bin/bash /start.sh"37 seconds ago Up 34 seconds 0.0.0.0:6381->6379/ TCP docker-redis-cluster_redis-3_1 60BF1405fd3e redis-cluster-node"/bin/bash /start.sh"37 seconds ago Up 35 seconds 0.0.0.0:6383->6379/ TCP docker-redis-cluster_redIS-5_1Copy the code

2.3 log

To better understand how to create a cluster, examine one of the container logs.

$ docker logs docker-redis-cluster_redis-1_1 $ docker logs docker-redis-cluster_redis-2_1 $ docker logs docker-redis-cluster_redis-3_1 $ docker logs docker-redis-cluster_redis-4_1 $ docker logs docker-redis-cluster_redis-5_1  $ docker logs docker-redis-cluster_redis-6_1Copy the code

2.4 Connect to either Master

$ docker execIt docker-redis-cluster_redis-1_1 redis-cli -c -p 6379 127.0.0.1:6379> cluster Nodes B42c322538ca174995277ced98534a4253dd6948 10.0.0.13:6382 @ 16382 slave d0429fa1e0ba3ece8898f7bd84969839d2d3c516 0 1611892729662 3 connected e5396fcd18de2126b1cc725a83308b944575704f 10.0.0.10:6379 @ 16379 myself, master 1611892729000-0 1 connected 0-5460-05 f0201fbb705d0701c9c0da9175a7c22e25e030 10.0.0.11:6380 @ 16380 master - 0 1611892729000 2 connected 5461-10922 d0429fa1e0ba3ece8898f7bd84969839d2d3c516 10.0.0.12:6381 @ 16381 master - 0 1611892728157 3 connected 10923-16383-1 bf41a4c744c880ba7aaf70ae770f51cc139bedf 10.0.0.14:6383 @ 16383 slave e5396fcd18de2126b1cc725a83308b944575704f 0 1611892728000 1 connected b5d420d8461fcee9f73172752fc508c41c46a870 10.0.0.15:6384 @ 16384 slave 05f0201fbb705d0701c9c0da9175a7c22e25e030 0 1611892730162 2 connectedCopy the code

2.5 Viewing Cluster Slots

127.0.0.1:6379> cluster slots
1) 1) (integer) 0, 2) (integer), 5460 (3) 1)"10.0.0.10"(2)integer), 6379 (3)"e5396fcd18de2126b1cc725a83308b944575704f"
   4) 1) "10.0.0.14"(2)integer), 6383 (3)"1bf41a4c744c880ba7aaf70ae770f51cc139bedf"1) (2)integer), 5461 (2)integer), 10922 (3) 1)"10.0.0.11"(2)integer), 6380 (3)"05f0201fbb705d0701c9c0da9175a7c22e25e030"
   4) 1) "10.0.0.15"(2)integer), 6384 (3)"b5d420d8461fcee9f73172752fc508c41c46a870"1) (3)integer), 10923 (2)integer), 16383 (3) 1)"10.0.0.12"(2)integer), 6381 (3)"d0429fa1e0ba3ece8898f7bd84969839d2d3c516"
   4) 1) "10.0.0.13"(2)integer), 6382 (3)"b42c322538ca174995277ced98534a4253dd6948"
Copy the code

2.6 to empty

$  docker-compose down
Stopping docker-redis-cluster_redis-4_1 ... done
Stopping docker-redis-cluster_redis-6_1 ... done
Stopping docker-redis-cluster_redis-2_1 ... done
Stopping docker-redis-cluster_redis-5_1 ... done
Stopping docker-redis-cluster_redis-1_1 ... done
Stopping docker-redis-cluster_redis-3_1 ... done
Removing docker-redis-cluster_redis-cluster_1 ... done
Removing docker-redis-cluster_redis-4_1       ... done
Removing docker-redis-cluster_redis-6_1       ... done
Removing docker-redis-cluster_redis-2_1       ... done
Removing docker-redis-cluster_redis-5_1       ... done
Removing docker-redis-cluster_redis-1_1       ... done
Removing docker-redis-cluster_redis-3_1       ... done
Removing network docker-redis-cluster_redisnet
Copy the code

Reference: itsmetommy.com/2018/05/24/…