1、查看Redis主从关系
192.168.210.85:6379> info replication
# Replication
role:master
connected_slaves:0
master_replid:fac1551ca0575f9f12c44f169403530574952c65
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
[root@k8s-node1 bin]# ./redis-cli -h 192.168.210.177 -p 6379
192.168.210.177:6379> info replication
# Replication
role:master
connected_slaves:0
master_replid:cff4e3c204bfbb1841c866383db529abb3862a24
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
节点都是扮演的 Master 角色,把177变成slave角色
192.168.210.177:6379> slaveof 192.168.210.85 6379
OK
192.168.210.177:6379> info replication
# Replication
role:slave
master_host:192.168.210.85
master_port:6379
master_link_status:up
master_last_io_seconds_ago:5
master_sync_in_progress:0
slave_repl_offset:14
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:130d987e5bda59fc2a5727b7709e7ec76e4175bc
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:14
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:14
192.168.210.177:6379>
查看master
2、配置哨兵模式
前面的配置,主节点Master 只有一个,一旦主节点挂掉之后,从节点没法担起主节点的任务,那么整个系统也无法运行。如果主节点挂掉之后,从节点能够自动变成主节点,那么问题就解决了,于是哨兵模式诞生了。
哨兵模式就是不时地监控redis是否按照预期良好地运行(至少是保证主节点是存在的),若一台主机出现问题时,哨兵会自动将该主机下的某一个从机设置为新的主机,并让其他从机和新主机建立主从关系。
[root@k8s-master1 redis]# vim sentinel.conf
[root@k8s-master1 redis]#
sentinel monitor k8s-master 192.168.210.85 6379 1
分别配置被监控的名字,ip地址,端口号,以及得票数。上面的得票数为1表示表示主机挂掉后salve投票看让谁接替成为主机,得票数大于1便成为主机
配置哨兵
[root@k8s-master1 bin]# ls
redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-sentinel redis-server
[root@k8s-master1 bin]# ./redis-sentinel /usr/local/redis/sentinel.conf
*** FATAL CONFIG FILE ERROR (Redis 6.0.10) ***
Reading the configuration file, at line 125
>>> 'sentinel down-after-milliseconds mymaster 30000'
No such master with specified name.
启动报错,需要改成你设置的被监控的服务器名
[root@k8s-master1 redis]# egrep -v "^$|^#" sentinel.conf
port 26379
daemonize no
pidfile "/var/run/redis-sentinel.pid"
logfile ""
dir "/data/redis"
sentinel myid a0654fc76b88b8411204b14245dce76797b1df9d
sentinel deny-scripts-reconfig yes
sentinel monitor k8s 192.168.210.85 6379 1
sentinel config-epoch k8s 0
sentinel leader-epoch k8s 0
protected-mode no
user default on nopass ~* +@all
sentinel known-replica k8s 192.168.210.177 6379
sentinel current-epoch 0
其它配置可以不修改
3、重新启动正常
[root@k8s-master1 bin]# ./redis-sentinel /usr/local/redis/sentinel.conf
先把6379端口关闭,看从节点是否变化
[root@k8s-master1 bin]# ./redis-cli -h 192.168.210.85 -p 6379
192.168.210.85:6379> shutdown
not connected> exit
[root@k8s-master1 bin]#
查看177服务器从的节点信息(切换成master节点)
[root@k8s-node1 bin]# ./redis-cli -h 192.168.210.177 -p 6379
192.168.210.177:6379> info replication
# Replication
role:master
connected_slaves:0
master_replid:470fef30d6fd166a19b54a5da11e58eeb8f90a59
master_replid2:130d987e5bda59fc2a5727b7709e7ec76e4175bc
master_repl_offset:122479
second_repl_offset:93406
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:122479
原来的主节点变成slave了
哨兵模式也存在单点故障问题,如果哨兵机器挂了,那么就无法进行监控了,解决办法是哨兵也建立集群,Redis哨兵模式是支持集群的
查看redis日志
[root@k8s-node1 bin]# ./redis-cli -h 192.168.210.177 -p 6379
192.168.210.177:6379> MONITOR
相关阅读:
5、欢迎加入技术交流