一、试验拓扑

 

keepalived+nginx构建高可用web_高可用web

二、环境描述

WEB1MASTER):192.168.232.131

WEB2BACKUP):192.168.232.132

VIP 192.168.232.150

三、安装配置

#yum -y install keepalived

web1配置信息如下:

# cat /etc/keepalived/keepalived.conf 

global_defs {

   notification_email {

   admin@localhost.com

   }

   notification_email_from Alexandre.Cassen@firewall.loc

   smtp_server 127.0.0.1 

   smtp_connect_timeout 30

   router_id LVS_DEVEL

}

vrrp_script chk_run {

    script "/root/nginx_check.sh"

    interval 1

    weight -2

}

vrrp_instance VI_1 {

    state MASTER

    interface eth0

    virtual_router_id 55

    priority 101

    advert_int 1

    nopreempt

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.232.150

    }

    track_script {

        chk_run

    }

}



 

web2如下:

# cat /etc/keepalived/keepalived.conf 

 

global_defs {

   notification_email {

        admin@localhost.com

   }

   notification_email_from Alexandre.Cassen@firewall.loc

   smtp_server 127.0.0.1 

   smtp_connect_timeout 30

   router_id LVS_DEVEL

}

 

vrrp_script chk_run {

    script "/root/nginx_check.sh"

    interval 1

    weight -2

}

 

 

vrrp_instance VI_1 {

    state BACKUP

    interface eth0

    virtual_router_id 55

    priority 100

    advert_int 1

    nopreempt

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.232.150

    }

    track_script {

        chk_run

    } 

}


启动keepalived#service keepalived start

看下master的日志

VRRP_Instance(VI_HA) Dropping received VRRP packet...

Terminating on signal

Stopping Keepalived v1.2.1 (01/04,2012)

Terminating VRRP child process on signal

Starting Keepalived v1.2.1 (01/04,2012)

Registering Kernel netlink reflector

Registering Kernel netlink command channel

Registering gratutious ARP shared channel

Opening file '/etc/keepalived/keepalived.conf'.

Configuration is using

Using LinkWatch kernel netlink reflector...

VRRP sockpool

Starting VRRP child process, pid=4997

VRRP_Instance(VI_HA) Transition to MASTER STATE

VRRP_Instance(VI_HA) Entering MASTER STATE

VRRP_Instance(VI_HA) setting protocol VIPs.

VRRP_Instance(VI_HA) Sending gratuitous ARPs on eth0 for 192.168.232.150

VRRP_Group(VGM) Syncing instances to MASTER state

Registering new address record for 192.168.232.150 on eth0.

VRRP_Instance(VI_HA) Sending gratuitous ARPs on eth0 for 192.168.232.150

Keepalived_vrrp: VRRP_Instance(VI_HA) Sending gratuitous ARPs on eth0 for 192.168.232.150

ip信息:

# ip a

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue

link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

inet 127.0.0.1/8 scope host lo

inet6 ::1/128 scope host

valid_lft forever preferred_lft forever

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

link/ether 00:0c:29:6e:b2:0f brd ff:ff:ff:ff:ff:ff

inet 192.168.232.130/24 brd 192.168.152.255 scope global eth0

inet 192.168.232.150/24 scope global secondary eth0

inet6 fe80::20c:29ff:fe6e:b20f/64 scope link

valid_lft forever preferred_lft forever


 

再看backup的日志:

VRRP_Instance(VI_HA) ignoring received advertisment...

Terminating on signal

Stopping Keepalived v1.2.1 (01/05,2012)

Terminating VRRP child process on signal

Starting Keepalived v1.2.1 (01/05,2012)

Registering Kernel netlink reflector

Registering Kernel netlink command channel

Registering gratutious ARP shared channel

Opening file '/etc/keepalived/keepalived.conf'.

Configuration is using

Using LinkWatch kernel netlink reflector...

VRRP_Instance(VI_HA) Entering BACKUP STATE

VRRP sockpool

Starting VRRP child process, pid=10968

ip信息:

# ip a

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue

link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

inet 127.0.0.1/8 scope host lo

inet6 ::1/128 scope host

valid_lft forever preferred_lft forever

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

link/ether 00:0c:29:c6:24:a8 brd ff:ff:ff:ff:ff:ff

inet 192.168.232.131/24 brd 192.168.152.255 scope global eth0

inet6 fe80::20c:29ff:fec6:24a8/64 scope link

valid_lft forever preferred_lft forever

四、验证

首先访问看下是谁提供服务:

 

keepalived+nginx构建高可用web_keepalived+nginx_02


之后我们把masterkeepalived服务关掉,看backup日志就可发现已经切换了

 keepalived+nginx构建高可用web_高可用web_03

但是现在有个问题就是,当web1在启动时会抢占回来,虽然设置了nopreempt不抢占,但是不生效

所以要将web1的state MASTER改成state BACKUP  将2节点都设成BACKUP状态,之后就不会再抢占了