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

nginx+keepalived(双主)

时间:2019-09-21 19:06:45      阅读:80      评论:0      收藏:0      [点我收藏+]

标签:oba   root   star   auth   strong   red   timeout   安装   lin   

一、环境

nginx1   192.168.40.211  

nginx2   192.168.40.132 

vip1     192.168.40.223  主为keep1,从为keep2

vip2     192.168.40.222  主为keep2,从为keep1

(此处安装过程皆采用yum方式,不作累述)

二、keepalived配置文件

nginx1

vi /etc/keepalived/keepalived.conf

global_defs {

    notification_email {

        root@localhost

    }

    notification_email_from keepalived@localhost

    smtp_server 127.0.0.1

    smtp_connect_timeout 30

    route_id LVS_DEVEL

}

vrrp_script_chk_nginx {

    script "/etc/keepalived/ck_ng.sh"

    interval 2

    weight 2

}

#VIP1

vrrp_instance VI_1 {

    state MASTER

    interface ens33

    lvs_sync_daemon_interface ens33

    virtual_router_id 151

    priority 100

    advert_int 5

    nopreempt

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.40.223

    }

    track_script {

        chk_nginx

    }

}

#VIP2

vrrp_instance VI_2 {

    state BACKUP

    interface ens33

    lvs_sync_daemon_interface ens33

    virtual_router_id 152

    priority 90

    advert_int 5

    nopreempt

    authentication {

        auth_type PASS

        auth_pass 2222

    }

    virtual_ipaddress {

        192.168.40.222

    }

    track_script {

        chk_nginx

    }

}


 

nginx2

#vi /etc/keepalived/keepalived.conf

global_defs {

    notification_email {

        root@192.168.40.132

    }

    notification_email_from keepalived@192.168.40.132

    smtp_server 127.0.0.1

    smtp_connect_timeout 30

    route_id LVS_DEVEL

}

vrrp_script_chk_nginx {

    script "/etc/keepalived/ck_ng.sh"

    interval 2

    weight 2

}

#VIP1

vrrp_instance VI_1 {

    state BACKUP

    interface ens33

    lvs_sync_daemon_interface ens33

    virtual_router_id 151

    priority 90

    advert_int 5

    nopreempt

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.40.223

    }

    track_script {

        chk_nginx

    }

}

#VIP2

vrrp_instance VI_2 {

    state MASTER

    interface ens33

    lvs_sync_daemon_interface ens33

    virtual_router_id 152

    priority 100

    advert_int 5

    nopreempt

    authentication {

        auth_type PASS

        auth_pass 2222

    }

    virtual_ipaddress {

        192.168.40.222

    }

    track_script {

        chk_nginx

    }

}

三、nginx检测脚本(两台机子一样)

#vi /etc/keepalived/ck_ng.sh

#!/bin/bash

#检查nginx进程是否存在

counter=$(ps -C nginx --no-heading|wc -l)

if [ "${counter}" = "0" ]; then

#尝试启动一次nginx,停止5秒后再次检测

    service nginx start

    sleep 5

    counter=$(ps -C nginx --no-heading|wc -l)

    if [ "${counter}" = "0" ]; then

#如果启动没成功,就杀掉keepalive触发主备切换

        service keepalived stop

    fi

fi

#chmod +x /etc/keepalived/ck_ng.sh

四、测试

[root@localhost ~]# ip a (nginx1)

....

2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:17:c0:db brd ff:ff:ff:ff:ff:ff

    inet 192.168.40.211/24 brd 192.168.40.255 scope global ens33

       valid_lft forever preferred_lft forever

    inet 192.168.40.223/32 scope global ens33

       valid_lft forever preferred_lft forever

    inet6 fe80::20c:29ff:fe17:c0db/64 scope link

       valid_lft forever preferred_lft forever

[root@localhost ~]# ip a  (nginx2)

....

2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:5e:ba:a3 brd ff:ff:ff:ff:ff:ff

    inet 192.168.40.132/24 brd 192.168.40.255 scope global ens33

       valid_lft forever preferred_lft forever

    inet 192.168.40.222/32 scope global ens33

       valid_lft forever preferred_lft forever

    inet6 fe80::20c:29ff:fe5e:baa3/64 scope link

       valid_lft forever preferred_lft forever

停掉nginx1的keepalived,此时nginx1的vip转移到nginx2,如下

[root@localhost ~]# ip a

....

2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:5e:ba:a3 brd ff:ff:ff:ff:ff:ff

    inet 192.168.40.132/24 brd 192.168.40.255 scope global ens33

       valid_lft forever preferred_lft forever

    inet 192.168.40.222/32 scope global ens33

       valid_lft forever preferred_lft forever

    inet 192.168.40.223/32 scope global ens33

       valid_lft forever preferred_lft forever

    inet6 fe80::20c:29ff:fe5e:baa3/64 scope link

       valid_lft forever preferred_lft forever

两个虚拟IP跑到一台机子上,实验成功

nginx+keepalived(双主)

标签:oba   root   star   auth   strong   red   timeout   安装   lin   

原文地址:https://www.cnblogs.com/zjz20/p/11563885.html

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