设为首页 收藏本站
查看: 1346|回复: 0

[经验分享] LVS—NAT群集配置

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-11-17 14:18:42 | 显示全部楼层 |阅读模式
LVS是4(传输)层群集解决方案NAT,DR,TUN,FULLNAT四种模型本文主要介绍NAT模型
NAT模型的特性
RealServer应该使用私有IP地址,节约IP地址;
RealServer的网关应该指向DirectorIP;
RIP和DIP应该在同一个网段内;
进出的报文都得经过DirectoryServer,在高负载下,DirectoryServer会成为系统性能瓶颈,所以可能只适用于中小企业;
支持端口映射;
RealServer可以使用任意OS:
ipvsadm 用法基本介绍
命令选项解释:

有两种命令选项格式,长的和短的,具有相同的意思。在实际使用时,两种都可
以。
-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 地址和端口的数字形式

ipvs采用如下10种调度算法

静态算法
轮叫调度rr(Round-Robin Scheduling)
加权轮叫调度wrr(Weighted Round-Robin Scheduling)
源地址散列调度sh(Source Hashing Scheduling)
目标地址散列调度dh(Destination Hashing Scheduling)
动态算法
最小连接调度lc(Least-Connection Scheduling)
加权最小连接调度wlc(Weighted Least-Connection Scheduling)
基于局部性的最少链接lblc(Locality-Based Least Connections Scheduling)
带复制的基于局部性最少链接lblcr(LocalityBased Least Connections with Replication Scheduling)
最短预期延时调度sed(Shortest Expected Delay Scheduling)
不排队调度nq(Never Queue Scheduling)


wKioL1Roj7CiwgSOAAPVVJkoR4E728.jpg
使用方法
1
2
3
4
[iyunv@www ~]# ipvsadm -A -t VirtualIP:PROT
[iyunv@www ~]# ipvsadm -a -t VirtualIP:PROT -r realserverIP -m
[iyunv@www ~]# sysctl -w net.ipv4.ip_forward=1
net.ipv4.ip_forward = 1



1)后端RealServer服务器配置
RealServer 1

1
2
[iyunv@localhost ~]# ifconfig eth0 172.15.1.2/24
[iyunv@localhost ~]# route add default gw 172.15.1.254



安装Apache服务
1
2
3
4
[iyunv@localhost ~]# yum install httpd
[iyunv@localhost ~]# cd /var/www/html/
[iyunv@localhost html]# touch index.html
[iyunv@localhost html]# echo "ipvsadm 2" > index.html



RealServer 2
1
2
3
[iyunv@localhost ~]# ifconfig eth0 172.15.1.3/24
[iyunv@localhost ~]# route add default gw 172.15.1.254
[iyunv@localhost ~]# yum install httpd



安装Apache服务
1
2
3
4
[iyunv@localhost ~]# yum install httpd
[iyunv@localhost ~]# cd /var/www/html/
[iyunv@localhost html]# touch index.html
[iyunv@localhost html]# echo "ipvsadm 3" > index.html




验证两Realserver  httpd服务配置成功
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[iyunv@CentOS ~]# curl -i 172.15.1.2
HTTP/1.1 200 OK
Date: Sun, 16 Nov 2014 10:57:09 GMT
Server: Apache/2.2.15 (CentOS)
Last-Modified: Sun, 16 Nov 2014 10:45:41 GMT
ETag: "6154f-a-507f79036a1f3"
Accept-Ranges: bytes
Content-Length: 10
Connection: close
Content-Type: text/html; charset=UTF-8

ipvsadm 2

[iyunv@CentOS ~]# curl -i 172.15.1.3
HTTP/1.1 200 OK
Date: Sun, 16 Nov 2014 10:59:43 GMT
Server: Apache/2.2.15 (CentOS)
Last-Modified: Sat, 15 Nov 2014 12:05:49 GMT
ETag: "2735f-a-507e490f7c682"
Accept-Ranges: bytes
Content-Length: 10
Connection: close
Content-Type: text/html; charset=UTF-8

ipvsadm 3




2)Director Server配置
将VirtualIP配置在eth0的别名上
1
2
[iyunv@www ~]# ifconfig eth0:0 192.168.1.250/24
[iyunv@www ~]# ifconfig eth2 172.15.1.254/24



打开网络地址转发
1
2
[iyunv@www ~]# sysctl -w net.ipv4.ip_forward=1
net.ipv4.ip_forward = 1



