keepalived + lvs_DR 实现lvs高可用

870阅读 0评论2015-10-11 TT小饭
分类:系统运维

Lvs主:10.53.80.63

Lvs备:10.53.80.66

RealserverA10.53.80.67

RealserverB10.53.80.68

Vip10.53.80.70

 

配置Super63

配置IP

DIP    eth0     10.53.80.63  

VIP    eth0:1    10.53.80.70

网关指向

10.53.80.1

生成配置文件ifcfg

 

配置LVS-DR规则

rpm -ivh /mnt/Packages/ipvsadm-1.26-2.el6.x86_64.rpm

ipvsadm -A -t 10.53.80.70:80 -s rr

ipvsadm -a -t 10.53.80.70:80 -r 10.53.80.67 -g

ipvsadm -a -t 10.53.80.70:80 -r 10.53.80.68 -g

ipvsadm -L -n

 

 

配置super67

配置IP

etho      10.53.80.67 

lo1     10.53.80.70    255.255.255.255

网关指向192.168.1.1

生成配置文件ifcfg

 

 

关闭ARP转发   #使机器只相应eth0

[root@super67~]#echo 1 > /proc/sys/net/ipv4/conf/eth0/arp_ignore

[root@super67 ~]#echo 2 > /proc/sys/net/ipv4/conf/eth0/arp_announce

[root@super67~]#echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore

[root@super67 ~]#echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce

 

写入配置文件

[root@super67 network-scripts]# vim /etc/sysctl.conf

#尾部追加

net.ipv4.conf.eth0.arp_ignore = 1

net.ipv4.conf.eth0.arp_announce = 2  

[root@super67 network-scripts]# sysctl -p

http服务器主页上写入内容便于测试

[root@super67 ~# echo "192.168.1.65" > /var/www/html/index.html                 

 

配置super68     #方法与super67原理一样

 

 

部署安装keepalived

 

Super63

tar zxvf keepalived-1.2.16.tar.gz

cd keepalived-1.2.16

./configure --prefix=/usr/local/keepalived

make

make install

 

cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived

chmod +x /etc/init.d/keepalived

 

修改启动脚本:

vim /etc/init.d/keepalived

#. /etc/sysconfig/keepalived

. /usr/local/keepalived/etc/sysconfig/keepalived

export PATH="$PATH:/usr/local/keepalived/sbin"

 

修改配置文件:

vim /usr/local/keepalived/etc/sysconfig/keepalived

KEEPALIVED_OPTIONS="-D -f /usr/local/keepalived/etc/keepalived/keepalived.conf"

 

启动:

/etc/init.d/keepalived start

chkconfig --add keepalived

chkconfig keepalived on

 

配置super63为主节点:

[root@super63 ~]# vim /usr/local/keepalived/etc/keepalived/keepalived.conf

! Configuration File for keepalived

 

global_defs {

   notification_email {

 

        root@localhost

}

   notification_email_from root@localhost

   smtp_server localhost

   smtp_connect_timeout 30

   router_id super63

}

 

vrrp_instance apache {

    state MASTER

    interface eth0

    virtual_router_id 51

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        10.53.80.70

}

}

virtual_server 10.53.80.70 80 {

    delay_loop 6

    lb_algo rr

    lb_kind DR

#    persistence_timeout 50

    protocol TCP

 

    sorry_server 192.168.200.200 1358

 

    real_server 10.53.80.67 80 {

        weight 1

#节点之间检查,有三种方法,TCP_CHECK是其中一种

       TCP_CHECK {

                     weight 1

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

            connect_port 80

        }

    }

    real_server 10.53.80.68 80 {

        weight 1

       TCP_CHECK {

                   weight 1

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

            connect_port 80

        }

    }

}

 

Super66方法类似此次不在重复:

安装keepalived

可以用拷贝配置文件的方式从主节点复制文件。

注意配置文件的部分不同:

优先级、备节点

启动服务:

开机启动:

查看vip
主:

