[云原生网络]到底什么是IPVS

352 阅读5分钟

实话讲IPVS技术我第一次在是在kube-proxy的各种模式中发现的,对于iptables有过了解的我,第一发现IPVS只知道它的性能相较于iptables 更好,对应的时间复杂度更低,后面才渐渐了解到ipvs到底是什么,在这里记录下我的理解。

一 IPVS的来源已经功能

首先想要了解IPVS 那么一定跳不过LVS,我们通过官网可以了解到LVS指的是Linux 虚拟服务器是一个高度可扩展的 以及建立在真实服务器集群上的高可用性服务器。而IPVS是LVS中的一个子部分ipvs我们根据IPVS的官网链接也可以看的出IPVS是lvs整体中的一个子软件,官网描述的是IPVS(IP 虚拟服务器)实现了传送层负载平衡 在 Linux 内核中,即所谓的第 4 层交换。运行在 上的 IPVS 主机充当 Real 集群前端的负载均衡器 服务器,它可以将基于TCP/UDP的服务请求定向到真正的 服务器,并使真实服务器的服务显示为虚拟服务器 单个 IP 地址上的服务。简单点来说IPVS一个四层代理

IPVS与netfilter

ipvs和iptables一样也集成在netfilter 框架中,在我上一篇文章iptables中可以知道iptables 的五链四表中,5链分别对应着netfilter 的5个hook点,ipvs对应的hook点在Local_INFORWARDLOCAL_OUT这三个hook 上 《一篇搞懂》系列之二——IPVS - 知乎 (zhihu.com)

ipvs 工作原理

ipvs 通过在netfilter 框架中的不同位置注册自己的处理函数来捕获数据包,并根据与ipvs相关的信息表对数据进行处理,按照ipvs 规则中定义的不同的包转发模式,对数据包进行不同的转发处理

ipvsadm

ipvsadm 是位于用户空间,为用户提供了向ipvs中写入规则的一个管理ipvs 的必备工具 centos 安装 yum install -y ipvsadm 用法

[root@cni-1 ~]# ipvsadm -h

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)

ipvs 包转发模式

TODO 这里的转发模式需要再详细描述下

  • -mnat 也叫地址伪装模式(masquerading)
  • -g direct routing 默认模式网关模式(gateway)
  • -i ip tunneling ipip封装模式(ipip encapsulation)

nat 模式

direct routing 模式

IP Tunneling 模式

ipvs均衡算法

round robin rr

-s rr 轮询模式,这种算法会将新请求的调度到后端服务列表中的下一个服务器上

weighted round-robin

-s wrr 加权轮询:根据每台后端服务器配置的权重生成轮询序列

Least-Connection

-s lc 最小连接数调度,动态调度算法根据当前后端服务器的连接数来动态选择一个连接数最小的后端服务进行数据包调度,所以他需要动态记录每一台后端服务器的连接情况。 这种算法适合后端服务器工作性能差不多,不适用于后端服务器性能差别较大,因为tcp 报文中有TIME_WAIT状态,TIME_WAIT状态会在断开连接后保持2msl也就是说服务器处理完请求并返回后不会立即释放,

weighted least-connection

-s wlc 加权最小连接数调度,可以理解成加权轮询和最小连接数组合了一下,该算法可以给性能较好的后端服务器配置较高的权重,以此来接收更多的数据包,当心的数据包到来时,根据公式 min(ci/wi) (i = 1...n) n代表后端服务器的个数,i代表某个后端服务器,wi表示后端服务器的权重,ci 表示当前服务器的连接数。计算所有的后端服务器节点的连接数和权重的比值,取最小的那个数对应的服务器来调度

locality-based least-connection

-s lblc 本地的最少连接数调度

Locality-Based Least-Connection with Replication

-s lblcr 多副本的lblc

Destination Hashing

-s dh 目的地址匹配

Source Hashing

-s sh 源地址匹配

Shortest Expected Delay

-s sed 最短期望延迟

Never Queue

-s nq 用不排队,该算法采用双速模型(two-speed model),选择后端服务器的判断如下

  • 如果有空闲的后端服务器,那么数据包将被调度到这台服务器上,不论性能好坏
  • 如果没有空闲的服务器,那么采用Shortest Expected Delay 算法调度数据包