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

网络拓扑:

lvs高可用集群(双主DR模型)_第1张图片
image

准备工作

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

getenforce 0
iptables -F

对各主机内的配置文件:

对lvs1:

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

global_defs {
   notification_email {
     [email protected]
   }
   notification_email_from [email protected]
   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 {
     [email protected]
   }
   notification_email_from [email protected]
   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:  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:  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

你可能感兴趣的:(lvs高可用集群(双主DR模型))