centos7 安装redis sentinel

#MAC 可视化查询工具
#RedisDesktopManager for mac download:
https://github.com/uglide/RedisDesktopManager/releases/download/0.8.3/redis-desktop-manager-0.8.3-2550.dmg

1.说明
xxx.16.2.202、xxx.16.2.203、xxx.16.2.204三台服务器同时部署redis和sentinel
redis为一组两从
redis哨兵集群和传统的HA不同,没有VIP的概念,可以实现故障切换但是IP会变

2.安装
yum -y install wget
yum -y install sysstat gcc gcc-c++ glibc libstdc++ wget vim lsof dstat nload iftop iotop lrzsz zip unzip nmap make net-snmp-utils expect bc openssl* ntp net-snmp* cmake autoconf net-tools psmisc pciutils bind-utils lftp
cd /opt
wget http://download.redis.io/releases/redis-4.0.10.tar.gz
tar -xzf redis-4.0.10.tar.gz
cd redis-4.0.10

make
make PREFIX=/opt/redis install
cp /opt/redis-4.0.10/src/redis-trib.rb /opt/redis/bin/

mkdir -p /opt/redis/sentinel /opt/redis/log /opt/redis/6389/rdb

[root@dev-gz-kyf-redis-202 redis]# ll
总用量 0
drwxr-xr-x 2 root root   6 7月  31 13:37 6389
drwxr-xr-x 2 root root 155 7月  31 12:44 bin
drwxr-xr-x 2 root root  42 7月  31 13:52 log
drwxr-xr-x 2 root root  45 7月  31 13:52 sentinel

修改内核参数
net.core.somaxconn = 32768
vm.overcommit_memory = 1
echo never > /sys/kernel/mm/transparent_hugepage/enabled

查看redis状态
[root@dev-gz-kyf-redis-202 log]# /opt/redis/bin/redis-cli -h xxx.16.2.202 -p 6389 quit
Could not connect to Redis at xxx.16.2.202:6389: Connection refused  #未开启

[root@dev-gz-kyf-redis-202 log]# /opt/redis/bin/redis-cli -h xxx.16.2.202 -p 6389 quit
OK   #已开启

3.建立主redis配置文件redis.conf
文件路径在/opt/redis/sentinel

cd /opt/redis/sentinel
vim redis.conf

bind xxx.16.2.202
protected-mode yes
port 6389
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile "/opt/redis/6389/redis-6389.pid"
loglevel notice
logfile "/opt/redis/log/6389.log"
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
dir "/opt/redis/6389/rdb"
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes

# Generated by CONFIG REWRITE

4.建立从redis配置文件redis.conf
注意从redis配置文件redis.conf只有监听IP和slaveof和主redis配置不同,别的一模一样,同时slaveof 10.14.255.52 6389 配置必须预先写入从redis配置文件,否则sentinel无法自动切换主动redis
文件路径在/opt/redis/sentinel

cd /opt/redis/sentinel
vim redis.conf

bind xxx.16.2.203
protected-mode yes
port 6389
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile "/opt/redis/6389/redis-6389.pid"
loglevel notice
logfile "/opt/redis/log/6389.log"
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
dir "/opt/redis/6389/rdb"
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes

# Generated by CONFIG REWRITE

slaveof xxx.16.2.202 6389

5.建立sentinel配置文件
三台服务器都要建立次配置文件sentinel.conf
文件路径在/opt/redis/sentinel

cd /opt/redis/sentinel
vim sentinel.conf

除了监听IP不同,别的三台服务器配置一模一样
bind xxx.16.2.202
protected-mode yes
port 7111
dir "/opt/redis/sentinel"                        #工作目录,随便写
daemonize yes                                    #后台启动模式
logfile "/opt/redis/log/sentinel.log"                #日志路径
sentinel monitor mymaster xxx.16.2.202 6389 1    #默认主REDIS的IP和端口
sentinel down-after-milliseconds mymaster 5000   #主redis5秒没响应就认为down掉了
# Generated by CONFIG REWRITE
#sentinel auth-pass mymaster 123456
sentinel failover-timeout mymaster 18000         #网上写的是切换时间,默认是18s,实际上观察3个sentinel切换时间为5秒以内,2个sentinel在2秒以内


6.启动三台服务器的sentinel
/opt/redis/bin/redis-sentinel /opt/redis/sentinel/sentinel.conf --sentinel

