LVS+Keepalived高可用集群
一、高可用群集概述
Keepalived案例分析
- 企业应用中,单台服务器承担应用存在单点故障的危险
- 单点故障一旦发生,企业服务将发生中断,造成极大的危害
Keeppalived工具介绍
- 专为LVS和HA设计的一款健康检查工具
1.支持故障自动切换(Failover)
2.支持节点健康状态查询(Health Checking)
3.官方网站:http://www.keepalived.org/
Keepalived实现原理剖析
- Keppalived采用VRRP热备份协议实现Linux服务器的多机热备功能
- VRRP (虚拟路由冗余协议)是针对路由器的一种备份解决方案
1.由多台路由组成一个热备组,通过共同的虚拟IP地址对外提供服务
2.每个热备组内同时只有一台主路由器提供服务,其他路由器处于冗余状态
3.若当前在线的路由器失效,则其路由器会根据设置的优先级自动接替虚拟IP地址,继续提供服务
Keepalived案例讲解
- Keepalived可实现多机热备,每个热备组可有多台服务器
- 双机热备的故障切换是由虚拟IP地址的漂移来实现,适用于各种应用服务器
- 实现基于Web服务的双击热备
配置Keepalived master服务器
Keepalived配置目录位于/etc/keepalived/
Keepalived.conf是主配置文件
global_defs {...}区段指定全局参数
vrrp_instance实例名称 {...}区段指定VRRP热备参数
注释文字以"!"符号开头
目录samples,提供了许多配置样例作为参考
常用配置选项
选项 | 内容 |
---|---|
router_id HA_TEST_R1 | 本路由器(服务器)的名称 |
vrrp_instance VI_1 | 定义VRRP热备实例 |
state MASTER | 热备状态,MASTER表示主服务器 |
interface ens33 | 承载VIP地址的物理接口 |
virtual_router_id 1 | 虚拟路由器的ID号,每个热备组保持一致 |
priority 100 | 优先级,数值越大优先级越高 |
advert_int 1 | 通告间隔秒数(心跳频率) |
auth_type PASS | 认证类型 |
auth_pass 123456 | 密码字串 |
virtual_ipaddress{vip} | 指定漂移地址(VIP),可以有多个 |
配置Keepalived slave服务器
Keepalived备份服务器的配置与master的配置有三个选项不同
选项 | 内容 |
---|---|
router_id | 设为自有名称 |
state | 设为BACKUP |
priority | 值低于主服务器 |
其他选项与master相同
部署LVS-keepalived
部署环境
192.168.1.15 lvs01+keepalive(主) virtual-ip 20.0.0.200
192.168.1.16 lvs02+keepalive(备) virtual-ip 20.0.0.200
192.168.1.17 web1
192.168.1.18 web2
192.168.1.19 nfs
配置NFS存储服务器
-
安装nfs-utils、rpcbind
yum -y install nfs-utils rpcbind
-
创建两个网页
mkdir /opt/web1 mkdir /opt/web2 echo "<h1>this is web1</h1>" > /opt/web1/index.html echo "<h1>this is web2</h1>" > /opt/web2/index.html vi /etc/exports /opt/web1 192.168.1.17(ro) /opt/web2 192.168.1.18(ro)
-
重启,查看本机发布的NFS共享目录
systemctl restart nfs showmount -e
web1
-
安装httpd
yum -y install httpd
-
挂载nfs存储服务器
mount 192.168.1.19:/opt/web1 /var/www/html/
-
启动服务
systemctl start httpd
-
测试网页
curl http://localhost
-
编辑脚本
vim web1.sh #!/bin/bash #Lvs-Dr模式 web1 ifconfig lo:0 192.168.1.100 broadcast 192.168.1.100 netmask 255.255.255.255 up route add -host 192.168.1.100 dev lo:0 echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce sysctl -p &> /dev/null
web2节点
-
安装httpd用于测试
yum -y install httpd
-
查看NFS服务器共享的目录
showmount -e 192.168.1.19
-
将目录挂载到网站根目录
mount 192.168.1.19:/opt/web2 /var/www/html/
-
检查一下是否挂载成功
cat /var/www/html/index.html
-
编辑脚本
vim web2.sh #!/bin/bash #Lvs-Dr模式 web2 ifconfig lo:0 192.168.1.100 broadcast 192.168.1.100 netmask 255.255.255.255 up route add -host 192.168.1.100 dev lo:0 echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce sysctl -p &> /dev/null
LVS1
-
手动加载ip_vs模块
modprobe ip_vs
-
添加keepalived-2.0.13.tar.gz
-
安装依赖包
yum -y install gcc gcc-c++ make popt-devel kernel-devel openssl-devel
-
解压压缩包
yum -y install ipvsadm tar zxvf keepalived-2.0.13.tar.gz
-
开始配置
cd keepalived-2.0.13/ ./configure --prefix=/ make && make install cd keepalived/etc/init.d/ cp keepalived /etc/init.d/ cd systemctl enable keepalived.service
-
编辑配置
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_01
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 110
advert_int 1
authentication {
auth_type PASS
auth_pass 6666
}
virtual_ipaddress {
192.168.1.100
}
}
virtual_server 192.168.1.100 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 6
protocol TCP
real_server 192.168.1.17 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get retry 3
delay_before_retry 3
}
}
real_server 192.168.1.18 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get retry 3
delay_before_retry 3
}
}
}
-
重新加载服务
systemctl daemon-reload
-
开启Keepalived
systemctl start keepalived
LVS2
-
手动加载ip_vs模块
modprobe ip_vs
-
查看,并安装
cat /proc/net/ip_vs yum -y install ipvsadm
-
上传 解压
tar zxvf keepalived-2.0.13.tar.gz yum -y install gcc gcc-c++ make popt-devel kernel-devel openssl-devel cd keepalived-2.0.13/ ./configure --prefix=/ make && make install cp keepalived/etc/init.d/keepalived /etc/init.d/
-
配置文件
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.1.200
}
}
virtual_server 192.168.1.200 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 6
protocol TCP
real_server 192.168.1.12 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get retry 3
delay_before_retry 3
}
}
real_server 192.168.1.13 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get retry 3
delay_before_retry 3
}
}
}
-
重新加载服务
systemctl start keepalived.service systemctl status keepalived.service ipaddr
- 测试
关闭主调度器
- 最直观的方法就是使用ip addr 查看,这时候发现主调度器上的虚拟IP已经消失了
- 备用调度器这时候出现虚拟IP地址
- 浏览器测试