Table of Contents
1. Cluster configuration
2. Write redis.conf
3. Create a redis.conf file in each redis-* folder and write the following content:
4. Write the docker-compose.yml file
cluster nodes
Copy after login
" >
cluster nodes
Copy after login
Home Database Redis How Docker-Compose builds a Redis cluster

How Docker-Compose builds a Redis cluster

May 30, 2023 pm 04:46 PM
redis docker docker-compose

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
Copy after login

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 #设置密码
Copy after login

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"]
Copy after login

Start the container and execute the command:

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

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
Copy after login

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
Copy after login

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
Copy after login

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>
Copy after login

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>
Copy after login

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
Copy after login

7.2 Edit the docker-compose.yml file

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

7.3 Connect to the cluster

redis-cli -c -h 192.168.66.180 -p 6379 -a 123456
Copy after login

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to exit the container by docker How to exit the container by docker Apr 15, 2025 pm 12:15 PM

Four ways to exit Docker container: Use Ctrl D in the container terminal Enter exit command in the container terminal Use docker stop <container_name> Command Use docker kill <container_name> command in the host terminal (force exit)

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to copy files in docker to outside How to copy files in docker to outside Apr 15, 2025 pm 12:12 PM

Methods for copying files to external hosts in Docker: Use the docker cp command: Execute docker cp [Options] <Container Path> <Host Path>. Using data volumes: Create a directory on the host, and use the -v parameter to mount the directory into the container when creating the container to achieve bidirectional file synchronization.

How to restart docker How to restart docker Apr 15, 2025 pm 12:06 PM

How to restart the Docker container: get the container ID (docker ps); stop the container (docker stop <container_id>); start the container (docker start <container_id>); verify that the restart is successful (docker ps). Other methods: Docker Compose (docker-compose restart) or Docker API (see Docker documentation).

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

How to update the image of docker How to update the image of docker Apr 15, 2025 pm 12:03 PM

The steps to update a Docker image are as follows: Pull the latest image tag New image Delete the old image for a specific tag (optional) Restart the container (if needed)

How to start containers by docker How to start containers by docker Apr 15, 2025 pm 12:27 PM

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

How to create containers for docker How to create containers for docker Apr 15, 2025 pm 12:18 PM

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

See all articles