LB/LVS之NAT实战

来源:互联网 发布:安卓串口助手源码 编辑:程序博客网 时间:2024/05/02 02:22


cip:192.168.1.1

vip:192.168.1.100/32
dip:192.168.10.254

rip1:192.168.10.1     gw 192.168.10.254
rip2:192.168.10.2     gw 192.168.10.254

1.IP, hostname, hosts, iptables, SELinux, ssh trust, ntp 每台
cat /etc/hosts
127.0.0.1          localhost
192.168.10.254 director1.uplook.com director1
192.168.10.1     node1.uplook.com node1
192.168.10.2      node2.uplook.com node2

2. RS配置
配置好网站服务器,测试所有RS       //为了看到测试效果,建议提供不同的页面
RIP配置httpd gw转向dip

3.DR配置
 ip addr add dev eth0 192.168.1.2/24        
vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
sysctl -p        //确保打开路由转发
 
定义LVS的分发策略
 yum -y install ipvsadm          //确保LoadBalancer仓库可用
 ipvsadm -A -t 192.168.1.100:80  -s rr
 ipvsadm -a -t  192.168.1.100:80  -r 10.10.10.10 -m  //-m masquerading (NAT)
 ipvsadm -a -t 192.168.1.100:80 -r 10.10.10.20 -m
 ipvsadm -a -t 192.168.1.100:80 -r 10.10.10.30 -m
 service ipvsadm save
 ipvsadm -L
 ipvsadm -Ln


 ipvsadm -L -n --stats   
 ipvsadm -L -n --rate    //看速率
 ipvsadm -Ln -c         //查看LVS的连接条目
watch -n.5 'ipvsadm -Ln -c'
 
4. 测试
[root@client ~]# elinks -dump
http://192.168.1.100/
[root@client ~]# ab -c 1000 -n 1000 http://192.168.1.100/
 
5. 小结
 VS/NAT模式的原理是:当Director收到Client请求时,Director将数据包的目标IP由VIP转换为选中的Real Server的RIP来实现分发,要求RS将网关指向Director的DIP。
 特点是:配置简单,所有的入站、出站数据包都经过分发器。当数据量比较大时,分发器可能会出现网络瓶颈!因而支持的RS数量少。
 
Director必须开启kernel ip_forward
所有RealServer默认网关指向DIP
Director使用DIP和RealServer RIP通信

[root@director1 ~]# watch -n.5 'ipvsadm -Ln -c'
Every 0.5s: ipvsadm -Ln -c                            Tue Dec 29 06:41:15 2015

IPVS connection entries
pro expire state       source             virtual            destination
TCP 00:32  SYN_RECV    192.168.122.1:55989 192.168.122.100:80 10.10.10.10:80
TCP 00:42  SYN_RECV    192.168.122.1:55991 192.168.122.100:80 10.10.10.20:80
TCP 00:41  SYN_RECV    192.168.122.1:55990 192.168.122.100:80 10.10.10.30:80
TCP 00:29  SYN_RECV    192.168.122.1:55988 192.168.122.100:80 10.10.10.20:80
TCP 00:59  SYN_RECV    192.168.122.1:55993 192.168.122.100:80 10.10.10.30:80
TCP 00:42  SYN_RECV    192.168.122.1:55992 192.168.122.100:80 10.10.10.10:80
=================================================================================

有两种命令选项格式,长的和短的,具有相同的意思。在实际使用时,两种都可
以。
-A –add-service 在内核的虚拟服务器表中添加一条新的虚拟服务器记录。也
就是增加一台新的虚拟服务器。
-E –edit-service 编辑内核虚拟服务器表中的一条虚拟服务器记录。
-D –delete-service 删除内核虚拟服务器表中的一条虚拟服务器记录。
-C –clear 清除内核虚拟服务器表中的所有记录。
-R –restore 恢复虚拟服务器规则
-S –save 保存虚拟服务器规则,输出为-R 选项可读的格式
-a –add-server 在内核虚拟服务器表的一条记录里添加一条新的真实服务器
记录。也就是在一个虚拟服务器中增加一台新的真实服务器
-e –edit-server 编辑一条虚拟服务器记录中的某条真实服务器记录
-d –delete-server 删除一条虚拟服务器记录中的某条真实服务器记录
-L|-l –list 显示内核虚拟服务器表
-Z –zero 虚拟服务表计数器清零(清空当前的连接数量等)
–set tcp tcpfin udp 设置连接超时值
–start-daemon 启动同步守护进程。他后面可以是master 或backup,用来说
明LVS Router 是master 或是backup。在这个功能上也可以采用keepalived 的
VRRP 功能。
–stop-daemon 停止同步守护进程
-h –help 显示帮助信息
其他的选项:
-t –tcp-service service-address 说明虚拟服务器提供的是tcp 的服务
[vip:port] or [real-server-ip:port]
-u –udp-service service-address 说明虚拟服务器提供的是udp 的服务
[vip:port] or [real-server-ip:port]
-f –fwmark-service fwmark 说明是经过iptables 标记过的服务类型。
-s –scheduler scheduler 使用的调度算法,有这样几个选项
rr|wrr|lc|wlc|lblc|lblcr|dh|sh|sed|nq,
默认的调度算法是: wlc.
-p –persistent [timeout] 持久稳固的服务。这个选项的意思是来自同一个客
户的多次请求,将被同一台真实的服务器处理。timeout 的默认值为300 秒。
-M –netmask netmask persistent granularity mask
-r –real-server server-address 真实的服务器[Real-Server:port]
-g –gatewaying 指定LVS 的工作模式为直接路由模式(也是LVS 默认的模式)
-i –ipip 指定LVS 的工作模式为隧道模式
-m –masquerading 指定LVS 的工作模式为NAT 模式
-w –weight weight 真实服务器的权值
–mcast-interface interface 指定组播的同步接口
-c –connection 显示LVS 目前的连接 如:ipvsadm -L -c
–timeout 显示tcp tcpfin udp 的timeout 值 如:ipvsadm -L –timeout
–daemon 显示同步守护进程状态
–stats 显示统计信息
–rate 显示速率信息
–sort 对虚拟服务器和真实服务器排序输出
–numeric -n 输出IP 地址和端口的数字形式

--stat选项是统计自该条转发规则生效以来的包
1. Conns (connections scheduled) 已经转发过的连接数
2. InPkts (incoming packets) 入包个数
3. OutPkts (outgoing packets) 出包个数
4. InBytes (incoming bytes) 入流量(字节)
5. OutBytes (outgoing bytes) 出流量(字节)
 --rate选项是显示速率信息
1. CPS (current connection rate) 每秒连接数
2. InPPS (current in packet rate) 每秒的入包个数
3. OutPPS (current out packet rate) 每秒的出包个数
4. InBPS (current in byte rate) 每秒入流量(字节)
5. OutBPS (current out byte rate) 每秒入流量(字节)
        

0 0
原创粉丝点击