一个计算机技术爱好者与学习者

0%

ipvs入门篇

1. ipvs简介

Linux Virtual Server (LVS) is load balancing software for Linux kernel–based operating systems.

LVS is a free and open-source project started by Wensong Zhang in May 1998, subject to the requirements of the GNU General Public License (GPL), version 2. The mission of the project is to build a high-performance and highly available server for Linux using clustering technology, which provides good scalability, reliability and serviceability.

The major work of the LVS project is now to develop advanced IP load balancing software (IPVS), application-level load balancing software (KTCPVS), and cluster management components.

IPVS (IP Virtual Server) implements transport-layer load balancing, usually called Layer 4 LAN switching, as part of the Linux kernel. It’s configured via the user-space utility ipvsadm(8) tool.

IPVS is incorporated into the Linux Virtual Server (LVS), where it runs on a host and acts as a load balancer in front of a cluster of real servers. IPVS can direct requests for TCP- and UDP-based services to the real servers, and make services of the real servers appear as virtual services on a single IP address. IPVS is built on top of the Netfilter.

简而言之,lvs是一个四层负载均衡软件(项目),ipvs是lvs的一部分。ipvs内置在linux内核空间中,在用户空间使用ipvsadm对ipvs进行管理。

参考文档:

2. ipvs工作原理

参考文档:

2.1. ipvs术语

  • DS:Director Server。指的是前端负载均衡器节点。
  • RS:Real Server。后端真实的工作服务器。
  • VIP:Virtual IP,向外部直接面向用户请求,作为用户请求的目标的IP地址。
  • DIP:Director Server IP,主要用于和内部主机通讯的IP地址。
  • RIP:Real Server IP,后端服务器的IP地址。
  • CIP:Client IP,访问客户端的IP地址。

2.2. ipvs基本原理

1、当用户向负载均衡调度器(Director Server)发起请求,调度器将请求发往至内核空间。
2、PREROUTING链首先会接收到用户请求,判断目标IP确定是本机IP,将数据包发往INPUT链。
3、IPVS是工作在INPUT链上的,当用户请求到达INPUT时,IPVS会将用户请求和自己已定义好的集群服务进行比对,如果用户请求的就是定义的集群服务,那么此时IPVS会强行修改数据包里的目标IP地址及端口,并将新的数据包发往POSTROUTING链。
4、POSTROUTING链接收数据包后发现目标IP地址刚好是自己的后端服务器,那么此时通过选路,将数据包最终发送给后端的服务器。

2.3. ipvs工作模式

  • nat模式:通过网络地址转换,调度器重写请求报文的目标地址,根据预设的调度算法,将请求分派给后端的真实服务器;真实服务器的响应报文通过调度器时,报文的源地址被重写,再返回给客户,完成整个负载调度过程。
  • tunnel模式:改写请求的IP地址,将请求和响应分开处理,即在负载调度器中只负责调度请求而响应直接返回给客户,将极大地提高整个集群系统的吞吐量。
  • dr模式:改写请求报文的MAC地址,将请求发送到真实服务器,而真实服务器将响应直接返回给客户。这种方法没有IP隧道的开销,对集群中的真实服务器也没有必须支持IP隧道协议的要求,但是要求调度器与真实服务器都有一块网卡连在同一物理网段上。

2.4. ipvs调度算法

固定调度算法:即调度器不会去判断后端服务器的繁忙与否,一如既往得将请求派发下去。具体包括rr、wrr、dh、sh
动态调度算法:调度器会去判断后端服务器的繁忙程度,然后依据调度算法动态得派发请求。具体包括wlc、lc、lblc、lblcr

2.4.1. rr

rr:轮询(round robin)
这种算法是最简单的,就是按依次循环的方式将请求调度到不同的服务器上,该算法最大的特点就是简单。轮询算法假设所有的服务器处理请求的能力都是一样的,调度器会将所有的请求平均分配给每个真实服务器,不管后端 RS 配置和处理能力,非常均衡地分发下去。这个调度的缺点是,不管后端服务器的繁忙程度是怎样的,调度器都会讲请求依次发下去。如果A服务器上的请求很快请求完了,而B服务器的请求一直持续着,将会导致B服务器一直很忙,而A很闲,这样便没起到均衡的左右。

