redis哨兵加上vip

技术文章 1年前 (2020) 完美者
1,296 0

标签:clu   local   tor   cond   soft   ast   wget   ping   etc   

redis主从模式加上vip,业务直接连接vip,这样在主发生down时,业务不用做任何调整,但是仍然需要人工介入将从库变成主库
cluster模式,我们的php支持也不太好,并且cluster模式在某个节点down后,如果没有从库来补上,那这个slot的数据就访问不了
哨兵模式,当主节点down时,由Redis Sentinel自动完成故障发现和转移,不需要人工介入,但是业务连接的ip发生了变化,现在加上vip,判断新的主,然后vip漂到新主上,业务通过vip连接,这样业务也不用调整

环境准备:
三个节点做成一主两从,每个节点部署一个哨兵
ip为

10.99.35.209
10.99.35.215
10.99.35.216

一、安装redis(三个节点都要执行)

cd /apps
wget http://download.redis.io/releases/redis-4.0.3.tar.gz
tar xf /apps/redis-4.0.3.tar.gz -C /apps/
cd /apps/redis-4.0.3
make && make PREFIX=/usr/local/redis4.0.3 install
ln -s /usr/local/redis4.0.3/ /usr/local/redis
mkdir -p /data/redis_data/6379
cd /usr/local/redis
mkdir etc

10.99.35.209为主,10.99.35.215、10.99.35.216做成10.99.35.209的从
10.99.35.209上的配置文件如下:

bind 0.0.0.0
protected-mode no
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile "/data/redis_data/6379/redis_6379.pid"
loglevel notice
logfile "/data/redis_data/6379/redis_6379.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
dir "/data/redis_data/6379"
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
maxmemory 2gb
maxmemory-policy volatile-ttl
maxmemory-samples 3
masterauth "redis123456"
requirepass "redis123456"
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
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 no
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

10.99.35.215、10.99.35.216上的配置文件如下:

bind 0.0.0.0
protected-mode no
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile "/data/redis_data/6379/redis_6379.pid"
loglevel notice
logfile "/data/redis_data/6379/redis_6379.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
dir "/data/redis_data/6379"
slaveof 10.99.35.209 6379
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
maxmemory 2gb
maxmemory-policy volatile-ttl
maxmemory-samples 3
masterauth "redis123456"
requirepass "redis123456"
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
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 no
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

三个节点启动redis

/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis_6379.conf

二、配置哨兵(三个节点都要执行)

cd /data/redis_data/
mkdir 26379
cd 26379/
vim sentinel.conf
port 26379
daemonize yes
dir /data/redis_data/26379
logfile "/data/redis_data/26379/sentinel.log"
sentinel monitor mymaster 10.99.35.209 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel auth-pass mymaster redis123456
sentinel deny-scripts-reconfig yes

启动哨兵

/usr/local/redis/bin/redis-sentinel /data/redis_data/26379/sentinel.conf

三、安装配置keepalived

cd /apps
wget http://www.keepalived.org/software/keepalived-1.3.5.tar.gz
tar -zvxf keepalived-1.3.5.tar.gz
cd keepalived-1.3.5
./configure --prefix=/usr/local/keepalived
make && make install

cd /apps/keepalived-1.3.5
cp keepalived/etc/init.d/keepalived /etc/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
mkdir /etc/keepalived/
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
vrrp_script chk_redis_port {
    script "/etc/keepalived/chk_redis.sh" #脚本路径
    interval 2 #脚本检测频率
    weight -5 #脚本执行成功与否,权重怎么计算
    fall 2 #如果连续两次检测失败,认为节点服务不可用
    rise 1 #如果连续2次检查成功则认为节点正常
}
vrrp_script chk_redis_master {
    script "/etc/keepalived/chk_redis_master.sh"
    interval 2
    weight 10
}
vrrp_instance VI_1 {
    state MASTER
    interface eth0 #节点IP的网卡
    virtual_router_id 88 #同一个instance相同
    priority 100 # 优先级,数值越大,优先级越高
    advert_int 1
    authentication { #节点间的认证,所有的必须一致
        auth_type PASS
        auth_pass 1111
    }

    virtual_ipaddress { #VIP,自定的,我觉得和外网的IP要一个网段
        10.99.35.239
    }
    track_script { #指定前面脚本的名字
        chk_redis_port
        chk_redis_master
    }
}

四、编写简单脚本

cd /etc/keepalived/
cat chk_redis.sh
#!/bin/bash
# check redis is alived

/usr/local/redis/bin/redis-cli -h 10.99.35.209 -a redis123456 -p 6379 ping
a=`echo $?`
if [ $a -eq 1 ] ;then
    echo "/etc/init.d/keepalived stop"
    /etc/init.d/keepalived stop
fi

echo $a

cat chk_redis_master.sh
#!/bin/bash
# check redis is master

set -x

redis_master_host=`/usr/local/redis/bin/redis-cli -h 10.99.35.209 -p 26379  SENTINEL get-master-addr-by-name mymaster | awk ‘{print $1}‘ |grep ‘10.99‘`
local_host=`ip ad sh|grep "scope global eth0"|grep -v grep |awk ‘{print $2}‘|awk -F ‘/‘ ‘{print $1}‘`

if [[ $redis_master_host == $local_host ]]
then
   echo "Redis Instance is master ........"
   exit 0
else
   echo "Redis Instance is slave ........"
   exit 1
fi

五、测试
209为主,vip漂在209上,如下

[root@opsys-35-209 ~]# ip ad sh
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether 00:0c:29:a7:dc:df brd ff:ff:ff:ff:ff:ff
    inet 10.99.35.209/24 brd 10.99.35.255 scope global eth0
    inet 10.99.35.239/32 scope global eth0
    inet6 fe80::20c:29ff:fea7:dcdf/64 scope link
       valid_lft forever preferred_lft forever

