Redis 边看边学主从哨兵配置

 

前言

边看边学redis

示例以个人云服务器为例

Redis端口:16379,16380,16381

Sentinel端口:26379,26380,26381


 

一、redis主从配置

安装redis

在/usr/local下创建redis文件夹,下载并解压

[root@VM_0_3_centos redis]# wget http://download.redis.io/releases/redis-4.0.9.tar.gz
[root@VM_0_3_centos redis]# tar xf redis-4.0.9.tar.gz

复制两份,为图方便,命名为redis-4.0.9,redis-4.0.9.2,redis-4.0.9.3 

[root@VM_0_3_centos redis]# ll
total 1716
drwxrwxr-x 6 root root    4096 Jan 13 15:15 redis-4.0.9
drwxr-xr-x 6 root root    4096 Jan 13 15:15 redis-4.0.9.2
drwxr-xr-x 6 root root    4096 Jan 13 15:16 redis-4.0.9.3
-rw-r--r-- 1 root root 1737022 Dec 30 10:44 redis-4.0.9.tar.gz

分别进入redis-4.0.9,redis-4.0.9.2,redis-4.0.9.3目录执行make && make install

[root@VM_0_3_centos redis-4.0.9]# make && make install

在/var下面创建redis,redis2,redis3文件夹,分别创建data,log,run三个目录

[root@VM_0_3_centos redis-4.0.9]# mkdir -p /var/redis/{data,log,run}
[root@VM_0_3_centos redis-4.0.9]# mkdir -p /var/redis2/{data,log,run}
[root@VM_0_3_centos redis-4.0.9]# mkdir -p /var/redis3/{data,log,run}

将/usr/local/redis/redis-4.0.9/redis.conf 复制到/etc目录下(这里可不用分别从redis-4.0.9,redis-4.0.9.2,redis-4.0.9.3下复制)

[root@VM_0_3_centos redis-4.0.9]# cp redis.conf /etc redis.conf

配置文件修改,修改以下参数(若未设置密码,则都不用设置),之后分别复制redis.conf为redis2.conf,redis3.conf并修改端口号,pidfile,logfile,dir路径

[root@VM_0_3_centos etc]# vi redis.conf 
port 16379                                    #不要使用默认端口号
pidfile    /var/redis/run/redis-16379.pid     #pid文件
logfile    /var/redis/log/redis.log           #日志文件
dir        /var/redis/data                    #redis文件保存路径
#主节点不需要设置slaveof

[root@VM_0_3_centos etc]# vi redis2.conf 
port 16380            #不能与master节点端口冲突
pidfile    /var/redis/run/redis-16379.pid     #pid文件
logfile    /var/redis/log/redis.log           #日志文件
dir        /var/redis/data                    #redis文件保存路径
slaveof    127.0.0.1:16379                    #表明redis2是redis的从

[root@VM_0_3_centos etc]# vi redis3.conf 
port 16381            #不能与master节点端口冲突
pidfile    /var/redis/run/redis-16379.pid     #pid文件
logfile    /var/redis/log/redis.log           #日志文件
dir        /var/redis/data                    #redis文件保存路径
slaveof    127.0.0.1:16379                    #表明redis3是redis的从

redis添加到环境变量并生效

[root@VM_0_3_centos redis-4.0.9]# echo "export PATH=$PATH:/usr/local/redis/redis-4.0.9/src" >> /etc/profile
[root@VM_0_3_centos redis-4.0.9]# echo "export PATH=$PATH:/usr/local/redis/redis-4.0.9.2/src" >> /etc/profile
[root@VM_0_3_centos redis-4.0.9]# echo "export PATH=$PATH:/usr/local/redis/redis-4.0.9.3/src" >> /etc/profile
[root@VM_0_3_centos redis-4.0.9]# source /etc/profile

启动redis

[root@VM_0_3_centos etc]# redis-server redis.conf
[root@VM_0_3_centos etc]# redis-server redis2.conf
[root@VM_0_3_centos etc]# redis-server redis3.conf