7.启动三台服务器的redis
/opt/redis/bin/redis-server /opt/redis/sentinel/redis.conf

8.查看所有redis状态
主从redis显示的信息是不同的,主要是role:slave和role:master的区别
/opt/redis/bin/redis-cli -h xxx.16.2.202 -p 6389 info Replication 
/opt/redis/bin/redis-cli -h xxx.16.2.203 -p 6389 info Replication 
/opt/redis/bin/redis-cli -h xxx.16.2.204 -p 6389 info Replication 

主redis显示信息:
[root@dev-gz-kyf-redis-202 sentinel]# /opt/redis/bin/redis-cli -h xxx.16.2.202 -p 6389 info Replication
# Replication
role:master
connected_slaves:2
slave0:ip=xxx.16.2.203,port=6389,state=online,offset=2576,lag=1
slave1:ip=xxx.16.2.204,port=6389,state=online,offset=2576,lag=1
master_replid:a5f0816a7bde8d98e623a9d2e02c3721213e5356
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2576
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:2576

从redis显示信息:
[root@dev-gz-kyf-redis-203 sentinel]# /opt/redis/bin/redis-cli -h xxx.16.2.203 -p 6389 info Replication
# Replication
role:slave
master_host:xxx.16.2.202
master_port:6389
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:8303
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:a5f0816a7bde8d98e623a9d2e02c3721213e5356
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:8303
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:8303

从redis显示信息:
[root@dev-gz-kyf-redis-204 sentinel]# /opt/redis/bin/redis-cli -h xxx.16.2.204 -p 6389 info Replication
# Replication
role:slave
master_host:xxx.16.2.202
master_port:6389
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:16852
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:a5f0816a7bde8d98e623a9d2e02c3721213e5356
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:16852
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:455
repl_backlog_histlen:16398

9.查看sentinel状态
3台服务器的sentinel显示的信息是一模一样的

[root@dev-gz-kyf-redis-202 sentinel]# /opt/redis/bin/redis-cli -h xxx.16.2.202 -p 7111 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=xxx.16.2.202:6389,slaves=2,sentinels=3

10.查看sentinel启动后的配置文件
sentinel启动后会自动修改sentinel.conf配置文件
查看3台服务器的sentinel.conf配置文件
[root@dev-gz-kyf-redis-202 sentinel]# cat sentinel.conf
bind xxx.16.2.202
protected-mode yes
port 7111
dir "/opt/redis/sentinel"
daemonize yes
logfile "/opt/redis/log/sentinel.log"
sentinel myid 98b466c2aac0d3b19412da080035d4207bfb4b4e #sentinel启动后自动增加,每个sentinel的ID不同
sentinel monitor mymaster xxx.16.2.202 6389 1          #sentinel启动后会依据主redis在哪个节点自动变化
# Generated by CONFIG REWRITE                          
#sentinel auth-pass mymaster 123456
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 18000
sentinel config-epoch mymaster 0                       #sentinel启动后自动增加,代表redis切换次数
sentinel leader-epoch mymaster 18
sentinel known-slave mymaster xxx.16.2.204 6389
sentinel known-slave mymaster xxx.16.2.203 6389
sentinel known-sentinel mymaster xxx.16.2.203 7111 9f083d336278270a288dccdc229e2d573d388882
sentinel known-sentinel mymaster xxx.16.2.204 7111 b0f0dfd6e860a4121bca3ec681e83eac2269dd44
sentinel current-epoch 18

[root@dev-gz-kyf-redis-203 sentinel]# cat sentinel.conf
bind xxx.16.2.203
protected-mode yes
port 7111
dir "/opt/redis/sentinel"
daemonize yes
logfile "/opt/redis/log/sentinel.log"
sentinel myid 9f083d336278270a288dccdc229e2d573d388882
sentinel monitor mymaster xxx.16.2.202 6389 1
# Generated by CONFIG REWRITE
#sentinel auth-pass mymaster 123456
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 18000
sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 18
sentinel known-slave mymaster xxx.16.2.204 6389
sentinel known-slave mymaster xxx.16.2.203 6389
sentinel known-sentinel mymaster xxx.16.2.204 7111 b0f0dfd6e860a4121bca3ec681e83eac2269dd44
sentinel known-sentinel mymaster xxx.16.2.202 7111 98b466c2aac0d3b19412da080035d4207bfb4b4e
sentinel current-epoch 18

