I. deployment environment
System: centos7
The deployment of the master-slave cluster is completed by starting two different redis instances on the Linux system
yum source deployed
II. Download and install redis
1. Download: download on official website
2, installation
Create the / app / directory and install redis in the / app / directory
[root@liyg ~]# mkdir /app [root@liyg ~]# cd /usr/local/src/ [root@liyg src]# ls redis-4.0.11.tar.gz [root@liyg src]# tar zxf redis-4.0.11.tar.gz -C /app/ [root@liyg~]# yum install -y gcc* [root@liyg ~]# cd /app/redis-4.0.11/ [root@liyg redis-4.0.11]# make [root@liyg redis-4.0.11]# make install 3,Start different instances by configuring different ports [root@liyg ~]# mkdir /app/redis_replication [root@liyg ~]# mkdir /app/redis_replication/7001_master [root@liyg ~]# mkdir /app/redis_replication/7002_slave [root@liyg ~]# cp /app/redis-4.0.11/redis.conf /app/redis_replication/7001_master/ [root@liyg ~]# cp /app/redis-4.0.11/redis.conf /app/redis_replication/7002_slave/
Edit master profile
[root@liyg ~]# cd /app/redis_replication/ [root@liyg redis_replication]# vim 7001_master/redis.conf #Change port 6379 to 7001 dir /app/redis_replication/7001_master pidfile /var/run/redis_7001.pid logfile "/app/redis_replication/7001_master/7001.log" protected-mode no ##yes to no to turn off security mode daemonize yes ##Start as a Daemons requirepass qwer1234 ##Turn on password authentication masterauth qwer1234 ##When the sentinel mode is turned on, it needs to be added to avoid that the slave cannot be synchronized when upgrading to master
Edit the slave profile
[root@liyg redis_replication]# vim 7001_master/redis.conf #Port changed to 7002 dir /app/redis_replication/7002_slave pidfile /var/run/redis_7002.pid logfile "/app/redis_replication/7002_slave/7002.log" protected-mode no masterauth qwer1234 ##Password required to synchronize master requirepass qwer1234 ##Turn on password authentication slaveof 127.0.0.1 7001 ##Newly added
Start redis
[root@liyg redis_replication]# redis-server 7001_master/redis.conf 2521:C 03 Dec 15:50:54.660 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 2521:C 03 Dec 15:50:54.661 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=2521, just started 2521:C 03 Dec 15:50:54.661 # Configuration loaded [root@liyg redis_replication]# redis-server 7002_slave/redis.conf 2530:C 03 Dec 15:51:07.037 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 2530:C 03 Dec 15:51:07.037 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=2530, just started 2530:C 03 Dec 15:51:07.037 # Configuration loaded
4, validation
[root@liyg redis_replication]# redis-cli -p 7001 -a qwer1234 info replication role:master connected_slaves:1 slave0:ip=127.0.0.1,port=7002,state=online,offset=70,lag=1 [root@liyg redis_replication]# redis-cli -p 7002 -a qwer1234 info replication master_host:127.0.0.1 master_port:7001 ...
Create a KEY in the master to check whether the slave is synchronized
[root@liyg ~]# redis-cli -p 7001 -a qwer1234 set k1 t1 OK [root@liyg ~]# redis-cli -p 7002 -a qwer1234 get k1 "t1"
At this point, the redis master-slave configuration is complete
III. configure the redis master-slave sentinel mode
[root@liyg app]# cp redis-4.0.11/sentinel.conf redis_replication/ [root@liyg app]# vim redis_replication/sentinel.conf protected-mode no ##Turn off safe mode sentinel monitor mymaster 127.0.0.1 7002 1 sentinel auth-pass mymaster qwer1234 ##Enable when the redis master-slave cluster has password verification
start-up
[root@liyg app]# redis-sentinel redis_replication/sentinel.conf
Verification
Close the master 7001 port and check whether the 7002 port is upgraded to master through sentry
[root@liyg redis_replication]# redis-cli -p 7001 -a qwer1234 shutdown
When the master is off, you can see the output of the sentry
... 2138:X 04 Dec 09:59:15.625 # +failover-end master mymaster 127.0.0.1 7001 2138:X 04 Dec 09:59:15.625 # +switch-master mymaster 127.0.0.1 7001 127.0.0.1 7002 2138:X 04 Dec 09:59:15.625 * +slave slave 127.0.0.1:7001 127.0.0.1 7001 @ mymaster 127.0.0.1 7002 2138:X 04 Dec 09:59:18.645 # +sdown slave 127.0.0.1:7001 127.0.0.1 7001 @ mymaster 127.0.0.1 7002 [root@liyg redis_replication]# redis-cli -p 7002 -a qwer1234 info replication role:master connected_slaves:0
You can see that the original slave7002 has been upgraded to master and the 7001 instance has been started again
[root@liyg redis_replication]# redis-server 7001_master/redis.conf
[root@liyg redis_replication]# redis-cli -p 7001 -a qwer1234 info replication
role:slave master_host:127.0.0.1 master_port:7002
7001 has become a slave of 7002, so far, the verification is successful