服务器群集LVS+ Keepalived
作者:互联网
目录
- KeepAlive
- Keepalived需求分析
- Keepalived工具介绍
- Keepalived实现原理剖析
- Keepalived安装与启动
- 配置Keepalived master服务器
- 配置Keepalived slave服务器
- 项目测试
KeepAlive
Keepalived需求分析
- 企业应用中,单台服务器承担应用存在单点故障的危险
- 单点故障-旦发生, 企业服务将发生中断, 造成极大的危害
Keepalived工具介绍
专为LVS和HA设计的一款健康检查工具
- 支持故障自动切换(Failover)
- 支持节点健康状态检查(Health Checking)
- 官方网站: http://www.keepalived.org/
Keepalived实现原理剖析
Keepalived采用VRRP热备份协议 实现Linux服务器的多机热备功能
VRRP (虚拟路由冗余协议) 是针对路由器的一种备份解决方案
- 由多台路由器组成一个热备组, 通过共用的 虚拟IP地址对外提供服务
- 每个热备组内同时只有一台主路由器提供服 务,其他路由器处于冗余状态
- 若当前在线的路由器失效,则其他路由器会 根据设置的优先级自动接替虚拟IP地址,继 续提供服务
Keepalived安装与启动
在LVS群集环境中应用时,也需用到ipvsadm管理工具
YUM安装Keepalived
启用Keepalived服务
配置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的配置有3三个
选项不同
- router_ id: 设为自有名称
- state:设为BACKUP
- priority: 值低于主服务器
其他选项与master相同
项目测试
实验环境
nfs配置
配置文件
[root@server5 ~]# hostnamectl set-hostname nfs
[root@server5 ~]# bash
[root@nfs ~]# mkdir /web1
[root@nfs ~]# mkdir /web2
[root@nfs ~]# echo "<h1>this is aaa.</h1>" > /web1/index.html
[root@nfs ~]# echo "<h1>this is bbb.</h1>" > /web2/index.html
[root@nfs ~]# rpm -qa | grep nfs-utils
nfs-utils-1.3.0-0.48.el7.x86_64
[root@nfs ~]# rpm -qa | grep rpcbind
rpcbind-0.2.0-42.el7.x86_64
[root@nfs ~]# vi /etc/exports
/web1 20.0.0.10(ro)
/web2 20.0.0.11(ro)
查看共享
[root@nfs ~]# systemctl restart rpcbind
[root@nfs ~]# systemctl restart nfs
[root@nfs ~]# showmount -e
Export list for nfs:
/web2 20.0.0.13
/web1 20.0.0.12
Web1配置
配置文件
[root@web1 ~]# yum -y install httpd
[root@web1 ~]# systemctl start httpd
[root@web1 ~]# mount 20.0.0.14:/web1 /var/www/html/
[root@web1 ~]# df -Th
验证
[root@web1 ~]# curl http://localhost
<h1>this is aaa.</h1>
配置脚本
[root@web1 ~]# vi web1.sh
#!/bin/bash
# lvs web1
ifconfig lo:0 20.0.0.200 broadcast 20.0.0.200 netmask 255.255.255.255 up
route add -host 20.0.0.200 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
[root@web1 ~]# chmod +x web1.sh
[root@web1 ~]# ./web1.sh
[root@web1 ~]# ifconfig 查看虚拟接口
[root@web1 ~]# route -n 查看增加的路由
Web2配置
Web2配置与Web1相同
修改特定IP即可
Lvs01配置
查看状态
[root@lvs01 ~]# modprobe ip_vs
[root@lvs01 ~]# cat /proc/net/ip_vs
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
[root@lvs01 ~]# yum -y install ipvsadm
编写脚本
[root@lvs01 ~]# vi lvs01.sh
#!/bin/bash
# lvs01
ifconfig ens33:0 20.0.0.200 broadcast 20.0.0.200 netmask 255.255.255.255 up
route add -host 20.0.0.200 dev ens33:0
ipvsadm -C
ipvsadm -A -t 20.0.0.200:80 -s rr
ipvsadm -a -t 20.0.0.200:80 -r 20.0.0.10:80 -g
ipvsadm -a -t 20.0.0.200:80 -r 20.0.0.11:80 -g
ipvsadm -Ln
[root@lvs01 ~]# chmod +x lvs01.sh
[root@lvs01 ~]# ./lvs01.sh
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 20.0.0.200:80 rr
-> 20.0.0.12:80 Route 1 0 0
-> 20.0.0.13:80 Route 1 0 0
验证配置
[root@lvs01 ~]# curl http://20.0.0.200
配置keepalive
[root@lvs01 ~]# yum -y install gcc gcc-c++ make popt-devel kernel-devel openssl-devel
[root@lvs01 ~]# tar zxvf keepalived-2.0.13.tar.gz
[root@lvs01 ~]# cd keepalived-2.0.13/ 进入配置编译安装
[root@lvs01 keepalived-2.0.13]# ./configure --prefix=/ 配置
[root@lvs01 keepalived-2.0.13]# make && make install 编译安装
[root@lvs01 keepalived-2.0.13]# ls -lh
[root@lvs01 keepalived-2.0.13]# cd keepalived/
[root@lvs01 keepalived]# cd etc/init.d/
[root@lvs01 init.d]# cp keepalived /etc/init.d/ 复制文件
[root@lvs01 init.d]# cd
[root@lvs01 ~]# systemctl enable keepalived.service 设置开机自启动
配置文件
[root@lvs01 ~]# vi /etc/keepalived/keepalived.conf
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 {
20.0.0.200
}
}
virtual_server 20.0.0.200 80 {
lb_algo rr
lb_kind DR
persistence_timeout 6
protocol TCP
real_server 20.0.0.12 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 20.0.0.13 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@lvs01 ~]# vi /var/log/messages
[root@lvs01 ~]# tail -100 /var/log/messages
[root@lvs01 ~]# ipvsadm -Ln
Lvs02配置
DR配置与Lvs01配置相同
keepalive配置
基础安装及解压同Lvs01
[root@lvs01 ~]# vi /etc/keepalived/keepalived.conf
global_defs {
router_id lvs_02
}
vrrp_instance vi_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 108
advert_int 1
authentication {
auth_type PASS
auth_pass 6666
}
virtual_ipaddress {
20.0.0.200
}
}
virtual_server 20.0.0.200 80 {
lb_algo rr
lb_kind DR
persistence_timeout 6
protocol TCP
real_server 20.0.0.12 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
[root@lvs02 ~]# ip addr
验证
lvs01
访问http://20.0.0.200
验证主备转换
将lvs01服务停止
[root@lvs01 ~]# systemctl stop keepalived.service 关闭keepalived服务
Lvs02查看
变为主调度器
标签:20.0,LVS,群集,lvs01,配置,Keepalived,keepalived,web1,root 来源: https://blog.csdn.net/weixin_49343462/article/details/110950332