[root@dev-gz-kyf-redis-204 sentinel]# cat sentinel.conf
bind xxx.16.2.204
protected-mode yes
port 7111
dir "/opt/redis/sentinel"
daemonize yes
logfile "/opt/redis/log/sentinel.log"
sentinel myid b0f0dfd6e860a4121bca3ec681e83eac2269dd44
sentinel monitor mymaster xxx.16.2.202 6389 1
# Generated by CONFIG REWRITE
#sentinel auth-pass mymaster 123456
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 18000
sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 18
sentinel known-slave mymaster xxx.16.2.204 6389
sentinel known-slave mymaster xxx.16.2.203 6389
sentinel known-sentinel mymaster xxx.16.2.203 7111 9f083d336278270a288dccdc229e2d573d388882
sentinel known-sentinel mymaster xxx.16.2.202 7111 98b466c2aac0d3b19412da080035d4207bfb4b4e
sentinel current-epoch 18

sentinel启动日志如下
19330:X 31 Jul 13:52:24.762 # Sentinel ID is 98b466c2aac0d3b19412da080035d4207bfb4b4e
19330:X 31 Jul 13:52:24.762 # +monitor master mymaster xxx.16.2.202 6389 quorum 1
19330:X 31 Jul 14:02:57.688 * +sentinel sentinel b0f0dfd6e860a4121bca3ec681e83eac2269dd44 xxx.16.2.204 7111 @ mymaster xxx.16.2.202 6389
19330:X 31 Jul 14:02:59.222 * +sentinel sentinel 9f083d336278270a288dccdc229e2d573d388882 xxx.16.2.203 7111 @ mymaster xxx.16.2.202 6389
19330:X 31 Jul 14:03:07.453 * +slave slave xxx.16.2.203:6389 xxx.16.2.203 6389 @ mymaster xxx.16.2.202 6389 #标明了xxx.16.2.202目前是主redis
19330:X 31 Jul 14:03:17.492 * +slave slave xxx.16.2.204:6389 xxx.16.2.204 6389 @ mymaster xxx.16.2.202 6389

11.测试故障切换,模拟服务器故障或者kill主redis进程都可以
直接重启xxx.16.2.202主redis服务器,模拟服务器硬件故障

[root@dev-gz-kyf-redis-202 redis]# ps -ef|grep redis
root     19330     1  0 13:52 ?        00:00:01 /opt/redis/bin/redis-sentinel xxx.16.2.202:7111 [sentinel]
root     19406     1  0 14:02 ?        00:00:00 /opt/redis/bin/redis-server xxx.16.2.202:6389
root     19480  5043  0 14:21 pts/1    00:00:00 grep --color=auto redis
您在 /var/spool/mail/root 中有邮件
[root@dev-gz-kyf-redis-202 redis]# kill -9 19406

sentinel切换redis日志如下

