Deploy Redis standalone and cluster using Docker Compose

Deploy Redis standalone and cluster using Docker Compose

Redis is an open source, high-performance in-memory database that is often used in scenarios such as caching and message queues. It not only supports a variety of data structures, but also has excellent performance, making it one of the important components of distributed systems. In daily development and production environments, it is very common to use Docker containerized Redis deployment. This article will detail how to use Docker Compose to deploy Redis standalone and Redis clusters respectively.

1. Preliminary preparation

Before you begin, make sure you have the following tools installed on your system:

  1. Docker: Tools for containerized applications, available from Docker official websiteDownload and install.
  2. Docker Compose:Docker Compose is a tool officially provided by Docker for defining and running multiple container applications, allowing us to write a docker-compose.yml File to start multiple services simultaneously.

Verify that Docker and Docker Compose are installed by running the following command in the command line:

docker --version

Output Docker version information

docker-compose --version

Output Docker Compose version information.

If the above command outputs the version information correctly, it means the installation is successful.

2. Deploy Redis standalone version using Docker Compose

2.1 Create a project directory

First, we need to create a file to store the Redis configuration files and docker-compose.yml The project directory for the file.

mkdir redis-single && cd redis-single

2.2 Writing docker-compose.yml

Next, create a docker-compose.yml The file has the following contents:

version: '3' 

services: 
  redis: 
    image: redis:latest 
    container_name: redis-single 
    ports: 
      - "6379:6379" 
    volumes: 
      - redis-data:/data 
    command: ["redis-server", "--appendonly" , "yes"] 
    
  volumes: 
    redis-data: 
    driver: local

explain:

  • version: '3': The version number of the Docker Compose file,3 It is a common version that supports most functions.
  • services: Define a service. Here we define a Redis service.
  • redis: Service name, representing the Redis container.
  • image: Specify the image. Here we use the latest version of the Redis official image. redis:latest.
  • container_name: Container name, name the Redis container redis-single.
  • ports: Map the port and map the local port 6379 to the port 6379 in the container. Redis runs on port 6379 by default.
  • volumes: Specify a data volume to store Redis data persistently locally to prevent data loss due to container restart or deletion.
  • command: Start command, here we specify redis-server Start and enable the AOF (Append-Only File) persistence mechanism.

2.3 Start the Redis container

In the project directory, run the following command to start the Redis container:

docker-compose up -d

Parameter explanation:

  • -d: Indicates that the container is running in the background.

After running this command, Docker will docker-compose.yml File pulls the Redis image and starts the service.

2.4 Verify the Redis standalone version

  1. View the running Redis container:
docker ps

If all goes well, you should see redis-single The container is running.

  1. use redis-cli Connect to the Redis instance:
docker exec -it redis-single redis-cli

In the Redis CLI, enter the following command to verify that Redis is working properly:

ping

The output should be PONG, indicating that Redis is running normally.

2.5 Stop and delete the Redis container

If you want to stop and remove the Redis container, you can use the following command:

docker-compose down

This command will stop and delete all docker-compose.yml The container and network created by the file.

3. Deploy Redis cluster using Docker Compose

Redis Cluster is a distributed Redis architecture that allows data to be sharded and distributed across multiple nodes for high availability and scalability. A Redis Cluster requires at least 6 nodes, 3 of which are master nodes and 3 are slave nodes. Next, we will demonstrate how to deploy a Redis Cluster using Docker Compose.

3.1 Create a project directory

Create a new project directory for the Redis cluster:

mkdir redis-cluster && cd redis-cluster

3.2 Create a Redis cluster configuration file

We need to create a separate configuration file for each Redis instance. First, create a config Directory to store these files:

mkdir config

Then create a configuration file for each node. redis-cluster-node.conf Template configuration:

port 6379
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes

Copy this configuration file into 6 copies and name them node-7000.conf,node-7001.conf, and so on, until node-7005.confThese 6 configuration files will be used for 6 Redis nodes respectively.

3.3 Writing docker-compose.yml

exist redis-cluster Create a directory docker-compose.yml The file has the following contents:

version: '3'
services:
  redis-node-1:
    image: redis:latest
    container_name: redis-node-1
    ports:
      - "7000:6379"
    volumes:
      - ./config/node-7000.conf:/usr/local/etc/redis/redis.conf
    command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
  redis-node-2:
    image: redis:latest
    container_name: redis-node-2
    ports:
      - "7001:6379"
    volumes:
      - ./config/node-7001.conf:/usr/local/etc/redis/redis.conf
    command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
  redis-node-3:
    image: redis:latest
    container_name: redis-node-3
    ports:
      - "7002:6379"
    volumes:
      - ./config/node-7002.conf:/usr/local/etc/redis/redis.conf
    command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
  redis-node-4:
    image: redis:latest
    container_name: redis-node-4
    ports:
      - "7003:6379"
    volumes:
      - ./config/node-7003.conf:/usr/local/etc/redis/redis.conf
    command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
  redis-node-5:
    image: redis:latest
    container_name: redis-node-5
    ports:
      - "7004:6379"
    volumes:
      - ./config/node-7004.conf:/usr/local/etc/redis/redis.conf
    command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
  redis-node-6:
    image: redis:latest
    container_name: redis-node-6
    ports:
      - "7005:6379"
    volumes:
      - ./config/node-7005.conf:/usr/local/etc/redis/redis.conf
    command: ["redis-server", "/usr/local/etc/redis/redis.conf"]

explain:

  • We created 6 Redis containers, listening on ports 7000 to 7005 respectively.
  • Each container mounts the corresponding configuration file, using redis-server Command start.

3.4 Start Redis Cluster

exist redis-cluster In the directory, execute the following command to start the cluster:

docker-compose up -d

3.5 Configuring Redis Cluster

After the Redis container is started, we also need to manually configure the Redis cluster. First, execute redis-cli Command to configure the cluster:

docker exec -it redis-node-1 redis-cli --cluster create \
  127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 \
  127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 \
  --cluster-replicas 1

explain:

  • --cluster create: Command is used to create a Redis cluster.
  • 127.0.0.1:7000 127.0.0.1:7001 ...: Lists all nodes in the cluster.
  • --cluster-replicas 1: Specifies that each master node has one slave node.

After executing this command, Redis will automatically configure 7000, 7001, and 7002 as master nodes, and 7003, 7004, and 7005 as corresponding slave nodes.

3.6 Verify the Redis cluster

You can check the status of the cluster with the following command:

docker exec -it redis-node-1 redis-cli -c -p 7000 cluster info

You can also connect to the Redis node and view the node information of the cluster with the following command:

docker exec -it redis-node-1 redis-cli -c -p 7000 cluster nodes

IV. Conclusion

This article details how to use Docker Compose to deploy Redis standalone and cluster versions. In a development environment, Docker Compose can be used to quickly deploy and manage Redis services, while in a production environment, more complex cluster management can be achieved through similar configurations.

No Comments

Send Comment Edit Comment

|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠(ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ°Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
Emoticons
Emoji
Little Dinosaur
flower!
Previous
Next