一、部署環(huán)境
系統(tǒng):centos7
通過在Linux系統(tǒng)上啟動兩個不同的redis實例來完成主從集群的部署
yum源已部署
創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站設(shè)計、做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的溆浦網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
二、redis的下載與安裝
1、下載:官網(wǎng)下載
2、安裝
創(chuàng)建/app/目錄,redis安裝在/app/目錄下
[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、通過配置不同端口,來啟動不同實例
[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/
編輯master配置文件
[root@liyg ~]# cd /app/redis_replication/
[root@liyg redis_replication]# vim 7001_master/redis.conf
#修改端口6379為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改為no 關(guān)閉安全模式
daemonize yes ##以守護(hù)進(jìn)程方式啟動
requirepass qwer1234 ##開啟密碼認(rèn)證
masterauth qwer1234 ##當(dāng)開啟哨兵模式時需要添加,以免slave升級為master時不能同步
編輯slave配置文件
[root@liyg redis_replication]# vim 7001_master/redis.conf
#端口改為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 ##同步master時所需密碼
requirepass qwer1234 ##開啟密碼認(rèn)證
slaveof 127.0.0.1 7001 ##新增
啟動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、驗證
[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
...
在master創(chuàng)建KEY查看slave是否同步
[root@liyg ~]# redis-cli -p 7001 -a qwer1234 set k1 t1
OK
[root@liyg ~]# redis-cli -p 7002 -a qwer1234 get k1
"t1"
至此,redis主從配置完成
三、配置redis主從哨兵模式
[root@liyg app]# cp redis-4.0.11/sentinel.conf redis_replication/
[root@liyg app]# vim redis_replication/sentinel.conf
protected-mode no ##關(guān)閉安全模式
sentinel monitor mymaster 127.0.0.1 7002 1
sentinel auth-pass mymaster qwer1234 ##當(dāng)redis主從集群有密碼驗證時開啟
啟動
[root@liyg app]# redis-sentinel redis_replication/sentinel.conf
驗證
關(guān)閉master7001端口,查看7002端口是否通過哨兵升級為master
[root@liyg redis_replication]# redis-cli -p 7001 -a qwer1234 shutdown
當(dāng)master關(guān)閉時,可看到哨兵的輸出內(nèi)容
...
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
可以看到原來的slave7002已升級為master,再次啟動7001實例
[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已變?yōu)?002的從庫,至此,驗證成功