ipvsadm實用總結

ipvsadm命令格式說明

以ipvsadm v1.27版本爲例說明ipvsadm命令使用方式,對應內核ipvs版本v1.2.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)

ipvsadm命令分爲命令(Commands)和選項(Options)兩部分。命令有兩種形式,一個是全寫,一個是縮寫,功能一樣,實際使用中一般縮寫使用更頻繁。
常用命令說明如下:

常用命令 說明
-A --add-service 添加一條虛擬服務記錄
-E --edit-service 編輯一條虛擬服務記錄
-D --delete-service 刪除一條虛擬服務記錄
-C --clear 清空虛擬服務ipvs規則
-R --restore 恢復虛擬服務規則
-S --save 保存虛擬服務規則
-a --add-server 指定一條內核虛擬服務器記錄添加一條real server記錄
-e --edit-server 指定一條內核虛擬服務器記錄編輯一條real server記錄
-d --delete-server 指定一條內核虛擬服務器記錄刪除一條real server記錄
-L -l --list 顯示內核虛擬服務器列表
-Z --zero 虛擬服務表計數器清零(--stats)
--set tcp tcpfin udp 設置連接超時時間,分別爲TCP、TCPFIN(TCP關閉連接狀態)、UDP連接超時時間。
--start-daemon 啓動同步守護進程。在選項後面指定自己角色,是master還是backup,主負載調度器會同步所有策略及連接狀態到備負載調度器,當主故障,備可以立即接替工作
--stop-daemon 停止同步守護進程

選項同樣也有兩種形式,一個是全寫,一個是縮寫,功能一致。
常用選項說明如下:

常用選項 說明
--tcp-service -t service-address 服務IP使用TCP協議,IP[:port]
--udp-service -u service-address 服務IP使用UDP協議,IP[:port]
--fwmark-service -f fwmark 使用整型的防火牆標記標識集羣服務,可以結合iptables將多個以調度器爲目標的端口定義成一個防火牆標識,ipvsadm通過此關聯標識,可以實現對一個IP多個端口調度,即實現後端服務器可以開放多個服務
--scheduler -s scheduler 參數爲rr/wrr/lc/wlc/lblc/lblcr/dh/sh/sed/nq其中之一,默認調度算法是wlc
--pe engine 設置持久化引擎如源IP,默認不設置
--persistent -p [timeout] 開啓持久化,在指定時間timeout內,來自同一個IP的請求都會轉發到同一臺real server
--netmask -M netmask 使用網絡掩碼來屏蔽持久化源IP範圍,默認32位掩碼,即所有源IP的請求都會享受持久化服務
--real-server -r server-address 指定real server的IP和port
--gatewaying -g 設置轉發模式使用DR模式(direct routing),默認配置
--ipip -i 設置轉發模式使用TUN模式
--masquerading -m 設置轉發模式使用NAT模式
--weight -w weight 設置real server的權重
--u-threshold -x uthreshold 設置轉發請求的最大上連接閾值,範圍0~65535,當連接數超過上限,lvs則不會轉發請求
--l-threshold -y lthreshold 設置轉發請求的下連接閾值,範圍0~65535,當連接數降低至指定值,lvs則繼續提供服務,默認0
--mcast-interface interface 設置用於連接同步守護進程的組播接口
--syncid sid 設置連接同步守護進程的SID號,用於標識連接同步組,範圍0~255
--connection -c 輸出lvs的當前連接,一般和“-l”共用
--timeout 輸出tcp、tcpfin、udp的超時信息,一般和“-l”共用
--daemon 輸出連接同步守護信息,一般和“-l”共用
--stats 輸出lvs統計信息,一般和“-l”共用
--rate 輸出轉發速率信息,一般和“-l”共用
--exact 顯示數據包和字節數的準確值,擴大字符長度
--thresholds 顯示閾值信息,一般和“-l”共用
--persistent-conn 輸出持久化連接信息,一般和“-l”共用
--numeric -n 將輸出的IP和port以數字化顯示
--sched-flags -b flags 設置調度算法的範圍表示,用於SH算法,兩個標識:sh-fallback,如果real server不可用,將其轉發到其他real server;sh-port,將源地址的端口號也添加到散列鍵=值中

ipvsadm命令實用舉例

  • 添加一條虛擬服務器記錄,指定加權輪詢調度方式,然後指定該虛擬服務器記錄添加real server,指定DR轉發模式和權重

    ipvsadm -A -t 192.168.14.100:80 -s wrr
    ipvsadm -a -t 192.168.14.100:80 -r 192.168.14.129 -g -w 1
    ipvsadm -a -t 192.168.14.100:80 -r 192.168.14.130 -g -w 1
  • 查看當前內核虛擬服務器表及其統計數據,“-n”參數將所有輸出以數字顯示

    [root@proxy ~]# ipvsadm -ln --stats
    IP Virtual Server version 1.2.1 (size=4096)
    Prot LocalAddress:Port               Conns   InPkts  OutPkts  InBytes OutBytes
    -> RemoteAddress:Port
    TCP  192.168.14.100:80                   0        0        0        0        0
    -> 192.168.14.129:80                   0        0        0        0        0
    -> 192.168.14.130:80                   0        0        0        0        0
    [root@proxy ~]# ipvsadm -l --stats 
    IP Virtual Server version 1.2.1 (size=4096)
    Prot LocalAddress:Port               Conns   InPkts  OutPkts  InBytes OutBytes
    -> RemoteAddress:Port
    TCP  proxy:http                          0        0        0        0        0
    -> 192.168.14.129:http                 0        0        0        0        0
    -> 192.168.14.130:http                 0        0        0        0        0
  • 參數“-S”將現有ipvs規則保存輸出到stdout,可重定向到配置中保存。另外,只用-S會將IP和port轉義成server name和常用服務口,加 -n參數可設置數字輸出
[root@proxy ~]# ipvsadm -S
-A -t proxy:http -s wrr
-a -t proxy:http -r 192.168.14.129:http -g -w 1
-a -t proxy:http -r 192.168.14.130:http -g -w 1
[root@proxy ~]# ipvsadm -Sn
-A -t 192.168.14.100:80 -s wrr
-a -t 192.168.14.100:80 -r 192.168.14.129:80 -g -w 1
-a -t 192.168.14.100:80 -r 192.168.14.130:80 -g -w 1
  • -R --restore可以從stdin從恢復虛擬服務記錄,和-S --save正好對應:
    [root@proxy ~]# ipvsadm -Sn > ipvs_test
    [root@proxy ~]# ipvsadm --restore < ipvs_test
    [root@proxy ~]# ipvsadm -ln
    IP Virtual Server version 1.2.1 (size=4096)
    Prot LocalAddress:Port Scheduler Flags
    -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
    TCP  192.168.14.100:80 wrr
    -> 192.168.14.129:80            Route   1      0          0         
    -> 192.168.14.130:80            Route   1      0          0  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章