CentOS7.4—构建LVS+Keepalived高可用群集
LVS+Keepalived高可用群集目录
第一部分环境准备
第二部分使用Keepalived搭建双机热备
第三部分配置Web节点服务器
第四部分测试LVS+Keepalived高可用群集
第一部分环境准备
一:调度器两台(双机热备)
系统:Linux—CentOS 7.4
IP地址:192.168.80.10(主)
IP地址:192.168.80.20(备)
二:Web服务器两台
系统:Linux—CentOS 7.4
IP地址:192.168.80.30(SERVER AA)
IP地址:192.168.80.40(SERVER BB)
http://i2.运维网.com/images/blog/201804/09/6a6644d6bce1cdb5b134b8807a99887f.png
三:客户端电脑一台:以win7为例,测试用
IP地址:192.168.80.2
第二部分使用Keepalived搭建双机热备
第一步:配置主调度器
# modprobe ip_vs //加载ip_vs模块
# rpm -ivh /mnt/Packages/ipvsadm-1.27-7.el7.x86_64.rpm //安装管理软件ipvsadm
# yum -y install gcc gcc-c++ make popt-devel kernel-devel openssl-devel
//安装编译工具与插件
# tar xzvf keepalived-1.4.2.tar.gz //解压keepalived安装包
# cd keepalived-1.4.2/
# ./configure --prefix=/ //配置
# make && make install //编译与安装
# cp keepalived/etc/init.d/keepalived /etc/init.d/
//加入系统管理服务
# systemctl enable keepalived //设置开机自启动
# 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
}
}
}
保存退出
# service keepalived start
# ip addr show dev ens33 //验证绑定了的虚拟地址
http://i2.运维网.com/images/blog/201804/09/762b9f7a4f8697c6310e4bf6aed1bba7.png
# ipvsadm –L //查看LVS虚拟服务器
http://i2.运维网.com/images/blog/201804/09/fa4ade30bd748d5694fcd6dae3e82c9c.png
# tail -f /var/log/messages
http://i2.运维网.com/images/blog/201804/09/eb39127e4d7d082acb1f30b0d8b0a50d.png
第二步:配置从调度器
# modprobe ip_vs //加载ip_vs模块
# rpm -ivh /mnt/Packages/ipvsadm-1.27-7.el7.x86_64.rpm //安装管理软件ipvsadm
# yum -y install gcc gcc-c++ make popt-devel kernel-devel openssl-devel
# tar xzvf keepalived-1.4.2.tar.gz
cd keepalived-1.4.2/
# ./configure --prefix=/
# make && make install
# cp keepalived/etc/init.d/keepalived /etc/init.d/
# systemctl enable keepalived
# 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
}
}
}
保存退出
# service keepalived start
# ipvsadm –L
http://i2.运维网.com/images/blog/201804/09/ba42c11dd8f1a315a42c99c4dc6da8e9.png
第三步:验证结果
[*]dd01主服务器keepalived服务开启状态,验证dd02从服务器状态
# ip addr show dev ens33
http://i2.运维网.com/images/blog/201804/09/385a2db859257489816ea029a991cbec.png
# tail -f /var/log/messages
http://i2.运维网.com/images/blog/201804/09/5df3c974d59d40d301266cb4c9d64575.png
//备机状态
[*]模拟主服务器故障,将dd01主服务器keepalived服务关闭,验证dd02从服务器状态
# systemctl stop keepalived
http://i2.运维网.com/images/blog/201804/09/3bc7c68fc39294ed6d74c36bee9465cd.png
//备机自己切换到主机状态
http://i2.运维网.com/images/blog/201804/09/e90515fe84ae1a7220aa3c4343ef2b76.png
//双机热备搭建完成
第三部分配置Web节点服务器
第一步:配置SERVER AA服务器(192.168.80.30)
[*]配置http服务
//安装http服务
# yum install -y httpd
//编辑主配置文件
# vi /etc/httpd/conf/httpd.conf
ServerName aa //将“#”号去掉,修改主机名
保存退出
//配置默认显示网页
# cd /var/www/html/
# echo "SERVER AA" > index.html
http://i2.运维网.com/images/blog/201804/09/748d9f7ffc75e41c6dfbf04015d82e63.png
# service httpd start //重启http服务
//win7验证http服务
访问http://192.168.80.30
http://i2.运维网.com/images/blog/201804/09/4cb50468284d469f187d052d0bc753a6.png
[*]配置DR模式
# 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
保存退出
# sh web.sh //执行脚本
# ifconfig //查看虚拟接口
http://i2.运维网.com/images/blog/201804/09/52132a45697a55c6e7f22bc0cbb74cac.png
第二步:配置SERVER BB服务器(192.168.80.40)
[*]配置http服务
//安装http服务
# yum install -y httpd
//编辑主配置文件
# vi /etc/httpd/conf/httpd.conf
ServerName bb //将“#”号去掉,修改主机名
保存退出
//配置默认显示网页
# cd /var/www/html/
# echo "SERVER BB" > index.html
http://i2.运维网.com/images/blog/201804/09/61933d12a732a4225dc454d74de3fb3e.png
# service httpd start //重启http服务
//win7验证http服务
访问http://192.168.80.40
http://i2.运维网.com/images/blog/201804/09/bde3dfe911d239cba69cbff8b9e5760b.png
----------配置DR模式---------
# 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
保存退出
# sh web.sh //执行脚本
# ifconfig //查看虚拟接口
http://i2.运维网.com/images/blog/201804/09/0999e7b9ca9f40ea6adfd9f42e7ef2a4.png
第四部分测试LVS+Keepalived高可用群集
在客户端的浏览器中,能够通过LVS+Keepalived群集的漂移地址(192.168.80.100)正常访问web页面内容,则验证群集构建成功。
[*] 验证两台Web服务器轮询工作
Win7访问http://192.168.80.100
http://i2.运维网.com/images/blog/201804/09/377ea832f5c2a36c63a413fef58d7c79.png
//由于设置了连接保持时间为60秒,一分钟后再重新访问该地址
http://i2.运维网.com/images/blog/201804/09/800984c94b629a202c316dd692aa7f7c.png
//自动轮询交给另一台Web服务器
[*] 模拟主调度器故障,验证结果
# systemctl stop keepalived
//主调度器keepalived停止工作
http://i2.运维网.com/images/blog/201804/09/70445b791015ea5e455a054126cc89fe.png
//从调度器自动切换,继续工作
http://i2.运维网.com/images/blog/201804/09/0c8599a4e68831d8ed57136f88daa288.png
//win7访问http://192.168.80.100,查看结果
http://i2.运维网.com/images/blog/201804/09/d79c81629a6c89bdb9d4bd93d32a7633.png
//一分钟后,重新访问http://192.168.80.100,查看结果
http://i2.运维网.com/images/blog/201804/09/77e8ef0e857ff46e2c4a6cd04d998e62.png
//双机热备已经起作用
[*]模拟Web服务器aa故障
# service httpd stop //将bb服务器停止工作
# tail -f /var/log/messages
http://i2.运维网.com/images/blog/201804/09/0409b22437bc5c4c8eeb2a75ac1f5476.png
访问http://192.168.80.40
http://i2.运维网.com/images/blog/201804/09/5b5401fd9d58a2adf164800f2c97f4de.png
一分钟后,重新访问http://192.168.80.40
http://i2.运维网.com/images/blog/201804/09/cac2dec9772b4d7ef735c19ef460ef9d.png
//无法访问,验证成功
# service httpd start //恢复启动bb服务器
//查看调度器日志:
http://i2.运维网.com/images/blog/201804/09/7496617635e371c91cb598fd8c792d19.png
//已成功将bb服务器加入到服务器池
//LVS+Keepalived已成功搭建并测试完成
页:
[1]