keepalived实现nginx反向代理的高可用
实现keepalived调用脚本进行资源监控
keepalived调用外部的辅助脚本进行资源监控,并根据监控的结果状态能实现优先动态调整
vrrp_script:自定义资源监控脚本,vrrp实例根据脚本返回值,公共定义,可被多个实例调用,定义在vrrp实例之外
track_script:调用vrrp_script定义的脚本去监控资源,定义在实例之内,调用事先定义的vrrp_script
分两步:(1) 先定义一个脚本;(2) 调用此脚本
vrrp_script <SCRIPT_NAME> { script "" interval INT weight -INT }
track_script { SCRIPT_NAME_1 SCRIPT_NAME_2 }
1、环境搭建:
类型 |
机器名称 |
IP配置 |
服务角色 |
A |
client |
192.168.37.7 |
客户端访问(桥接模式) |
C |
lvs-server1 |
VIP:192.168.37.100 DIP:192.168.37.17 |
负载均衡调度器(NAT模式) |
D |
lvs-server2 |
VIP:192.168.37.100 DIP:192.168.37.27 |
负载均衡调度器(NAT模式) |
E |
rs1 |
RIP:192.168.37.37 |
后端服务器(NAT模式) |
F |
rs2 |
RIP:192.168.37.47 |
后端服务器(NAT模式) |
2、在keepalived主服务器配置
配置keepalived服务
[root@centos17~]#yum install keepalived [root@centos17~]#vim /etc/keepalived/keepalived.conf global_defs { notification_email { 974212253@qq.com 接收邮箱地址 } notification_email_from www.magedu.net 从哪个邮箱发送 smtp_server 127.0.0.1 本地IP地址 smtp_connect_timeout 30 router_id ka1 本地的主机名 vrrp_skip_check_adv_addr vrrp_garp_interval 0 vrrp_gna_interval 0 } vrrp_script chk_down { script "/etc/keepalived/test.sh" 调用本地脚本test.sh interval 1 weight -30 权重减30 } vrrp_script chk_nginx { script "/etc/keepalived/check_nginx.sh" 调用本地脚本check_nginx.sh interval 1 weight -30 权重减30 } vrrp_instance VI_1 { state MASTER 本机的主节点 interface ens33 本机的网卡名称 virtual_router_id 66 本机路由ID号 priority 100 本机keepalived优先级 unicast_src_ip 192.168.37.17 # 配置源地址的IP地址,绑定单播地址的目的是为了防止与其他主机的IP地址存在冲突问题。 unicast_peer { 192.168.37.27 # 配置从节点的目标IP地址 } advert_int 1 authentication { auth_type PASS auth_pass 123456 密码 } virtual_ipaddress { 192.168.37.100/24 dev ens33 label ens33:1 添加一个VIP地址 } track_script { chk_down 对宕机进行检查 } track_script { chk_nginx 对nginx进行检查 } notify_master "/etc/keepalived/notify.sh master" 调用脚本,返回当前keepalived状态信息:master notify_backup "/etc/keepalived/notify.sh backup" 返回backup信息 notify_fault "/etc/keepalived/notify.sh fault" 返回fault信息 } [root@centos17~]#systemctl start keepalived
配置主节点nginx服务,配置完成之后启动nginx服务:nginx
yum install nginx -y 安装nginx服务包
[root@centos17~]#vim /etc/nginx/nginx.conf upstream websrvs{ server 192.168.37.37:80; server 192.168.37.47:80; } server { listen 80; server_name www.magedu.net; root /data/site1; index index.html; location / { proxy_pass http://websrvs; }
在keepalived主节点创建脚本
在keepalived主节点创建一个检查nginx状态脚本
[root@centos17keepalived]#vim check_nginx.sh #!/bin/bash killall -0 nginx &> /dev/null || exit 1 检查此时的nginx服务进程是否正常,如果非0(死机),就会执行退出显示1。 [root@centos17]#chmod +x check_nginx.sh 添加执行权限
当keepalived服务器宕机时,在keepalived主节点创建一个降优先级脚本。
[root@centos17keepalived]#vim test.sh #!/bin/bash [ -f /etc/keepalived/down ] && exit 1 || exit 0 判断/etc/keepalived/目录下是否有down文件,如果有就返回1,没有就返回0 [root@centos17keepalived]#chmod +x test.sh 加上执行权限
当nginx停止服务或者死机时,此时nginx服务器变成从节点,检测到主节点变成从节点后,此脚本就会提示主节点变为从节点,返回backup,就会重新启动nginx服务,又会抢占从节点nginx服务,变成主节点。
实现脚本如下:
[root@centos17keepalived]#vim notify.sh #!/bin/bash contact='root@localhost' notify() { mailsubject="$(hostname) to be $1, vip floating" mailbody="$(date +'%F %T'): vrrp transition, $(hostname) changed to be $1" echo "$mailbody" | mail -s "$mailsubject" $contact } case $1 in master) notify master ;; backup) notify backup systemctl restart nginx 当主节点的nginx宕机之后,返回backup,就会有重启nginx,又抢回主节点位置,继续为客户端提供服务。 ;; fault) notify fault ;; *) echo "Usage: $(basename $0) {master|backup|fault}" exit 1 ;; esac
3、在keepalived从服务器配置
在keepalived从服务器上配置相关文件
[root@centos27~]#vim /etc/keepalived/keepalived.conf global_defs { notification_email { 974212253@qq.com } notification_email_from ka1@magedu.net smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id ka2 vrrp_skip_check_adv_addr vrrp_garp_interval 0 vrrp_gna_interval 0 } vrrp_script chk_down { script "/etc/keepalived/test.sh" interval 1 weight -30 } vrrp_script chk_nginx { script "/etc/keepalived/check_nginx.sh" interval 1 weight -30 } vrrp_instance VI_1 { state BACKUP interface ens33 virtual_router_id 66 priority 80 unicast_src_ip 192.168.37.27 配置从节点的IP地址 unicast_peer { 192.168.37.17 配置主节点的目标IP地址 } advert_int 1 authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 192.168.37.100/24 dev ens33 label ens33:1 } track_script { chk_down } track_script { chk_nginx } notify_master "/etc/keepalived/notify.sh master" notify_backup "/etc/keepalived/notify.sh backup" notify_fault "/etc/keepalived/notify.sh fault" }
配置从节点的nginx服务,配置完成之后启动nginx服务:nginx
[root@centos17~]#vim /etc/nginx/nginx.conf upstream websrvs{ server 192.168.37.37:80; server 192.168.37.47:80; } server { listen 80; server_name www.magedu.net; root /data/site1; index index.html; location / { proxy_pass http://websrvs; }
在keepalived从节点创建脚本
在keepalived从节点创建一个检查nginx状态脚本
[root@centos17keepalived]#vim check_nginx.sh #!/bin/bash killall -0 nginx &> /dev/null || exit 1 检查此时的nginx服务进程是否正常,如果非0(死机),就会执行退出显示1。 [root@centos17]#chmod +x check_nginx.sh 添加执行权限
当keepalived服务器宕机时,在keepalived从节点创建一个降优先级脚本。
[root@centos17keepalived]#vim test.sh #!/bin/bash [ -f /etc/keepalived/down ] && exit 1 || exit 0 判断/etc/keepalived/目录下是否有down文件,如果有就返回1,没有就返回0 [root@centos17keepalived]#chmod +x test.sh 加上执行权限
此时nginx从节点服务器变成主节点。
创建完成之后加上执行权限,实现脚本如下:
[root@centos17keepalived]#vim notify.sh #!/bin/bash contact='root@localhost' notify() { mailsubject="$(hostname) to be $1, vip floating" mailbody="$(date +'%F %T'): vrrp transition, $(hostname) changed to be $1" echo "$mailbody" | mail -s "$mailsubject" $contact } case $1 in master) notify master ;; backup) notify backup systemctl restart nginx ;; fault) notify fault ;; *) echo "Usage: $(basename $0) {master|backup|fault}" exit 1 ;; esac
后端http服务器上创建网页
在两个后端服务器上创建网页,作为测试效果使用。
[root@cenots47~]#yum install httpd -y [root@cenots47~]#echo 192.168.37.47 > /var/www/html/index.html [root@cenots47~]#systemctl start httpd [root@cenots37~]#yum install httpd -y [root@cenots37~]#echo 192.168.37.37 > /var/www/html/index.html [root@cenots37~]#systemctl start httpd
客户端验证效果
在客户端修改hosts配置文件,并测试效果,如果主keepalived服务器宕机了,VIP地址就会漂移到从节点上去,继续运行nginx服务,不会影响用户访问效果。
vim /etc/hosts
[root@centos7network-scripts]#while true;do curl 192.168.37.100;sleep 0.5;done 192.168.37.47 192.168.37.47 192.168.37.37 192.168.37.47 192.168.37.37
当主节点的nginx服务器死机了,此时的VIP地址会临时漂移到从节点上,当脚本检测到主节点的nginx服务死机后,就会重新启动nginx服务,将VIP地址就会从从节点服务器抢过来,继续作为主节点为客户端进行服务。
测试如下:
1、杀掉主节点的nginx服务
[root@centos17keepalived]#killall nginx
2、在http后端服务器上监控多播地址,此时可以看到主节点的优先级由100降到70,然后从节点的优先级80进行为客户端提供服务,由于有脚本检测主节点返回了backup,就会重新启动nginx,此时主节点就又会将优先级变回100,将从节点的服务抢占回来。
3、在客户端进行查看效果,当主节点的nginx死机后,重启nginx后就会又为客户端进行服务。