lvs高可用集群(双主DR模型)

网络拓扑:

image
image

准备工作

禁用selinux以及清空防火墙规则

getenforce 0
iptables -F

对各主机内的配置文件:

对lvs1:

vim /etc/keepalived/keepalived.conf
 Configuration File for keepalived

global_defs {
   notification_email {
     jevon@danran.com
   }
   notification_email_from ka_admin@danran.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id keepaliveA
   vrrp_mcast_group4 224.103.13.13

                             }
vrrp_instance VI_A {
    state MASTER
    interface ens38
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass qr8hQHuL
    }
    virtual_ipaddress {
       172.16.0.13/32
    }

                              }
    }
    vrrp_instance VI_B {
      state BACKUP
    interface ens38
    virtual_router_id 52
    priority 95
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass qr8hQH99
    }
    virtual_ipaddress {
       172.16.13.13/32
    }
}


virtual_server 172.16.0.13 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    protocol TCP
   sorry_server 127.0.0.1 80

    real_server 172.16.252.59 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 1
        }
}
real_server 172.16.251.240 80 {
    weight 1
    HTTP_GET {
        url {
          path /
          status_code 200
        }
        connect_timeout 3
        nb_get_retry 3
        delay_before_retry 1
    }
}

}

对lvs2:

     Configuration File for keepalived

global_defs {
   notification_email {
     jevon@danran.com
   }
   notification_email_from ka_admin@danran.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id keepaliveA
   vrrp_mcast_group4 224.103.13.13

}
}
vrrp_instance VI_A {
    state BACKUP
    interface ens37
    virtual_router_id 51
    priority 95
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass qr8hQHuL
    }
    virtual_ipaddress {
       172.16.0.13/32
    }
                              }
    }
    vrrp_instance VI_B {
        state BACKUP
        interface ens37
        virtual_router_id 52
       priority 95
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass qr8hQH99
    }
    virtual_ipaddress {
       172.16.13.13/32
    }
}


virtual_server 172.16.0.13 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    protocol TCP
   sorry_server 127.0.0.1 80

    real_server 172.16.252.59 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 1
        }
    }
    real_server 172.16.251.240 80 {
        weight 1
        HTTP_GET {
               url {
          path /
          status_code 200
        }
        connect_timeout 3
        nb_get_retry 3
        delay_before_retry 1
    }
}
}

对后端2个httpd主机修改内科参数并启动httpd服务

脚本内容

#!/bin/bash


vip=172.16.0.13
mask=255.255.255.255
iface="lo:0"

case $1 in
start)
        echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
        echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
        echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
        echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce

        ifconfig $iface $vip netmask $mask broadcast $vip up
        route add -host $vip dev $iface
        ;;
stop)

        ifconfig $iface down

        echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
        echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
        echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
        echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce

测试效果:


只启动lvs1时,得到2个虚拟ip
ens38: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:06:5b:13 brd ff:ff:ff:ff:ff:ff
inet 172.16.250.217/16 brd 172.16.255.255 scope global dynamic ens38
   valid_lft 81513sec preferred_lft 81513sec
inet 172.16.0.13/32 scope global ens38
   valid_lft forever preferred_lft forever
inet 172.16.13.13/32 scope global ens38
   valid_lft forever preferred_lft forever
inet6 fe80::d5bb:4675:529c:d8f2/64 scope link 
   valid_lft forever preferred_lft forever

启动了lvs2之后,有一个ip被抢走(针对与lvs1)
实现了双ip的转移
ens38: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:06:5b:13 brd ff:ff:ff:ff:ff:ff
inet 172.16.250.217/16 brd 172.16.255.255 scope global dynamic ens38
   valid_lft 81379sec preferred_lft 81379sec
inet 172.16.0.13/32 scope global ens38
   valid_lft forever preferred_lft forever
inet6 fe80::d5bb:4675:529c:d8f2/64 scope link 
   valid_lft forever preferred_lft forever

效果:
[root@lkbq ~]# for i in {1..10};do curl 172.16.0.13;done
22222222222222222222222222222
1111111111111111111111111
22222222222222222222222222222
1111111111111111111111111
22222222222222222222222222222
1111111111111111111111111
22222222222222222222222222222
1111111111111111111111111
22222222222222222222222222222
1111111111111111111111111
[root@lkbq ~]# for i in {1..10};do curl 172.16.13.13;done
22222222222222222222222222222
1111111111111111111111111
22222222222222222222222222222
1111111111111111111111111
22222222222222222222222222222
1111111111111111111111111
22222222222222222222222222222
1111111111111111111111111
22222222222222222222222222222
1111111111111111111111111

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 75,703评论 12 117
  • 一、项目目标 搭建一个高可用web集群网站 二、项目规划 2.1 ip地址规划 2.2 拓扑图 2.3 相关说明 ...
    夏日之光阅读 1,410评论 0 1
  • 转自陈明乾的博客,可能有一定更新。 转原文声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、...
    C86guli阅读 514评论 1 6
  • 天下没我不散的宴席,有相逢,就有离别。可离别时,我们都说了。有时间就聚聚,聊聊各自的最近,说说心里的疾苦,感情...
    似冰的梦阅读 136评论 2 1
  • 世间有不少的人为了家庭抛弃朋友,至少也会在家庭和朋友之间划一个界限,把家庭看得比朋友重过若干倍。这似乎是很自然的事...
    諪諪_0c2f阅读 47评论 0 0