LVS+Keepalived高可用群集

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LVS+Keepalived高可用群集相关的知识,希望对你有一定的参考价值。

LVS+Keepalived高可用群集

 

目录

一部分  环境准备

第二部分  使用Keepalived搭建双机热备

三部分  配置Web节点服务器

四部分  测试LVS+Keepalived可用群集

 

 

一部分  环境准备

调度器两台(双机热

    系统LinuxCentOS 7.4

IP址:192.168.80.10主)

IP址:192.168.80.20备)

二:Web服务器两台

    系统LinuxCentOS 7.4

IP址:192.168.80.30SERVER AA

IP址:192.168.80.40SERVER BB

 技术分享图片

三:客户电脑一台:以win7例,测试用

IP址:192.168.80.2

 

 

第二部分  使用Keepalived搭建双机热

一步:配置主调度器

[[email protected] ~]# modprobe ip_vs      //加载ip_vs模块

[[email protected] ~]# rpm -ivh /mnt/Packages/ipvsadm-1.27-7.el7.x86_64.rpm    //安装管理软件ipvsadm

[[email protected] ~]# yum -y install gcc gcc-c++ make popt-devel kernel-devel openssl-devel

//安装编译工具与插件

[[email protected] ~]# tar xzvf keepalived-1.4.2.tar.gz      //解压keepalived安装

[[email protected] ~]# cd keepalived-1.4.2/

[[email protected] keepalived-1.4.2]# ./configure --prefix=/       //配置

[[email protected] keepalived-1.4.2]# make && make install      //编译与安装

[[email protected] keepalived-1.4.2]# cp keepalived/etc/init.d/keepalived /etc/init.d/     

//加入系统管理服务

[[email protected] keepalived-1.4.2]# systemctl enable keepalived     //设置开机自启

[[email protected] keepalived-1.4.2]# vi /etc/keepalived/keepalived.conf    //编辑配置文件

! Configuration File for keepalived

global_defs {

   router_id LVS_01            //服务器的名称

}

vrrp_instance VI_1 {            //定义VRRP热备实例

    state MASTER             //热备状态,MASTER表示主服务器,BACKUP表示从服务器

    interface ens33            //承载VIP址的物理接口

    virtual_router_id 51        //虚拟路由器ID,每个热备组保持

    priority 110               //优先级,数值越大优先越高

    advert_int 1               //通告间隔秒数心跳频率)

    authentication {            //热备认证信息,每个热备组保持一致

        auth_type PASS        //认证类型

        auth_pass 6666        //密码字符串

    }

    virtual_ipaddress {          //指定漂移地址(VIP,可以有多个

        192.168.80.100

    }

}

virtual_server 192.168.80.100 80 {      //虚拟服务器地址(VIP、端口

        delay_loop 6               //健康检查的间隔时间(秒)

        lb_algo rr                  //询(rr)调度算法

        lb_kind DR                 //直接路由(DR)群集工作模式

        persistence_timeout 60       //连接保持时间(秒)

        protocol TCP                //应用服务器采用的是TCP协议

real_server 192.168.80.30 80 {         //一个Web服务器节点的地址、端口

        weight 1                   //节点的权重

        TCP_CHECK {                //健康检查方式

        connect_port 80             //检查的目标端口

        connect_timeout 3           //连接超时(秒)

        nb_get_retry 3              //试次数

        delay_before_retry 3          //试间隔

        }

}

real_server 192.168.80.40 80 {           //二个Web服务器节点的地址、端口

        weight 1

        TCP_CHECK {

             connect_port 80

             connect_timeout 3

             nb_get_retry 3

             delay_before_retry 3

     }

  }

}

保存退出

[[email protected] keepalived-1.4.2]# service keepalived start

[[email protected] keepalived-1.4.2]# ip addr show dev ens33    //验证绑定了虚拟地址

技术分享图片

[[email protected] keepalived-1.4.2]# ipvsadm –L        //查看LVS虚拟服务器

技术分享图片

[[email protected] keepalived-1.4.2]# tail -f /var/log/messages

技术分享图片

 

 

第二步:配置从调度器

[[email protected] ~]# modprobe ip_vs      //加载ip_vs模块

[[email protected] ~]# rpm -ivh /mnt/Packages/ipvsadm-1.27-7.el7.x86_64.rpm    //安装管理软件ipvsadm

[[email protected] ~]# yum -y install gcc gcc-c++ make popt-devel kernel-devel openssl-devel

[[email protected] ~]# tar xzvf keepalived-1.4.2.tar.gz

[[email protected] ~]cd keepalived-1.4.2/

[[email protected] keepalived-1.4.2]# ./configure --prefix=/

[[email protected] keepalived-1.4.2]# make && make install

[[email protected] keepalived-1.4.2]# cp keepalived/etc/init.d/keepalived /etc/init.d/

[[email protected] keepalived-1.4.2]# systemctl enable keepalived

[[email protected] keepalived-1.4.2]# vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {

   router_id LVS_02

}

vrrp_instance VI_1 {

    state BACKUP

    interface ens33

    virtual_router_id 51

    priority 105

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 6666

    }

    virtual_ipaddress {

        192.168.80.100

    }

}

