Keepalives+lvs实现高可用httpd的双主模型

基础环境Centos 7.3 最小化安装四台 关闭防火墙及selinux

简单原理:

keepalived是以VRRP协议为实现基础的,VRRP全称Virtual
Router Redundancy Protocol
,即虚拟路由冗余协议。

虚拟路由冗余协议,可以认为是实现路由器高可用的协议,即将N台提供相同功能的路由器组成一个路由器组,这个组里面有一个master和多个backupmaster上面有一个对外提供服务的vip(该路由器所在局域网内其他机器的默认路由为该vip),master会发组播,当backup收不到vrrp包时就认为master宕掉了,这时就需要根据VRRP的优先级来选举一个backupmaster。这样的话就可以保证路由器的高可用了。

LVSLinux Virtual
Server
的简写,意即linux虚拟服务器,是一个虚拟的服务器集群系统。

通过keepalives+lvs实现负载均衡及高可用。

Keepalives+lvs实现高可用httpd的双主模型

 

主机名

IP

提供的服务

客户机CIP

172.10.32.0

测试服务使用

lvs-1

172.10.32.1

主负载均衡器

lvs-2

172.10.32.2

备用负载均衡器

http-1

172.10.32.3

群集web1

http-2

172.10.32.4

群集web2

实验模型为:lvs-DR模型,对外提供的虚拟IP172.10.32.200    LVS-1为主,LVS_2为备

实验模型为:lvs-DR模型,对外提供的虚拟IP172.10.32.201    LVS-1为备,LVS_2为主

 

http组:
#yum install net-tools httpd –y

#echo $ip > /var/www/html/index.html           //$ipIP地址,如httpd-1172.10.32.3

#systemctl start httpd.service

#curl $ip   //查看对应web服务是不是正常访问

#vim web.sh

#!/bin/bash

#

vip=172.10.32.200

vic=172.10.32.201

mask=’255.255.255.255′

 

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/all/arp_announce

    echo
2 > /proc/sys/net/ipv4/conf/lo/arp_announce

    ifconfig
lo:0 $vip netmask $mask broadcast $vip up

    route
add -host $vip dev lo:0

    ifconfig
lo:1 $vic netmask $mask broadcast $vic up

    route
add -host $vic dev lo:1

    ;;

stop)

    ifconfig
lo:0 down

    ifconfig
lo:1 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/all/arp_announce

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

    ;;

*)

    echo
“Usage $(basename $0) start|stop”

    exit
1

    ;;

esac

#bash bash start

#ifconfig //查看对外虚拟ip绑定在lo网卡上

 

Lvs组:

#yum install ipvsadm.x86_64
keepalived.x86_64 httpd.x86_64 –y

#systemctl start httpd.server

Lvs-1#vi /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 abd

   vrrp_mcast_group4 224.0.100.71

}

 

vrrp_instance web1 {

    state MASTER

    interface ens33

    virtual_router_id 88

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111ACSD

    }

    virtual_ipaddress {

    172.10.32.200
dev ens33 label ens33:0

    }

}

 

virtual_server
172.10.32.200  80 {

    delay_loop 6

    lb_algo rr

    lb_kind DR

    protocol TCP

 

    sorry_server
127.0.0.1 80

 

real_server 172.10.32.3 80 {

        weight 10

        HTTP_GET {

            url {

              path /

        status_code
200

            }

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

        }

    }

real_server 172.10.32.4 80 {

        weight 10

        HTTP_GET {

            url {

              path /

                status_code 200

            }

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

        }

    }

}

 

vrrp_instance web2 {

    state BACKUP

    interface ens33

    virtual_router_id 89

    priority 98

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass ANXLazsw

    }

    virtual_ipaddress {

        172.10.32.201 dev ens33 label ens33:1

    }

}

 

virtual_server
172.10.32.201  80 {

    delay_loop 6

    lb_algo rr

    lb_kind DR

    protocol TCP

       

        sorry_server 127.0.0.1 80

 

real_server 172.10.32.3 80 {

        weight 10

        HTTP_GET {

            url {

              path /

                status_code 200

            }

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

        }

    }

real_server 172.10.32.4 80 {

        weight 10

        HTTP_GET {

            url {

              path /

                status_code 200

            }

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

        }

    }

}

 

Lvs-2# vi /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 abd

   vrrp_mcast_group4 224.0.100.71

}

 

vrrp_instance web1 {

    state BACKUP

    interface ens32

    virtual_router_id 88

    priority 98

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111ACSD

    }

    virtual_ipaddress {

    172.10.32.200
dev ens32 label ens32:0

    }

}

 

 

