lvs简介
LVS:Linux Virtual Server,负载调度器,内核集成,章文嵩,阿里的四层SLB(Server LoadBalance)是基于LVS+keepalived实现。
lvs相关概念
VS:Virtual Server
RS:Real Server
CIP:Client IP
VIP: Virtual serve IP VS外网的IP
DIP: Director IP VS内网的IP
RIP: Real server IP
lvs集群的类型
lvs-nat: 修改请求报文的目标IP,多目标IP的DNAT
lvs-dr: 操纵封装新的MAC地址
lvs-tun: 在原请求IP报文之外新加一个IP首部
lvs-fullnat: 修改请求报文的源和目标IP
LVS的nat模式
实验环境:三台虚拟机:lvs主机、webserver1、webserver2
lvs配置
安装web服务:dnf install httpd -y
在lvs中打开内核路由功能:vim /etc/sysctl.conf
webserver配置
下载httpd服务 dnf install httpd -y
ROUTDR
router主机开启两个网卡
[root@router ~]# vmset.sh eth0 172.25.254.100
[root@router ~]# vim /etc/NetworkManager/system-connections/eth0.nmconnection
[root@router ~]# nmcli connection reload
[root@router ~]# nmcli connection up eth0
[root@router ~]# vmset.sh eth1 192.168.0.100
[root@router ~]# vim /etc/NetworkManager/system-connections/eth1.nmconnection
[root@router ~]# nmcli connection reload
[root@router ~]# nmcli connection up eth1
查看eth0网卡信息
[root@client ~]# cat /etc/NetworkManager/system-connections/eth0.nmconnection
[connection]
id=eth0
type=ethernet
interface-name=eth0
[ipv4]
address1=172.25.254.100/24,172.25.254.2
method=manual
查看eth1网卡信息
[root@client ~]# cat /etc/NetworkManager/system-connections/eth1.nmconnection
[connection]
id=eth1
type=ethernet
interface-name=eth1
[ipv4]
address1=192.168.0.100/24
method=manual
配置网卡信息
[root@client ~]# vmset.sh eth0 172.25.254.200
[root@client ~]# vim /etc/NetworkManager/system-connections/eth0.nmconnection
[root@client ~]# nmcli connection reload
[root@client ~]# nmcli connection up eth0
查看
[root@client ~]# cat /etc/NetworkManager/system-connections/eth0.nmconnection
[connection]
id=eth0
type=ethernet
interface-name=eth0
[ipv4]
address1=172.25.254.200/24,172.25.254.100
method=manual
在两台webserver主机中设置vip不对外响应
[root@webserver1 ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
[root@webserver1 ~]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
[root@webserver1 ~]# echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
[root@webserver1 ~]# echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
LVS火墙标记
两台web服务器安装
[root@webserver1 ~]# yum install mod_ssl -y
安装后重启httpd
[root@webserver1 ~]# systemctl restart httpd
lvs中配置策略:
[root@lvs ~]# ipvsadm -A -t 192.168.0.200:443 -s rr
[root@lvs ~]# ipvsadm -a -t 192.168.0.200:443 -r 192.168.0.10:443 -g
[root@lvs ~]# ipvsadm -a -t 192.168.0.200:443 -r 192.168.0.20:443 -g
查询结果 curl -k https://192.168.0.200
lvs打标签
[root@lvs ~]# iptables -t mangle -A PREROUTING -d 192.168.0.200 -p tcp -m multiport --dports 80,443 -j MARK --set-mark 66
清除原先的策略
[root@lvs ~]# ipvsadm -C