2.4.2. wrr

wrr:加权轮询(weight round robin)
这种算法比 rr 的算法多了一个权重的概念,可以给 RS 设置权重,权重越高,那么分发的请求数越多,权重的取值范围 0 – 100。主要是对rr算法的一种优化和补充, LVS 会考虑每台服务器的性能,并给每台服务器添加要给权值,如果服务器A的权值为1,服务器B的权值为2,则调度到服务器B的请求会是服务器A的2倍。权值越高的服务器,处理的请求越多。

2.4.3. dh

dh:目标地址散列调度算法 (destination hash)
简单的说,即将同一类型的请求分配给同一个后端服务器,例如将以 .jgp、.png等结尾的请求转发到同一个节点。这种算法其实不是为了真正意义的负载均衡,而是为了资源的分类管理。这种调度算法主要应用在使用了缓存节点的系统中,提高缓存的命中率。

2.4.4. sh

sh:源地址散列调度算法(source hash)
即将来自同一个ip的请求发给后端的同一个服务器,如果后端服务器工作正常没有超负荷的话。这可以解决session共享的问题,但是这里有个问题,很多企业、社区、学校都是共用的一个IP,这将导致请求分配的不均衡。

2.4.5. lc

lc:最少连接数(least-connection)
这个算法会根据后端 RS 的连接数来决定把请求分发给谁,比如 RS1 连接数比 RS2 连接数少,那么请求就优先发给 RS1。这里问题是无法做到会话保持,即session共享。

2.4.6. wlc

wlc:加权最少连接数(weight least-connection)
这个比最少连接数多了一个加权的概念,即在最少连接数的基础上加一个权重值,当连接数相近,权重值越大,越优先被分派请求。

2.4.7. lblc

lblc:基于局部性的最少连接调度算法(locality-based least-connection)
将来自同一目的地址的请求分配给同一台RS如果这台服务器尚未满负荷,否则分配给连接数最小的RS,并以它为下一次分配的首先考虑。

2.4.8. lblcr

lblcr:基于地址的带重复最小连接数调度 (Locality-Based Least-Connection with Replication)

3. 安装ipvsadm

3.1. ubuntu

1
apt-get install ipvsadm

3.2. centos

1
2
3
yum install ipvsadm
#systemctl start ipvsadm
#systemctl enable ipvsadm

4. 使用ipvsadm

4.1. ipvsadm命令帮助

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
ipvsadm v1.27 2008/5/15 (compiled with popt and IPVS v1.2.1)
Usage:
ipvsadm -A|E -t|u|f service-address [-s scheduler] [-p [timeout]] [-M netmask] [--pe persistence_engine] [-b sched-flags]
ipvsadm -D -t|u|f service-address
ipvsadm -C
ipvsadm -R
ipvsadm -S [-n]
ipvsadm -a|e -t|u|f service-address -r server-address [options]
ipvsadm -d -t|u|f service-address -r server-address
ipvsadm -L|l [options]
ipvsadm -Z [-t|u|f service-address]
ipvsadm --set tcp tcpfin udp
ipvsadm --start-daemon state [--mcast-interface interface] [--syncid sid]
ipvsadm --stop-daemon state
ipvsadm -h

Commands:
Either long or short options are allowed.
--add-service -A add virtual service with options
--edit-service -E edit virtual service with options
--delete-service -D delete virtual service
--clear -C clear the whole table
--restore -R restore rules from stdin
--save -S save rules to stdout
--add-server -a add real server with options
--edit-server -e edit real server with options
--delete-server -d delete real server
--list -L|-l list the table
--zero -Z zero counters in a service or all services
--set tcp tcpfin udp set connection timeout values
--start-daemon start connection sync daemon
--stop-daemon stop connection sync daemon
--help -h display this help message

