编程知识 cdmana.com

5 minutes to build redis cluster mode and sentinel mode with docker

If you are asked to develop 、 The test environment builds a set of sentinel and cluster modes respectively redis, How soon do you need , Maybe you need a day ?2 Hours ? The fact is that it can be shorter . Yes , You've already guessed , use docker Deploy , It really only takes more than ten minutes .

One . preparation

Pull redis Mirror image

Run the following command :

docker pull redis

The image pulled by this command is the official image , Of course, you can search other images , We don't go deep here
View mirroring :
file

Two . Deploy redis Sentinel master-slave mode

What is sentinel mode ?-- Please Baidu

1、 What is? docker compose?

Docker Compose It can be understood as solidifying the operation mode and configuration of multiple containers !

Take the simplest example , If we want to prepare a for our application container MySQL Container and a Redis Containers , So every time you start , Let's first put MySQL Container and Redis The container starts , Then run the application container . Don't forget to connect the container network to when creating the application container MySQL Container and Redis On the container , So that applications can connect to them and exchange data .

It is not enough , If we also have various configurations for the container , We'd better also save the container creation and configuration commands , So that you can use it directly next time .

In this case , We have to lead to the multi container definition and running software most commonly used in our development , That is to say Docker Compose 了 .
file

2、 To write reids Master-slave docker-compose.yml

version: '3.7'
services:
  master:
    image: redis
    container_name: redis-master
    restart: always
    command: redis-server --requirepass redispwd  --appendonly yes
    ports:
      - 6379:6379
    volumes:
      - ./data1:/data
  slave1:
    image: redis
    container_name: redis-slave-1
    restart: always
    command: redis-server --slaveof redis-master 6379  --requirepass redispwd --masterauth redispwd  --appendonly yes
    ports:
      - 6380:6379
    volumes:
      - ./data2:/data
  slave2:
    image: redis
    container_name: redis-slave-2
    restart: always
    command: redis-server --slaveof redis-master 6379  --requirepass redispwd --masterauth redispwd  --appendonly yes
    ports:
      - 6381:6379
    volumes:
      - ./data3:/data

A term is used to explain :
file

3、 Start the master-slave redis

Get into redis Corresponding docker-compose.yml The catalog of , Carry out orders :

docker-compose up -d

-d Means running in the background
Use command docker ps Command to view the startup results : file The screenshot shows , Indicates successful operation

4. Write sentinels docker-compose.yml

version: '3.7'
services:
  sentinel1:
    image: redis
    container_name: redis-sentinel-1
    restart: always
    ports:
      - 26379:26379
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    volumes:
      - ./sentinel1.conf:/usr/local/etc/redis/sentinel.conf
  sentinel2:
    image: redis
    container_name: redis-sentinel-2
    restart: always
    ports:
    - 26380:26379
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    volumes:
      - ./sentinel2.conf:/usr/local/etc/redis/sentinel.conf
  sentinel3:
    image: redis
    container_name: redis-sentinel-3
    ports:
      - 26381:26379
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    volumes:
      - ./sentinel3.conf:/usr/local/etc/redis/sentinel.conf
networks:
  default:
    external:
      name: redis_default

5. Write sentinels sentinel.conf

#  Custom cluster name , among 172.19.0.3  by  redis-master  Of  ip,6379  by  redis-master  The port of ,2  Is the minimum number of votes ( Because there is  3  platform  Sentinel  So it can be set to  2)
port 26379
dir /tmp
sentinel monitor mymaster 172.19.0.3 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel auth-pass mymaster redispwd
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes

Copy the above documents separately 3 The copies are named sentinel1.conf、sentinel2.conf、sentinel3.conf And docker-compose.yml The configuration file in corresponds to , Then place and the sentry's docker-compose.yml In the same directory

6. Activate the sentry

Enter the sentry docker-compose.yml In the directory , Carry out orders :

docker-compose up -d

Look at the container , You can see the sentry and the master and slave redis It's all up
file

6.1 Sentry start log

file As can be seen from the above log , Sentinels monitor master and slave node

6.2 Turn off the master node

Stop by command redis Of master node

docker stop redis-master

file Through the above logs , We can see sdown,odown, What do they mean ?
sdown It's subjective downtime , Just a sentinel, if you feel like one master It's down. , So it's subjective downtime
odown It's objective downtime , If quorum The number of sentinels felt that one master It's down. , So it's objective downtime
Then the election begins , It can be seen from the log that the two sentinels chose the same slave node , At this time, the minimum number of votes we configure , So this one slave Was chosen as the new master.

6.3 Reopen master node

file The above log indicates that the sentinel detected the original master Restart , The original master The node becomes new master The slave node

3、 ... and . Deploy redis Cluster pattern

1、 Create directories and files

├── docker-compose.yml
├── redis-6371
│   ├── conf
│   │   └── redis.conf
│   └── data
├── redis-6372
│   ├── conf
│   │   └── redis.conf
│   └── data
├── redis-6373
│   ├── conf
│   │   └── redis.conf
│   └── data
├── redis-6374
│   ├── conf
│   │   └── redis.conf
│   └── data
├── redis-6375
│   ├── conf
│   │   └── redis.conf
│   └── data
└── redis-6376
    ├── conf
    │   └── redis.conf
    └── data

2、redis.conf The configuration file

port 6371
cluster-enabled yes
cluster-config-file nodes-6371.conf
cluster-node-timeout 5000
appendonly yes
protected-mode no
requirepass 1234
masterauth 1234
cluster-announce-ip 10.12.12.10 #  This is the host computer IP
cluster-announce-port 6371
cluster-announce-bus-port 16371

