In this installation, Ubuntu Server X64 18.04 LTS version is used to install the Redis cluster environment. The cluster nodes are 1 master and 2 slave. PS: Due to insufficient resources, docker-Compose bridge network is used to deploy on the same machine. In normal production, such a situation of putting all eggs in one basket should be avoided. In the future, Redis high-availability cluster may be built based on Sentinel, and Docker will install it by itself
Environment configuration
The host name | IP | role | system | The CPU/memory | disk |
---|---|---|---|---|---|
redsis-master | 192.168.23.134 | The main | Ubuntu Server 18.04 | 2 nuclear 4 g | 40G |
redsis-slave-1 | 192.168.23.134 | from | Ubuntu Server 18.04 | 2 nuclear 4 g | 40G |
redsis-slave-2 | 192.168.23.134 | from | Ubuntu Server 18.04 | 2 nuclear 4 g | 40G |
redis-sentinel-1 | 192.168.23.134 | The main | Ubuntu Server 18.04 | 2 nuclear 4 g | 40G |
redis-sentinel-2 | 192.168.23.134 | from | Ubuntu Server 18.04 | 2 nuclear 4 g | 40G |
redis-sentinel-3 | 192.168.23.134 | from | Ubuntu Server 18.04 | 2 nuclear 4 g | 40G |
redis
# docker-compose.yml
version: '3'
services:
master:
image: redis
container_name: redis-master
command: redis-server --requirepass redis_pwd --masterauth redis_pwd
ports:
- 6380:6379
slave1:
image: redis
container_name: redis-slave-1
ports:
- 6381:6379
command: redis-server --slaveof redis-master 6379 --requirepass redis_pwd --masterauth redis_pwd
slave2:
image: redis
container_name: redis-slave-2
ports:
- 6382:6379
command: redis-server --slaveof redis-master 6379 --requirepass redis_pwd --masterauth redis_pwd
Copy the code
Docker – compose up running, add the background can be accomplished by the parameters – d, I specially print log here
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d48696ad6446 redis "Docker - entrypoint. S..."About a minute ago Up About a minute 0.0.0.0:6382->6379/ TCP redis-slave-2 49e9f9057550 redis"Docker - entrypoint. S..."About a minute ago Up About a minute 0.0.0.0:6381->6379/ TCP redis-slave-1 18cf4f04eBBC redis"Docker - entrypoint. S..."About a minute ago Up About a minute 0.0.0.0:6380->6379/ TCP redis-masterCopy the code
Three Redis containers are opened, and the exposed port number is 6382,6381,6380 (related to ports in the layout file).
Redis Sentinel
Redis’ Sentinel system is used to manage multiple Redis servers (instance). The system performs three tasks:
- Monitoring: Sentinel constantly checks that your master and slave servers are up and running.
- Notification: When a monitored Redis server has a problem, Sentinel can send a Notification to administrators or other applications through the API.
- Automatic failover: When a master server fails to work properly, Sentinel will start an automatic failover operation, which will upgrade one of the slave servers of the failed master server to the new master server, and make the other slave servers of the failed master server to replicate the new master server. When a client tries to connect to a failed primary server, the cluster also returns the address of the new primary server to the client, enabling the cluster to replace the failed primary server with the new primary server.
Redis Sentinel is a distributed system where you can run multiple Sentinel processes in a single architecture. These processes use gossipprotocols to receive information about whether the primary server is offline. Agreementprotocols is used to determine whether automatic failover is performed and which slave server is selected as the new master. Although Redis Sentinel is released as a separate executable file, Redish-Sentinel, it is actually just a Redis server running in a special mode, You can start Redis Sentinel by giving the — Sentinel option when starting a regular Redis server.
# docker-compose.yml
version: '3'
services:
sentinel1:
image: redis
container_name: redis-sentinel-1
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
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
Copy the code
Attach the data volume
# sentinel1.conf
port 26379
dir /tmp# 172.18.0.2 is the primary IP address of redis
# Instruct Sentinel to monitor a master server named myMaster with IP address 172.18.0.2 (obtained by docker inspect [containerIP]) and port number 6379
# At least 2 Sentinels are required to agree to make the primary server invalid (automatic failover will not be performed as long as the number of Sentinels is not up to standard)Sentinel Monitor MyMaster 172.18.0.2 6379 2#
sentinel auth-pass mymaster redis_pwd
# specifies the number of milliseconds Sentinel needs to consider the server disconnected.
sentinel down-after-milliseconds mymaster 30000
# specifies the maximum number of secondary servers that can synchronize the new primary server at the same time when a failover is performed.
# The smaller the number, the longer it will take to complete the failover.
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
Copy the code
Docker-compose up -d: docker-compose up -d: docker-compose up -d: docker-compose up -d: docker-compose up -d: docker-compose up -d
# -> Output is as follows
NTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bbd6093a6bdf redis "Docker - entrypoint. S..."About a minute ago Up About a minute 6379/ TCP, 0.0.0.0:26380->26379/ TCP Redis-Sentinel-2 69eb1530998d Redis"Docker - entrypoint. S..."About a minute ago Up About a minute 6379/ TCP, 0.0.0.0:26379->26379/ TCP Redis-Sentinel-1 019a72CDBc95 Redis"Docker - entrypoint. S..."About a minute ago Up About a minute 6379/ TCP, 0.0.0.0:26381->26379/ TCP Redis-Sentinel-3 d48696AD6446 Redis"Docker - entrypoint. S..."18 minutes ago Up 18 minutes 0.0.0.0:6382->6379/ TCP redis-slave-2 49e9f9057550 redis"Docker - entrypoint. S..."18 minutes ago Up 18 minutes 0.0.0.0:6381->6379/ TCP redis-slave-1 18cf4f04eBBC redis"Docker - entrypoint. S..."18 minutes ago Up 18 minutes 0.0.0.0:6380->6379/ TCP redis-masterCopy the code
validation
redis
Docker exec -it 18cf4f04eBBC redis- CLI to enter the main machine
Auth redis_pwd (requirePass value)127.0.0.1:6379 > info replication# Replication
role:master
connected_slaves:2
# Secondary node information= 172.18.0.4 slave0: IP and port = 6379, state = online, offset = 2492, lag = 0 = 172.18.0.3 slave1: IP and port = 6379, state = online, offset = 2492, lag = 1 master_replid: 2760 a09897b3d82d0e304265fcc2b030e1740300 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:2492 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:2492Copy the code
The exit went out
Redis Sentinel
docker exec -it 69eb1530998d bash
1) "name"
2) "mymaster"
3) "ip"
4) "172.18.0.2"
5) "port"
6) "6379"
7) "runid"
8) "3fc8105ab0b5722b5deb266b9fdaddfa15a7e683"
9) "flags"
10) "master"
11) "link-pending-commands"
12) "0"
13) "link-refcount"
14) "1"
15) "last-ping-sent"
16) "0"
17) "last-ok-ping-reply"
18) "747"
19) "last-ping-reply"
20) "747"
21) "down-after-milliseconds"
22) "30000"
23) "info-refresh"
24) "8849"
25) "role-reported"
26) "master"
27) "role-reported-time"
28) "69121"
29) "config-epoch"
30) "0"
31) "num-slaves"
32) "2"
33) "num-other-sentinels"
34) "2"
35) "quorum"
36) "2"
37) "failover-timeout"
38) "180000"
39) "parallel-syncs"
40) "1"127.0.0.1:26379 >Copy the code
record
Redis enables password authentication
Redis master/slave and CLSUter support password authentication. To enable password authentication in Redis, both RequirePass and Masterauth must be set. If the master node is configured with requirePass login authentication, the slave will need to send a ping message to the master node to verify that the master node is alive, listen to the master node’s ports, and use the master’s login password to synchronize data. Otherwise, you cannot log in, and the log will respond with an error. In other words, the masterauth of slave and requirePass of master are the same. Therefore, it is recommended that Redis set the masterauth and requirePass of each node to the same password when enabling the password to reduce the operation and maintenance cost. Of course, it is possible to set it to be different, just pay attention to the relationship between the slave masterauth and the master requirePass.
-
Masterauth Function: The masterauth function is set for the slave node corresponding to the master and is used for data synchronization on the slave node.
-
Requirepass is used to restrict login permissions. The requirePass function can be independent and different for each node in Redis.
Subjective and objective referrals
The down-after-milliseconds option specifies the number of milliseconds Sentinel needs to think the server is disconnected. If the server does not reply to the PING command sent by Sentinel within a given number of milliseconds, or if Sentinel returns an error, Sentinel will mark the server as subjectively offline (subjectively down, or SDOWN).
- Subjective downline (Subjectively Down, SDOWN for short) refers to the downline judgment made by a single Sentinel instance on a server.
- Objectively Down (ODOWN for short) refers to that after multiple Sentinel instances make SDOWN judgment on the same server and communicate with each other through Sentinel is-master-down-by-ADDR command, we should make an Objectively Down judgment on the same server. The server offline judgment. (A Sentinel can ask another Sentinel whether it thinks a given server is offline by sending the Sentinel is-master-down-by-addr command.) The objective offline condition applies only to the primary server
If a server does not return a valid response [+PONG, -loading, or -masterdown] to Sentinel for sending PING messages within the time specified in the masterdown-after-milliseconds option. , Sentinel will mark the server as subjective offline.
Sentinel command
- PING: PONG is returned.
- SENTINEL Masters: Lists all monitored master servers and the current status of those master servers.
- SENTINEL Slaves: Lists all slaves for a given master server and the current state of those slaves. Sentinel-get-master-addr-by-name: Returns the IP address and port number of the primary server with a given name. This command returns the IP address and port number of the new primary server if the primary server is failing over, or failover has been completed for the primary server.
- SENTINEL reset: Resets all primary servers whose names match the given pattern. The pattern parameter is a Glob style pattern. The reset operation knows all current status of the primary server, including failover in progress, and removes all secondary servers and Sentinel that have been identified and associated with the primary server so far.
- SENTINEL failover: When the primary server fails, an automatic failover is forced to start without asking the opinion of the other Sentinels (however, the Sentinel that initiates the failover sends a new configuration to the other Sentinels, who are updated accordingly based on this configuration).
The end of the
Successful test!! 🍾 🍾 🍾