k8s的IPVS

2022-01-19  本文已影响0人  玄德公笔记

[toc]

1. 概念

1.1 简述

IPVS:IP虚拟服务器(IP Virtual Server)。是运行在LVS下的提供负载平衡功能的一种技术。
作为传输层(四层)负载均衡,可以将基于TCP和UDP的服务请求转发到真实服务器上,并使真实服务器的服务在单个 IP 地址上显示为虚拟服务。

1.2 轮询策略

实际就是lvs的轮询策略

2. ipvsadm 命令

2.1 --help

[root@DoM01 ~]# ipvsadm --help
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)

2.2 常用组合

2.2.1 ipvsadm -ln

[root@DoM01 ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  127.0.0.1:30011 rr
  -> 10.244.3.5:6380              Masq    1      0          0
  -> 10.244.6.83:6380             Masq    1      0          0
  -> 10.244.8.140:6380            Masq    1      0          0
TCP  127.0.0.1:30521 rr
  -> 10.244.4.34:30521            Masq    1      0          0
TCP  127.0.0.1:30569 rr
  -> 10.244.6.165:80              Masq    1      0          0
TCP  127.0.0.1:30572 rr
  -> 10.244.9.20:8720             Masq    1      0          0
TCP  172.17.0.1:30006 rr
  -> 10.244.5.159:3306            Masq    1      0          0
  -> 10.244.8.229:3306            Masq    1      0          0
  -> 10.244.10.236:3306           Masq    1      0          0
......

下边以 30006 这一条为例说明

[root@DoM01 ~]# ip a 
......
3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
    link/ether 02:42:dc:95:0b:b3 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:dcff:fe95:bb3/64 scope link
       valid_lft forever preferred_lft forever
......       
[root@DoM01 ~]# kubectl get service -A|grep 30006
mysql                       mysqlha-readonly                        NodePort       10.1.61.20     <none>        3306:30006/TCP                                                                               155d

定位到了mysql这各namespace 下的 mysqlha-readonly 这个service。

[root@DoM01 ~]# kubectl get pod -n mysql  -o wide
NAME                               READY   STATUS    RESTARTS   AGE    IP              NODE    NOMINATED NODE   READINESS GATES
mysqld-exporter-657cd49787-bg228   1/1     Running   0          62d    10.244.10.234   don08   <none>           <none>
mysqlha-0                          2/2     Running   0          3d2h   10.244.10.236   don08   <none>           <none>
mysqlha-1                          2/2     Running   0          22d    10.244.8.229    don06   <none>           <none>
mysqlha-2                          2/2     Running   0          21d    10.244.5.159    don03   <none>           <none>
phpmyadmin-579d966787-9gcpr        1/1     Running   1          155d   10.244.8.139    don06   <none>           <none>

2.2.2 ipvsadm -l --rate

[root@DoM01 ~]# ipvsadm -l --rate
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port                 CPS    InPPS   OutPPS    InBPS   OutBPS
  -> RemoteAddress:Port
TCP  10.10.239.100:30018                 0       37       29    11695     8651
  -> 10.244.4.68:8848                    0       32       26    10726     7469
TCP  10.10.239.100:30019                 0        0        0        0        0
  -> 10.244.4.68:7848                    0        0        0        0        0
TCP  10.10.239.100:30020                 0        1        1       48      499
  -> 10.244.8.143:6379                   0        1        1       36      375
......

说明:
CPS (current connection rate) 每秒连接数
InPPS (current in packet rate) 每秒的入包个数
OutPPS (current out packet rate) 每秒的出包个数
InBPS (current in byte rate) 每秒入流量(字节)
OutBPS (current out byte rate) 每秒入流量(字节)

2.2.3 ipvsadm -l --stats

[root@DoM01 ~]# ipvsadm -l --stats
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port               Conns   InPkts  OutPkts  InBytes OutBytes
  -> RemoteAddress:Port
......
TCP  10.10.239.100:30018           1640595  174592K  140093K   54052M   42661M
  -> 10.244.4.68:8848               921414  114163K 92214336   35976M   28035M
TCP  10.10.239.100:30019                 0        0        0        0        0
  -> 10.244.4.68:7848                    0        0        0        0        0
TCP  10.10.239.100:30020            197157  3886077  3422894  247816K    2565M
  -> 10.244.8.143:6379              197157  3886077  3422894  247816K    2565M
TCP  10.10.239.100:30021                 0        0        0        0        0
  -> 10.244.8.143:26379                  0        0        0        0        0
TCP  10.10.239.100:30022            197165  4075671  3501341  257677K    2500M
  -> 10.244.6.80:6379               197166  4075691  3501358  257678K    2500M
......

说明:
Conns (connections scheduled) 已经转发过的连接数
InPkts (incoming packets) 入包个数
OutPkts (outgoing packets) 出包个数
InBytes (incoming bytes) 入流量(字节)
OutBytes (outgoing bytes) 出流量(字节

2.2.4 超时时间

[root@DoM01 ~]# ipvsadm -ln --timeout
Timeout (tcp tcpfin udp): 900 120 300

说明:
tcpfin, 对于本端断开的socket连接,TCP保持在FIN_WAIT_2状态的时间。

# ipvsadm --set 900 60 300
上一篇 下一篇

猜你喜欢

热点阅读