执行redis-cli -p 16379登录redis命令行工具,输入info replication

role:master 主节点;role:slaver 从节点

[root@VM_0_3_centos etc]# redis-cli -p 16379
127.0.0.1:16379> info replication
# Replication
role:master
connected_slaves:0
master_replid:dc159ef496c5ff4f8525465c8f66f00dfd85348b
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
127.0.0.1:16379> 

二、redis-sentinel

分别进入/usr/local/redis/redis-4.0.9,/usr/local/redis/redis-4.0.9.2,/usr/local/redis/redis-4.0.9.3;编辑sentinel.conf三个配置文件配置一样,区分端口号即可

sentinel monitor 主机名(自定义)ip(主节点地址) host(主节点端口号) quorum(master宕机需要多少哨兵主观下线进行failover操作)

[root@VM_0_3_centos redis-4.0.9]# vi sentinel.conf
port 26379                                    #修改端口号
logfile "/var/redis/log/redis-sentinel.log"   #哨兵日志文件 
sentinel monitor mymaster 127.0.0.1 16379 2   #监控主节点

[root@VM_0_3_centos redis-4.0.9.2]# vi sentinel.conf
port 26380                                    #修改端口号
logfile "/var/redis/log/redis-sentinel.log"   #哨兵日志文件 
sentinel monitor mymaster 127.0.0.1 16379 2   #监控主节点

[root@VM_0_3_centos redis-4.0.9.3]# vi sentinel.conf
port 26381                                    #修改端口号
logfile "/var/redis/log/redis-sentinel.log"   #哨兵日志文件 
sentinel monitor mymaster 127.0.0.1 16379 2   #监控主节点

启动sentinel。(执行redis-sentinel /usr/local/redis/redis-4.0.9/sentinel.conf无效,遂更改启动命令)

[root@VM_0_3_centos redis-4.0.9]# nohup /usr/local/redis/redis-4.0.9/src/redis-sentinel /usr/local/redis/redis-4.0.9/sentinel.conf > output 2>&1 &
[root@VM_0_3_centos redis-4.0.9]# nohup /usr/local/redis/redis-4.0.9.2/src/redis-sentinel /usr/local/redis/redis-4.0.9/sentinel.conf > output 2>&1 &
[root@VM_0_3_centos redis-4.0.9]# nohup /usr/local/redis/redis-4.0.9.3/src/redis-sentinel /usr/local/redis/redis-4.0.9/sentinel.conf > output 2>&1 &

 进入哨兵,输入info sentinel查看,最后一行即当前哨兵监控主节点状态,name为redis.conf 中slaveof设置的主机名,status监控状态,address监控地址,slaves主节点下的从节点数量,sentinels当前启动哨兵数量

[root@VM_0_3_centos redis-4.0.9]# redis-cli -p 26379
127.0.0.1:26379> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=127.0.0.1:16379,slaves=2,sentinels=3
127.0.0.1:26379> 

进入主节点 执行shutdown,之后sentinel会重新选举新的master

[root@VM_0_3_centos redis-4.0.9]# redis-cli -p 16379
127.0.0.1:16379> shutdown
not connected> exit
[root@VM_0_3_centos redis-4.0.9]# redis-cli -p 26379
127.0.0.1:26379> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=127.0.0.1:16380,slaves=2,sentinels=3
127.0.0.1:26379> 

参考资料:

https://blog.csdn.net/qw311113qin/article/details/97486798  单台服务器实现redis-sentinel集群

https://www.jianshu.com/p/06ab9daf921d   Redis哨兵(Sentinel)模式

https://blog.csdn.net/miss1181248983/article/details/82011884  NoSQL Redis

https://www.cnblogs.com/kevingrace/p/9004460.html      Redis哨兵模式(sentinel)学习总结及部署记录(主从复制、读写分离、主从切换)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值