keepalived与LVS
keepalived与LVS结合时无需另外写lvs规则,可以直接在keepalived配置文件中进行定义
LVS配置参数
virtual_server IP port #定义虚拟主机IP地址及其端口
virtual_server fwmark int #ipvs的防火墙打标,实现基于防火墙的负载均衡集群
virtual_server group string #将多个虚拟服务器定义成组,将组定义成虚拟服务
语法格式
virtual_server IP port
{
delay_loop NUM #检查后端服务器的时间间隔
lb_algo rr|wrr|lc|wlc|lblc|sh|dh #定义调度算法
lb_kind NAT|DR|TUN #定义lvs的模型
persistence_timeout NUM #持久连接时长
protocol TCP|UDP|SCTP #指定服务的协议
sorry_server IP Port #当所有RS都发生故障时的备用服务器地址
real_server {
weight NUM #权重
notify_up /path/to/script #RS上线通知脚本
notify_down /path/to/script #RS下线通知脚本
HTTP_GET|SSL_GET|TCP_CHECK|SMTP_CHECK|MISC_CHECK { ... } #定义当前主机的健康状态检测方法
}
...
}
keepalived+lvs 实现
s1节点配置
1.修改keepalived配置文件
[root@s1 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@mylinuxops.com
}
notification_email_from root@mylinuxops.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id s1.mylinuxops.com
vrrp_skip_check_adv_addr
#vrrp_strict
vrrp_iptables
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state Master
interface ens33
virtual_router_id 27
priority 100
advert_int 2
authentication {
auth_type PASS
auth_pass 1111
}
unicast_src_ip 172.20.27.10
unicast_peer {
172.20.27.11
}
virtual_ipaddress {
172.20.27.100 dev ens33 label ens33:0
}
}
virtual_server 172.20.27.100 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
protocol TCP
real_server 172.20.27.20 80 {
weight 1
TCP_CHECK { #对后端服务器做tcp的监测
connect_timeout 5 #定义连接超时时长
retry 3 #重试次数
delay_before_retry 3 #每次重试的间隔时间
connect_port 80 #监测的端口
}
}
real_server 172.20.27.21 80 {
weight 1
TCP_CHECK {
connect_timeout 5
retry 3
delay_before_retry 3
connect_port 80
}
}
}
重启服务
[root@s1 ~]# systemctl restart keepalived
查看lvs规则
[root@s1 ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.20.27.100:80 wrr
-> 172.20.27.20:80 Route 1 0 0
-> 172.20.27.21:80 Route 1 0 0
s2节点配置
1.修改配置文件
[root@s2 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@mylinuxops.com
}
notification_email_from root@mylinuxops.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id s2.mylinuxops.com
vrrp_skip_check_adv_addr
#vrrp_strict
vrrp_iptables
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 27
priority 80
advert_int 2
authentication {
auth_type PASS
auth_pass 1111
}
unicast_src_ip 172.20.27.11
unicast_peer {
172.20.27.10
}
virtual_ipaddress {
172.20.27.100 dev ens33 label ens33:0
}
}
virtual_server 172.20.27.100 80 {
delay_loop 5
lb_algo wrr
lb_kind DR
protocol TCP
real_server 172.20.27.20 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 5
retry 3
delay_before_retry 3
}
}
real_server 172.20.27.21 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 5
retry 3
delay_before_retry 3
}
}
}
重启服务
[root@s2 ~]# vim /etc/keepalived/keepalived.conf
查看lvs规则是否定义
[root@s2 ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.20.27.100:80 wrr
-> 172.20.27.20:80 Route 1 0 0
-> 172.20.27.21:80 Route 1 0 0
web1,web2配置
在web1和web2上分别执行lvs-rs脚本
[root@localhost ~]# bash lvs_dr_rs.sh start
脚本内容
vip=172.20.27.100
mask='255.255.255.255'
dev=lo:1
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 $dev $vip netmask $mask #broadcast $vip up
#route add -host $vip dev $dev
echo "The RS Server is Ready!"
;;
stop)
ifconfig $dev 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 "The RS Server is Canceled!"
;;
*)
echo "Usage: $(basename $0) start|stop"
exit 1
;;
esac
分别在web1和web2上创建测试主页文件
web1主页
[root@localhost ~]# cat /data/www/index.html
mylinuxops.com server1
web2主页
[root@localhost ~]# cat /data/www/index.html
mylinuxops.com server2
测试
在客户端上进行测试
[root@client ~]# curl www.mylinuxops.com
mylinuxops.com server1
[root@client ~]# curl www.mylinuxops.com
mylinuxops.com server2
当将s1节点停止服务,后再次测试
[root@s3 ~]# curl www.mylinuxops.com
mylinuxops.com server1
[root@s3 ~]# curl www.mylinuxops.com
mylinuxops.com server2
访问不受影响,此时vip已经转换到s2节点上
[root@s2 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.20.27.11 netmask 255.255.0.0 broadcast 172.20.255.255
inet6 fe80::20c:29ff:fe4d:1ce3 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:4d:1c:e3 txqueuelen 1000 (Ethernet)
RX packets 367120 bytes 29261794 (27.9 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 22395 bytes 2212792 (2.1 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.20.27.100 netmask 255.255.255.255 broadcast 0.0.0.0
ether 00:0c:29:4d:1c:e3 txqueuelen 1000 (Ethernet)
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 6 bytes 482 (482.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 6 bytes 482 (482.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
转载于:https://blog.51cto.com/11886307/2406624