单主模型的IPVS高可用和nginx+keepalived的双主高可用模型

Posted sanqianfanhua01

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了单主模型的IPVS高可用和nginx+keepalived的双主高可用模型相关的知识,希望对你有一定的参考价值。

一、IPVS的单主高可用

(1)配置RS,我们使用两台服务器作为RS服务器配置过程我们可以使用脚本来配置,这样当我们完成试验后便于卸载,脚本如下:

#!/bin/bash
#
vip=192.168.125.198 #你用的虚拟路由的IP地址
case $1 in
start)

echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 1 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_announce
ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip dev lo:0
;;
stop)
ifconfig lo:0 down
echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
;;
*)echo "Usage: $(basename $0) start|stop"
exit 1
;;
esac

接下来我们可以配置keepalived服务器两台服务器配置大多是相通的,只需修改少量配置参数即可[email protected] keepalived]# vim /etc/keepalived/keepalived.conf! Configuration File for keepalived

#全局配置段
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 15
router_id node1
vrrp_mcast_group4 224.0.192.100
}


#配置虚拟IP vrrp_instance VI_1 { state MASTER interface ens33 virtual_router_id 11 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 11111111 } virtual_ipaddress { 192.168.125.198/24 dev ens33 label ens33:0 } track_interface { ens33 } 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 21 priority 98 advert_int 1 authentication { auth_type PASS auth_pass 18181818 } virtual_ipaddress { 192.168.125.199/24 dev ens33 } track_interface { ens33 } notify_master "/etc/keepalived/notify.sh master" notify_backup "/etc/keepalived/notify.sh backup" notify_fault "/etc/keepalived/notify.sh fault" } #定义虚拟服务器 virtual_server 192.168.125.198 80 { delay_loop 2 lb_kind DR lb_algo rr protocol TCP sorry_server 127.0.0.1 80 real_server 192.168.125.156 80 { weight 1 HTTP_GET { url { path / status_code 200 } nb_get_retry 3 delay_before_retry 1 connect_timeout 3 } } real_server 192.168.125.157 80 { weight 2 HTTP_GET { url { path / status_code 200 } nb_get_retry 3 delay_before_retry 1 connect_timeout 3 } } }

最后完成测试即可;

二、keepalived+nginx的高可用集群服务

(1)分析:在做此实验时我们不仅要监控节点我们还需要监控节点上nginx是否可用,这就需要我们调用脚本来监控他们,首先我们做通知脚本示例如下:

#!/bin/bash
#实现通知脚本

contact=‘[email protected]

notify() {
local mailsubject="$(hostname) to be $1, vip floating"
notify() {
local mailsubject="$(hostname) to be $1, vip floating"
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

(2) 脚本调用方法如下:


第一步将脚本放在/etc/keepalived/下并给执行权限
第二步编辑配置文件:vim /etc/keepalived/keepalived.conf
第三步:在vrrp_instance 的上下文编写如下语句:
track_interface {
ens33
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"

(3)接着我们要完成脚本监控nginx服务:

keepalived调用外部辅助脚本进行资源监控,并根据监控的结果状态能实现优先级的动态调整:

  分两步: (1)先定义一个脚本
           (2)调用此脚本

vrrp_script <SCRIPT_NAME> {
      script “”
      interval INT #返回值0或者1
      weight -INT
    }
    track_script {
      SCRIPT _NAME_1
      SCRIPT_NAME_2
      ..... 

    }  

  最后我们在上一个脚本的backup下加一行重启nginx服务的命令就可以起到一定的排错能力,如果是nginx服务听了他可以自动帮我们讲服务重启。

(4)接着我们要配置nginx的反代服务这个是在keepalived的两台服务器上做:

编辑代理服务器nginx的主配置文件:vim /etc/nginx/nginx.conf
在http上下文编辑:
upstream websrvs {
server 192.168.125.156:80 weight=1;
server 192.168.125.157:80 weight=1;
}

记着:nginx -s reload
在server段的location中写下:proxy_pass http://websrvs;

(5)接着编辑反代服务器的keepalived 文件:

! Configuration File for keepalived

global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 15
router_id node1
vrrp_mcast_group4 224.0.192.100
}
vrrp_script chk_nginx {
script "killall -0 nginx"
interval 1
weight -5
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 11
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 11111111
}
virtual_ipaddress {
192.168.125.198/24 dev ens33 label ens33:0
}
track_interface {
ens33
}
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"
}

vrrp_instance VI_2 {
state BACKUP
interface ens33
virtual_router_id 21
priority 98
advert_int 1
authentication {
auth_type PASS
auth_pass 18181818
}
virtual_ipaddress {
192.168.125.199/24 dev ens33
}
track_interface {
ens33
}
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"

}

  

另一个发代节点也这样编辑就可以完成然后启动keepalived服务即可。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  

  

















































以上是关于单主模型的IPVS高可用和nginx+keepalived的双主高可用模型的主要内容,如果未能解决你的问题,请参考以下文章

keepalive单主双主模型的配置

Keepalive 之 keepalive概念介绍

Linux 高可用keepalive

Keepalived 高可用ipvs和nginx服务

Keepalive配置文件说明和实现高可用

keepalive初窥及实现