Note:
If you are looking for a job, you can read my “Operation Interview Guide column” (continuously updated).
If you are confused about your career development, you can read “IT Student’s Growth Guide” (updating).
Those who want to systematically learn cloud computing operation and maintenance can read The Column of “Gradually Learning Operation and maintenance” (continuously updating).
For those who want to change their mind and build their own side business, check out “Essays on a $300 Salary” (constantly updated).
Author’s brief introduction
Author name: Internet old xin
Introduction: CSDN blog expert, front-line Internet practitioner, architect, cloud computing operation and maintenance lecturer, Internet serial entrepreneur, began to do live teaching from 2016, in recent years, has helped nearly 3000+ partners to obtain high salary employment. Welcome to join me to consult related questions, take you out of confusion;
Motto: I don’t think I am the best, but I can be the most hard-working
The body of the
Redis overview and install Redis
1. Introduction to Redis
Redis is a fully open source, BSD compliant, high-performance key-value database.
Redis and other key-value cache products have the following three features:
- Redis supports data persistence. Data stored in memory can be saved to disk, which can be reloaded and used upon restart.
- Redis supports not only simple key-value data, but also lists, sets, zsets, hash and other data structures.
- Redis supports data backup in master-slave mode.
The recommended version on the official website is 6.2.3
2. Data types supported by Redis
Supported data types for storage are: String (String containing integers), List (List), Hash (associative array), Sets (set), Sorted Sets (ordered set), Bitmaps (Bitmaps), and HyperLoglog.
3. Redis performance evaluation
- A million smaller keys store the string, consuming about 100 megabytes of memory
- Since Redis is single-threaded, if there are multiple cpus on the server host, only one can be used, but it does not mean that the CPU will become a bottleneck, because Redis is a relatively simple K-V data storage, CPU usually will not become a bottleneck
- On a common Linux server, 500K (500,000) of concurrency takes only a second to process, and can reach millions of concurrent requests per second with better host hardware
The differences between Reids and memcache can be read in my op-ed “Operations Interview Guide”.
Here are two examples:
- (1) Redis supports persistence, memcache also supports persistence, but generally does not (restart data loss)
- (2) [Data type Supported] Redis has five data types: string, list, hash, set, and sorted Set. Memcache can only be a string
4. Install redis
- When installing the Remi Repository source, you must rely on the epel source, so install the epel source first
[root@itlaoxin17 ~]# yum -y install epel-release
Copy the code
- Install the Remi Repository source
[root@itlaoxin~]# yum -y install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
Copy the code
- Install Redis using the Remi Repository source
[root@itlaoxin ~]# yum --enablerepo=remi install -y redis # yum --enablerepo=remi install -y redis # yum --enablerepo=remi install -y redis
Copy the code
- Start the Redis
[root@itlaoxin ~]# systemctl start redis
[root@itlaoxin-17 ~]# netstat -antup | grep redisTCP 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 68199/redis-server TCP60 0 0: :1:6379 :::* LISTEN 68199/redis-server note: Redis listens on port 6379 by defaultCopy the code
Redis basic operations
Basic operation
[root@itlaoxin ~]# redis-cli Option: -h <hostname> Specify host IP -p <port> Specify port socket file for communication. [root@itlaoxin ~]# redis-cli -h 127.0.0.1127.0.0.1:6379 >exit Exit the connection.Or: [root @ itlaoxin ~]Use redis-cli to connect directly, default connection is 127.0.0.1 IP.
Copy the code
Example 1: Set the value of the system key to centos
[root@itlaoxin ~]# redis - cli - h 127.0.0.1127.0.0.1:6379 >set system centos #set is used to create key valuesOK 127.0.0.1:6379 > get the systemYou can see the value in the key by adding the key after get
"centos"
Copy the code
Example 2: Set the name of a key to Zhangsan and the expiration time to 60 seconds.
127.0.0.1:6379 >setName itlaoxin EX 60 OK 127.0.0.1:6379> get name"itlaoxin"
Copy the code
Example 3: Enable the Redis user authentication service
[root@itlaoxin ~]# vim /etc/redis.confTo: 901# requirePass Foobared # Enable this and specify the password.901 requirePass JFVip123# specifies password jfvip123
[root@itlaoxin ~]# systemctl restart redis # restart the service
[root@itlaoxin ~]# iptables -f # disable firewallTest user authentication: [root@itlaoxin ~]# redis-cli # login test
127.0.0.1:6379> get system
(error) NOAUTH Authentication required. # Error detected, user password authentication required127.0.0.1:6379 > auth jfvip123# Enter passwordOK 127.0.0.1:6379 > get the system"centos"
Copy the code
Configure reIDS persistence
1. Introduction to Redis persistence
Overview of Redis persistence
There are two main types of ROD and AOF for redis persistence, but you can also mix the two and introduce them from reids4.0.
RDB implementation principle: RDB is similar to snapshot, at a certain point in time, Redis in memory database state (database key peer information) saved to disk. The RDB files generated by the RDB persistence function are compressed binary files.
AOF: Saves all write operation commands executed by the Redis server to record the database status and restores the dataset by re-executing these commands when the server starts. AOF is off by default, and enabling AOF persistence with appendonley yes can be accomplished in three steps: apending commands, writing files, and synchronizing files.
2. Set parameters related to RDB in the configuration file
[root@itlaoxin ~]# vim /etc/redis.conf
235 stop-writes-on-bgsave-error yes # Whether to stop a snapshot backup if an error occurs.
241 rdbcompression yes The RDB file is compressed. Compression consumes CPU.
250 rdbchecksum yes When redis loads the RDB file, the RDB file checks the verification code. When Redis generates the RDB file, the RDB file checks the verification information. When Redis starts or loads the RDB file, the RDB file checks the verification information.
253 dbfilename dump.rdb Define the name of the RDB file.
263 dir /var/lib/redis # Define the directory where the RDB file is stored.
Copy the code
Practical construction of Redis master – slave architecture
- Principle:
The Redis master/slave replication is automatically performed without user intervention. The slave will automatically connect to the master and perform data synchronization. If the slave is temporarily disconnected from the master, the slave will synchronize with the master once the connection is recovered. To ensure that the data is consistent.
- The topology:
Configure itlaoxin17 as the redis primary
1. Install Redis on the ITlaOXin17 host
[root@itlaoxin-17 ~]# rm -rf /var/run/yum.pid
[root@itlaoxin ~]# yum -y install epel-release # yum -y install epel-release
[root@itlaoxin ~]# # yum - y install http://rpms.remirepo.net/enterprise/remi-release-7.rpm installation remi source.
[root@itlaoxin ~]# yum --enablerepo=remi install -y redis # yum --enablerepo=remi install -y redis
Copy the code
2. Modify the Redis configuration file on host ITlaOXin17
[root@itlaoxin-17 ~]# vim/etc/redis. Conf:To: 75bind127.0.0.1 for: 75bind 0.0.0.0 Redis listens at 127.0.0.1 by default. Change to 0.0.0.0 or 192.168.1.17To: 507# requirePass Foobared # Enable this and specify the password.It is 507 requirePass jfxinvip123# specify password jfxinvip123
Copy the code
Restart the service
[root@itlaoxin ~]# systemctl restart redis # restart the service
[root@itlaoxin ~]# iptables -F
Copy the code
3. Configure itlaOXin18 as the redis slave and install Redis on the ITlaoxin18 host
[root@itlaoxin ~]# yum -y install epel-release # yum -y install epel-release
[root@itlaoxin ~]# # yum - y install http://rpms.remirepo.net/enterprise/remi-release-7.rpm installation remi source.
[root@itlaoxin ~]# yum --enablerepo=remi install -y redis # yum --enablerepo=remi install -y redis
Copy the code
4. Modify the Redis configuration file on the itlaOXin18 host
[root@itlaoxin ~]# vim /etc/redis.confTo: 478# replicaof < masterIP >
# Replicaof
478 replicaof 192.168.1.17 6379 change to 486# masterauth <master-password>486 masterauth jfxinvip123# Write the password of the redis master. If the redis master does not have a password, do not write it here
[root@itlaoxin ~]# systemctl start redis
Copy the code
5. Log in to host ITlaoxin18 to check the primary/secondary replication status
[root@itlaoxin ~]# redis-cli127.0.0.1:6379 > info replication# Replication
role:slave # Role: slaveMaster_host: 192.168.1.17# Primary server IP address.
master_port:6379 # Primary server port
master_link_status:up The connection status of the primary server is up, indicating that the primary server has been synchronized with the secondary server
master_last_io_seconds_ago:8
master_sync_in_progress:0
slave_repl_offset:112
slave_priority:100
slave_read_only:1 After # becomes from service, 1 indicates that the server is read-only.
Copy the code
6. Log in to the itlaoxin17 host to view the information about the primary and secondary replications
[
root@itlaoxin ~]# redis-cli
127.0.0.1:6379>AUTH jfxinvip123
</pre>OK
127.0.0.1:6379> info replication
# Replication
role:master # Role: Master.
connected_slaves:1 # Number of slave servers.= 192.168.1.18 slave0: IP and port = 6379, state = online, offset = 476, lag = 0# Secondary server information..Copy the code
7. Verify master/slave replication on ITlaOXin17:
Log in to the Redis main service and create a user key. [root@itlaoxin-17 ~]# redis-cli
127.0.0.1:6379> AUTH jfxinvip123
OK
127.0.0.1:6379> setUser itlaOXIN OK 8. Log in to the Redis secondary service and obtain the user key created on the primary service. [root@itlaoxin ~]# redis-cli127.0.0.1:6379 > get userIf the key value can be viewed, it indicates that the primary and secondary data synchronization is successful.
"itlaoxin"
Copy the code
Practice 2: Redis cluster high availability using Sentinel
Experimental topology: Restart the three new servers, which must be the three new servers. If you do this in the previous experimental environment, the configuration will be chaotic. Redis and Sentinel are configured on each server. There are one master and two slaves. As shown in the figure below:
1. Install Redis on three machines
[root@itlaoxin ~]# yum -y install epel-release
[root@itlaoxin ~]# yum -y install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
[root@itlaoxin ~]# yum --enablerepo=remi install -y redis # yum --enablerepo=remi install -y redis
Copy the code
2. Modify the Redis configuration file on host ITlaOXin17
[root@itlaoxin17 ~]# vim /etc/redis.confTo: 675bind127.0.0.1 for: 75bind 0.0.0.0 Redis listens on all network interfaces改 : 94 protected-mode yes = 94 protected-mode noDisable protected-mode to allow external access to redis server
[root@itlaoxin17 ~]# systemctl restart redis # restart the service
[root@itlaoxin17 ~]# iptables -F
Copy the code
3. Configure itLAOXin18/19 as the Redis secondary server
[root@itlaoxin18 ~]# vim /etc/redis.confTo: 75bind127.0.0.1 for: 75bind 0.0.0.0 Redis listens on all network interfaces改 : 94 protected-mode yes 改 : 94 protected-mode no 改 : 477# replicaof <masterip> <masterport> 478 replicaof 192.168.1.17 6379 [root@itlaoxin18 ~]# systemctl start redis
[root@itlaoxin18 ~]# iptables -F
Copy the code
4. Log in to host ITlaoxin19 to check the primary/secondary replication status
[root@itlaoxin19 ~]# redis-cli127.0.0.1:6379 > info replication# Replication
role:slave # Role: slave.Master_host: 192.168.1.17# Primary server IP address.
master_port:6379 # Primary service port.
master_link_status:up The primary server is up, indicating that the primary server has been synchronized with the secondary serverUp to this point, the Redis master-slave replication architecture of 1 master and 2 slave has been established successfully. Now we start to configure the Sentinel architecture.Copy the code
Configure Sentinel1 on Itlaoxin17
[root@itlaoxin17 ~]# vim /etc/redis-sentinel.confChange: 17# protected-mode noMymaster 127.0.0.1 6379 2 = : 84 Sentinel monitor mymaster 192.168.1.17 6379 2 改 : 113 Sentinel down after milliseconds mymaster 30000The default unit is milliseconds, which is 10 seconds113 sentinel down-post-milliseconds mymaster 180000 错 : 146 sentinel failover mymaster 180000 错 : 113 sentinel down-post-milliseconds mymaster 180000 错 : 146 sentinel failover mymaster 180000 146 Sentinel failover-timeout mymaster 60000 If the failover operation is not completed within the time (ms), the failover failsCopy the code
6. Send redis-sentinel.conf to itlaoxin18 and itlaoxin19
[root@itlaoxin17 ~]# SCP/etc/redis - sentinel. Conf 192.168.1.18: / etc/redis - sentinel. Conf
[root@itlaoxin17 ~]# SCP/etc/redis - sentinel. Conf 192.168.1.19: / etc/redis - sentinel. Conf
11111111111111111111111111111
Copy the code
7. Start the Redis and Sentinel servers
[root@itlaoxin17 ~]# systemctl start redis && systemctl start redis-sentinel
[root@itlaoxin18 ~]# systemctl start redis && systemctl start redis-sentinel
[root@itlaoxin19 ~]# systemctl start redis && systemctl start redis-sentinelBe sure to turn selinux offCopy the code
8. Simulate the fault
Viewing the current primary/secondary status [root@itlaoxin17 ~]# redis - 192.168.1.18 cli - h192.168.1.18:6379 > info replication# ReplicationRole :slave master_host:192.168.1.17 master_port:6379 [root@itlaoxin17 ~]# systemctl stop redis # stop master on itlaoxin17
[root@itlaoxin-17 ~]# redis - 192.168.1.19 cli - h192.168.1.19:6379 > info replication# ReplicationRole: slave master_host: 192.168.1.8# Master has been transferred to itlaoxin18
master_port:6379
Copy the code
9. Itlaoxin17 recovery
[root@itlaoxin17 ~]# systemctl start redis
[root@itlaoxin17 ~]# redis - 192.168.1.17 cli - h192.168.1.17:6379 > info replication# ReplicationRole: slave master_host: 192.168.1.18# The primary redis is still itlaoxin18, and will not voluntarily give up permissions because itlaoxin17 is restored successfully. In this way, service interruption can be avoided during the failback.
# Log in to the primary Redis to view the secondary node information
[root@itlaoxin-19 etc]# redis-cli 127.0.0.1:6379 > info replication# ReplicationRole :slave master_host:192.168.1.18 master_port:6379 master_link_status:up master_last_io_seconds_ago:1 master_sync_in_progress:0 slave_repl_offset:33537 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:49a65486e84936ed563d6796f253945b492660a8 master_replid2:7514fbacc143345f567dd8e93a081592b24e0c9a master_repl_offset:33537 second_repl_offset:2702 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:163 repl_backlog_histlen:33375 127.0.0.1:6379 >Copy the code
10. View Sentinel information
[root@itlaoxin17 ~]# redis-cli -h 192.168.1.17 -p 26379 # Redis -cli -h 192.168.1.17 -p 26379
[root@itlaoxin18 ~]# redis-cli -h 192.168.1.17 -p 26379 192.168.1.17:26379 > info sentinel# Sentinelsentinel_masters:1 sentinel_tilt:0 sentinel_running_scripts:0 sentinel_scripts_queue_length:0 Sentinel_simulate_failure_flags: 0 master0: name = mymaster, status = sdown, address = 192.168.1.17:6379, slaves = 0, sentinels = 1 192.168.1.17:26379 >exit
14
Copy the code
The old Internet xin, set a flag in 2021, live sharing technology 5 hours a day, lasting 10,000 hours, that is, 2000 days
Today is the 110/2000 day of continuous live teaching. Ask for likes, comments and collections. Any questions can be answered in the comments section
Collect this article, later may use to oh ~