LVS DR原理(路由直连):
用户请求LVS上的VIP,在LVS中修改报文中的MAC地址为真实服务器的MAC地址,不改源IP与目标IP,源IP为用户IP不变,然后LVS将报文给RS服务器,RS回返时,从真实网关走数据,不再经过LVS
环境:
VIP: 172.17.0.100
DIP: 172.17.0.5
RIP: 172.17.0.2
RIP: 172.17.0.3
端口: 80
后端服务: NGINX
安装配置
安装LVS
[root@0f34df37cdaf ansible]# yum install -y ipvsadm* -y
加载模块
[root@0f34df37cdaf ansible]# lsmod | grep ip_vs
LVS服务器上配置eth0:1
[root@0f34df37cdaf /]# cd /etc/sysconfig/network-scripts/
[root@0f34df37cdaf network-scripts]# vim ifcfg-eth0:1
DEVICE=eth0:1
IPADDR=172.17.0.100
NETMASK=255.255.255.255
ONBOOT=yes
~
或者
[root@0f34df37cdaf network-scripts]# ifconfig eth0:1 172.17.0.100/24
[root@0f34df37cdaf network-scripts]# ifconfig
[root@0f34df37cdaf network-scripts]# ifconfig eth0:1
eth0:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.100 netmask 255.255.255.0 broadcast 172.17.0.255
ether 02:42:ac:11:00:05 txqueuelen 0 (Ethernet)
添加LVS模块
[root@0f34df37cdaf /]# yum install -y kernel kernel-devel
添加模块
[root@0f34df37cdaf /]# modprobe ip_vs_wrr
[root@0f34df37cdaf /]# modprobe ip_vs_rr
查看模块信息
[root@0f34df37cdaf /]# lsmod |grep ip_vs
配置LVS
[root@0f34df37cdaf /]# ipvsadm -A -t 172.17.0.100:80 -s rr
[root@0f34df37cdaf /]# ipvsadm -a -t 172.17.0.100:80 -r 172.17.0.3:80 -g -w 100
[root@0f34df37cdaf /]# ipvsadm -a -t 172.17.0.100:80 -r 172.17.0.2:80 -g -w 100
[root@0f34df37cdaf /]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.17.0.100:80 rr
-> 172.17.1.2:80 Route 100 0 0
-> 172.17.1.3:80 Route 100 0 0
NGINX服务器上配置SIP
ifconfig lo:0 172.17.0.100 broadcast 172.17.0.100 netmask 255.255.255.255 up
测试
[root@a2deb4fa0a27 ~]# curl 172.17.0.100
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
删除所有RIP
ipvsadm -D -t 172.17.0.100:80
注:
1、如果没有数据,可以LVS上加条路由
route add -host 172.17.0.100 eth0
2、LVS机器上要配置转发
内容开启转发功能
echo 1 >/proc/sys/net/ipv4/ip_forward
--------------------end