Home  >  Article  >  Database  >  How Docker-Compose builds a Redis cluster

How Docker-Compose builds a Redis cluster

PHPz
PHPzforward
2023-05-30 16:46:221709browse

1. Cluster configuration

  • 3 Master 3 Slave

  • Since it is only for testing, I only use 1 server for simulation here

redis list

How Docker-Compose builds a Redis cluster

2. Write redis.conf

Create a directory on the server for storage redis cluster deployment file. The path I put here is /root/redis-cluster

Create redis-1, redis-2, redis-3, redis-4, redis-5 in the /opt/docker/redis-cluster directory, redis-6 folder

mkdir -p /opt/docker/redis-cluster/{redis-1,redis-2,redis-3,redis-4,redis-5,redis-6}
 
#创建持久化目录
mkdir -p /opt/docker/redis-cluster/redis-1/data
mkdir -p /opt/docker/redis-cluster/redis-2/data
mkdir -p /opt/docker/redis-cluster/redis-3/data
mkdir -p /opt/docker/redis-cluster/redis-4/data
mkdir -p /opt/docker/redis-cluster/redis-5/data
mkdir -p /opt/docker/redis-cluster/redis-6/data

3. Create a redis.conf file in each redis-* folder and write the following content:

cluster-enabled yes # 开启集群
cluster-config-file nodes.conf # 集群配置文件
cluster-node-timeout 5000 # 集群节点多少时间未响应视为该节点丢失
appendonly yes
port 6379 # redis监听端口
masterauth passwd123  #设置master节点密码
requirepass passwd123 #设置密码

Note: The port value cannot all be 6379 , according to the port numbers set in the redis list above, set the port numbers 6379~6384 for redis-1 ~ redis-6 in sequence

4. Write the docker-compose.yml file

Use daocloud directly here The provided redis image address is daocloud.io/library/redis:6.0.4

Create the docker-compose.yml file in the /root/redis-cluster folder.

docker-compose.yml file content is as follows:

version: '3.1'
services:
  # redis1配置
  redis1:
    image: daocloud.io/library/redis:6.0.4
    container_name: redis-1
    restart: always
    network_mode: "host"
    volumes:
      - /opt/docker/redis-cluster/redis-1/data:/data
      - /opt/docker/redis-cluster/redis-1/redis.conf:/usr/local/etc/redis/redis.conf
    command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
  # redis2配置
  redis2:
    image: daocloud.io/library/redis:6.0.4
    container_name: redis-2
    restart: always
    network_mode: "host"
    volumes:
      - /opt/docker/redis-cluster/redis-2/data:/data
      - /opt/docker/redis-cluster/redis-2/redis.conf:/usr/local/etc/redis/redis.conf
    command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
  # redis3配置
  redis3:
    image: daocloud.io/library/redis:6.0.4
    container_name: redis-3
    restart: always
    network_mode: "host"
    volumes:
      - /opt/docker/redis-cluster/redis-3/data:/data
      - /opt/docker/redis-cluster/redis-3/redis.conf:/usr/local/etc/redis/redis.conf
    command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
  # redis4配置
  redis4:
    image: daocloud.io/library/redis:6.0.4
    container_name: redis-4
    restart: always
    network_mode: "host"
    volumes:
      - /opt/docker/redis-cluster/redis-4/data:/data
      - /opt/docker/redis-cluster/redis-4/redis.conf:/usr/local/etc/redis/redis.conf
    command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
  # redis5配置
  redis5:
    image: daocloud.io/library/redis:6.0.4
    container_name: redis-5
    restart: always
    network_mode: "host"
    volumes:
      - /opt/docker/redis-cluster/redis-5/data:/data
      - /opt/docker/redis-cluster/redis-5/redis.conf:/usr/local/etc/redis/redis.conf
    command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
  # redis6配置
  redis6:
    image: daocloud.io/library/redis:6.0.4
    container_name: redis-6
    restart: always
    network_mode: "host"
    volumes:
      - /opt/docker/redis-cluster/redis-6/data:/data
      - /opt/docker/redis-cluster/redis-6/redis.conf:/usr/local/etc/redis/redis.conf
    command: ["redis-server", "/usr/local/etc/redis/redis.conf"]

Start the container and execute the command:

#启动容器
docker-compose -f xxx.yaml up -d

How Docker-Compose builds a Redis cluster

##5. Start the cluster

Find any container to enter. Here I choose redis-1 to enter. After entering the container, enter the following command to start the cluster:

redis-cli --cluster create 192.168.66.180:6379 \
192.168.66.180:6380 \
192.168.66.180:6381 \
192.168.66.180:6382 \
192.168.66.180:6383 \
192.168.66.180:6384 \
--cluster-replicas 1

How Docker-Compose builds a Redis cluster

The following picture indicates success.

How Docker-Compose builds a Redis cluster

6. Test

Use the redis-cli command to connect to any node in the cluster. I just tested it on a computer that can ping the IP address of the cluster. Here I used the local computer

6.1 View node properties

redis-cli -c -h 192.168.66.180 -p 6379
192.168.66.180:6379> cluster info

It is displayed as shown below, which is cluster health Status

How Docker-Compose builds a Redis cluster

#6.2 View node information

cluster nodes

How Docker-Compose builds a Redis cluster

Pay attention to the slave, master, myself and other keywords in the picture.

How Docker-Compose builds a Redis cluster

6.3 Insert a value

192.168.66.180:6379> set test 'hello world'
-> Redirected to slot [6918] located at 192.168.66.180:6380
OK
192.168.66.180:6380>

How Docker-Compose builds a Redis cluster

Note: Here, the node where the data shard is located is automatically switched based on the slice. on, so you can see below that the connected node has changed to 192.168.1.4:6380

get test

168.66.180:6379> set test 'hello world'
-> Redirected to slot [6918] located at 192.168.66.180:6380
OK
192.168.66.180:6380> get test
"hello world"
192.168.66.180:6380>

This is because the query is performed on the shard where the data is located, so the connected node has not changed. .

7. Add password:

7.1 Edit the redis.conf file

#为redis.conf文件添加如下配置。这里设置密码为123456
masterauth 123456
requirepass 123456

7.2 Edit the docker-compose.yml file

#为docker-compose.yml中每个容器添加如下配置:
environment:
  - REDISCLI_AUTH=123456

7.3 Connect to the cluster

redis-cli -c -h 192.168.66.180 -p 6379 -a 123456

The above is the detailed content of How Docker-Compose builds a Redis cluster. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete