nginx+keepalived(双主)

一、环境

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跑到一台机子上,实验成功

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

塞北酒鬼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值