19039:X 31 Jul 14:24:59.492 # +sdown master mymaster xxx.16.2.202 6389
19039:X 31 Jul 14:24:59.492 # +odown master mymaster xxx.16.2.202 6389 #quorum 1/1
19039:X 31 Jul 14:24:59.492 # +new-epoch 19
19039:X 31 Jul 14:24:59.492 # +try-failover master mymaster xxx.16.2.202 6389
19039:X 31 Jul 14:24:59.493 # +vote-for-leader 9f083d336278270a288dccdc229e2d573d388882 19
19039:X 31 Jul 14:24:59.494 # b0f0dfd6e860a4121bca3ec681e83eac2269dd44 voted for 9f083d336278270a288dccdc229e2d573d388882 19
19039:X 31 Jul 14:24:59.494 # 98b466c2aac0d3b19412da080035d4207bfb4b4e voted for 9f083d336278270a288dccdc229e2d573d388882 19
19039:X 31 Jul 14:24:59.564 # +elected-leader master mymaster xxx.16.2.202 6389
19039:X 31 Jul 14:24:59.564 # +failover-state-select-slave master mymaster xxx.16.2.202 6389
19039:X 31 Jul 14:24:59.622 # +selected-slave slave xxx.16.2.204:6389 xxx.16.2.204 6389 @ mymaster xxx.16.2.202 6389
19039:X 31 Jul 14:24:59.622 * +failover-state-send-slaveof-noone slave xxx.16.2.204:6389 xxx.16.2.204 6389 @ mymaster xxx.16.2.202 6389
19039:X 31 Jul 14:24:59.684 * +failover-state-wait-promotion slave xxx.16.2.204:6389 xxx.16.2.204 6389 @ mymaster xxx.16.2.202 6389
19039:X 31 Jul 14:25:00.570 # +promoted-slave slave xxx.16.2.204:6389 xxx.16.2.204 6389 @ mymaster xxx.16.2.202 6389
19039:X 31 Jul 14:25:00.570 # +failover-state-reconf-slaves master mymaster xxx.16.2.202 6389
19039:X 31 Jul 14:25:00.635 * +slave-reconf-sent slave xxx.16.2.203:6389 xxx.16.2.203 6389 @ mymaster xxx.16.2.202 6389
19039:X 31 Jul 14:25:01.571 * +slave-reconf-inprog slave xxx.16.2.203:6389 xxx.16.2.203 6389 @ mymaster xxx.16.2.202 6389
19039:X 31 Jul 14:25:01.571 * +slave-reconf-done slave xxx.16.2.203:6389 xxx.16.2.203 6389 @ mymaster xxx.16.2.202 6389
19039:X 31 Jul 14:25:01.647 # +failover-end master mymaster xxx.16.2.202 6389
19039:X 31 Jul 14:25:01.647 # +switch-master mymaster xxx.16.2.202 6389 xxx.16.2.204 6389 #此行表示切换成功,xxx.16.2.204已经转为主redis
19039:X 31 Jul 14:25:01.647 * +slave slave xxx.16.2.203:6389 xxx.16.2.203 6389 @ mymaster xxx.16.2.204 6389
19039:X 31 Jul 14:25:01.647 * +slave slave xxx.16.2.202:6389 xxx.16.2.202 6389 @ mymaster xxx.16.2.204 6389
19039:X 31 Jul 14:25:06.710 # +sdown slave xxx.16.2.202:6389 xxx.16.2.202 6389 @ mymaster xxx.16.2.204 6389

查看xxx.16.2.204 redis.conf配置文件,确定
slaveof xxx.16.2.204 6389 参数是否被自动删除

查看xxx.16.2.203 redis.conf配置文件,确定
slaveof xxx.16.2.202 6389 参数是否修改为slaveof xxx.16.2.204 6389  
主redis换了服务器,slaveof参数会自动发生变化

也可以通过/opt/redis/bin/redis-cli -h xxx.16.2.203 -p 6389 info Replication 指令查看redis的复制状态

[root@dev-gz-kyf-redis-203 sentinel]# /opt/redis/bin/redis-cli -h xxx.16.2.203 -p 6389 info Replication
# Replication
role:slave
master_host:xxx.16.2.204
master_port:6389
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:344725
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:7e90cbedc888862278aed2fe1fceb556ed76c8b5
master_replid2:a5f0816a7bde8d98e623a9d2e02c3721213e5356
master_repl_offset:344725
second_repl_offset:269456
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:344725

[root@dev-gz-kyf-redis-204 sentinel]# /opt/redis/bin/redis-cli -h xxx.16.2.204 -p 6389 info Replication
# Replication
role:master
connected_slaves:1
slave0:ip=xxx.16.2.203,port=6389,state=online,offset=362071,lag=0
master_replid:7e90cbedc888862278aed2fe1fceb556ed76c8b5
master_replid2:a5f0816a7bde8d98e623a9d2e02c3721213e5356
master_repl_offset:362071
second_repl_offset:269456
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:455
repl_backlog_histlen:361617

12.xxx.16.2.202重启后查看redis状态是否为slave
/opt/redis/bin/redis-server /opt/redis/sentinel/redis.conf

[root@dev-gz-kyf-redis-202 sentinel]#  /opt/redis/bin/redis-cli -h xxx.16.2.202 -p 6389 info Replication
# Replication
role:slave
master_host:xxx.16.2.204
master_port:6389
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:380266
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:7e90cbedc888862278aed2fe1fceb556ed76c8b5
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:380266
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:377290
repl_backlog_histlen:2977


sentinel日志如下
19330:X 31 Jul 14:33:25.707 # -sdown slave xxx.16.2.202:6389 xxx.16.2.202 6389 @ mymaster xxx.16.2.204 6389
19330:X 31 Jul 14:33:35.712 * +convert-to-slave slave xxx.16.2.202:6389 xxx.16.2.202 6389 @ mymaster xxx.16.2.204 6389
 

你可能感兴趣的:(redis)