keepalived主备,双主的部署应用

准备工作

4台服务器,两台负载均衡通过keepalived实现高可用,后面两台web。

安装配置

1、yum install -y keepalived

2、修改配置文件 vim /etc/keepalived/keepalived.conf

负载节点1

global_defs {
	router_id lb01
}
       
vrrp_instance gorup01 {	 	# VRRP实例定义区块名字,可任意命名
    state MASTER			# 当前实例的状态,有MASTER ,BACKUP两种
    interface eth0			# 
    virtual_router_id 51	# 虚拟路由ID标识,同一个实例里必须一致,例如master和backup里必须一样
    priority 150			# 优先级
    advert_int 1			# MASTER与BACKUP之间通信检查的时间间隔,单位为秒,默认为1
    authentication {		# 权限认证,同一实例master和backup 必须相同才可以通信
    auth_type PASS
    auth_pass 1111
    }
    virtual_ipaddress { 	# 虚拟IP地址
        10.0.0.88/24 dev eth0 label eth0:1	# 绑定接口为eth0,别名为eth0:1
        
    }
}

负载节点2

global_defs {
    router_id lb02
}
       
vrrp_instance group01 {
    state BACKUP			
    interface eth0
    virtual_router_id 51	# 和MASTER保持一致
    priority 100			
    advert_int 1
    authentication {
    auth_type PASS
    auth_pass 1111
    }
    virtual_ipaddress {
    	10.0.0.88/24 dev eth0 label eth0:1
    }
}

监控负载服务

1、实现高可用首先需要使keepalived和nginx负载节点建立关系,当nginx停止的话,keepalived也要停止,实现方式就是监控nginx进程数量,来判断ningx是否正常运行。

#check_nginx.sh该脚本需要有执行权限chmod +x check_nginx.sh
#!/bin/bash
if [ `netstat -antup | grep nginx | wc -l` -eq 0 ];then
    /etc/init.d/keepalived stop
fi    

2、编辑keepalived配置文件,使keepalied去执行第一步的脚本实现监控。

# 指定执行的脚本代码
vrrp_script check_nignx {
    script "/server/script/check_nginx.sh"
    interval 2 # 多久执行一次check_nginx.sh脚本
    weight 2 # 表示当脚本运行为真,让优先级和权重值做响应的运算实现主备切换
}

# 调用脚本
track_script {
    check_nginx
}

3、完整配置文件

global_defs {
       router_id lb01
}
vrrp_script check_nignx {
    script "/server/script/check_nginx.sh"
    interval 2 
    weight 2 
}       
vrrp_instance gorup01 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 150
    advert_int 1
    authentication {
    auth_type PASS
    auth_pass 1111
    }
    virtual_ipaddress { 
        10.0.0.88/24 dev eth0 label eth0:1
    }
    track_script {
        check_nginx
    }
}

keepalived双主的实现

与主备不同的是,双主每个节点都需要配置两个实例,group01 ,group02

负载节点1

global_defs {
   router_id lb01
}
vrrp_script check_nignx {
    script "/server/scripts/check_nignx.sh"
    interval 2
    weight 2
}
vrrp_instance group01 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
       10.0.0.88/24 dev eth0 label eth0:1 
    }
    track_script {
        check_nignx
    }
}
vrrp_instance group02 {
    state BACKUP
    interface eth0
    virtual_router_id 52
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
       10.0.0.89/24 dev eth0 label eth0:1 
    }

}

负载节点2

global_defs {
   router_id lb02
}
vrrp_script chk_nginx_proxy {
    script "/server/scripts/check_nignx.sh"
    interval 2
    weight 2
}
   vrrp_instance group01 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {      
	10.0.0.88/24 dev eth0 label eth0:1 
    }
    track_script {
        check_nignx
    }
}
vrrp_instance group02 {
    state MASTER
    interface eth0
    virtual_router_id 52
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
       10.0.0.89/24 dev eth0 label eth0:1
    }
}