we23213 发表于 2014-11-17 14:18:42

LVS—NAT群集配置

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 的服务
or
-u --udp-service service-address 说明虚拟服务器提供的是udp 的服务
or
-f --fwmark-service fwmark 说明是经过iptables 标记过的服务类型。
-s --scheduler scheduler 使用的调度算法,有这样几个选项
rr|wrr|lc|wlc|lblc|lblcr|dh|sh|sed|nq,
默认的调度算法是: wlc.
-p --persistent 持久稳固的服务。这个选项的意思是来自同一个客
户的多次请求,将被同一台真实的服务器处理。timeout 的默认值为300 秒。
-M --netmask netmask persistent granularity mask
-r --real-server server-address 真实的服务器
-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)



使用方法

1
2
3
4
# ipvsadm -A -t VirtualIP:PROT
# ipvsadm -a -t VirtualIP:PROT -r realserverIP -m
# sysctl -w net.ipv4.ip_forward=1
net.ipv4.ip_forward = 1




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


1
2
# ifconfig eth0 172.15.1.2/24
# route add default gw 172.15.1.254




安装Apache服务

1
2
3
4
# yum install httpd
# cd /var/www/html/
# touch index.html
# echo "ipvsadm 2" > index.html




RealServer 2

1
2
3
# ifconfig eth0 172.15.1.3/24
# route add default gw 172.15.1.254
# yum install httpd




安装Apache服务

1
2
3
4
# yum install httpd
# cd /var/www/html/
# touch index.html
# echo "ipvsadm 3" > index.html





验证两Realserverhttpd服务配置成功

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
# 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

# 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
# ifconfig eth0:0 192.168.1.250/24
# ifconfig eth2 172.15.1.254/24




打开网络地址转发

1
2
# sysctl -w net.ipv4.ip_forward=1
net.ipv4.ip_forward = 1




3)配置群集服务
安装ipvsadm

1
2
3
4
# 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
# 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






5)查看群集状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# ipvsadm -L -n--stats    查看统计信息
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port               Conns   InPktsOutPktsInBytes OutBytes
-> RemoteAddress:Port
TCP192.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
TCP192.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
# ipvsadm -L -n--rate    查看访问速率
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port               CPS    InPPS   OutPPS    InBPS   OutBPS
-> RemoteAddress:Port
TCP192.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
TCP192.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
# ipvsadm -D -t 192.168.1.250:22




保存当前配置

1
2
3
4
5
6
# service ipvsadm save
ipvsadm: Saving IPVS table to /etc/sysconfig/ipvsadm:      
# 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
# ipvsadm -S > ./ipvsadm.txt
# 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
# ipvsadm -C
# 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
# ipvsadm -R < ./ipvsadm.txt
# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port         Forward Weight ActiveConn InActConn
TCP192.168.1.250:22 wlc
-> 172.15.1.2:22                Masq    1      1          0         
-> 172.15.1.3:22                Masq    1      2          0         
TCP192.168.1.250:80 rr
-> 172.15.1.2:80                Masq    1      0          0         
-> 172.15.1.3:80                Masq    1      0          0




修改调度算法

1
# ipvsadm -E -t 192.168.1.250:80 -s rr




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

页: [1]
查看完整版本: LVS—NAT群集配置