; 利用keepalived实现nginx七层负载均衡器双主模型高可用集群 | Linux运维部落

利用keepalived实现nginx七层负载均衡器双主模型高可用集群

nginx通过ngx_http_upstream_module可以实现七层http服务的负载均衡,同时还自带RS的健康监测功能,但是也有弊端,nginx负载均衡器如果发生故障则整个集群失效,此文通过keepalived构建nginx负载均衡器的高可用集群。

一、实验环境:

实验需要4台服务器:

nginx负载均衡器1:192.168.11.100

nginx负载均衡器2:192.168.11.101

nginx静态网页服务器1:192.168.11.201

nginx静态网页服务器2:192.168.11.202

拓扑如下:

二、实验步骤:

负载均衡器配置:

  1. 安装nginx和keepalived
    yum install -y nginx keepalived
  2. 修改keepalived配置文件:
    ! Configuration File for keepalived  global_defs {   notification_email {      root@localhost   }   notification_email_from keepalived@localhost   smtp_server 127.0.0.1   smtp_connect_timeout 30   router_id node1   vrrp_mcast_group1 224.0.115.17 #组播ip地址# }  #服务检测脚本1:当/etc/keepalived目录下存在down文件,则权重-5# vrrp_script chk_down {    script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"    interval 1 #每隔1秒检测一次#    weight -5 #权重减5# }  vrrp_script chk_nginx {    script "killall -0 nginx && exit 0 || exit 1" #检测nginx进程是否存在#    interval 1     weight -5    fall 2 #检测两次都不存在则降低权重#    rise 1 #检测一次存在则加权重# }  vrrp_instance VI_1 {    state MASTER    interface ens33    virtual_router_id 1      priority 100    advert_int 1    authentication {      auth_type PASS      auth_pass hello    }    virtual_ipaddress {      192.168.11.250 label ens33:0    }    #检测chk_down和chk_nginx两个脚本#    track_script {      chk_down      chk_nginx    }    notify_master "/etc/keepalived/notify.sh master"    notify_backup "/etc/keepalived/notify.sh backup"    notify_fault "/etc/keepalived/notify.sh fault" }  vrrp_instance VI_2 {    state BACKUP    interface ens33    virtual_router_id 2    priority 96    advert_int 1    authentication {      auth_type PASS      auth_pass world    }    virtual_ipaddress {      192.168.11.251 label ens33:1        }    track_script {      chk_down      chk_nginx    }    #定义触发通知脚本条件#    notify_master "/etc/keepalived/notify.sh master"    notify_backup "/etc/keepalived/notify.sh backup"    notify_fault "/etc/keepalived/notify.sh fault" }
  3. 通知脚本内容如下:
     #!/bin/bash # contact='root@localhost' #定义函数:当主次节点发生变更之后给root@localhost发邮件通知# notify() {      local mailsubject="`hostname` to be $1,vip floating"      local mailbody="`data +"%F %T"`:vrrp transition,`hostname` changed to be $1"      echo "$mailbody" | mail -s "$mailsubject" $contact }  case $1 in master)      notify master      ;; backup)      notify backup      ;; fault)      notify fault      ;; *)      echo "Usage:$(basename $0) {master|backup|fault}"      exit 1      ;; esac

     

     

原创文章,作者:王子豪,如若转载,请注明出处:/79340

发表评论

电子邮件地址不会被公开。 必填项已用*标注

联系我们

400-080-6560

在线咨询:点击这里给我发消息

邮件:1660809109@qq.com

工作时间:周一至周五,9:30-18:30,节假日同时也值班

友情链接:万达娱乐登录  guoqibee.com  万达直属QQ  万达娱乐开户  万达娱乐注册  万达娱乐招商QQ  万达注册  万达娱乐直属QQ  万达开户  华宇招商