3)配置群集服务
安装ipvsadm
1
2
3
4
[iyunv@www ~]# yum install ipvsadm
ipvsadm -A -t 192.168.1.250:80 -s rr 如果不指定调度算法默认是wlc
ipvsadm -a -t 192.168.1.250:80 -r 172.15.1.2 -m
ipvsadm -a -t 192.168.1.250:80 -r 172.15.1.3 -m



4)验证
1
2
3
4
5
6
7
8
9
10
11
12
[iyunv@www ~]# curl -i 192.168.1.250
HTTP/1.1 200 OK
Date: Sun, 16 Nov 2014 11:08:57 GMT
Server: Apache/2.2.15 (CentOS)
Last-Modified: Sun, 16 Nov 2014 10:45:41 GMT
ETag: "6154f-a-507f79036a1f3"
Accept-Ranges: bytes
Content-Length: 10
Connection: close
Content-Type: text/html; charset=UTF-8

ipvsadm 2



wKiom1RojyOhh9bwAACZxXigriI995.jpg

5)查看群集状态
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[iyunv@www ~]# ipvsadm -L -n  --stats    查看统计信息
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port               Conns   InPkts  OutPkts  InBytes OutBytes
  -> RemoteAddress:Port
TCP  192.168.1.250:22                    3      983      715    71154    57282
  -> 172.15.1.2:22                       2      611      433    44165    35533
  -> 172.15.1.3:22                       1      372      282    26989    21749
TCP  192.168.1.250:80                  138      624      418    69800    44372
  -> 172.15.1.2:80                      69      319      215    35834    22456
  -> 172.15.1.3:80                      69      305      203    33966    21916
[iyunv@www ~]# ipvsadm -L -n  --rate    查看访问速率
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port                 CPS    InPPS   OutPPS    InBPS   OutBPS
  -> RemoteAddress:Port
TCP  192.168.1.250:22                    0        0        0        2        1
  -> 172.15.1.2:22                       0        0        0        2        1
  -> 172.15.1.3:22                       0        0        0        0        0
TCP  192.168.1.250:80                    0        0        0        0        0
  -> 172.15.1.2:80                       0        0        0        0        0
  -> 172.15.1.3:80                       0        0        0        0        0



删除群集
1
[iyunv@www ~]# ipvsadm -D -t 192.168.1.250:22



保存当前配置
1
2
3
4
5
6
[iyunv@www ~]# service ipvsadm save
ipvsadm: Saving IPVS table to /etc/sysconfig/ipvsadm:      [  OK  ]
[iyunv@www ~]# cat /etc/sysconfig/ipvsadm
-A -t 192.168.1.250:80 -s rr
-a -t 192.168.1.250:80 -r 172.15.1.2:80 -m -w 1
-a -t 192.168.1.250:80 -r 172.15.1.3:80 -m -w 1



将当前配置定义到其他位置
1
2
3
4
5
6
7
8
[iyunv@www ~]# ipvsadm -S > ./ipvsadm.txt
[iyunv@www ~]# cat ./ipvsadm.txt
-A -t 192.168.1.250:ssh -s wlc
-a -t 192.168.1.250:ssh -r 172-15-1-2.lightspeed.stlsmo.sbcglobal.net:ssh -m -w 1
-a -t 192.168.1.250:ssh -r 172-15-1-3.lightspeed.stlsmo.sbcglobal.net:ssh -m -w 1
-A -t 192.168.1.250:http -s rr
-a -t 192.168.1.250:http -r 172-15-1-2.lightspeed.stlsmo.sbcglobal.net:http -m -w 1
-a -t 192.168.1.250:http -r 172-15-1-3.lightspeed.stlsmo.sbcglobal.net:http -m -w 1



清除当前所有规则
1
2
3
4
5
[iyunv@www ~]# ipvsadm -C
[iyunv@www ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn



恢复规则
1
2
3
4
5
6
7
8
9
10
11
[iyunv@www ~]# ipvsadm -R < ./ipvsadm.txt
[iyunv@www ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.1.250:22 wlc
  -> 172.15.1.2:22                Masq    1      1          0         
  -> 172.15.1.3:22                Masq    1      2          0         
TCP  192.168.1.250:80 rr
  -> 172.15.1.2:80                Masq    1      0          0         
  -> 172.15.1.3:80                Masq    1      0          0



修改调度算法
1
[iyunv@www ~]# ipvsadm -E -t 192.168.1.250:80 -s rr



NAT模型的LVS有很大的局限性所以企业里面应该会用DR模型。


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-30470-1-1.html 上篇帖子: LVS DR模型环境搭建 下篇帖子: LVS (Linux Virtual Server)集群项目实践
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表