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 {

    root@node1.localhost.localdomain

   }

   notification_email_from
Alexandre.Cassen@firewall.loc

   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 {

    root@node2.localhost.localdomain

   }

   notification_email_from
Alexandre.Cassen@firewall.loc

   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

相关推荐

  • ftp服务器介绍与配置

    一、 ftp服务,File Transfer Protocal 文件传输协议      1. ftp基于tcp协议,C/S架构          Server: wu-ftpd, proftpd ,pureftp,vsf…

    Linux干货 2015-06-21
  • grep、egrep正则表达式之初窥门径

    何谓正则表达式 正则表达式,又称正规表示法、常规表示法(Regular Expression,在代码中常简写为regex、regexp或RE),是一类字符所书写的模式,其中许多字符不表示其字面意义,而是表达控制或通配等功能。正则表达式使用单个字符串来描述、匹配一系列符合某个句法规则的字符串。在很多文本编辑器里,正则表达式通常被用来检索、替换那些符合某个模式的…

    2015-03-19
  • 第二次作业

    一、只显示某目录下隐藏文件    答案1:#ls -d /tmp/*/    但是此答案是我在同学知道下完成,但是原理没明白 如果单独执行        #ls -d  &nbsp…

    Linux干货 2016-08-03
  • Linux概述

    计算机组成概述 计算机组成原理 如上图计算机的组成结构,CPU运行速度远远高于内存,而内存运行速度又远远高于I/O;由于三者运行速度巨大差异,如果系统仅运行一个任务(单任务系统),那么将有大量的CPU空闲时间等待缓慢的I/O及内存的读取。为提高CPU的使用效率,于是便产生了多任务系统系统的需求。 多任务系统 多任务实现的基础,我们知道计算机处理任务主要靠CP…

    Linux干货 2016-12-02
  • LVM

    LVM是 Logical Volume Manager(逻辑卷管理)的简写,它是Linux环境下对磁盘分区进行管理的一种机制,它由Heinz Mauelshagen在Linux 2.4内核上实现,目前最新版本为:稳定版1.0.5,开发版 1.1.0-rc2,以及LVM2开发版。Linux用户安装Linux操作系统时遇到的一个常见的难以决定的问题就是如何正确地…

    Linux干货 2016-12-17
  • centos6启动故障及修复

    前言 前文中我们熟悉了linux的启动流程,如果在启动过程中文件丢失或者损坏了怎么办呢?难道只有重装系统这一条路吗?那我们不是成了普通网管了吗?作为专业的运维工程师,我们需要掌握如何快速修复系统启动。 实验1: 破坏/boot/grup/下的文件——-stage2 阶段  不影响启动  mv /boot/grub …

    2017-09-03