redis sentry high availability deployment scheme

redis high availability cluster deployment scheme

1. redis installation and deployment

redis installation is recommended to use source code compilation and installation, which is applicable to most linux systems

# To download the redis file, it is recommended to create a new folder where you want to install it, and then download it to the folder. You can select the version number according to your needs.
wget http://download.redis.io/releases/redis-5.0.7.tar.gz 
# Unzip the source file
tar -xzf redis-5.0.7.tar.gz
# Create connection
ln -s redis-5.0.7 redis
# Compile (before compiling, you need to confirm that the gcc environment is installed on the system. If it is not installed, centos can be installed using sudo yum -y install gcc)
make
# install
make install
# It is recommended to start redis in the form of configuration file
# There are three startup paths: the current installation directory, the directory where the soft connection is created, / usr/local/bin
nohup ./redis-server /opt/redis/redis/redis.conf &

2. redis sentinel solution

2.1 preparation before configuration

In this simulation, three virtual machines are deployed on this machine as deployment preparation, respectively:

  • master: 192.168.159.128
  • slave-1: 192.168.159.129
  • salve-2: 192.168.159.130
    It is planned to use one server as the master and two servers as the slave. The three servers are sentinel nodes for each other to realize master-slave replication and automatic fault migration without human interference
    The architecture diagram is as follows:

2.2 master node configuration

logfile "redis.log"  # Configure log file storage location
port 6379    # Configure the port. Use the default port here
daemonize yes   # Configuration starts as a daemon
dir /opt/redis/redis/data/   # Configuration file write directory
bind 0.0.0.0  # There are many online statements about this parameter. I tried it several times. If several nodes are configured on the same device and distinguished by port number, it is configured as 127.0.0.1, otherwise it is configured as 0.0.0.0

Restart redis service after configuration

2.3 slave node configuration

logfile "redis.log"  # Configure log file storage location
port 6379    # Configure the port. Use the default port here
daemonize yes   # Configuration starts as a daemon
dir /opt/redis/redis/data/   # Configuration file write directory
bind 0.0.0.0  # There are many online statements about this parameter. I tried it several times. If several nodes are configured on the same device and distinguished by port number, it is configured as 127.0.0.1, otherwise it is configured as 0.0.0.0
replicaof 192.168.159.128 6379  # Configure the IP and port of the master node. Some configuration files do not have this item. You can directly add it to the configuration file;
masterauth 123  # Configure the password of the primary node redis; If the password is empty, this item can be omitted

2.4 verification

Master node Perspective

127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.159.129,port=6379,state=online,offset=11335,lag=0
slave1:ip=192.168.159.130,port=6379,state=online,offset=11335,lag=1
master_replid:013734ec7f21c521d15d003f57165c290c88bdaf
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:11335
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:11335

From the node Perspective

127.0.0.1:6379> INFO replication
# Replication
role:slave
master_host:192.168.159.128
master_port:6379
master_link_status:up
master_last_io_seconds_ago:5
master_sync_in_progress:0
slave_repl_offset:14583
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:013734ec7f21c521d15d003f57165c290c88bdaf
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:14583
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:14583

Master slave replication verification

#  The master node generates key value pairs
127.0.0.1:6379> set redis best
OK
127.0.0.1:6379> get redis
"best"
#  Verify replication from node 1
127.0.0.1:6379> get redis
"best"

# Verify replication from node 2
127.0.0.1:6379> get redis
"best"

So far, the master-slave copy function is completed

2.5 pits stepped at this stage

  • ERROR CONDITION ON SOCKET FOR SYNC: NO ROUTE TO HOST
    It is found that the ip: port of the primary node through telnet is also unavailable. Check the reason for the limitation of the firewall of the primary node server
    Solution: turn off the firewall of the master node server (this method is not recommended. It is recommended to configure the firewall on the master node to allow it to pass through)

2.6 sentinel configuration

sentinel will finally implement failover. During failover, a salve will be randomly selected as the master

Stepped pits, and solutions
  • If the redis node needs to be configured with a password, all nodes need to be configured with the same password and the configuration needs to be synchronized, which is mainly reflected in:
    redis.conf–>masterquth xxx
  • Similarly, in order to ensure that the fault can jump accurately, all nodes need to pay attention to the firewall settings (the three nodes need to be able to access each other normally)

sentinel.conf configuration:

sentinel.conf
# Configure the port. The bind parameter is masked
port 26379
daemonize yes
logfile "redis-sentinel.log"
dir /opt/redis/redis/data
# Configuring the monitored redis master node IP, port and the following 2 means that two or more sentinels are required to agree before the master node is determined to be down and a new master node is jointly elected
sentinel monitor mymaster 192.168.159.128 6379 2
# The last three items are configured as the default configuration in sentinel
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000

Start sentinel

./redis-sentinel sentinel.conf

When all sentinel nodes are up, the sentinel configuration file will change as follows:

# In addition to the master node, two slave nodes were found
sentinel known-replica mymaster 192.168.159.128 6379
sentinel known-replica mymaster 192.168.159.130 6379
# Two other sentry nodes
sentinel known-sentinel mymaster 192.168.159.129 26379 e3a68083cc8f7bb61f217e923963cadfb856ed40
sentinel known-sentinel mymaster 192.168.159.128 26379 9341e0e29c853e035bbfdcd02b0f0e6e79ad7dae

After the above changes, the side proves that all sentinel nodes have been up;

Simulate failover

  • No failure:
  • Master node failure
  • Failover
  • After the original down master node is up:
    -
    To sum up:
  • It is recommended to use the source code to install redis
  • If redis has a password, all redis nodes need to configure the same password and the masyerauth field;

Due to the first deployment, many problems were encountered during the process. The downloads were not recorded one by one. Read the logs and report errors. The basic problem is not big.

Keywords: Operation & Maintenance Database Redis sentinel

Added by Asinox on Tue, 12 Oct 2021 10:04:20 +0300