企业级Redis开发运维从入门到实践 (24)— Redis Sentinel(哨兵)的安装配置

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zx711166/article/details/83107640

Redis Sentinel(哨兵)的安装配置

  1. 配置开启主从节点。
  2. 配置开启 sentinel 监控节点。(sentinel 是特殊的 redis,默认端口26379
  3. 实际应该多台机器。
  4. 详细配置节点。
    在这里插入图片描述
Redis主节点
  • 启动:redis-server redis-7000.conf
  • 配置:
port 7000
daemonize yes
pidfile /var/run/redis-7000.pid
logfile "7000.log"
dir "/opt/soft/redis/data/"

## ----- RDB -----
...
## ----- AOF -----
...
Redis从节点
  • 启动:redis-server redis-7001.conf、redis-server redis-7002.conf
  • 配置:
# slave - 1
port 7001
daemonize yes
pidfile /var/run/redis-7001.pid
logfile "7001.log"
dir "/opt/soft/redis/data/"
slaveof 127.0.0.1:7000

## ----- RDB -----
...
## ----- AOF -----
...
# slave - 2
port 7002
daemonize yes
pidfile /var/run/redis-7002.pid
logfile "7002.log"
dir "/opt/soft/redis/data/"
slaveof 127.0.0.1:7000

## ----- RDB -----
...
## ----- AOF -----
...

RDB 和 AOF 的配置查看:企业级Redis开发运维从入门到实践 (16)— RDB企业级Redis开发运维从入门到实践 (17)— AOF

sentinel 主要配置
port ${port}
dir "/opt/soft/redis/data/"
logfile "${port}/log"

# sentinel监控的master的名字叫做mymaster,地址为127.0.0.1:7000;
# 当集群中有2个sentinel认为master死了时,才能真正认为该master已经不可用。
sentinel monitor mymaster 127.0.0.1 7000 2

# sentinel会向master发送心跳PING来确认master是否存活;默认配置是 30000 毫秒(即30秒)
sentinel down-after-milliseconds mymaster 30000

# 在发生failover主备切换时,并发或串行处理设置
sentinel parallel-syncs mymaster 1

# 故障转移时间
sentinel failover-timeout mymaster 180000

Sentinel节点配置时要注意的地方

  • Sentinel 节点配置尽可能一致,这样在判断节点故障时会更加准确。
  • Sentinel 节点支持的命令有限,例如config命令是不支持的,而 Sentinel节点也需要dir、loglevel之类的配置,所以尽量在一开始规划好,不过所幸Sentinel节点不存储数据,如果需要修改配置,重新启动即可。
  • Sentinel节点只支持如下命令:ping、sentinel、subscribe、unsubscribe、 psubscribe、punsubscribe、publish、info、role、client、shutdown。
主、从节点的安装

主节点配置:

[root@localhost config]# vim  redis-7000.conf
port 7000
daemonize yes
pidfile /var/run/redis-7000.pid
logfile "7000.log"
dir "/opt/soft/redis/redis/data/"

从节点配置:

  1. 复制并修改7001、7002的配置文件
[root@localhost config]# sed "s/7000/7001/g" redis-7000.conf > redis-7001.conf
[root@localhost config]# sed "s/7000/7002/g" redis-7000.conf > redis-7002.conf
[root@localhost config]# ll
total 24
-rw-r--r--	1    carlosful    staff      109B  10 21 15:18   redis-7000.conf
-rw-r--r--	1    carlosful    staff      109B  10 21 15:19   redis-7001.conf
-rw-r--r--	1    carlosful    staff      109B  10 21 15:19   redis-7002.conf
  1. 给7001、7002的配置文件追加复制的主节点信息并查看追加修改情况
[root@localhost config]# echo "slaveof 127.0.0.1 7000" >> redis-7001.conf
[root@localhost config]# echo "slaveof 127.0.0.1 7000" >> redis-7002.conf
[root@localhost config]# ll
total 24
-rw-r--r--	1    carlosful    staff      109B  10 21 15:18   redis-7000.conf
-rw-r--r--	1    carlosful    staff      132B  10 21 15:19   redis-7001.conf
-rw-r--r--	1    carlosful    staff      132B  10 21 15:19   redis-7002.conf
[root@localhost config]# cat redis-7000.conf
port 7000
daemonize yes
pidfile /var/run/redis-7000.pid
logfile "7000.log"
dir "/opt/soft/redis/redis/data/"
[root@localhost config]# cat redis-7001.conf
port 7001
daemonize yes
pidfile /var/run/redis-7001.pid
logfile "7001.log"
dir "/opt/soft/redis/redis/data/"
slaveof 127.0.0.1 7000
[root@localhost config]# cat redis-7002.conf
port 7002
daemonize yes
pidfile /var/run/redis-7002.pid
logfile "7002.log"
dir "/opt/soft/redis/redis/data/"
slaveof 127.0.0.1 7000
  1. 启动7000、7001、7002三个节点并查看进程和节点信息
[root@localhost config]# redis-server redis-7000.conf
[root@localhost config]# redis-cli -p 7000 ping
PONG
[root@localhost config]# redis-server redis-7001.conf
[root@localhost config]# redis-server redis-7002.conf
[root@localhost config]# ps -ef | grep redis-server | grep 700
501	93358		1	0	3:20下午	??	     0:00.03	redis-server	*:7000
501	93371		1	0	3:20下午	??	     0:00.01	redis-server	*:7001
501	93378		1	0	3:20下午	??	     0:00.01	redis-server	*:7002
[root@localhost config]# redis-cli -p 7000 info replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port:7001,state=online,offser=29,lag=1
slave1:ip=127.0.0.1,port:7002,state=online,offser=29,lag=1
master_repl_offset:29
repl_backing_active:1
repl_backing_size:1048576
repl_backing_first_byte_offset:2
repl_backing_histlen:28
sentinel 安装配置
  1. 复制sentinel.conf配置文件、进行修改
[root@localhost redis]# cp sentinel.conf config/
[root@localhost config]# cd config
[root@localhost config]# cat sentinel.conf | grep -v "#" | grep -v "^$"
port 26379
dir /tmp
sentinel monitor mymaster 127.0.0.1:6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
[root@localhost config]# cat sentinel.conf | grep -v "#" | grep -v "^$" > redis-sentinel-26379.conf
[root@localhost config]# ll
total 48
-rw-r--r--	1    carlosful    staff        0B  10 21 15:20   7000.log
-rw-r--r--	1    carlosful    staff        0B  10 21 15:20   7001.log
-rw-r--r--	1    carlosful    staff        0B  10 21 15:20   7002.log
-rw-r--r--	1    carlosful    staff      109B  10 21 15:18   redis-7000.conf
-rw-r--r--	1    carlosful    staff      109B  10 21 15:19   redis-7001.conf
-rw-r--r--	1    carlosful    staff      109B  10 21 15:19   redis-7002.conf
-rw-r--r--	1    carlosful    staff      188B  10 21 15:44   redis-sentinel-26379.conf
-rw-r--r--	1    carlosful    staff      6.9K  10 21 15:23   sentinel.conf
[root@localhost config]# vim  redis-sentinel-26379.conf
port 26379
daemonize yes
dir /opt/soft/redis/redis/data/
logfile "26379.log"
sentinel monitor mymaster 127.0.0.1:7000 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
[root@localhost config]# ll
total 48
-rw-r--r--	1    carlosful    staff        0B  10 21 15:20   7000.log
-rw-r--r--	1    carlosful    staff        0B  10 21 15:20   7001.log
-rw-r--r--	1    carlosful    staff        0B  10 21 15:20   7002.log
-rw-r--r--	1    carlosful    staff      109B  10 21 15:18   redis-7000.conf
-rw-r--r--	1    carlosful    staff      109B  10 21 15:19   redis-7001.conf
-rw-r--r--	1    carlosful    staff      109B  10 21 15:19   redis-7002.conf
-rw-r--r--	1    carlosful    staff      224B  10 21 15:44   redis-sentinel-26379.conf
-rw-r--r--	1    carlosful    staff      6.9K  10 21 15:23   sentinel.conf
  1. 启动sentinel的26379节点并查看节点信息及配置文件(发现:sentinel不能做节点的操作,例如set;sentinel启动后会自动追加配置,隐藏默认配置)
[root@localhost config]# redis-sentinel redis-sentinel-26379.conf
[root@localhost config]# ps -ef | grep redis-sentinel
501	93821		1	0	3:47下午	??	     0:00.01	redis-sentinel	*:26379 [sentinel]
501	93828	92491	0	3:47下午	ttys000	 0:00.00	grep --color=auto  --exclude-dir-.bar --exclude-dir-CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn redis-sentinel

# sentinel不能做节点的操作,例如set
[root@localhost config]# redis-cli -p 26379
127.0.0.1:26379> set hello world
(error) ERR unknown command 'set'
127.0.0.1:26379> ping
PONG
127.0.0.1:26379> info
...
# Sentinel
sentinel_master:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
master0:name=mymaster,status=ok,address=127.0.0.1:7000,slaves=2,sentinels=1
127.0.0.1:26379>exit

# sentinel启动后会自动追加配置,隐藏默认配置
[root@localhost config]# cat redis-sentinel-26379.conf
port 26379
daemonize yes
dir "/opt/soft/redis/redis-3.0.7/data"
logfile "26379.log"
sentinel monitor mymaster 127.0.0.1 7000 2
sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 0
sentinel known-slave mymaster 127.0.0.1 7002
# Generated by CONFIG REWRITE
sentinel known-slave mymaster 127.0.0.1 7001
sentinel curent-epoch 0
  1. 复制并修改26380、26381的配置文件
[root@localhost config]# sed "s/26379/26380/g" redis-sentinel-26379.conf > redis-sentinel-26380.conf
[root@localhost config]# sed "s/26379/26381/g" redis-sentinel-26379.conf > redis-sentinel-26381.conf
[root@localhost config]# ll
total 48
-rw-r--r--	1    carlosful    staff        0B  10 21 15:20   7000.log
-rw-r--r--	1    carlosful    staff        0B  10 21 15:20   7001.log
-rw-r--r--	1    carlosful    staff        0B  10 21 15:20   7002.log
-rw-r--r--	1    carlosful    staff      109B  10 21 15:18   redis-7000.conf
-rw-r--r--	1    carlosful    staff      109B  10 21 15:19   redis-7001.conf
-rw-r--r--	1    carlosful    staff      109B  10 21 15:19   redis-7002.conf
-rw-r--r--	1    carlosful    staff      338B  10 21 15:44   redis-sentinel-26379.conf
-rw-r--r--	1    carlosful    staff      338B  10 21 15:44   redis-sentinel-26380.conf
-rw-r--r--	1    carlosful    staff      338B  10 21 15:44   redis-sentinel-26381.conf
-rw-r--r--	1    carlosful    staff      6.9K  10 21 15:23   sentinel.conf
  1. 启动26380、26381的sentinel、查看sentinel进程及节点信息
[root@localhost config]# redis-sentinel redis-sentinel-26380.conf
[root@localhost config]# redis-sentinel redis-sentinel-26381.conf 
[root@localhost config]# ps -ef | grep redis-sentinel
501	93821		1	0	3:47下午	??	     0:00.46	redis-sentinel	*:26379 [sentinel]
501	93887		1	0	3:50下午	??	     0:00.20	redis-sentinel	*:26380 [sentinel]
501	93896		1	0	3:50下午	??	     0:00.20	redis-sentinel	*:26381 [sentinel]
501	93828	92491	0	3:53下午	ttys000	 0:00.00	grep --color=auto  --exclude-dir-.bar --exclude-dir-CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn redis-sentinel

# 连接26380,查看信息,sentinel变为3个
[root@localhost config]# redis-cli -p 26380
127.0.0.1:26380> info
...
# Sentinel
sentinel_master:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
master0:name=mymaster,status=ok,address=127.0.0.1:7000,slaves=2,sentinels=3
127.0.0.1:26380>exit

# 连接26381,查看信息,sentinel变为3个
[root@localhost config]# redis-cli -p 26381
127.0.0.1:26381> info
...
# Sentinel
sentinel_master:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
master0:name=mymaster,status=ok,address=127.0.0.1:7000,slaves=2,sentinels=3
127.0.0.1:26391>exit

猜你喜欢

转载自blog.csdn.net/zx711166/article/details/83107640