Linux Enterprise Operation and maintenance -- redis configuration, master-slave replication and master-slave automatic switching of redis service

Linux Enterprise Operation and maintenance The system used in the experiment is Redhat-rhel7.6. Linux Enterprise Opera...
1, Introduction
2, redis configuration and master-slave replication
3, Guidelines for using common redis instructions
4, redis master-slave automatic switching (based on sentinel)
Linux Enterprise Operation and maintenance

The system used in the experiment is Redhat-rhel7.6.

Linux Enterprise Operation and maintenance – (7)redis configuration, master-slave replication and master-slave automatic switching of redis services

1, Introduction

Redis (Remote Dictionary Server), i.e. remote dictionary service, is an open source log and key value database written in ANSIC language, supporting network, memory based and persistent, and provides API s in multiple languages.

redis is a key value storage system. Similar to Memcached, it supports more types of stored values, including string (string), list (linked list), set (set), zset(sorted set -- ordered set) and hash (hash type) . these data types support push/pop, add/remove, intersection, union, difference and richer operations, and these operations are atomic.

Redis is a high-performance key value database. The emergence of redis largely compensates for the lack of key/value storage such as memcached, which can be a good supplement to relational databases on some occasions. It provides Java, C/C + +, c#, PHP, JavaScript, Perl, Object-C, Python, Ruby, Erlang and other clients.

Redis supports master-slave synchronization. Data can be synchronized from the master server to any number of slave servers, and the slave server can be the master server associated with other slave servers. This enables redis to perform single-layer tree replication. The save disk can write data intentionally or unintentionally. Due to the full implementation of the publish / subscribe mechanism, it is possible to synchronize the tree from the slave database anywhere Subscribe to a channel and receive a complete message publishing record from the master server. Synchronization is very helpful for scalability and data redundancy of read operations.

1. Software download and installation

https://redis.io/download tar zxf redis-5.0.8.tar.gz make && make install utils/install_server.sh

2. Data types supported by redis

strings
hashes
lists
sets
sorted sets

3. redis common instructions

config get * ##View configuration select 1 ##Select database flushdb ##Empty the current database flushall ##Empty all databases move key 1 ##Move key del key ##delete rename oldkey newkey ##Rename expire key 10 ##Set expiration time persist key ##Set persistence keys user* ##query exists key ##Judge whether it exists

2, redis configuration and master-slave replication

Redis uses asynchronous replication, so it is impossible to ensure whether the slave actually receives the given write command.
Process:
When the host connects to a new slave, the slave will delete all its own data flushall. This may also happen because the slave timed out during the connection and the data will be deleted when reconnecting. Then, the slave will copy data from the host. If the host continues to write, the slave will copy according to the host's records.

First, install and compile redis on the server1 host.

###server1 cd lftp 172.25.254.250 > cd pub/docs/redis > get redis-6.2.4.tar.gz tar zxf redis-6.2.4.tar.gz cd redis-6.2.4/ make make install


Run the built-in installer. The installation fails. Modify the file and reinstall.

cd utils/ ls ./install_server.sh ##Failed to run the built-in installer vim install_server.sh ##The following section will be annotated /// #if [ "$" = systemd ] #then # echo "This systems seems to use systemd." # echo "Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!" # exit 1 #fi /// ./install_server.sh ##success




At this time, switch to the configuration directory, you can see that the configuration file is automatically generated, view the port, and the port is opened. Modify the configuration file and restart the service.

cd /etc/redis ls ##6379.conf netstat -antlp ##6379 vim 6379.conf /// 94 protected-mode no ##Set off protection mode 75 #bind 127.0.0.1 -::1 ##When linking redis, you can only link through the local localhost (127.0.0.1), not the network ip(192.168..). Because it is in the protection mode, you can only link locally, so set this comment /// /etc/init.d/redis_6379 stop /etc/init.d/redis_6379 start netstat -antlp | grep :6379






Copy this installation package to the server2 host.

cd scp redis-6.2.4.tar.gz server2:


On the server2 host, use another method to configure redis.
First, unzip the file first, and then build it with systemd support, but the dependency gcc is missing. Install the dependency and make again, but it still fails to remind jemalloc of the lack of dependency. At this time, choose to delete the redis directory, that is, delete the cache generated in the c environment, unzip it again, make, remind the lack of systemd, continue to make after installation, succeed, and make install.