The configuration of each node only needs to change the port .

3、docker-compose The configuration file

version: "3"

#  Defining services , Can be more  services: redis-6371: #  The service name  image: redis #  The image required to create the container  container_name: redis-6371 #  Container name  restart: always #  The container always restarts  volumes: #  Data volume , Directory mount  - ./redis-6371/conf/redis.conf:/usr/local/etc/redis/redis.conf - ./redis-6371/data:/data ports: - 6371:6371 - 16371:16371 command: redis-server /usr/local/etc/redis/redis.conf redis-6372: image: redis container_name: redis-6372 volumes: - ./redis-6372/conf/redis.conf:/usr/local/etc/redis/redis.conf - ./redis-6372/data:/data ports: - 6372:6372 - 16372:16372 command: redis-server /usr/local/etc/redis/redis.conf redis-6373: image: redis container_name: redis-6373 volumes: - ./redis-6373/conf/redis.conf:/usr/local/etc/redis/redis.conf - ./redis-6373/data:/data ports: - 6373:6373 - 16373:16373 command: redis-server /usr/local/etc/redis/redis.conf redis-6374: image: redis container_name: redis-6374 restart: always volumes: - ./redis-6374/conf/redis.conf:/usr/local/etc/redis/redis.conf - ./redis-6374/data:/data ports: - 6374:6374 - 16374:16374 command: redis-server /usr/local/etc/redis/redis.conf redis-6375: image: redis container_name: redis-6375 volumes: - ./redis-6375/conf/redis.conf:/usr/local/etc/redis/redis.conf - ./redis-6375/data:/data ports: - 6375:6375 - 16375:16375 command: redis-server /usr/local/etc/redis/redis.conf redis-6376: image: redis container_name: redis-6376 volumes: - ./redis-6376/conf/redis.conf:/usr/local/etc/redis/redis.conf - ./redis-6376/data:/data ports: - 6376:6376 - 16376:16376 command: redis-server /usr/local/etc/redis/redis.conf 

After writing, use docker-compose up -d Start the container , Host mode is not used here (host), But use NAT Pattern , Because the host mode may cause external clients to be unable to connect .

4、 Into the container , Create clusters

It just started 6 individual Redis example , Not built into Cluster colony .
perform docker exec -it redis-6371 bash Enter a Redis Node container , Any one will do .
Continue with the following command to create the cluster :

#  Cluster creation command 
redis-cli -a 1234 --cluster create 10.35.30.39:6371 10.35.30.39:6372 10.35.30.39:6373 10.35.30.39:6374 10.35.30.39:6375 10.35.30.39:6376 --cluster-replicas 1
#  After execution, there will be the following output 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 10.35.30.39:6375 to 10.35.30.39:6371
Adding replica 10.35.30.39:6376 to 10.35.30.39:6372
Adding replica 10.35.30.39:6374 to 10.35.30.39:6373
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: e9a35d6a9d203830556de89f06a3be2e2ab4eee1 10.35.30.39:6371
   slots:[0-5460] (5461 slots) master
M: 0c8755144fe6a200a46716371495b04f8ab9d4c8 10.35.30.39:6372
   slots:[5461-10922] (5462 slots) master
M: fcb83b0097d2a0a87a76c0d782de12147bc86291 10.35.30.39:6373
   slots:[10923-16383] (5461 slots) master
S: b9819797e98fcd49f263cec1f77563537709bcb8 10.35.30.39:6374
   replicates fcb83b0097d2a0a87a76c0d782de12147bc86291
S: f4660f264f12786d81bcf0b18bc7287947ec8a1b 10.35.30.39:6375
   replicates e9a35d6a9d203830556de89f06a3be2e2ab4eee1
S: d2b9f265ef7dbb4a612275def57a9cc24eb2fd5d 10.35.30.39:6376
   replicates 0c8755144fe6a200a46716371495b04f8ab9d4c8
Can I set the above configuration? (type 'yes' to accept): yes #  Input here  yes  And return   Confirm node   Master slave identity   as well as   Allocation of hash slots 
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
.
>>> Performing Cluster Check (using node 10.35.30.39:6371)
M: e9a35d6a9d203830556de89f06a3be2e2ab4eee1 10.35.30.39:6371
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 0c8755144fe6a200a46716371495b04f8ab9d4c8 10.35.30.39:6372
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: b9819797e98fcd49f263cec1f77563537709bcb8 10.35.30.39:6374
   slots: (0 slots) slave
   replicates fcb83b0097d2a0a87a76c0d782de12147bc86291
M: fcb83b0097d2a0a87a76c0d782de12147bc86291 10.35.30.39:6373
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: f4660f264f12786d81bcf0b18bc7287947ec8a1b 10.35.30.39:6375
   slots: (0 slots) slave
   replicates e9a35d6a9d203830556de89f06a3be2e2ab4eee1
S: d2b9f265ef7dbb4a612275def57a9cc24eb2fd5d 10.35.30.39:6376
   slots: (0 slots) slave
   replicates 0c8755144fe6a200a46716371495b04f8ab9d4c8
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

See the output above as Cluster Cluster configuration complete . And for 3 Lord 3 from .

summary :

That's through docker compose The whole process of deploying sentinel mode and cluster mode ,redis Deployed in docker in , Applicable to local 、 Development 、 Testing and other environments , Please use the production environment carefully , Unless you're right docker Have strong control . Pay attention to me , Continue next
file

版权声明
本文为[Programmer a Niu]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/10/20211002145418057v.html

Scroll to Top