virtual_server
172.10.32.200  80 {

    delay_loop 6

    lb_algo rr

    lb_kind DR

    protocol TCP

 

    sorry_server
127.0.0.1 80

 

real_server 172.10.32.3 80 {

        weight 10

        HTTP_GET {

            url {

              path /

        status_code
200

            }

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

        }

    }

real_server 172.10.32.4 80 {

        weight 10

        HTTP_GET {

            url {

              path /

                status_code 200

            }

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

        }

    }

 

}

 

vrrp_instance web2 {

    state MASTER

    interface ens32

    virtual_router_id 89

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass ANXLazsw

    }

    virtual_ipaddress {

        172.10.32.201 dev ens32 label ens32:1

    }

}

 

virtual_server
172.10.32.201  80 {

    delay_loop 6

    lb_algo rr

    lb_kind DR

    protocol TCP

       

        sorry_server 127.0.0.1 80

 

real_server 172.10.32.3 80 {

        weight 10

        HTTP_GET {

            url {

              path /

                status_code 200

            }

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

        }

    }

real_server 172.10.32.4 80 {

        weight 10

        HTTP_GET {

            url {

              path /

                status_code 200

            }

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

        }

    }

}

 

提示:两个配置文件相差不大,但需要注意网卡名、主备模式、虚拟路由的id

 

Lvs

#systemctl start
keepalived.service

CIP进行测试

# for i in {1..14} ; do curl
172.10.32.201 ;done

# for i in {1..14} ; do curl
172.10.32.200 ;done

查看结果是不是为web服务器组ip并轮询

 

关闭web服务器组中一个web服务器,并运用上面命令测试,查看结果是不是显示未关闭服务器ip地址

 

关闭lvs服务器组,并运用上面命令测试,查看结果是不是显示为当前绑定对外虚拟ip的服务器ip

 

在关闭lvs服务器组中的一个,测试,查看结果是不是为当前绑定对外虚拟ip的服务器ip

 

然后重启web服务器进行测试。

 

 

 

原创文章,作者:chenbin,如若转载,请注明出处:http://www.178linux.com/75799

(1)
chenbinchenbin
上一篇 2017-05-15 19:27
下一篇 2017-05-15 20:08

相关推荐

  • shell中的if else语句与文件查找find浅析

    shell中的if else语句与文件查找find浅析    上篇文章中我们讲述了shell脚本编程的初步入门,其中讲到了shell编程中的顺序执行,顺序执行时一种简单的小脚本,如果在编辑脚本的时候遇到要做出条件判断执行的时候要怎么办呢?我们学习过if之后你会发现这会很简单。if 语句通过关系运算符判断表达式的真假来决定执行哪个分支。 S…

    Linux干货 2016-08-16
  • 马哥教育网络班21期+第8周课程练习

    1、请描述网桥、集线器、二层交换机、三层交换机、路由器的功能、使用场景与区别。 集线器(HUB)是在OSI模型的第一层——物理层——连接多台主机、延长网络的设备。其主要功能是将从一个端口接收到的数据包转发给所有端口。中继器(Repeater)是将由电缆传过来的电信号或光信号调整波形和放大再传给另一个电缆的设备,提供多端口服务的中继器称作集线器。二者的主要区别…

    Linux干货 2016-09-05
  • 第一周初入Linux世界 —马哥教育网络班N22_第一周课程练习

    一、描述计算机的组成及其功能        计算机体系结构以图灵机理论为基础,属于冯·诺依曼体系结构。    计算机由运算器、控制器、寄存器、输入输出设备组成            运算器:是计算机中执行各种算术和逻辑运算操作的部件。运算器…

    Linux干货 2016-08-15
  • 网络配置——CentOS7

    CentOS 7 网络属性配置 使用传统命名方式: (1) 编辑/etc/default/grub 配置文件 GRUB_CMDLINE_LINUX ="rhgb quiet net.ifnames=0" 或:修改/boot/grub2/grub.cfg (2) 为grub2 生成其配置文件 grub2-mkconfig -o /etc/g…

    Linux干货 2016-09-11
  • 没有自动ip解决办法

    用虚拟机下载好centos6.9后,本来想查看ip地址,结果发现没有自动获取ip地址,在网上找了很多方法都不行,问题如下: 用命令ifconfig查看ip地址,发现没有,如下 查看eth0如下 解决办法如下: 1、关闭NetworkManager服务 2、关闭NetworkManager开机启动 3、添加 /etc/sysconfig/network-scr…

    2017-07-16
  • Linux软件包管理

    一、rpm包管理工具 1、rpm包简介 rpm是redhat的软件包管理器。rpm包管理器全称rpm: Redhat Package Manager RPM  Package Manager。它的软件包后缀也为.rpm。除此外,debian,deb文件有dpkg包管理器。 rpm包的命名方式: 源代码:name-version.tar.{gz|bz…

    Linux干货 2016-08-21