登陆哨兵查看

10.99.35.209:26379> sentinel master mymaster
1) "name"
2) "mymaster"
3) "ip"
4) "10.99.35.209"
5) "port"
6) "6379"

登陆215,查看

[root@opsys-vm5-215 26379]# ip ad sh
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether 00:0c:29:ca:fd:8d brd ff:ff:ff:ff:ff:ff
    inet 10.99.35.215/24 brd 10.99.35.255 scope global eth0
    inet6 fe80::20c:29ff:feca:fd8d/64 scope link
       valid_lft forever preferred_lft forever

登陆216,查看

[root@opsys-vm6-216 ~]# ip ad sh
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether 00:0c:29:06:ca:4c brd ff:ff:ff:ff:ff:ff
    inet 10.99.35.216/24 brd 10.99.35.255 scope global eth0
    inet6 fe80::20c:29ff:fe06:ca4c/64 scope link
       valid_lft forever preferred_lft forever

模拟故障,验证vip是否漂在新的master节点上
登陆209,kill redis进程

[root@opsys-35-209 ~]# ps aux|grep redis
root     10065  0.3  1.3 250408 114316 ?       Ssl  Sep08   5:06 /usr/local/redis/bin/redis-server 0.0.0.0:6379                        
root     12986  0.5  0.0 143912  7752 ?        Ssl  Sep08   7:27 /usr/local/redis/bin/redis-sentinel *:26379 [sentinel]                  
root     31970  0.0  0.0 103320   864 pts/5    S+   17:12   0:00 grep redis
[root@opsys-35-209 ~]# kill -9 10065
[root@opsys-35-209 ~]# clear
[root@opsys-35-209 ~]# ip ad sh
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether 00:0c:29:a7:dc:df brd ff:ff:ff:ff:ff:ff
    inet 10.99.35.209/24 brd 10.99.35.255 scope global eth0
    inet6 fe80::20c:29ff:fea7:dcdf/64 scope link
       valid_lft forever preferred_lft forever

哨兵查看,发现216为主了

10.99.35.209:26379> sentinel master mymaster
1) "name"
2) "mymaster"
3) "ip"
4) "10.99.35.216"
5) "port"
6) "6379"

登陆215,查看

[root@opsys-vm5-215 26379]# ip ad sh
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether 00:0c:29:ca:fd:8d brd ff:ff:ff:ff:ff:ff
    inet 10.99.35.215/24 brd 10.99.35.255 scope global eth0
    inet6 fe80::20c:29ff:feca:fd8d/64 scope link
       valid_lft forever preferred_lft forever

登陆216查看,发现vip漂过来了

[root@opsys-vm6-216 ~]# ip ad sh
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether 00:0c:29:06:ca:4c brd ff:ff:ff:ff:ff:ff
    inet 10.99.35.216/24 brd 10.99.35.255 scope global eth0
    inet 10.99.35.239/32 scope global eth0
    inet6 fe80::20c:29ff:fe06:ca4c/64 scope link
       valid_lft forever preferred_lft forever

启动209,模拟节点加入后,新加入的节点是否为从,vip是否还在master节点
登陆209,启动redis,查看

/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis_6379.conf
[root@opsys-35-209 ~]# ip ad sh
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether 00:0c:29:a7:dc:df brd ff:ff:ff:ff:ff:ff
    inet 10.99.35.209/24 brd 10.99.35.255 scope global eth0
    inet6 fe80::20c:29ff:fea7:dcdf/64 scope link
       valid_lft forever preferred_lft forever

登陆215查看

[root@opsys-vm5-215 26379]# ip ad sh
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether 00:0c:29:ca:fd:8d brd ff:ff:ff:ff:ff:ff
    inet 10.99.35.215/24 brd 10.99.35.255 scope global eth0
    inet6 fe80::20c:29ff:feca:fd8d/64 scope link
       valid_lft forever preferred_lft forever

登陆216,查看

# Replication
role:master
connected_slaves:2
slave0:ip=10.99.35.215,port=6379,state=online,offset=17460952,lag=0
slave1:ip=10.99.35.209,port=6379,state=online,offset=17460813,lag=1

[root@opsys-vm6-216 26379]# ip ad sh
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether 00:0c:29:06:ca:4c brd ff:ff:ff:ff:ff:ff
    inet 10.99.35.216/24 brd 10.99.35.255 scope global eth0
    inet 10.99.35.239/32 scope global eth0
    inet6 fe80::20c:29ff:fe06:ca4c/64 scope link
       valid_lft forever preferred_lft forever

登陆哨兵查看

10.99.35.209:26379> SENTINEL get-master-addr-by-name mymaster
1) "10.99.35.216"
2) "6379"
10.99.35.209:26379> sentinel master mymaster
1) "name"
2) "mymaster"
3) "ip"
4) "10.99.35.216"
5) "port"
6) "6379"

10.99.35.209:26379> sentinel slaves mymaster
1)  1) "name"
    2) "10.99.35.209:6379"
    3) "ip"
    4) "10.99.35.209"
    5) "port"
    6) "6379"

2)  1) "name"
    2) "10.99.35.215:6379"
    3) "ip"
    4) "10.99.35.215"
    5) "port"
    6) "6379"

redis哨兵加上vip

标签:clu   local   tor   cond   soft   ast   wget   ping   etc   

原文地址:https://blog.51cto.com/qhd2004/2530948

版权声明:完美者 发表于 2020-09-17 20:32:09。
转载请注明:redis哨兵加上vip | 完美导航

暂无评论

暂无评论...