[root@super63 ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.53.80.70:80 rr
  -> 10.53.80.67:80               Route   1      0          0         
  -> 10.53.80.68:80               Route   1      0          0         
[root@super63 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    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 state UNKNOWN qlen 1000
    link/ether 00:0c:29:ba:6b:85 brd ff:ff:ff:ff:ff:ff
    inet 10.53.80.63/20 brd 10.53.95.255 scope global eth0
    inet 10.53.80.70/32 scope global eth0
    inet 10.53.80.70/20 brd 10.53.95.255 scope global secondary eth0:0
    inet6 fe80::20c:29ff:feba:6b85/64 scope link tentative dadfailed 
       valid_lft forever preferred_lft forever

备:
[root@super66 ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.53.80.70:80 rr
  -> 10.53.80.67:80               Route   1      0          0         
  -> 10.53.80.68:80               Route   1      0          0         
[root@super66 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    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 state UP qlen 1000
    link/ether 00:0c:29:3e:34:32 brd ff:ff:ff:ff:ff:ff
    inet 10.53.80.66/20 brd 10.53.95.255 scope global eth0
    inet6 fe80::20c:29ff:fe3e:3432/64 scope link tentative dadfailed 
       valid_lft forever preferred_lft forever

测试:

 

[root@super65 ~]# curl 10.53.80.70

67

[root@super65 ~]# curl 10.53.80.70

68

[root@super65 ~]# curl 10.53.80.70

67

[root@super65 ~]# curl 10.53.80.70

68

 

 

模拟主节点故障

[root@super63 ~]# /etc/init.d/keepalived stop

 

查看vip转移情况:

主:
[root@super66 ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.53.80.70:80 rr
  -> 10.53.80.67:80               Route   1      0          0         
  -> 10.53.80.68:80               Route   1      0          0         
[root@super66 ~]# 
[root@super66 ~]# 
[root@super66 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    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 state UP qlen 1000
    link/ether 00:0c:29:3e:34:32 brd ff:ff:ff:ff:ff:ff
    inet 10.53.80.66/20 brd 10.53.95.255 scope global eth0
    inet 10.53.80.70/32 scope global eth0
    inet6 fe80::20c:29ff:fe3e:3432/64 scope link tentative dadfailed 
       valid_lft forever preferred_lft forever

备:
[root@super63 ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
[root@super63 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    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 state UNKNOWN qlen 1000
    link/ether 00:0c:29:ba:6b:85 brd ff:ff:ff:ff:ff:ff
    inet 10.53.80.63/20 brd 10.53.95.255 scope global eth0
    inet 10.53.80.70/20 brd 10.53.95.255 scope global secondary eth0:0
    inet6 fe80::20c:29ff:feba:6b85/64 scope link tentative dadfailed 
       valid_lft forever preferred_lft forever

查看日志:

[root@super66 ~]# tail  /var/log/messages
Oct 11 13:46:33 super66 ntpd[2125]: Deleting interface #12 eth0, 10.53.80.70#123, interface stats: received=0, sent=0, dropped=0, active_time=7672 secs
Oct 11 13:46:33 super66 ntpd[2125]: peers refreshed
Oct 11 16:26:15 super66 Keepalived_vrrp[21014]: VRRP_Instance(apache) Transition to MASTER STATE
Oct 11 16:26:16 super66 Keepalived_vrrp[21014]: VRRP_Instance(apache) Entering MASTER STATE
Oct 11 16:26:16 super66 Keepalived_vrrp[21014]: VRRP_Instance(apache) setting protocol VIPs.
Oct 11 16:26:16 super66 Keepalived_vrrp[21014]: VRRP_Instance(apache) Sending gratuitous ARPs on eth0 for 10.53.80.70
Oct 11 16:26:16 super66 Keepalived_healthcheckers[21013]: Netlink reflector reports IP 10.53.80.70 added
Oct 11 16:26:17 super66 ntpd[2125]: Listen normally on 13 eth0 10.53.80.70 UDP 123
Oct 11 16:26:17 super66 ntpd[2125]: peers refreshed
Oct 11 16:26:21 super66 Keepalived_vrrp[21014]: VRRP_Instance(apache) Sending gratuitous ARPs on eth0 for 10.53.80.70


测试

 

[root@super65 ~]# curl 10.53.80.70

68

[root@super65 ~]# curl 10.53.80.70

67

[root@super65 ~]# curl 10.53.80.70

68

[root@super65 ~]# curl 10.53.80.70

67


上一篇:梦想
下一篇:python基础