redis 哨兵服务

梦想不抛弃苦心追求的人,只要不停止追求,你们会沐浴在梦想的光辉之中。再美好的梦想与目标,再完美的计划和方案,如果不能尽快在行动中落实,最终只能是纸上谈兵,空想一番。只要瞄准了大方向,坚持不懈地做下去,才能够扫除挡在梦想前面的障碍,实现美好的人生蓝图。redis 哨兵服务,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

哨兵服务

监视主从复制结构中主服务器,发现主服务器无法连接后,会把对应的从服务器升级为主服务器,并继续监视新的主数据库服务器。坏掉的主服务器恢复后,会自动做当前主服务器的从服务器。

redis主从+哨兵服务

实现redis服务的自动备份和高可用

配置要求

如果主从结构中的redis服务设置有连接密码,必须全部数据库服务器都要设置相同的密码。
如果主从结构中的redis服务设置有连接密码,宕机的服务器恢复后,若配置文件没有设置连接主服务器的密码,要人为指定主服务器的连接密码

配置环境

节点 IP地址 角色
host145 192.168.44.145 master
host146 192.168.44.146 slave(master)
host147 192.168.44.147 slave01
host148 192.168.44.148 哨兵服务

redis主从从配置

配置host145为master

安装部署redis

yum -y install make gcc
tar xf redis-4.0.8.tar.gz
cd redis-4.0.8
make && make install
./utils/install_server.sh
[root@host145 redis-4.0.8]# ss -utnlp | grep redis
tcp    LISTEN     0      128    127.0.0.1:6379                  *:*                   users:(("redis-server",pid=1332,fd=6))

配置redis物理网卡,密码

[root@host145 redis-4.0.8]# vim /etc/redis/6379.conf
[root@host145 ~]# sed -n '70p;1309p;1310p' /etc/redis/6379.conf
bind 127.0.0.1 192.168.44.145
requirepass "123456"	#自身密码
masterauth "123456"	#连接主服务器的密码

配置host146为slave(master)

安装部署redis

yum -y install make gcc
tar xf redis-4.0.8.tar.gz
cd redis-4.0.8
make && make install
./utils/install_server.sh
[root@host146 redis-4.0.8]# ss -utnlp | grep redis
tcp    LISTEN     0      128    127.0.0.1:6379                  *:*                   users:(("redis-server",pid=1309,fd=6))

配置redis物理网卡,角色,密码

[root@host146 redis-4.0.8]# vim /etc/redis/6379.conf
[root@host146 redis-4.0.8]# sed -n '70p;1309p;1310p;1311p' /etc/redis/6379.conf
bind 127.0.0.1 192.168.44.146
slaveof 192.168.44.145 6379
requirepass "123456"
masterauth "123456"

配置host147为slave01

安装部署redis

yum -y install make gcc
tar xf redis-4.0.8.tar.gz
cd redis-4.0.8
make && make install
./utils/install_server.sh
[root@host147 redis-4.0.8]# ss -utnlp | grep redis
tcp    LISTEN     0      128    127.0.0.1:6379                  *:*                   users:(("redis-server",pid=1305,fd=6))

配置redis物理网卡,角色,密码

[root@host147 redis-4.0.8]# vim /etc/redis/6379.conf
[root@host147 redis-4.0.8]# sed -n '70p;1309p;1310p;1311p' /etc/redis/6379.conf
bind 127.0.0.1 192.168.44.147
slaveof 192.168.44.146 6379
requirepass "123456"
masterauth "123456"

重启服务

host145

[root@host145 redis-4.0.8]# /etc/init.d/redis_6379 stop
Stopping ...
Redis stopped
[root@host145 redis-4.0.8]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@host145 redis-4.0.8]# redis-cli -h 192.168.44.145 -p 6379
192.168.44.145:6379> ping
(error) NOAUTH Authentication required.
192.168.44.145:6379> auth 123456
OK
192.168.44.145:6379> ping
PONG
192.168.44.145:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.44.146,port=6379,state=online,offset=98,lag=0
master_replid:92fcbaf1c34d35cd4564a5fad3a5a1d84cf6c724
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:98
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:98
192.168.44.145:6379> exit

host146

[root@host146 redis-4.0.8]# /etc/init.d/redis_6379 stop
Stopping ...
Redis stopped
[root@host146 redis-4.0.8]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@host146 redis-4.0.8]# redis-cli -h 192.168.44.146 -p 6379
192.168.44.146:6379> ping
(error) NOAUTH Authentication required.
192.168.44.146:6379> auth 123456
OK
192.168.44.146:6379> ping
PONG
192.168.44.146:6379> info replication
# Replication
role:slave
master_host:192.168.44.145
master_port:6379
master_link_status:up
master_last_io_seconds_ago:6
master_sync_in_progress:0
slave_repl_offset:168
slave_priority:100
slave_read_only:1
connected_slaves:1
slave0:ip=192.168.44.147,port=6379,state=online,offset=168,lag=0
master_replid:92fcbaf1c34d35cd4564a5fad3a5a1d84cf6c724
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:168
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:168
192.168.44.146:6379> exit