virtual_server 192.168.80.100 80 {

        delay_loop 6

        lb_algo rr

        lb_kind DR

        persistence_timeout 60

        protocol TCP

real_server 192.168.80.30 80 {

        weight 1

        TCP_CHECK {

        connect_port 80

        connect_timeout 3

        nb_get_retry 3

        delay_before_retry 3

        }

}

real_server 192.168.80.40 80 {

        weight 1

        TCP_CHECK {

             connect_port 80

             connect_timeout 3

             nb_get_retry 3

             delay_before_retry 3

     }

  }

}

保存退出

[[email protected] keepalived-1.4.2]# service keepalived start

[[email protected] keepalived-1.4.2]# ipvsadm –L       

技术分享图片

 

三步:验证结果

1. dd01主服务器keepalived服务开启状态,验证dd02从服务器状态

[[email protected] keepalived-1.4.2]# ip addr show dev ens33    

技术分享图片

[[email protected] keepalived-1.4.2]# tail -f /var/log/messages

技术分享图片

技术分享图片

 

//机状态

2. 模拟主服务器故障dd01服务器keepalived服务关闭,验证dd02服务器状态

[[email protected] keepalived-1.4.2]# systemctl stop keepalived

技术分享图片

//自己切换到主机状态

技术分享图片

//热备搭建完成

 

 

第三部分  配置Web节点服务器

一步:配置SERVER AA服务192.168.80.30

1. 配置http服务

//安装http服务

[[email protected] ~]# yum install -y httpd

//编辑主配置文件

[[email protected] ~]# vi /etc/httpd/conf/httpd.conf

ServerName aa       //“#”去掉,修改主机名

保存退出

//配置默认显示网页

[[email protected] ~]# cd /var/www/html/

[[email protected] html]# echo "<h1>SERVER AA</h1>" > index.html

技术分享图片

[[email protected] html]# service httpd start       //重启http服务

//win7验证http服务

访问http://192.168.80.30

技术分享图片

2. 配置DR模式

[[email protected] ~]# vi web.sh

#!/bin/bash

#haha

ifconfig lo:0 192.168.80.100 broadcast 192.168.80.100 netmask 255.255.255.255 up

route add -host 192.168.80.100 dev lo:0

echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore

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

echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore

echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce

sysctl -p &>/dev/null

保存退出

[[email protected] ~]# sh web.sh     //执行脚本

[[email protected] ~]# ifconfig       //查看虚拟接口

技术分享图片

 

二步:配置SERVER BB服务器192.168.80.40

1. 配置http服务

//安装http服务

[[email protected] ~]# yum install -y httpd

//编辑主配置文件

[[email protected] ~]# vi /etc/httpd/conf/httpd.conf

ServerName bb       //“#”去掉,修改主机名

保存退出

//配置默认显示网页

[[email protected] ~]# cd /var/www/html/

[[email protected] html]# echo "<h1>SERVER BB</h1>" > index.html

技术分享图片

[[email protected] html]# service httpd start      //重启http服务

//win7验证http服务

访问http://192.168.80.40

技术分享图片 

----------配置DR模式---------

[[email protected] ~]# vi web.sh

#!/bin/bash

#hehe

ifconfig lo:0 192.168.80.100 broadcast 192.168.80.100 netmask 255.255.255.255 up

route add -host 192.168.80.100 dev lo:0

echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore

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

echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore

echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce

sysctl -p &>/dev/null

保存退出

[[email protected] ~]# sh web.sh      //执行脚本

[[email protected] ~]# ifconfig        //查看接口

技术分享图片

 

 

第四部分  测试LVS+Keepalived可用群集

在客户端的浏览器中,能够通过LVS+Keepalived群集的漂移地址(192.168.80.100)正常访问web页面内容,则验证群集构建成功。

1. 验证两台Web服务器轮询工作

Win7访问http://192.168.80.100

技术分享图片

//由于设置了连接保持时间为60,一分钟后重新访问该地址

 技术分享图片

//自动轮询交给一台Web服务

 

2. 模拟主调度器故障验证结果

[[email protected] keepalived-1.4.2]# systemctl stop keepalived

//主调度器keepalived停止工作

 技术分享图片

//从调度器自动切换继续工作

技术分享图片

 

//win7访问http://192.168.80.100查看结果

 技术分享图片

//分钟后,重新访问http://192.168.80.100查看结果

 技术分享图片

//双机热备已经作用

 

3. 模拟Web服务aa故障

[[email protected] ~]# service httpd stop     //bb服务器停止工作

[[email protected] keepalived-1.4.2]# tail -f /var/log/messages

 技术分享图片

访问http://192.168.80.40

技术分享图片

分钟后,重新访问http://192.168.80.40

 技术分享图片

//法访问,验证成功

[[email protected] ~]# service httpd start    //恢复启动bb服务

//查看调度器日志

 技术分享图片

//成功将bb服务器加入到服务器池

 

//LVS+Keepalived成功搭建测试完成


以上是关于LVS+Keepalived高可用群集的主要内容,如果未能解决你的问题,请参考以下文章

高可用群集HA介绍与LVS+keepalived高可用群集

LVS+Keepalived高可用群集

Centos 7搭建LVS+Keepalived高可用Web服务群集

LVS+keepalived 高可用群集

搭建LVS(DR模式)+Keepalived高可用群集,可跟做!!!

LVS+Keepalived 高可用群集