负载均衡软件lvs实习

前言

mysql数据库必须融合到整个IT基础解决体系中,才能真正高效优质支撑应用系统的发展及运行。负载均衡软件作为其中的重要一环,必须认真了解与掌握,才能快速部署满足业务需要的IT解决方案。lvs是负载均衡软件非常优秀的一款产品。免费开源,普及度高。配置简单。

1,ipvsadm命令信息

[root@mygirl ipvsadm-1.26]# ipvsadm --help

ipvsadm v1.26 2008/5/15 (compiled with popt and IPVS v1.2.1)

Usage:

ipvsadm -AE -tuf service-address [-s scheduler] [-p [timeout]] [-M netmask] [--pe persistence_engine]

ipvsadm -D -tuf service-address

ipvsadm -C

ipvsadm -R

ipvsadm -S [-n]

ipvsadm -ae -tuf service-address -r server-address [options]

ipvsadm -d -tuf service-address -r server-address

ipvsadm -Ll [options]

ipvsadm -Z [-tuf 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 rrwrrlcwlclblclblcrdhshsednq,

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

[root@mygirl ipvsadm-1.26]#

1,显示lvs配置规则

[root@mygirl ipvsadm-1.26]# ipvsadm -l

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

-> RemoteAddress:Port Forward Weight ActiveConn InActConn

2,添加virtual service

[root@mygirl Packages]# ipvsadm -A -t 10.0.0.5:80 -s wlc

[root@mygirl Packages]# ipvsadm

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

-> RemoteAddress:Port Forward Weight ActiveConn InActConn

TCP mygirl:http wlc

[root@mygirl Packages]#

3,编辑virtual service

[root@mygirl Packages]# ipvsadm -E -t 10.0.0.5:80 -s wrr

[root@mygirl Packages]#

[root@mygirl Packages]# ipvsadm

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

-> RemoteAddress:Port Forward Weight ActiveConn InActConn

TCP mygirl:http wrr

[root@mygirl Packages]#

4,向virtual service添加real server

(注:virtual service包含2个real server)

[root@mygirl Packages]# ipvsadm -a -t 10.0.0.5:80 -r 10.0.0.8:80

[root@mygirl Packages]#

[root@mygirl Packages]# ipvsadm -a -t 10.0.0.5:80 -r 10.0.0.7:80

[root@mygirl Packages]# ipvsadm

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

-> RemoteAddress:Port Forward Weight ActiveConn InActConn

TCP mygirl:http wrr

-> 10.0.0.7:http Route 1 0 0

-> 10.0.0.8:http Route 1 0 0

[root@mygirl Packages]#

5,修改virtual service中的real server

[root@mygirl Packages]# ipvsadm -e -t 10.0.0.5:80 -r 10.0.0.7 -m -w 10

[root@mygirl Packages]#

(注:forward列对应ipvsadm选项-m)

[root@mygirl Packages]# ipvsadm

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

-> RemoteAddress:Port Forward Weight ActiveConn InActConn

TCP mygirl:http wrr

-> 10.0.0.7:http Masq 10 0 0

-> 10.0.0.8:http Route 1 0 0

6,删除virtual service中的real server

[root@mygirl Packages]# ipvsadm -d -t 10.0.0.5:80 -r 10.0.0.7

[root@mygirl Packages]# ipvsadm

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

-> RemoteAddress:Port Forward Weight ActiveConn InActConn

TCP mygirl:http wrr

-> 10.0.0.8:http Route 1 0 0

7,删除virtual service

[root@mygirl Packages]# ipvsadm -D -t 10.0.0.5:80

[root@mygirl Packages]# ipvsadm

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

-> RemoteAddress:Port Forward Weight ActiveConn InActConn

[root@mygirl Packages]#

8,清除ipvsadm全部规则

[root@mygirl Packages]# ipvsadm -A -t 10.0.0.5:80

[root@mygirl Packages]# ipvsadm -A -t 10.0.0.6:80

[root@mygirl Packages]#

[root@mygirl Packages]# ipvsadm

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

-> RemoteAddress:Port Forward Weight ActiveConn InActConn

TCP mygirl:http wlc

TCP 10.0.0.6:http wlc

[root@mygirl Packages]# ipvsadm -C

[root@mygirl Packages]# ipvsadm

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

-> RemoteAddress:Port Forward Weight ActiveConn InActConn

[root@mygirl Packages]#

9,--scheduler为负载均衡调度算法

(注:共计10种)

--scheduler -s scheduler one of rrwrrlcwlclblclblcrdhshsednq,

the default scheduler is wlc.

10,默认负载均衡调度算法为wlc

[root@mygirl Packages]# ipvsadm -A -t 10.0.0.6:80

[root@mygirl Packages]# ipvsadm

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

-> RemoteAddress:Port Forward Weight ActiveConn InActConn

TCP 10.0.0.6:http wlc

[root@mygirl Packages]#

[root@mygirl Packages]#

11,测试负载均衡调度算法为nq

(注:其它同理,不再复述)

[root@mygirl Packages]# ipvsadm -A -t 10.0.0.8:80 -s nq

[root@mygirl Packages]# ipvsadm

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

-> RemoteAddress:Port Forward Weight ActiveConn InActConn

TCP 10.0.0.6:http wlc

TCP 10.0.0.8:http nq

[root@mygirl Packages]#

12,负载均衡调度算法含义

1. 轮叫调度 rr

这种算法是最简单的,就是按依次循环的方式将请求调度到不同的服务器上,该算法最大的特点就是简单。轮询算法假设所有的服务器处理请求的能力都是一样的,

调度器会将所有的请求平均分配给每个真实服务器,不管后端 RS 配置和处理能力,非常均衡地分发下去。

2. 加权轮叫 wrr

这种算法比 rr 的算法多了一个权重的概念,可以给 RS 设置权重,权重越高,那么分发的请求数越多,权重的取值范围 0 – 100。主要是对rr算法的一种优化和补充, LVS 会考虑每台服务器的性能,

并给每台服务器添加要给权值,如果服务器A的权值为1,服务器B的权值为2,则调度到服务器B的请求会是服务器A的2倍。权值越高的服务器,处理的请求越多。

3. 最少链接 lc

这个算法会根据后端 RS 的连接数来决定把请求分发给谁,比如 RS1 连接数比 RS2 连接数少,那么请求就优先发给 RS1

4. 加权最少链接 wlc

这个算法比 lc 多了一个权重的概念。

5. 基于局部性的最少连接调度算法 lblc

这个算法是请求数据包的目标 IP 地址的一种调度算法,该算法先根据请求的目标 IP 地址寻找最近的该目标 IP 地址所有使用的服务器,如果这台服务器依然可用,并且有能力处理该请求,

调度器会尽量选择相同的服务器,否则会继续选择其它可行的服务器

6. 复杂的基于局部性最少的连接算法 lblcr

记录的不是要给目标 IP 与一台服务器之间的连接记录,它会维护一个目标 IP 到一组服务器之间的映射关系,防止单点服务器负载过高。

7. 目标地址散列调度算法 dh

该算法是根据目标 IP 地址通过散列函数将目标 IP 与服务器建立映射关系,出现服务器不可用或负载过高的情况下,发往该目标 IP 的请求会固定发给该服务器。

8. 源地址散列调度算法 sh

与目标地址散列调度算法类似,但它是根据源地址散列算法进行静态分配固定的服务器资源

9,最少期望延迟调度算法 sed

The shortest expected delay scheduling algorithm assigns network connections to the server with the shortest expected delay.

The expected delay that the job will experience is (Ci + 1) / Ui if sent to the ith server, in which Ci is the number of connections

on the the ith server and Ui is the fixed service rate (weight) of the ith server.

或者

Shortest Expect Delay 最短期望延迟,wlc的改进算法,简单地说就是调度器会选择响应速度快的RS(真实)服务器,先选择权重(weight)

较大的服务器计算方法:Overhead=(Active+1)*256/weight 这种方式可能会连接多个请求都发送给一台性能较强(即weight值较大)的RS,而别的RS却连一个请求都不会接收到。

10,nq调度算法

nq:Nerver Queus: sed算法的改进,永不排队,每个RS至少先处理一个请求,然后再按sed算法来。

  • 发表于:
  • 原文链接http://kuaibao.qq.com/s/20180206G0UFQI00?refer=cp_1026
  • 腾讯「云+社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 yunjia_community@tencent.com 删除。

扫码关注云+社区

领取腾讯云代金券