一、部署环境
系统:centos7
通过在Linux系统上启动两个不同的redis实例来完成主从集群的部署
yum源已部署
二、redis的下载与安装
1、下载:官网下载
2、安装
创建/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 关闭安全模式
daemonize yes ##以守护进程方式启动
requirepass qwer1234 ##开启密码认证
masterauth qwer1234 ##当开启哨兵模式时需要添加,以免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 ##开启密码认证
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创建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 ##关闭安全模式
sentinel monitor mymaster 127.0.0.1 7002 1
sentinel auth-pass mymaster qwer1234 ##当redis主从集群有密码验证时开启
启动
[root@liyg app]# redis-sentinel redis_replication/sentinel.conf
验证
关闭master7001端口,查看7002端口是否通过哨兵升级为master
[root@liyg redis_replication]# redis-cli -p 7001 -a qwer1234 shutdown
当master关闭时,可看到哨兵的输出内容
...
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已变为7002的从库,至此,验证成功
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。