SoFunction
Updated on 2025-04-14

docker-compose build a redis cluster (three servers, one master and one slave on each server)

1. Preface

Using Docker Compose to build a Redis cluster is a convenient and efficient method, which can quickly build a Redis cluster in a local or test environment. The following are detailed steps to use docker-compose to build a Redis cluster in a Docker environment.

This construction uses the Santai virtual machine as the host, and the IPs are as follows:

192.168.10.128
192.168.10.129
192.168.10.130

2. Preparation

2.1. Docker installation

Docker installation via the following command:

// Update package indexsudo apt-get update

// Allow APT to use HTTPSsudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common

// Add Docker's official GPG key:sudo  curl -fsSL /docker-ce/linux/ubuntu/gpg | sudo apt-key add -
// Set up a stable version warehousesudo add-apt-repository \
   "deb [arch=amd64] /docker-ce/linux/ubuntu/ \
  $(lsb_release -cs) \
  stable"
// Update package indexsudo apt-get update

// Install Dockersudo apt-get install docker-ce

// Check Docker statussudo systemctl status docker

2.2. Docker-compose installation

The docker-compose installation command is as follows:

sudo curl -L "/docker/compose/releases/download/v2.29.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

2.3 Directory creation

Create a data directory for redis on each station, use the following script:

#!/bin/bash

for dir in master/data slave/data; do
  mkdir -p "/home/arkham/docker/redis/redis-cluster/$dir";
done

3. Redis configuration file

3.1 Generate files

We need to create independent configuration files for each Redis node. The following is an example. The configuration files of other nodes are similar, and only need to modify the port and node names. For easy operation, it is also done through shell steps. Different servers only need to change cluster-announce-ip, and the script code is as follows:

#!/bin/bash

for dir in master slave; do
 if [ "$dir" == "master" ]; then
    port=6379
  elif [ "$dir" == "slave" ]; then
    port=6380
  fi
echo "port $port
save 900 1
save 300 10
save 60 10000
dbfilename 
dir /data
appendonly yes
appendfilename ""
appendfsync everysec
cluster-enabled yes
cluster-config-file 
cluster-node-timeout 5000
requirepass 123456arkham
masterauth 123456arkham
cluster-announce-ip 192.168.10.128" >/home/arkham/docker/redis/redis-cluster/$dir/;
done

3.2 The problem of shell file failure to execute is solved

The shell script written in Windows, copy the shell file to the corresponding server, and there may be no errors in the file during execution. At this time, the script file may fail to execute. One is a permission problem, and there may be a file problem. The end of the file in dos format is ^M, while the end of the file in unix format is $. The solution is as follows

# Add permissions to all files in the sehll directorychmod -R 775 /home/server/shell

# Check the end of the line character in the dos format file line ending is ^M$, while the end of the line ending of the file line ending is $sudo  cat -A 
# Revisesudo  sed -i "s/\r//" 

3.3 File writing

The file contents are as follows:

version: '3.1'

services:
  redis-master:
    image: redis:latest
    container_name: redis-master
    restart: always
    ports:
      - "6379:6379"
      - "16379:16379"
    environment:
      - TZ=Asia/Shanghai
    volumes:
    - /home/arkham/docker/redis/redis-cluster/master/data:/data
    - /home/arkham/docker/redis/redis-cluster/master/:/usr/local/etc/redis/
    deploy:
      resources:
        limits:
          cpus: '2'
          memory: 4G
        reservations:
          cpus: '2'
          memory: 4G
    command: ["redis-server","/usr/local/etc/redis/"]

  redis-slave:
    image: redis:latest
    container_name: redis-slave
    restart: always
    ports:
      - "6380:6380"
      - "16380:16380"
    environment:
      - TZ=Asia/Shanghai
    volumes:
      - /home/arkham/docker/redis/redis-cluster/slave/data:/data
      - /home/arkham/docker/redis/redis-cluster/slave/:/usr/local/etc/redis/
    deploy:
      resources:
        limits:
          cpus: '2'
          memory: 4G
        reservations:
          cpus: '2'
          memory: 4G
    command: [ "redis-server","/usr/local/etc/redis/" ]

4. Start the cluster and initialize it

4.1 Start the Redis cluster

In the redis directory, execute the following command to start the Redis node:

sudo docker-compose up -d

If the pulling image fails or the connection is refused, it may be because of the docker source and modified to the domestic source:

sudo mkdir -p /etc/docker

sudo tee /etc/docker/ <<-'EOF'
{
    "registry-mirrors": [
        "",
        "https://dc.",
        "",
        "",
        "",
        ""
    ]
}
sudo systemctl daemon-reload
sudo systemctl restart docker

4.2 Redis cluster initialization

After the cluster is started, the cluster initialization operation is required. You need to enter any Redis container and use the redis-cli command to create the cluster.

// Enter any Redis containerdocker exec -it redis-master  /bin/bash
// Execute the creation of the clusterredis-cli -a 123456arkham --cluster create 192.168.10.128:6379 192.168.10.128:6380 192.168.10.129:6379 192.168.10.129:6380 192.168.10.129:6379 192.168.10.129:6380 --cluster-replicas 1
// After successful creation, connect to any redis noderedis-cli -a 123456arkham -c -h 192.168.10.128 -p 6379
// View cluster informationcluster info

The execution results are as follows:

192.168.10.128:6379> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:4
cluster_size:3
cluster_current_epoch:4
cluster_my_epoch:1
cluster_stats_messages_ping_sent:2972
cluster_stats_messages_pong_sent:2841
cluster_stats_messages_fail_sent:4
cluster_stats_messages_sent:5817
cluster_stats_messages_ping_received:2841
cluster_stats_messages_pong_received:2970
cluster_stats_messages_fail_received:2
cluster_stats_messages_received:5813
total_cluster_links_buffer_limit_exceeded:0

5. Summary

Through the above steps, we have successfully built a Redis cluster using Docker Compose. This approach is especially suitable for development and testing environments because it is simple to configure and fast to deploy, making it ideal for simulation and verification of cluster architectures on-premises or in test environments. However, in a production environment, the deployment and use of Redis clusters require more careful and careful planning, including performance tuning, secure configuration, resource allocation and other aspects.

The main advantages of deploying a Redis cluster with Docker Compose are fast builds and flexible configuration. Through the Compose file, you can define the network, port and data volumes of multiple Redis instances, realizing the need to start the entire cluster in one click. During the development and testing phases, this method can save a lot of time, facilitate simulating complex cluster environments, and help developers verify the performance and problems of Redis clusters in different scenarios. Docker Compose's isolation and portability of resources make the construction and destruction of Redis clusters extremely simple, which is a great advantage for functional development, problem investigation and test cases verification.

This is the article about docker-compose building a redis cluster (three servers, one master and one slave on each server). For more related docker-compose building a redis cluster, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!