###server2 ls./install_server.sh tar zxf redis-6.2.4.tar.gz cd redis-6.2.4/ make USE_SYSTEMD=yes ##Building with systemd support fails and requires a c compiler yum install -y gcc make USE_SYSTEMD=yes ##You still need to download the dependent jemalloc cd rm -rf redis-6.2.4 ##Delete cache generated in c environment tar zxf redis-6.2.4.tar.gz ##Re decompress cd redis-6.2.4/ make USE_SYSTEMD=yes ##make again yum install -y systemd-devel ##Install systemd make USE_SYSTEMD=yes ##make again make install







At this time, switch to the utils directory, run the built-in installer, copy the service file to the configuration location where the startup file is automatically stored in the directory, reload the configuration file and restart the service.

cd utils/ ./install_server.sh cp systemd-redis_server.service /usr/lib/systemd/system/redis.service ##Directory is the configuration location where startup files are stored automatically systemctl daemon-reload ##Reload profile systemctl start redis.service


Modify the redis.service file, establish the directory where the configuration file and data file are stored, edit the configuration file, and set server2 host as the slave end of server1 host, that is, master-slave copy settings, reload the configuration file, and restart the service.

cd /usr/lib/systemd/system/ vim redis.service ##Take care to modify the file carefully /// [Unit] Description=Redis data structure server Documentation=https://redis.io/documentation #Before=your_application.service another_example_application.service #AssertPathExists=/var/lib/redis Wants=network-online.target After=network-online.target [Service] #ExecStart=/usr/local/bin/redis-server --supervised systemd --daemonize no ## Alternatively, have redis-server load a configuration file: ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf #LimitNOFILE=10032 #NoNewPrivileges=yes #OOMScoreAdjust=-900 #PrivateTmp=yes Type=forking #TimeoutStartSec=infinity #TimeoutStopSec=infinity #UMask=0077 #User=redis #Group=redis #WorkingDirectory=/var/lib/redis /// mkdir /etc/redis mkdir /var/lib/redis cd cd redis-6.2.4/ cp redis.conf /etc/redis/ vim /etc/redis/redis.conf /// 302 logfile "/var/log/redis.log" ##Log storage directory 94 protected-mode no ##Turn off protection mode 75 #bind 127.0.0.1 -::1 ##Note: allow network ip link redis 454 dir /var/lib/redis ##Directory where data files are placed 257 daemonize yes ##Run as a daemon 2052 slaveof 172.25.24.1 6379 ##Master slave replication /// systemctl daemon-reload systemctl restart redis








At this time, insert data on the server1 host. You can see on the server2 host that the master-slave replication is set successfully.

###server1 redis-cli > set name westos > get name ###server2 redis-cli > get name


3, Guidelines for using common redis instructions

redis Common instructions: config get * //View configuration select 1 //Select database flushdb //Empty the current database flushall //Empty all databases move key 1 //Move key del key //delete rename oldkey newkey //Rename expire key 10 //Set expiration time persist key //Set persistence keys user* //query exists key //Judge whether it exists

experiment:

###server2 [root@server2 system]# redis-cli 127.0.0.1:6379> get name "westos" 127.0.0.1:6379> select 1 ##Select database OK 127.0.0.1:6379[1]> get name (nil) 127.0.0.1:6379[1]> select 0 OK 127.0.0.1:6379> get name "westos" 127.0.0.1:6379> set user zxc (error) READONLY You can't write against a read only replica. ##slave read only
###server1 [root@server1 redis]# redis-cli 127.0.0.1:6379> select 1 ##Select database OK 127.0.0.1:6379[1]> set user zxc OK 127.0.0.1:6379[1]> get user "zxc" 127.0.0.1:6379[1]> FLUSHDB ##Empty the current database OK 127.0.0.1:6379[1]> get user ##Empty (nil) 127.0.0.1:6379[1]> FLUSHALL ##Empty all databases OK 127.0.0.1:6379[1]> SELECT 0 OK 127.0.0.1:6379> get name ##Empty (nil) 127.0.0.1:6379> set name westos OK 127.0.0.1:6379> EXPIRE name 5 ##Set expiration time (integer) 1 127.0.0.1:6379> get name "westos" ...wait 127.0.0.1:6379> get name ##Expires after 5s (nil) 127.0.0.1:6379> set user1 qwe OK 127.0.0.1:6379> set user2 asd OK 127.0.0.1:6379> set user3 zxc OK 127.0.0.1:6379> keys user* ##query 1) "user3" 2) "user2" 3) "user1" 127.0.0.1:6379> EXISTS user1 ##Judge whether it exists (integer) 1 ##1 is present 127.0.0.1:6379> EXISTS user (integer) 0 ##0 does not exist

