keepalived需要一个vip;如果用的是云服务,那么得在后台申请一个高可用ip,才能实现vip飘移;如果是自建服务器,那么一台服务器得使用两个网卡;只是为了测试,那么使用docker容器模拟集群是最合适的了

Nginx+keepalived

Nginx一主(172.1.1.11)一备(172.1.1.12),那么备服务器也太浪费了,所以除了keepalived高可用还涉及到四层网络的负载均衡;一般主备都需要配置haproxy,它是Linux内核自带的;为了方便,Nginxstream模块也能实现四层负载均衡

一备一主Nginx都需要类似配置 :

stream {
    upstream web_server {
        server 172.1.1.11:8080 weight=1 max_fails=3 fail_timeout=10s;
        server 172.1.1.12:8080 weight=1 max_fails=3 fail_timeout=10s;
    }

    server {
        listen 80 reuseport;
        proxy_pass web_server;
    }

}

http {
    server {
        listen 172.1.1.11:8080; #使用本机ip;stream和http不能监听相同端口,所以另用端口,除非指定不同的ip:port
        # ...
    }
}

一备一主keepalived都需要相同配置/etc/keepalived/keepalived.conf :

global_defs {
   router_id LVS_DEVEL
}

vrrp_script check_nginx {
    script "/etc/keepalived/check_nginx.sh"
    interval 2 #检测时间
    fall 2 #检测失败次数
    rise 1 #检测成功次数
    weight -20 #sh脚本执行了"exit 1"后的权重变化
}

vrrp_instance VI_1 {
    state BACKUP

    interface eth0 #网卡
    virtual_router_id 70 #组唯一id
    priority 100 #权重
    #nopreempt #非抢占模式,对BACKUP生效,宕机恢复后,不会抢占vip
    advert_int 1 #MASTER与BACKUP之间检查的时间
    virtual_ipaddress {
        #飘移的vip
        172.1.1.100/24
    }
    track_script {
        check_nginx
    }
}

一备一主都需要添加 检测nginx状态 的脚本/etc/keepalived/check_nginx.sh :

#!/bin/bash
A=`ps -C nginx --no-header | wc -l`

if [ $A -eq 0 ];then
    #/usr/sbin/nginx #重启nginx
    sleep 1
    if [ $A -eq 0 ];then
        #pkill keepalived
        exit 1
    fi
fi
exit 0

Redis+keepalived

Redis不同于Nginx,Redis需要实现多个服务器的redis数据同步,所以配置分为三步:

  1. 配置Redis集群主从库复制
  2. 配置Redis的集群哨兵模式,用于检测Redis状态,宕机及时切换主数据库,数据恢复
  3. 使用Nginxstream模块实现读写分离和读的负载均衡
  4. 配置keepalived,负责Redis切换主库后的飘移vip

添加配置到从库的redis.conf,实现复制主库(172.1.1.11)的数据

replicaof 172.1.1.11 6379

一备一主的哨兵都需要相同配置sentinel.conf :

port 26379

daemonize yes
protected-mode no

pidfile "/var/run/sentinel/redis-sentinel.pid"
logfile "/var/log/redis/redis-sentinel.log"
dir "/tmp"

sentinel myid db1f6c96877303e3aa393481d00359abae6b2e7d

sentinel deny-scripts-reconfig yes

sentinel monitor mymaster 172.1.1.11 6379 1
#查看主从状态
redis-cli -p 26379
    >sentinel master mymaster
    >SENTINEL get-master-addr-by-name mymaster

Nginx配置为Redis中间代理实现读写分离和负载均衡(读和写不同端口),一备一主Nginx都需要类似配置,可添加到上文的同一份配置文件 :

stream {
    upstream redis_r {
        server 172.1.1.11:6379 weight=1 max_fails=3 fail_timeout=10s;
        server 172.1.1.12:6379 weight=1 max_fails=3 fail_timeout=10s;
    }

    upstream redis_w {
        server 172.1.1.101:6379 weight=1 max_fails=3 fail_timeout=10s; #keepalived偏移vip
    }
    #redis读
    server {
        listen 6389 reuseport;
        proxy_pass redis_r;
        proxy_connect_timeout 3s;
        proxy_timeout 60s;
    }
    #redis写
    server {
        listen 6399 reuseport;
        proxy_pass redis_w;
        proxy_connect_timeout 3s;
        proxy_timeout 60s;
    }
}

http {
    # ...
}

一备一主keepalived都需要相同配置,添加到上文的同一份配置文件/etc/keepalived/keepalived.conf :

vrrp_script check_redis {
    script "/etc/keepalived/check_redis.sh"
    interval 2
    weight -30
}

vrrp_instance VI_2 {
    state BACKUP

    interface eth0
    virtual_router_id 80
    priority 100
    advert_int 1
    virtual_ipaddress {
        172.1.1.101/24
    }
    track_script {
        check_redis
    }
}

一备一主都需要添加 检测Redis状态 的脚本/etc/keepalived/check_redis.sh :

#!/bin/bash
#检测是否master主库(redis-sentinel推举出主库)
A=`redis-cli info|grep role:master`
if [ -z $A ]; then
    sleep 1
    if [ -z $A ]; then
        exit 1
    fi
fi
exit 0

相关