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

Programmer a Niu 2021-10-14 06:36:55
minutes build redis cluster mode


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://javamana.com/2021/10/20211002145418057v.html

  1. Hadoop Foundation - 03 - hdfs (Hadoop Distributed File System) Basic Concepts
  2. Hadoop Foundation - 05 - hdfs Project (word Frequency Statistics)
  3. Hadoop Foundation - 06 - hdfs Data Read and write
  4. The "monthly test" report card of the new car built under the lack of core: Xiaopeng and Weilai took the lead in "breaking 10000", and the ideal plummeted by 25%
  5. Introduction to making arch linux software package
  6. Hard core observation 407 HTTPS everywhere browser extension is about to retire
  7. How to use busybox on Linux
  8. In 2021, the talent incentive plan of Linux foundation open source software School Park was officially launched
  9. It's not windows or Linux. Shrink is the "God operating system"
  10. Install anydesk on Ubuntu Linux
  11. 2021, can we recommend using Linux to play games?
  12. not exist:org.springframework.kafka.listener.AbstractMessageListenerContaingetContainerProperties()
  13. Are you sure HTTPS is asymmetric encryption for content encryption? See the answers and reasons
  14. MySQL online slow log query
  15. Java JDK server installation
  16. 手把手教你使用Java开发在线生成pdf文档
  17. Questions d'entrevue dans la base de données MySQL (dernière version 2021)
  18. Java零基础小白入门必做题汇总(建议收藏)第一篇
  19. Day15 Java Foundation
  20. Day16 Java Foundation
  21. Day17 Java Foundation
  22. Day18 Java Foundation
  23. Linux installe JDK 1.8 et configure les variables d'environnement
  24. Tutoriel d'utilisation Maven super détaillé
  25. Spring boot reads project parameter configuration
  26. Docker installing rocketmq
  27. Java Zero Basic small white Beginner must make a summary of issues (recommended Collection) Chapitre 1
  28. Manuel pour vous apprendre à utiliser le développement Java pour générer des documents PDF en ligne
  29. 40 + comment les femmes s'habillent - elles pour montrer leur jeunesse?Un manteau et une jupe vous donnent un look haut de gamme tout au long de l'automne et de l'hiver
  30. Tutoriel d'installation Ubuntu 16.04 / Hadoop 3.1.3Configuration autonome / pseudo - distribuée
  31. L'apprentissage le plus détaillé de springboot à l'échelle du réseau - day01
  32. L'apprentissage le plus détaillé de springboot sur le Web - day02
  33. L'apprentissage le plus détaillé de springboot sur le Web - day03
  34. L'apprentissage le plus détaillé de springboot sur le Web - day04
  35. Tutoriel d'utilisation Maven super détaillé
  36. L'apprentissage le plus détaillé de springboot sur le Web - day05
  37. L'apprentissage le plus détaillé de springboot sur le Web - day06
  38. L'apprentissage le plus détaillé de springboot sur le Web - day07
  39. Introduction to JavaScript - write a photo album for your girlfriend
  40. [Hadoop 3. X] HDFS storage type and storage strategy (V) overview
  41. L'apprentissage le plus détaillé de springboot sur le Web - day08
  42. Introduction à la page Web de rabbitmq (3)
  43. No Converter found for return value of type: class java.util.arraylist Error Problem
  44. (16) , spring cloud stream message driven
  45. Que faut - il apprendre de l'architecture des microservices Spring Cloud?
  46. Résolution: erreur: Java: distribution cible invalide: 11problème d'erreur
  47. Springboot démarre en une minute et sort de l'enfer de la configuration SSM!
  48. Maven - un outil de gestion essentiel pour les grands projets d'usine, de l'introduction à la maîtrise![️ Collection recommandée]
  49. ️ Push to interview in Large Factory ᥧ - - Spring Boot Automatic Assembly Principle
  50. [️ springboot Template Engine] - thymeleaf
  51. Springboot - MVC Automatic configuration Principle
  52. Mybatis reverse engineering and the use of new version mybatisplus 3.4 reverse engineering
  53. Base de données MySQL - transactions et index
  54. Sécurité du printemps - [authentification, autorisation, déconnexion et contrôle des droits]
  55. Moteur de base de données InnoDB diffère de myisam
  56. Swagger - [springboot Integrated Swagger, configure Swagger, configure scan Interface, configure API Group]
  57. Cadre de sécurité Shiro - [QUICKstart, login Block, User Authentication, request Authorization]
  58. [Introduction à Java] installation de l'environnement de développement - Introduction à Java et construction de l'environnement
  59. 【 linux】 notes d'utilisation tmux
  60. MySQL + mybatis paging query - database series learning notes