码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-05-26 21:20:33      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:127.0.0.1   proc   basename   netmask   out   还需要   监控   权限   ase   

一、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服务器两台服务器配置大多是相通的,只需修改少量配置参数即可root@node2 keepalived]# vim /etc/keepalived/keepalived.conf! Configuration File for keepalived

#全局配置段
global_defs {
notification_email {
root@localhost
}
notification_email_from xiaoming@localhost
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=‘root@localhost‘

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 {
root@localhost
}
notification_email_from xiaoming@localhost
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的双主高可用模型

标签:127.0.0.1   proc   basename   netmask   out   还需要   监控   权限   ase   

原文地址:https://www.cnblogs.com/sanqianfanhua01/p/9094112.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!