Options:
--tcp-service -t service-address service-address is host[:port]
--udp-service -u service-address service-address is host[:port]
--fwmark-service -f fwmark fwmark is an integer greater than zero
--ipv6 -6 fwmark entry uses IPv6
--scheduler -s scheduler one of rr|wrr|lc|wlc|lblc|lblcr|dh|sh|sed|nq,
the default scheduler is wlc.
--pe engine alternate persistence engine may be sip,
not set by default.
--persistent -p [timeout] persistent service
--netmask -M netmask persistent granularity mask
--real-server -r server-address server-address is host (and port)
--gatewaying -g gatewaying (direct routing) (default)
--ipip -i ipip encapsulation (tunneling)
--masquerading -m masquerading (NAT)
--weight -w weight capacity of real server
--u-threshold -x uthreshold upper threshold of connections
--l-threshold -y lthreshold lower threshold of connections
--mcast-interface interface multicast interface for connection sync
--syncid sid syncid for connection sync (default=255)
--connection -c output of current IPVS connections
--timeout output of timeout (tcp tcpfin udp)
--daemon output of daemon information
--stats output of statistics information
--rate output of rate information
--exact expand numbers (display exact values)
--thresholds output of thresholds information
--persistent-conn output of persistent connection info
--nosort disable sorting output of service/server entries
--sort does nothing, for backwards compatibility
--ops -o one-packet scheduling
--numeric -n numeric output of addresses and ports
--sched-flags -b flags scheduler flags (comma-separated)

参考文档:

4.2. 环境描述

192.168.56.254作为DS,创建虚拟服务时,虚拟服务的VIP要使用这个IP。
192.168.56.102-104作为RS,上面部署nginx服务在80端口。

4.3. 查看虚拟服务和真实服务器

1
ipvsadm -Ln

4.4. 查看ipvs模块中的链接信息

1
ipvsadm -lnc

4.5. 查看ipvs模块中的转发情况信息

1
ipvsadm -Ln --stats | --rate

4.6. 添加虚拟服务

1
ipvsadm -A -t 192.168.56.254:80 -s rr

4.7. 修改虚拟服务算法

1
ipvsadm -E -t 192.168.56.254:80 -s wlc

4.8. 删除虚拟服务

1
ipvsadm -D -t 192.168.56.254:80

4.9. 添加真实服务器

NAT模式:

1
2
ipvsadm -a -t 192.168.56.254:80 -r 192.168.56.102:80 -m
ipvsadm -a -t 192.168.56.254:80 -r 192.168.56.103:80 -m

NAT模式加权轮询:

1
ipvsadm -a -t 192.168.56.254:80 -r 192.168.56.104:80 -m -w 1

访问测试:

1
curl 192.168.56.254

4.10. 修改真实服务器

1
ipvsadm -a -t 192.168.56.254:80 -r 192.168.56.102:80 -m -w 2

4.11. 删除真实服务器

1
ipvsadm -d -t 192.168.56.254:80 -r 192.168.56.103:80

4.12. 清空转发数据计数器

1
ipvsadm -Z

4.13. 删除所有ipvs规则

1
ipvsadm -C

4.14. 保存ipvs规则

1
ipvsadm-save > /path/to/ipvsadm

4.15. 加载ipvs规则

1
ipvsadm-restore < /path/to/ipvsadm

4.16. VIP自定义

上面的例子中,我们使用的VIP是宿主机的IP,如果想要自己再定义一个IP作为VIP,该怎么搞?
答:创建一个VIP,绑定给宿主机的网卡。

例如,创建一个VIP为10.0.0.10/24,后端RIP为192.168.56.102/24

1
2
3
4
ip addr add 10.0.0.10/24 dev enp0s3:0
ipvsadm -A -t 10.0.0.10:80 -s rr
ipvsadm -a -t 10.0.0.10:80 -r 192.168.56.102:80 -m
curl 10.0.0.10
  • 本文作者: 好好学习的郝
  • 原文链接: https://www.voidking.com/dev-ipvs-start/
  • 版权声明: 本文采用 BY-NC-SA 许可协议,转载请注明出处!源站会即时更新知识点并修正错误,欢迎访问~
  • 微信公众号同步更新,欢迎关注~