首页 > 代码库 > 32-4 keepalived(nginx架构)

32-4 keepalived(nginx架构)

配置环境:

技术分享

1、停止keepalived

[root@node1 keepalived]# systemctl stop keepalived.service

[root@node2 keepalived]# systemctl stop keepalived.service

[root@rs1 ~]# bash setka.sh stop

[root@rs2 ~]# bash setka.sh stop


2、配置nginx


[root@node1 ~]# yum install nginx

[root@node2 ~]# yum install nginx

[root@node1 ~]# cd /etc/nginx/

[root@node1 nginx]# vim nginx.conf

include /etc/nginx/conf.d/*.conf;和server { 之间(约33行后)

添加

    upstream websrvs {

        server  192.168.1.62:80 weight=1;

        server  192.168.1.72:80 weight=1;

    }   


修改

        location / {

        }

        location / { 

            proxy_pass http://websrvs/;

        }   


[root@node1 nginx]# systemctl start nginx.service

[root@node1 nginx]# scp nginx.conf 192.168.1.74:/etc/nginx/

[root@node2 nginx]# systemctl start nginx.service


3、配置keepalived

[root@node1 keepalived]# vim keepalived.conf (两个结点作同样配置)

1)在vrrp_instance VI_1(约19行)前添加如下内容

vrrp_script chk_nginx {

script "killall -0 nginx &> /dev/null"

interval 1

weight -10 

}


2)修改

track_script {

chk_mt

}   

    track_script {

        chk_nginx

    }

3)删除virtual_server段定义


[root@node1 keepalived]# systemctl start keepalived.service 

[root@node2 keepalived]# systemctl start keepalived.service 


4、测试keepalived

(1)关闭node1上的nginx服务

[root@node1 keepalived]# systemctl stop nginx


结果:


9月 04 08:40:32 node1 Keepalived_vrrp[49560]: VRRP_Script(chk_nginx) failed

9月 04 08:40:34 node1 Keepalived_vrrp[49560]: VRRP_Instance(VI_1) Received higher prio advert

9月 04 08:40:34 node1 Keepalived_vrrp[49560]: VRRP_Instance(VI_1) Entering BACKUP STATE

9月 04 08:40:34 node1 Keepalived_vrrp[49560]: VRRP_Instance(VI_1) removing protocol VIPs.

9月 04 08:40:34 node1 Keepalived_healthcheckers[49559]: Netlink reflector reports IP 192.168.1.80 removed

9月 04 08:40:34 node1 Keepalived_vrrp[49560]: Opening script file /etc/keepalived/notify.sh


脚本检测失败,node1节点从MASTER变成BACKUP,VIP节点被移除


附件文件

一、NODE1节点

keepalived.conf


! Configuration File for keepalived
global_defs {
   notification_email {
     root@localhost
   }
   notification_email_from kaadmin@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_mcast_group4 224.0.0.18
}
vrrp_script chk_mt {
script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
inerval 1
weight -2
}
vrrp_script chk_nginx {
script "killall -0 nginx &> /dev/null"
interval 1
weight -10
}
vrrp_instance VI_1 {
    state MASTER
    interface eno16777736
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass aaf0d876679a
    }
    virtual_ipaddress {
        192.168.1.80/16 dev eno16777736 label eno16777736:1
    }
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_script chk_nginx {
script "killall -0 nginx &> /dev/null"
interval 1
weight -10
}
vrrp_instance VI_2 {
    state BACKUP
    interface eno16777736
    virtual_router_id 133
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 5a806846279a
    }
    virtual_ipaddress {
        192.168.1.180/16 dev eno16777736 label eno16777736:2
    }
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"
}



notify.sh

#!/bin/bash
# Author: MageEdu <linuxedu@foxmail.com>
# description: An example of notify script
# 
vip=192.168.1.80
contact=‘root@localhost‘
notify() {
    mailsubject="`hostname` to be $1: $vip floating"
    mailbody="`date ‘+%F %H:%M:%S‘`: vrrp transition, `hostname` changed to be $1"
    echo $mailbody | mail -s "$mailsubject" $contact
}
case "$1" in
    master)
        notify master
#systemctl restart nginx.serivce
        exit 0
    ;;
    backup)
        notify backup
#systemctl restart nginx.serivce
        exit 0
    ;;
    fault)
        notify fault
        exit 0
    ;;
    *)
        echo ‘Usage: `basename $0` {master|backup|fault}‘
        exit 1
    ;;
esac


二、Node2

keepalived.conf


! Configuration File for keepalived
global_defs {
   notification_email {
     root@localhost
   }
   notification_email_from kaadmin@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_script chk_mt {
script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
inerval 1
weight -2
}
vrrp_script chk_nginx {
    script "killall -0 nginx &> /dev/null"
    interval 1
    weight -10 
}
vrrp_instance VI_1 {
    state BACKUP
    interface eno16777736
    virtual_router_id 51
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass aaf0d876679a
    }
    virtual_ipaddress {
        192.168.1.80/16 dev eno16777736 label eno16777736:1
    }
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 MASTER
    interface eno16777736
    virtual_router_id 133
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 5a806846279a
    }
    virtual_ipaddress {
        192.168.1.180/16 dev eno16777736 label eno16777736:2
    }
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"
}

notify.sh

#!/bin/bash
# Author: MageEdu <linuxedu@foxmail.com>
# description: An example of notify script
# 
vip=192.168.1.80
contact=‘root@localhost‘
notify() {
    mailsubject="`hostname` to be $1: $vip floating"
    mailbody="`date ‘+%F %H:%M:%S‘`: vrrp transition, `hostname` changed to be $1"
    echo $mailbody | mail -s "$mailsubject" $contact
}
case "$1" in
    master)
        notify master
#systemctl restart nginx.serivce
        exit 0
    ;;
    backup)
        notify backup
#systemctl restart nginx.serivce
        exit 0
    ;;
    fault)
        notify fault
        exit 0
    ;;
    *)
        echo ‘Usage: `basename $0` {master|backup|fault}‘
        exit 1
    ;;
esac


本文出自 “追梦” 博客,请务必保留此出处http://sihua.blog.51cto.com/377227/1846065

32-4 keepalived(nginx架构)