4, redis master-slave automatic switching (based on sentinel)

Sentinel (sentry) Sentinel is a tool used to monitor the master status in the Redis cluster. It is a high availability solution for Redis. Sentinel sentinel mode has been integrated in the version after Redis 2.4. Sentinel is a high availability solution for Redis. Sentinel system can monitor one or more redis master services and all slave services of these master services. When a master service goes offline, it will automatically Automatically upgrade a service under the master to a master service instead of the offline master service to continue processing requests.

Sentinel can enable redis to implement master-slave replication. When a master in a cluster fails, sentinel can elect a new master to automatically take over the work of the master, and other redis servers in the cluster automatically point to the new master to synchronize data. It is generally recommended that sentinel adopt an odd number to prevent a sentinel from being unable to connect to the master, resulting in false handover.

Principle:
In the case of one master and two slaves, when the master is disconnected from two slaves or due to the network relationship, the client does not know that the master is lost and will continue to write data, but the slave end cannot copy data at this time.

Slave will elect a new master to form a new master-slave relationship.

If the master recovers and rejoins the master-slave relationship, it will be converted to a new slave. However, due to the timeout connection, all its own data will be cleared, and the data written by the client will be lost.

If you need to solve this problem, you need two slaves to determine that you can't connect to the master at the same time, or you can't connect after the set time. At this time, the master side will refuse the client to continue writing, so there will be no data loss when the re access becomes a slave.

Start of experiment ~
Copy redis to server3 on server1 host.

scp redis-6.2.4.tar.gz server3:

Configure on the server3 host. Unzip, install gcc, make, delete the cache generated in the c environment, re unzip, make install, modify the installation file, and run the built-in installer. It is basically consistent with the above and will not be repeated.

###server3 ls tar zxf redis-6.2.4.tar.gz cd redis-6.2.4/ make yum install -y gcc make cd rm -rf redis-6.2.4 ##Delete cache generated in c environment tar zxf redis-6.2.4.tar.gz ##Re decompress cd redis-6.2.4/ make make install cd utils/ vim install_server.sh /// #if [ "$" = systemd ] #then # echo "This systems seems to use systemd." # echo "Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!" # exit 1 #fi /// ./install_server.sh







Modify the configuration file, restart the service, enter redis info, and you can see the role:slave master_host:172.25.24.1, that is, the current host server3 is the slave of server1.

vim /etc/redis/6379.conf /// 2052 slaveof 172.25.24.1 6379 94 protected-mode no 75 #bind 127.0.0.1 -::1 /// /etc/init.d/redis_6379 restart redis-cli > info ##role:slave master_host:172.25.24.1





In the server1 host, configure sentinel and copy it to server2 and server3.

Configure sentinel mode, i.e. high availability ###server1 cd redis-6.2.4/ cp sentinel.conf /etc/redis/ cd /etc/redis/ vim sentinel.conf /// sentinel monitor mymaster 172.25.24.1 6379 2 ##If the master is server1 and 2, it means that two votes are required to pass, and the host is deemed to be down sentinel down-after-milliseconds mymaster 10000 ##The connection timeout is 10s /// scp sentinel.conf server2:/etc/redis/ scp sentinel.conf server3:/etc/redis/ redis-sentinel /etc/redis/sentinel.conf ##You can see two slave ends







At this time, on server2 and server3, you can see one master and two slave terminals connected to each other.

###server2 & server3 redis-sentinel /etc/redis/sentinel.conf



Reconnect a server1, info, check that it is currently a master with two slave terminals, and then shut down the current host.

###Server1 (ssh again) redis-cli > info ##role:master connected_slaves:2 > shutdown




In the original server1, you can see that the machine is down, and the new slave is 1 and 3, that is, the new master is 2, and slave1 is hung at this time.

In the new server1, connect to server2. info shows that it is currently a master and there is a slave side, because server1 is currently suspended.

###Server1 (New) redis-cli -h 172.25.24.2 ##Connect to 2 > info ##You can see the role: master connected_ Slave: 1, a slave end



Restart server1 and check the info. It is currently in the slave state, and the master side is the server2 host. That is, when the Master goes down, sentinel automatically switches the master host successfully.

/etc/init.d/redis_6379 start redis-cli > info ##role:slave master_host:172.25.24.2. Currently, it is slave and master is 2


14 September 2021, 21:30 | Views: 7180

Add new comment

For adding a comment, please log in
or create account

0 comments