redis6.2.5 sentinel集群 + keepalived 主从自动切换和高可用
环境
ip | 服务 | 端口 |
---|---|---|
100.200.100.111 | redis redis-sentinel | 6379 26379 |
100.200.100.112 | redis redis-sentinel | 6379 26379 |
100.200.100.113 | redis redis-sentinel | 6379 26379 |
下载安装redis
yum install -y gcc-c++ make tcl
wget http://download.redis.io/releases/redis-6.2.5.tar.gz
tar –zxvf redis-6.2.5.tar.gz
cd redis-6.2.5
make
部署redis
操作3台机器
100.200.100.111:
[root@JUMPSER-100-200-100-111 jumpserver-installer-v2.13.1]# egrep -v "^#|^$" /etc/redis.conf
bind 0.0.0.0
protected-mode no
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
pidfile "/var/run/redis.pid"
loglevel notice
logfile "/var/log/redis/redis.log"
databases 16
always-show-logo no
set-proc-title yes
proc-title-template "{title} {listen-addr} {server-mode}"
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
rdb-del-sync-files no
dir "/var/lib/redis"
replicaof 10.20.100.113 6379
masterauth "123456"
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-diskless-load disabled
repl-ping-replica-period 10
repl-timeout 60
repl-disable-tcp-nodelay no
replica-priority 100
acllog-max-len 128
requirepass "123456"
maxmemory 4gb
maxmemory-policy allkeys-lru
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no
lazyfree-lazy-user-flush no
oom-score-adj no
oom-score-adj-values 0 200 800
disable-thp yes
appendonly yes
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
aof-use-rdb-preamble 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
stream-node-max-bytes 4kb
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
jemalloc-bg-thread yes
save 3600 1
save 300 100
save 60 10000
100.200.100.112:
[root@JUMPSER-100-200-100-112 jumpserver-installer-v2.13.1]# egrep -v "^#|^$" /etc/redis.conf
bind 0.0.0.0
protected-mode no
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
pidfile "/var/run/redis.pid"
loglevel notice
logfile "/var/log/redis/redis.log"
databases 16
always-show-logo no
set-proc-title yes
proc-title-template "{title} {listen-addr} {server-mode}"
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
rdb-del-sync-files no
dir "/var/lib/redis"
masterauth "123456"
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-diskless-load disabled
repl-ping-replica-period 10
repl-timeout 60
repl-disable-tcp-nodelay no
replica-priority 100
acllog-max-len 128
requirepass "123456"
maxmemory 4gb
maxmemory-policy allkeys-lru
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no
lazyfree-lazy-user-flush no
oom-score-adj no
oom-score-adj-values 0 200 800
disable-thp yes
appendonly yes
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
aof-use-rdb-preamble 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
stream-node-max-bytes 4kb
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
jemalloc-bg-thread yes
save 3600 1
save 300 100
save 60 10000
100.200.100.113:
[root@JUMPSER-100-200-100-113 0]# egrep -v "^#|^$" /etc/redis.conf
bind 0.0.0.0
protected-mode no
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
pidfile "/var/run/redis.pid"
loglevel notice
logfile "/var/log/redis/redis.log"
databases 16
always-show-logo no
set-proc-title yes
proc-title-template "{title} {listen-addr} {server-mode}"
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
rdb-del-sync-files no
dir "/var/lib/redis"
masterauth "123456"
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-diskless-load disabled
repl-ping-replica-period 10
repl-timeout 60
repl-disable-tcp-nodelay no
replica-priority 100
acllog-max-len 128
requirepass "123456"
maxmemory 4gb
maxmemory-policy allkeys-lru
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no
lazyfree-lazy-user-flush no
oom-score-adj no
oom-score-adj-values 0 200 800
disable-thp yes
appendonly yes
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
aof-use-rdb-preamble 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
stream-node-max-bytes 4kb
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
jemalloc-bg-thread yes
save 3600 1
save 300 100
save 60 10000
部署redis-sentinel
100.200.100.111、112、113: 使用同一个配置
[root@JUMPSER-100-200-100-111 jumpserver-installer-v2.13.1]# egrep -v "^#|^$" /etc/redis-sentinel.conf
protected-mode no
port 26379
daemonize yes
pidfile "/var/run/redis-sentinel.pid"
logfile "/var/log/redis/sentinel.log"
dir "/tmp"
sentinel monitor mymaster 100.200.100.113 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel auth-pass mymaster 123456
acllog-max-len 128
sentinel deny-scripts-reconfig yes
sentinel resolve-hostnames no
sentinel announce-hostnames no
启动redis和redis-sentinel
配置自启动项
[root@JUMPSER-100-200-100-113 0]# cat /etc/systemd/system/redis.service
[Unit]
Description=Redis persistent key-value database
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
Type=forking
ExecStart=/usr/local/redis/bin/redis-server /etc/redis.conf
PIDFile=/var/run/redis.pid
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
[Install]
WantedBy=multi-user.target
[root@JUMPSER-100-200-100-113 0]# cat /etc/systemd/system/redis-sentinel.service
[Unit]
Description=Redis Sentinel
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
Type=forking
ExecStart=/usr/local/redis/bin/redis-sentinel /etc/redis-sentinel.conf
PIDFile=/var/run/redis-sentinel.pid
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
[Install]
WantedBy=multi-user.target
开机自启动
systemctl enable redis redis-sentinel
启动redis
systemctl start redis redis-sentinel
验证集群
redis-cli -h 127.0.0.1 -a 123456 info Replication
# Replication
role:master
connected_slaves:2
slave0:ip=100.200.100.111,port=6379,state=online,offset=244414,lag=1
slave1:ip=100.200.100.112,port=6379,state=online,offset=244414,lag=0
master_failover_state:no-failover
master_replid:b7f646c4f8e0d4f1d036115f24c903406cb3ca7e50
master_replid2:fef7c063c9500ec000709ab51dd03d6a8cc88fd1f9
master_repl_offset:244414
second_repl_offset:21938
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:17657
repl_backlog_histlen:226758
关闭master节点,看某个slave节点是否成为master,然后在启动刚才关闭的节点,看是否加入到集群。(步骤省略,各位看官自行测试吧 ~~)
安装keepalived
yum install -y keepalived
配置keepalived
100.200.100.111:
[root@JUMPSER-100-200-100-111 jumpserver-installer-v2.13.1]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id redis-111
}
vrrp_script chk_redis {
script "/usr/bin/redis-cli -h 127.0.0.1 -a 123456 info|grep role:master >/dev/null 2>&1"
interval 1
}
vrrp_instance redis {
state BACKUP
interface eth0
virtual_router_id 79 #保持一致
priority 98 #优先级
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass redis
}
virtual_ipaddress {
100.200.100.114
}
track_script {
chk_redis
}
}
100.200.100.112:
[root@JUMPSER-100-200-100-112 jumpserver-installer-v2.13.1]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id redis-112
}
vrrp_script chk_redis {
script "/usr/bin/redis-cli -h 127.0.0.1 -a 123456 info|grep role:master >/dev/null 2>&1"
interval 1
}
vrrp_instance redis {
state BACKUP
interface eth0
virtual_router_id 79 #保持一致
priority 99 #优先级
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass redis
}
virtual_ipaddress {
100.200.100.114
}
track_script {
chk_redis
}
}
100.200.100.113:
[root@JUMPSER-100-200-100-113 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id redis-113
}
vrrp_script chk_redis {
script "/usr/bin/redis-cli -h 127.0.0.1 -a 123456 info|grep role:master >/dev/null 2>&1"
interval 1
}
vrrp_instance redis {
state MASTER
interface eth0
virtual_router_id 79
priority 100
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass redis
}
virtual_ipaddress {
100.200.100.114
}
track_script {
chk_redis
}
}
启动keepalived
三台操作
systemctl enable keepalived
systemctl start keepalived
验证集群
[root@JUMPSER-100-200-100-111 jumpserver-installer-v2.13.1]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 52:54:00:67:a0:b1 brd ff:ff:ff:ff:ff:ff
inet 100.200.100.111/21 brd 100.200.103.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet 100.200.100.114/32 scope global eth0
首先关闭vip漂移到的节点,查看vip状态是否移除,使用以下命令
/usr/bin/redis-cli -h 127.0.0.1 -a 123456 info|grep role:master 查看master在那个节点上,同时查看vip是否存在这个节点上,有的话成功。 以上操作多刷几次!!
还没有评论,来说两句吧...