ipvsadm使用和命令

ipvsadm是LVS在應用層的管理命令,我們可以通過這個命令去管理LVS的配置。需要使用yum單獨安裝。

modprobe -c|grep ipvs 查內核模塊
alias ip6t_ipvs xt_ipvs
alias ipt_ipvs xt_ipvs

查ipvsadm由某個rpm包提供:yum provides ipvsadm
查是否安裝了:rpm -q ipvsadm
安裝:yum install -y ipvsadm

基本用法:
ipvsadm COMMAND [protocol] service-address
[scheduling-method] [persistence options]
ipvsadm command [protocol] service-address
server-address [packet-forwarding-method]
[weight options]

第一條命令用於向LVS系統中添加一個用於負載均衡的virtual server(VS);第二條命令用來修改已經存在的VS的配置,service address用來指定涉及的虛擬服務即虛擬地址,server-address指定涉及的真實地址。

ipvsadm -h 或 man ipvsadm 查命令幫助.man手冊中有EXAMPLE例子

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

命令:
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

選項:
--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)
[root@node1 yum.repos.d]#

其中:
Virtual-service-address是指虛擬服務器的IP地址,本文是192.168.20.200;
Real-service-address是指Real Server的IP地址,本文是192.168.60.132/144;
Scheduler:指定調度算法。

ipvsadm命令選項的詳細含義
-A --add-service:在內核的虛擬服務器列表中添加一條新的虛擬IP記錄,也就是爲ipvs虛擬服務器添加一個虛擬服務。虛擬IP也就是虛擬服務器的IP地址。虛擬地址需要是ip地址,端口號,協議的形式。
-E --edit-service:編輯內核虛擬服務器列表中的一條虛擬服務器記錄
-D --delete-service:刪除內核虛擬服務器列表中的一條虛擬服務器記錄
-C --clear:清除內核虛擬服務器列表中的所有記錄
-R --restore:恢復虛擬服務器規則;從標準輸入獲取ipvsadm命令。一般結合下邊的-S使用。
-S --save:保存虛擬服務器規則,從標準輸出輸出虛擬服務器的規則。可以將虛擬服務器的規則保存,在以後通過-R直接讀入,以實現自動化配置。
-a --add-server:在內核虛擬服務器表的一條記錄裏添加一條新的真實服務器記錄(RIP)。也就是在一個虛擬服務器中增加一臺新的真實服務器
-e --edit-server:編輯一條虛擬服務器記錄中的某條真實服務器記錄
-d --delete-server:刪除一條虛擬服務器記錄中的某條真實服務器記錄
-L|-l --list:顯示內核中的虛擬服務器列表;列出虛擬服務表中的所有虛擬服務。可以指定地址。添加-c顯示連接表。
-Z --zero:虛擬服務器列表計數器清零(即清空當前的連接數量)這些記錄一般用於調度策略。
--set tcp tcpfin udp:設置連接超時值
--start-daemon 啓動同步守護進程。他後面可以是master 或backup,用來說明LVS Router 是master 或是backup。在這個功能上也可以採用keepalived 的VRRP 功能。置虛擬服務器的備服務器,用來實現主備服務器冗餘。(注:該功能只支持ipv4)
--stop-daemon 停止同步守護進程;停止備服務器
-h --help 顯示幫助信息
-p --persistent [timeout] 持久穩固的服務(持久性連接)。這個選項的意思是來自同一個客戶的多次請求,將被同一臺真實的服務器處理。timeout 的默認值爲360 秒。
-t:說明虛擬服務器提供的是TCP服務,此選項後面跟的格式是[virtual-service-address:port]或[real-server-ip:port];service-address要是host[:port]的形式。端口是0表示任意端口。如果需要將端口設置爲0,還需要加上-p選項(持久連接)。
-u:說明虛擬服務器提供的是UDP服務,此選項後面跟的格式是[virtual-service-address:port]或[real-server-ip:port]
-f fwmark:說明是經過iptables標記過的服務類型;用firewall mark取代虛擬地址來指定要被負載均衡的數據包,可以通過這個命令實現把不同地址、端口的虛擬地址整合成一個虛擬服務,可以讓虛擬服務器同時截獲處理去往多個不同地址的數據包。fwmark可以通過iptables命令指定。如果用在ipv6需要加上-6。
-s scheduler:此選項後面跟LVS使用的調度算法,有調度算法可以指定以下8種:rr(輪詢),wrr(權重),lc(最後連接),wlc(權重),lblc(本地最後連接),lblcr(帶複製的本地最後連接),dh(目的地址哈希),sh(源地址哈希),sed(最小期望延遲),nq(永不排隊),默認的是wlc調度算法。
-p [timeout]:在某個Real Server上持續的服務時間。也就是說來自同一個用戶的多次請求,將被同一個Real Server處理。此參數一般用於有動態請求的操作中,通常用於ftp或者ssl中。timeout的默認值爲300s。如-p 600表示持續服務時間爲600s。
-M --netmask netmask 指定客戶地址的子網掩碼。用於將同屬一個子網的客戶的請求轉發到相同服務器。
-r:指定真實服務器r的IP地址,此選項後面跟如下格式[real-server-ip:port];爲虛擬服務指定數據可以轉發到的真實服務器的地址。可以添加端口號。如果沒有指定端口號,則等效於使用虛擬地址的端口號。
-g --gatewaying:指定LVS的工作模式爲直接路由模式;使用網關(即直接路由)(此模式是LVS的默認工作模式)
-i --ipip:指定LVS的工作模式爲ipip隧道模式
-m --masquerading:指定LVS的工作模式爲NAT模式
-w --weight wegight:指定真實服務器的權重值;權重是0~65535的整數。如果將某個真實服務器的權重設置爲0,那麼它不會收到新的連接,但是已有連接還會繼續維持(這點和直接把某個真實服務器刪除時不同的)。
--mcast-interface interface 指定組播的同步接口
-x --u-threshold uthreshold:設置一個服務器可以維持的連接上限。0~65535。設置爲0表示沒有上限。
-y --l-threshold lthreshold:設置一個服務器的連接下限。當服務器的連接數低於此值的時候服務器纔可以重新接收連接。如果此值未設置,則當服務器的連接數連續三次低於uthreshold時服務器纔可以接收到新的連接。(PS:筆者以爲此設定可能是爲了防止服務器在能否接收連接這兩個狀態上頻繁變換)
--mcast-interface interface:指定使用備服務器時候的廣播接口。
--syncid syncid:指定syncid,同樣用於主備服務器的同步。
-L -c --connection:顯示目前的連接信息,如ipvsadm -L -c
-L --timeout:顯示tcp tcpin udp的timeout值,列出超時;如ipvsadm -L --timeout
-L --daemon:顯示同步守護進程狀態
-L --stats:顯示統計狀態信息
-L --rate:顯示傳輸速率信息
-L --sort:對虛擬服務器和真實服務器排序輸出,例ipvsadm -L --sort
--nosort:不排序
-n --numeric 輸出IP地址和端口的數字形式
--thresholds:列出閾值
--persistent-conn:堅持連接
--exact:單位
-6:如果fwmark用的是ipv6地址需要指定此選項。

