lvs

LVS server cluster     lamp
concept:
LVS: Linux Virtual Server Linux virtual server. Virtualize the entire server cluster into one server (Linux Virtual Server)
 
VIP: It is the IP address facing the public network on the lvs machine. Used to accept client requests. After LVS receives the client's request, it uses the scheduling algorithm to decide which rs to distribute the request to the backend. For example, to distribute to rs1, NAT model, how to send this request to rs1, LVS passes the DNAT (converts the destination ip in the request message from VIP to RIP), and sends the request message to rs through DIP. Encapsulate the response message (src RIP dst CIP ), rs first sends the message to the gateway (DIP), LVS cannot directly send the data packet to the public network, it needs SNAT (convert the original address RIP into VIP), after the public address network router to the client.
 
 
The client sends a request message:  src CIP dst VIP --"dst RIP
Request message received by rs: src CIP dst RIP
rs response message: src RIP dst CIP
The client receives the response message:   src VIP dst CIP
 
experiment:
 
Preparation: 5 virtual machines---client, router, lvs, two RS
 
lab environment:
OS: rhel6.7 kernel: 2.6.32
 
IP address planning and network interface connection:
CIP: 12.1.1.1 vmnet4 gateway : 12.1.1.2
Router:
eth0: 12.1.1.2 vmnet4
eth1:13.1.1.1 vmnet5 //Check the routing table to see if there are any responding routing entries
Lvs:
Eth0:13.1.1.2 vmnet5
Eth1:10.1.1.1 vmnet6 //lvs needs to enable packet forwarding (router)
RS1: 10.1.1.2 vmnet6 gateway: 10.1.1.1
RS2: 10.1.1.3 vmnet6
 
软件:fusion—3个vmnet
其他设置:关闭NetworkManager、关闭防火墙、关闭SeLinux
 
 
实验环境搭建后:
client上:ping 通VIP :客户端能够访问lvs
rs上:ping通CIP : rs能够将响应报文返回给客户端
 
rs提供web服务:
/var/www/html/   index.html
实验环境下,首页文件内容不一样
 
rs1    RS1
rs2 RS2
 
yum install httpd
cd /var/www/html
vim index.html
RS1
/etc/init.d/httpd restart
yum install elinks
elinks http://localhost --dump
 
ipvsadm 语法:
ipvs     ipvsadm命令安装:
 
 
算法:
rr
wrr
 
dh :对客户端访问的目的IP取hash值(模),实现的是所有客户端的链接请求始终被分发到一台固定的rs上,好处在于:可以保证session(会话)的一致性。
www.xx.com -》13.1.1.2    13+1+1+2=17%2=1          -->
 
sh: 根据客户端的源地址计算hash值来决定把请求分发给那一台rs。   12.1.1.1=15%2=1   rs2
12.1.1.2=16%2=0   rs1
 
取几的模看的是rs的数量!
 
rr-->怎样保证session(会话)的一致性     nfs、memcache
dh-->所有的请求分发到固定的一台rs上,rs压力非常大
sh-->保证会话的一致性。每一个客户端的请求分发到固定的rs上
lc least connection 最少链接
将请求根据rs上链接数量来进行分发。将请求分发给链接数量最少的rs,直到所有rs链接数量相等。
 
lvs三种模型
NAT
DR direct router直连路由
TUN tunnel  隧道
 
DR模型:
NAT:调度器压力大(请求和响应报文都经过调度器)支持rs数量10~20台
DR:响应报文直接发给路由器
NAT: client-->router-->lvs-->rs      rs-->lvs-->router-->client
DR: cleint-->router-->lvs-->rs rs-->router-->client  //响应报文不经过lvs,而是直接给路由器
客户端发包:src  CIP  dst VIP          客户端收到的响应报文:src VIP   dst CIP
怎样实现rs响应报文中封装src为VIP-->我们在rs上配置一个VIP    给lo:0接口配置上VIP   
 
cleint-->router-->lvs-->rs      src CIP   dst VIP
我们要确保先将请求包交给lvs    然后lvs经过调度算法的计算在交给rs
 
 
 
arp协议:address resolution protocol 地址解析协议
 
局域网协议:实现的是局域网通信。
本地ip 192.168.1.1     ping 192.168.1.2
封装ping包:
src  ip 192.168.1.1    dst ip 192.168.1.2
src mac  本机mac dst mac  ?????
如果封装好这个ping包,就需要直到192.168.1.2这个ip对应的mac地址!!-->arp
实现过程:
广播:谁是192.168.1.2    请告诉我192.168.1.2对应的mac是多少,只有1.2回复(单播回复),这样1.1就知道1.2对应的mac地址,ping包就可以发出去,继而得到1.2ping包的响应包。
 
通常情况下:交换机中会维护一个mac地址表,记录的是mac地址与IP的对应关系,一个ip对应一个mac地址;如果一个ip对应多个mac就叫做mac地址欺骗,arp欺骗。
 
rs闭嘴    保证客户端的请求包交给lvs       实现方法:修改数据包的mac,源mac为路由器上eth1的mac,目标mac为lvsVIP(eth1)对应的mac
lvs根据分发规则和调度算法,决定将该请求交给rs2 实现方法:修改数据包的源mac 为DIP对应的mac,目标mac rs2生的eth0的mac
rs收到包后拆包:    src  CIP   dst  VIP        
响应包:   src   VIP  lo:0   dst  CIP
   src mac  声明,使用eth0的mac   dst   router器eth1的mac
 
 
 
实验:
修改一下网络链接模式,路由器eth1接口和lvs上的DIP  VIP  以及rs上的eth0都连接到一个交换机上
1、lvs操作      配置VIP地址13,1,1,254   ipvsadm 添加分发规则,指定算法,rs ,模式-g
2、rs操作 配置VIP(lo:0) 闭嘴和声明
 
 
 
闭嘴:
echo 1 > /proc/sys/net/ipv4/conf/eth0/arp_ignore
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
声明:
echo 2 > /proc/sys/net/ipv4/conf/eth0/arp_announce
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
 
永久:vim /etc/sysctl.conf
sysctl -p
 
 
支持的rs数量100台左右,常用模式。描述一下数据包走向!client -->router --> lvs-->rs    rs-->router -->client  
rs上配置VIP-->路由器转发请求包给lvs时arp欺骗-->echo 1   闭嘴      
回报   声明
怎样实现包的转发:修改数据包的mac地址
 
 
 
 
lvs    eth0   eth1(VIP)    
 
 
TUN:
隧道模式     vpn  虚拟专用通道   总部 上海   分北京  
实现:rs和lvs不在一个局域网中,地址位置不同。   nat  dr 13.1.1.254   13.1.1.3       10.1.1.1   10.1.1.2
 
实验:基于dr,回包时直接交给路由器      rs上有VIP
rs:
VIP    tunl0        
echo 0 > /proc/sys/net/ipv4/conf/tunl0/rp_filter     不进行反向地址验证
lvs:
ipvsadm      -i   ipip   tunnel
 
走的协议:
ipip      ip多封装一次ip           src  src  dst  dst   
 
 
 
 
HA 高可用:
一台lvs如果发生单点故障,导致客户单访问不到网站。
 
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325279896&siteId=291194637
LVS
LVS
lvs
LVS
LVS