host147

[root@host147 redis-4.0.8]# /etc/init.d/redis_6379 stop
Stopping ...
Redis stopped
[root@host147 redis-4.0.8]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@host147 redis-4.0.8]# redis-cli -h 192.168.44.147 -p 6379
192.168.44.147:6379> ping
(error) NOAUTH Authentication required.
192.168.44.147:6379> auth 123456
OK
192.168.44.147:6379> ping
PONG
192.168.44.147:6379> info replication
# Replication
role:slave
master_host:192.168.44.146
master_port:6379
master_link_status:up
master_last_io_seconds_ago:3
master_sync_in_progress:0
slave_repl_offset:238
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:92fcbaf1c34d35cd4564a5fad3a5a1d84cf6c724
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:238
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:29
repl_backlog_histlen:210
192.168.44.147:6379> exit

配置哨兵服务

安装源码软件redis ,无需做初始化配置

yum -y install make gcc
tar xf redis-4.0.8.tar.gz
cd redis-4.0.8
make && make install

创建并编辑主配置文件,sentinel.conf

[root@host148 redis-4.0.8]# vim sentinel.conf
[root@host148 redis-4.0.8]# sed -n '15p;16p;53p;54p;73p;74p' sentinel.conf
# bind 127.0.0.1 192.168.1.1
bind 192.168.44.148
# sentinel monitor <master-name> <ip> <redis-port> <quorum>
sentinel monitor redis_server 192.168.44.145 6379  1
# sentinel auth-pass <master-name> <password>
sentinel auth-pass redis_server  123456

启动哨兵服务

[root@host148 redis-4.0.8]# cp sentinel.conf /etc/sentinel.conf
[root@host148 redis-4.0.8]# ls /etc/sentinel.conf
/etc/sentinel.conf
[root@host148 redis-4.0.8]# nohup redis-sentinel /etc/sentinel.conf &
[1] 11064
[root@host148 redis-4.0.8]# nohup: 忽略输入并把输出追加到"nohup.out"	#回车
[root@host148 redis-4.0.8]# jobs
[1]+  运行中               nohup redis-sentinel /etc/sentinel.conf &

验证哨兵服务

验证master宕机自动切换

停止当前master角色host145的redis服务

[root@host145 ~]# redis-cli -h 192.168.44.145 -p 6379 -a 123456 shutdown
[root@host145 ~]# ss -utnlp | grep redis

查看slave(master)角色host146的状态

角色变为master,只有host147一个从服务器

[root@host146 redis-4.0.8]# redis-cli -h 192.168.44.146 -p 6379 -a 123456
192.168.44.146:6379> ping
PONG
192.168.44.146:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.44.147,port=6379,state=online,offset=35739,lag=0
master_replid:54cc8566a6f9424015f167601ae4dc60a4c8ec62
master_replid2:92fcbaf1c34d35cd4564a5fad3a5a1d84cf6c724
master_repl_offset:35886
second_repl_offset:34219
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:35886
192.168.44.146:6379>

查看/etc/sentinel.conf

监控的主服务器已自动变成host146

[root@host148 redis-4.0.8]# sed -n '109p;201,205p' /etc/sentinel.conf
sentinel monitor redis_server 192.168.44.146 6379 1
sentinel config-epoch redis_server 1
sentinel leader-epoch redis_server 1
sentinel known-slave redis_server 192.168.44.145 6379
sentinel known-slave redis_server 192.168.44.147 6379
sentinel current-epoch 1

验证宕机服务器恢复自动添加

启动host145的redis服务

[root@host145 ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@host145 ~]# ss -utnlp | grep redis
tcp    LISTEN     0      128    192.168.44.145:6379                  *:*                   users:(("redis-server",pid=11264,fd=7))
tcp    LISTEN     0      128    127.0.0.1:6379                  *:*                   users:(("redis-server",pid=11264,fd=6))

查看host145状态

[root@host145 ~]# redis-cli -h 192.168.44.145 -p 6379 -a 123456
192.168.44.145:6379> ping
PONG
192.168.44.145:6379> info replication
# Replication
role:slave
master_host:192.168.44.146
master_port:6379
master_link_status:up	
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:117928
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:54cc8566a6f9424015f167601ae4dc60a4c8ec62
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:117928
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:117304
repl_backlog_histlen:625
192.168.44.145:6379>

查看host146状态

有两个从服务器,host145已自动添加

192.168.44.146:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.44.147,port=6379,state=online,offset=125271,lag=0
slave1:ip=192.168.44.145,port=6379,state=online,offset=125271,lag=0
master_replid:54cc8566a6f9424015f167601ae4dc60a4c8ec62
master_replid2:92fcbaf1c34d35cd4564a5fad3a5a1d84cf6c724
master_repl_offset:125271
second_repl_offset:34219
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:125271

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/154058.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!