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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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 build the redis cluster mode How to build the redis cluster mode Apr 10, 2025 pm 10:15 PM

Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

How to implement the underlying redis How to implement the underlying redis Apr 10, 2025 pm 07:21 PM

Redis uses hash tables to store data and supports data structures such as strings, lists, hash tables, collections and ordered collections. Redis persists data through snapshots (RDB) and append write-only (AOF) mechanisms. Redis uses master-slave replication to improve data availability. Redis uses a single-threaded event loop to handle connections and commands to ensure data atomicity and consistency. Redis sets the expiration time for the key and uses the lazy delete mechanism to delete the expiration key.

What to do if redis-server can't be found What to do if redis-server can't be found Apr 10, 2025 pm 06:54 PM

Steps to solve the problem that redis-server cannot find: Check the installation to make sure Redis is installed correctly; set the environment variables REDIS_HOST and REDIS_PORT; start the Redis server redis-server; check whether the server is running redis-cli ping.

How is the redis cluster implemented How is the redis cluster implemented Apr 10, 2025 pm 05:27 PM

Redis cluster is a distributed deployment model that allows horizontal expansion of Redis instances, and is implemented through inter-node communication, hash slot division key space, node election, master-slave replication and command redirection: inter-node communication: virtual network communication is realized through cluster bus. Hash slot: divides the key space into hash slots to determine the node responsible for the key. Node election: At least three master nodes are required, and only one active master node is ensured through the election mechanism. Master-slave replication: The master node is responsible for writing requests, and the slave node is responsible for reading requests and data replication. Command redirection: The client connects to the node responsible for the key, and the node redirects incorrect requests. Troubleshooting: fault detection, marking off line and re-

How to view all keys in redis How to view all keys in redis Apr 10, 2025 pm 07:15 PM

To view all keys in Redis, there are three ways: use the KEYS command to return all keys that match the specified pattern; use the SCAN command to iterate over the keys and return a set of keys; use the INFO command to get the total number of keys.

How to use redis zset How to use redis zset Apr 10, 2025 pm 07:27 PM

Redis Ordered Sets (ZSets) are used to store ordered elements and sort by associated scores. The steps to use ZSet include: 1. Create a ZSet; 2. Add a member; 3. Get a member score; 4. Get a ranking; 5. Get a member in the ranking range; 6. Delete a member; 7. Get the number of elements; 8. Get the number of members in the score range.

How to view the version number of redis How to view the version number of redis Apr 10, 2025 pm 05:57 PM

To view the Redis version number, you can use the following three methods: (1) enter the INFO command, (2) start the server with the --version option, and (3) view the configuration file.

How is the key unique for redis query How is the key unique for redis query Apr 10, 2025 pm 07:03 PM

Redis uses five strategies to ensure the uniqueness of keys: 1. Namespace separation; 2. HASH data structure; 3. SET data structure; 4. Special characters of string keys; 5. Lua script verification. The choice of specific strategies depends on data organization, performance, and scalability requirements.

See all articles