ipvsadm -A -t 192.168.60.200:80 -s rr -p 600
在內核的虛擬服務器列表中添加一條192.168.60.200的虛擬服務器記錄,並且指定此虛擬 服務器的服務端口爲80,調度策略算法爲輪詢調試,並勝在每個Real Server上持續服務時間爲600s,即10min。
ipvsadm -A -t 192.168.60.188:21 -s wlc
在內核虛擬服務器上添加一條192.168.60.188的虛擬服務器,此虛擬服務器的服務器端口爲21,即FTP服務,使用wlc加權最少鏈接算法。
ipvsadm -a -t 192.168.60.200:80 -r 192.168.60.132:80 -g
ipvsadm -a -t 192.168.60.200:80 -r 192.168.60.144:80 -g
以上兩條設置表示在虛擬服務器192.168.60.200中添加2條新的Real Server記錄,兩個Real Server的IP分別爲192.168.60.132和192.168.60.144,選項-g指定了中的工作模式爲DR直接路由模式 。
這樣設置完畢後,當用戶訪問192.168.60.200的80端口時,LVS會根據設置的調度策略和路由模式將請求分配到192.168.60.132以及192.168.60.144的80端口。

ipvsadm -S /opt/ipvs.bak 備份ipvs記錄??
ipvsadm -C 清空ipvs記錄
ipvsadm -Ln 查看ipvs記錄
ipvsadm -Ln --stats
-L|-l --list 顯示內核虛擬服務器表
-n --numeric 輸出IP 地址和端口的數字形式
--stats 顯示統計信息
ipvsadm -Sn > /etc/sysconfig/ipvsadm //保存記錄
ipvsadm -R < /etc/sysconfig/ipvsadm //恢復保存的記錄

注意:ipvsadm 命令配置的LVS集羣列表信息是臨時有效的,如果想系統重啓自動加載這些設置信息,必須將ipvsadm的操作命令寫入到開機啓動腳本/etc/rc.d/rc.local文件中。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章