nginx + keepalived (double main)

First, the environment

Nx1 192.168.40.211  

nginx2 192.168.40.132 

vip1 192.168.40.223 Lord keep1, is from keep2

vip2 192.168.40.222 Lord keep2, is from keep1

(Here, the installation mode yum are employed, is not tired)

Two, keepalived profile

Nx1

we /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 {

        [email protected]

    }

    notification_email_from [email protected]

    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

    }

}

Three, nginx detection script (two loom as)

#vi /etc/keepalived/ck_ng.sh

#!/bin/bash

# Check nginx process exists

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

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

# After trying to start a nginx, stopping five seconds is detected again

    service nginx start

    sleep 5

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

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

# If the startup is not successful, kill keepalive trigger switchover

        service keepalived stop

    be

be

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

Fourth, the test

[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

The stopped nginx1 keepalived, nginx1 at this time is transferred to the vip nginx2, as follows

[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

Two virtual IP to go on one of these machines were successfully

Guess you like

Origin www.cnblogs.com/zjz20/p/11563885.html