In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the meaning and usage of redis master-slave copy in detail for everyone. The detailed explanation of picture and text is easy to learn, and the effect of reading and understanding with the code is better. It is very suitable for beginners. Interested friends can refer to it.
1, master-slave replication overview
The replication function of redis is to support data synchronization between multiple servers. The replicated server is called the master server (master), and the one that replicates the server is the slave server (slave). The master server master can read and write, and when the write operation occurs, the data will be synchronized to the slave server automatically, while the slave server is generally read-only and receives the data synchronized by the master. A master can have multiple slave, and a slave can only have one master.
The process of master-slave replication:
1, the slave node executes the slaveof command; 2, the slave node only saves the information of the master node in the slavef command and does not initiate replication immediately; 3, the timing task within the slave node discovers the information of the master node and starts to use socket to connect to the master node; 4, after the connection is established successfully, send a ping command, hoping to get a response from the pong command, otherwise it will reconnect 5, if the master node has set permissions, then permission verification is required; if the verification fails, the replication is terminated; 6, after the permission verification is passed, data synchronization is carried out, which is the longest operation. The master node will send all the data to the slave node. 7. When the master node synchronizes the current data to the slave node, the establishment process of replication is completed, and the master node will continuously send write commands to the slave node to ensure the consistency of the master and slave data.
The role of master-slave replication:
Data redundancy. To achieve hot backup of data; fault recovery. Avoid unavailable services caused by a single point of failure; read-write separation and load balancing. Master node is responsible for reading and writing, slave node is responsible for reading, improving server concurrency; high availability foundation. Is the basis of Sentinel and cluster implementation; 2, master-slave deployment
Environment description:
Host address Port operating system Master redis172.16.1.1006379CentOS 7.3 Slave redis172.16.1.1106379CentOS 7.3
1, deploy the primary node
1) install redis
Download address on the official website: http://download.redis.io/releases/
[root@redis-master ~] # tar zxf redis-4.0.14.tar.gz
[root@redis-master ~] # cd redis-4.0.14
[root@redis-master redis-4.0.14] # make & & make install
From the figure above, we can easily see that redis is installed in the / usr/local,/usr/local/bin,/usr/local/share,/usr/local/include,/usr/local/lib,/usr/local/share/man directory.
Then change to the utisl directory and execute the redis initialization script install_server.sh, as follows:
[root@redis-master redis-4.0.14] # cd utils/ [root@redis-master utils] #. / install_server.sh Welcome to the redis service installerThis script will help you easily set up a running redis serverPlease select the redis port for this instance: [6379] Selecting default: 6379Please select the redis config file name [/ etc/redis/6379.conf] Selected default-/ etc/redis/6379.confPlease select the redis log file name [/ var/log/redis_6379.log] Selected default-/ var / log/redis_6379.logPlease select the data directory for this instance [/ var/lib/redis/6379] Selected default-/ var/lib/redis/6379Please select the redis executable path [/ usr/local/bin/redis-server] Selected config:Port: 6379Config file: / etc/redis/6379.confLog file: / var/log/redis_6379.logData dir: / var/lib/redis/6379Executable: / usr/local/bin/redis -serverCli Executable: / usr/local/bin/redis-cliIs this ok? Then press ENTER to go on or Ctrl-C to abort.Copied / tmp/6379.conf = > / etc/init.d/redis_6379Installing service...Successfully added to chkconfigsuccessful added to runlevels 345 starting Redis server...Installation fulfilling # all of the above default enter
After the installation process above, we can see that after redis initialization, the redis configuration file is / etc/redis/6379.conf, the log file is / var/log/redis_6379.log, the data file dump.rdb is stored in the / var/lib/redis/6379 directory, and the startup script is / etc/init.d/redis_6379.
# after initialization, the redis service is enabled by default (default listening port 6379):
[root@redis-master ~] # / etc/init.d/redis_6379 statusRedis is running (5693) [root@redis-master ~] # ss-anput | grep redistcp LISTEN 0 128 127.0.1 ss 6379 *: * users: (("redis-server", pid=5693,fd=6))
# Firewall rule settings:
[root@redis-master] # firewall-cmd-- add-port=6379/tcp-- permanentsuccess [root@redis-master] # firewall-cmd-- reloadsuccess
2), configure redis
The modification content of [root@redis-master ~] # vim / etc/redis/6379.conf # is as follows (uncomment and modify): 70 bind 172.16.1.100 # modify the listening address of redis to ip 501 requirepass pwd@123 # of redis host. For security, you need to start the requirepass parameter of the password verification feature of redis. 137 daemonize yes # runs the redis instance as a daemon
# restart redis after the modification is completed:
[root@redis-master ~] # / etc/init.d/redis_6379 restartStopping... Waiting for Redis to shutdown... Redis stoppedStarting Redis server... [root@redis-master ~] # ss-anput | grep redistcp LISTEN 0 128 172.16.1.100 pid=5739,fd=6 6379 *: * users: ("redis-server", pid=5739,fd=6))
# remote connection redis:
A redis client is required to execute the command on the redis service, and the Redis client is in the redis installation package we downloaded earlier.
[root@redis-master ~] # redis-cli-- versionredis-cli 4.0.14 [root@redis-master ~] # redis-cli-h 172.16.1.100-p 6379-a pwd@123Warning: Using a password with'- a 'option on the command line interface may not be safe.172.16.1.100:6379 > ping # this command is used to detect whether the redis service starts PONG
2, deploy the slave node
1) the process of installing redis is the same as above, and it will not be repeated here.
2) configure redis
[root@redis-slave ~] # vim / etc/redis/6379.conf 70 bind 172.16.1.110 # modified to redis host ip137 daemonize yes # background run 501 requirepass pwd@123 # set redis authentication password 282 slaveof 172.16.1.100 6379 # this configuration item is the key to master-slave replication, point to the address and port of master node 289 masterauth pwd@123 # configure master authorization password (if master does not set requirepass option Slave server does not need to be configured)
There are actually three ways to configure master-slave replication:
Add slaveof [masterHost] [masterPort] ② redis-server to the ① configuration file when starting-- slaveof [masterHost] [masterPort] ③ login redis directly use the command slaveof [masterHost] [masterPort] # restart the redis service: [root@redis-slave ~] # / etc/init.d/redis_6379 restartStopping. Redis stoppedStarting Redis server... [root@redis-slave ~] # ss-anput | grep redistcp LISTEN 0128 172.16 .1.110 6379 *: * users: ("redis-server" Pid=4886,fd=6)) tcp ESTAB 00 172.16.1.110 users: (("redis-server", pid=4886,fd=7)) # you can see that there is one more master-slave replication process # configure firewall: [root@redis-slave ~] # firewall-cmd-- add-port=6379/tcp-- permanentsuccess [root@redis-slave ~] # firewall-cmd-- reloadsuccess
3. Test data synchronization
Master redis: [root@redis-master ~] # redis-cli-h 172.16.1.100-p 6379-a pwd@123Warning: Using a password with'- a 'option on the command line interface may not be safe.172.16.1.100:6379 > set name abc # set a key/valueOK172.16.1.100:6379 > get name "abc" slave redis: [root@redis-slave ~] # redis-cli-h 172.16.1.110-p 6379-a pwd@123Warning: Using a password with'- a 'option on the command line interface may not be safe.172.16.1.110:6379 > get name # data synchronization "abc" successfully
4. Test read-write separation (redis defaults to read-write separation)
# Test on slave redis: 172.16.1.110 READONLY You can't write against a read only slave 6379 > set age 20 (error) READONLY You can't write against a read only slave. 3, master-slave switch
1. Stop the main redis and simulate the fault
[root@redis-master ~] # redis-cli-h 172.16.1.100-p 6379-a pwd@123 shutdown [root@redis-master ~] # ss-anput | grep redis [root@redis-master ~] #
2, set the slave redis to the master redis (turn off replication)
[root@redis-slave] # redis-cli-h 172.16.1.110-p 6379-a pwd@123 slaveof no one Warning: Using a password with'- a 'option on the command line interface may not be safe.OK
3. Test whether to switch from redis to master redis
# View the role of the current CVM:
[root@redis-slave] # redis-cli-h 172.16.1.110-p 6379-a pwd@123 info replicationWarning: Using a password with'- a 'option on the command line interface may not be safe.# Replicationrole:master / / role color is masterconnected_slaves:0master_replid:51ca62c64f31a7adedfb942a95d01c922f42124bmaster_replid2:e5a32a89b7806f0fa7954cb0c422172ea889fff0master_repl_offset:5583second_repl_offset:5584repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:4607repl_backlog_histlen:977
# Test read and write data:
[root@redis-slave] # redis-cli-h 172.16.1.110-p 6379-a pwd@123 Warning: Using a password with'- a 'option on the command line interface may not be safe.172.16.1.110:6379 > keys * 1) "age" 2) "name" 172.16.1.110option on the command line interface may not be safe.172.16.1.110:6379 6379 > get name "abc" 172.16.1.110option on the command line interface may not be safe.172.16.1.110:6379 6379 > set name zhangsanOK172.16.1.110:6379 > get name "zhangsan"
4. The original main redis has returned to normal and needs to be switched back.
1) Save the data of the current master redis
172.16.1.110 keys 6379 > get name * 1) "age" 2) "name" 172.16.1.110 name 6379 > saveOK172.16.1.110:6379 > get name "zhangsan"
2) overwrite the copy of the dump.rdb file under the current primary redis root directory to the root directory of the original primary redis (make sure that the rerunned master gets the latest data in redis):
[root@redis-slave ~] # scp / var/lib/redis/6379/dump.rdb root@172.16.1.100:/var/lib/redis/6379/
3) start the original main redis:
[root@redis-master ~] # / etc/init.d/redis_6379 startStarting Redis server... [root@redis-master ~] # ss-anput | grep redistcp LISTEN 0 128 172.16.1.100 ss 6379 *: * users: (("redis-server", pid=19649,fd=6))
4) switch in the current main redis:
[root@redis-slave] # redis-cli-h 172.16.1.110-p 6379-a pwd@123 slaveof 172.16.1.100 6379Warning: Using a password with'- a 'option on the command line interface may not be safe.OK
# View status:
You can see that the main redis state has now become slave.
# View the status of the main redis:
You can see that the status has become master, and the data is the latest data, but this manual method must be a little inadequate in the production environment, so let's introduce the redis sentinel mechanism.
4Jing Redis Sentinel
In the master-slave replication mode of redis, once the master node is unable to provide services due to failure, it is necessary to manually promote the slave node to the master node and notify the application to update the node address. This fault handling method is unacceptable for many application scenarios. Fortunately, redis has formally provided Redis Sentinel (Sentinel) mechanism to solve this problem since 2.8.To solve this problem.
Overview of Sentinel Mechanism
Redis's sentinel system, which manages multiple redis servers, performs the following three tasks:
1, Monitoring: the Sentinel will constantly check that your master and slave are working properly.
2, Notification: when there is a problem with a monitored redis, the Sentinel can send a notification to the administrator or this other application through API
3. Automatic failover (Automatic failover): when a master does not work properly, the sentry will start the automatic old migration operation in turn. It will upgrade one of the slave of the invalid master to the new master, and let the other slave of the invalid master copy the new master instead. When the client connection fails to the master, the cluster will also return the address of the new master to the client, so that the cluster can use the new master instead of the invalid master.
Sentinel is also a redis service in nature, but it provides different functions from ordinary redis services. Sentinel is a distributed architecture, because if you want to ensure the high availability of redis, you must first ensure your own high availability, so if we need to build Sentinel, we need to deploy at least three instances, preferably an odd number, because voting will be involved in subsequent failover.
Deploy sentinel to monitor and manage the redis master-slave architecture
Our master-slave architecture environment above is one master and one slave. According to the voting mechanism of the Sentinel, we need at least three instances, so add a slave slave node (172.16.1.120) to the original environment.
Omit the step and install and configure it according to the way the slave node is deployed above to ensure that the data on the master can be synchronized.
2. Configure sentinel (three hosts operate the same)
Configure 3 sentinels, each with the same configuration. In the redis installation directory, there is a sentinel.conf file, copy a copy to modify:
[root@redis-master ~] # cp redis-4.0.14/sentinel.conf / etc/redis/ [root@redis-master ~] # ls / etc/redis/6379.conf sentinel.conf [root@redis-master ~] # vim / etc/redis/sentinel.conf is modified as follows: # bind the ip address of the redis host. Note: the other two slave slave nodes need to point to their own local address bind 172.16.1.10 port number. The default is redis instance + 20000. So let's just follow this rule port 26379 # add daemon run daemonize yes# add log location, this is very important, through the log you can view the failover process logfile "26379.log" # working directory (sentinel related information files will be saved here, including log files) The default is maintained here (of course, you can also customize the path) dir / tmp# specifies the redis instance to be monitored by sentinel: monitor a redis server named mymaster (with a customizable name), which is the master ip address. The 2 at the end of # means that at least two sentinels think that the primary server fails before it will fail, otherwise the main service is not disabled, it is generally set to Ncanpl1 (N is the total number of sentinels). Sentinel monitor mymaster 172.16.1.100 6379 "defines the password of the service. Mymaster is the name of the service, followed by the password of the redis server. If your redis does not set a password, you need to turn off the protected mode (protected-mode no) sentinel auth-pass mymaster pwd@123#sentinel to determine the response time of the server failure. If you do not receive a response from the server beyond this time It is considered that the server failed sentinel down-after-milliseconds mymaster 3000 server # after the completion of the failover, the maximum number of slave servers can initiate data replication at the same time (concurrent number). The smaller the # number, the longer the time it takes to complete all slave service data replication, the greater the number, the greater the pressure on the master server, the greater the failover timeout time of sentinel parallel-syncs mymaster 1swap #. If the sentinel fails to complete the failover operation within the configuration value (that is, the master/slave automatically switches over in case of failure), the failover is considered to have failed. Sentinel failover-timeout mymaster 180000
3, start the Sentinel in turn: (two methods)
Method 1: [root@redis-master ~] # redis-sentinel / etc/redis/sentinel.conf method 2: [root@redis-master ~] # redis-server / etc/redis/sentinel.conf-- sentinel
Check to see if the port is working:
The other two slave are launched successively from the node.
Note the startup sequence: if redis and sentinel are started at the same time, start the redis service first, and then start sentinel.
# configure firewall: (you need to open the listening port of Sentinel on each node) [root@redis-slave2 ~] # firewall-cmd-- add-port=26379/tcp-- permanentsuccess [root@redis-slave2 ~] # firewall-cmd-- reloadfisuccess
# because Sentinel is also an redis instance, we use the following command to view the current Sentinel monitoring information:
[root@redis-master ~] # redis-cli-p 26379-h 172.16.1.100172.16.1.100 ok 26379 > info sentinel# Sentinelsentinel_masters:1sentinel_tilt:0sentinel_running_scripts:0sentinel_scripts_queue_length:0sentinel_simulate_failure_flags:0master0:name=mymaster,status=ok,address=172.16.1.100:6379,slaves=2,sentinels=3# you can see that the current state is ok, and the host listening is the current master node, 2 slave nodes and 3 sentinels
4. Simulate the failure of the master redis server, whether it is normally automatically migrated to other slave servers, and the slave server is automatically promoted to the master server
# shut down the redis service or kill the process [root@redis-master ~] # redis-cli-p 6379-h 172.16.1.100-a pwd@123 shutdownWarning: Using a password with'- a 'option on the command line interface may not be safe. [root@redis-master ~] # ps-ef | grep redisroot 19687 10 04:35? 00:00:00 redis-sentinel 172.16.1.100 redis-sentinel 172.16.1.100 redis-sentinel 26379 [sentinel] root 19700 2242 004 39 pts/0 00:00:00 grep-color=auto redis
# View the monitoring information of the Sentinel:
[root@redis-master] # redis-cli-h 172.16.1.100-p 26379 info sentinel# Sentinelsentinel_masters:1sentinel_tilt:0sentinel_running_scripts:0sentinel_scripts_queue_length:0sentinel_simulate_failure_flags:0master0:name=mymaster,status=ok,address=172.16.1.110:6379,slaves=2,sentinels=3# can find that the master node currently being monitored by the Sentinel is no longer the original node, but a slave node (172.16.1.110).
# View the log information of sentinel:
From the log information, we can know that the original master host has been down, and through the sentinel sentinel mechanism, it has automatically switched the master to the 172.16.1.110 slave node.
# verify whether the original slave node is switched successfully:
You can see that you have switched from the original slave state to master, and 172.16.1.20 is your own slave node.
172.16.1.110 keys * 1) "addr" 2) "age" 3) "name" 172.16.1.110 name > set linux redisOK172.16.1.110:6379 > get linux "redis" / / and can read and write normally
5. When the dead master master node returns to normal, will sentine be re-elected as master? Let's check it out:
# restart the redis service: [root@redis-master ~] # / etc/init.d/redis_6379 startStarting Redis server... [root@redis-master ~] # ps-ef | grep redisroot 19687 10 04:35? 00:00:02 redis-sentinel 172.16.1.100 etc/init.d/redis_6379 startStarting Redis server... 26379 [sentinel] root 19713 10 04:57? 00:00:00 / usr/local/bin/redis-server 172.16. 1.100:6379root 19718 2242 0 04:57 pts/0 00:00:00 grep-color=auto redis
# check your status:
As you can see, after the suspended mater is restored, it cannot become a master, but can only be used as a slave slave node of the current master. However, it should be noted that when the dead host is restored, the Sentinel mechanism will not help you restore the data lost during this period of time, so you need to back up the dump.rdb files of other nodes so that you can import the lost data after recovery.
After reading the above, do you have a general understanding of the master-slave replication of redis? If you want to know more about the content of the article, welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.