UCARP一個簡單的HA軟體

←手機掃碼閱讀     火星人 @ 2014-03-04 , reply:0

UCARP一個簡單的HA軟體

這兩天想做一個雙機熱備的東西,查了一下資料,發現ucarp和heartbeat用的都比較多。
但論壇里好像沒有關於ucarp的中文資料,這裡有點個人翻譯的。希望對大家又幫助。
ucarp這個軟體簡單易用,非常適合初學者。

UCARP 1.2版本的文檔

軟體介紹:
UCARP可以在一對普通的主機上設置虛擬IP地址,並讓他們實現自動熱備。
這是一個非常簡單地址冗餘協議,並且他是可以免費下載到的。
它可以用來替換OPENBSD下面的CARP,VRRP。
carp:http://www.openbsd.org/faq/pf/carp.html
vrrp:http://www.chinaunix.net/jh/30/284898.html
主頁地址是  http://www.ucarp.org

編譯條件:
你必須在你的系統上安裝libpcap,UCARP編譯需要libpcap的頭文件,如果沒有安裝可以在http://www.tcpdump.org上面找到。
如果你已經具備了上面的條件請按照傳統的方式編譯安裝。
./configure
make install-strip
具體的細節可以參考安裝文檔 INSTALL 。
這個軟體已經成功的在 Linux 2.4, Linux 2.6, MacOS X,OpenBSD, MirBSD and NetBSD 上運行過。

配置需求:
我們必須配置一對虛擬主機
還需要為這個虛擬主機配置一對虛擬的IP,這個是用來說明主機是否正常,如果你需要高可用的服務,這些配置是必需的。
主機共同標識的虛擬IP地址必須在1-255之間。
每個主機還需要一個真實的IP。
每個主機都會在一定的時間發布廣播公布他的狀態,默認的來說1就表示主機正常,0表示主機有問題。
這個協議非常的精簡,所使用的包也非常的小,所以請放心不會因為這個廣播而影響到你的網路的正常運行。
一個共享的密碼,他在網路上不是明文傳送的。
一個腳本,讓他在主機出錯的時候取代主機狀態。
另一個腳本,讓他在主機恢復的時候放棄主機狀態。

使用方法:
服務通常安裝在 :/usr/local/sbin/ucarp
所有側操作都是通過UCARP這個命令實現的。
想了解參數詳情請看 :/usr/local/sbin/ucarp -h

ucarp -h 幫助信息
ucarp 1.2 - Mar 27 2007

--interface=<if> (-i <if>;): bind interface <if>  (網路介面 綁定網路介面)
--srcip=<ip> (-s <ip>;): source (real) IP address of that host (源地址真實ip)
--vhid=<id> (-v <id>;): virtual IP identifier (1-255) (虛擬ip標識 (1-255))
--pass=<pass> (-p <pass>;): password (密碼)
--preempt (-P): becomes a master as soon as possible (以最快的速度成為主伺服器的角色)
--neutral (-n): don't run downscript at start if backup (如果是備份主機,啟動的時候不運行downscript.)
--addr=<ip> (-a <ip>;): virtual shared IP address( 虛擬共享ip地址)
--help (-h): summary of command-line options (命令行幫助)
--advbase=<seconds> (-b <seconds>;): advertisement frequency (廣播的頻率(秒))
--advskew=<skew> (-k <skew>;): advertisement skew (0-255)  (????不廣播)
--upscript=<file> (-u <file>;): run <file> to become a master (運行一個腳本文件,使本伺服器成為主伺服器)
--downscript=<file> (-d <file>;): run <file> to become a backup (運行一個腳本文件,使本伺服器成為從伺服器)
--deadratio=<ratio> (-r <ratio>;): ratio to consider a host as dead (認定主機已經死掉的比率(閥值))
--shutdown (-z): call shutdown script at exit (在退出的時候,執行關閉的腳本)
--daemonize (-B): run in background (運行在後台)
--facility=<facility> (-f): set syslog facility (default=daemon) (設置syslog工具,默認在後台)

使用例子:
管理設置網路介面eth0上的虛擬ip 10.1.1.252,虛擬ip地址標識為1,密碼是mypassword,真實地址為10.1.1.1,
運行腳本/etc/vip-up.sh當主機成為主伺服器的時候,運行腳本/etc/vip-down.sh當虛擬ip被禁用的時候.
請報告bug到bugs@uarp.org)
Sample usage:
Manage the 10.1.1.252 shared virtual address on interface eth0, with
1 as a virtual address idenfitier, mypassword as a password, and
10.1.1.1 as a real permanent address for this host.
Call /etc/vip-up.sh when the host becomes a master, and
/etc/vip-down.sh when the virtual IP address has to be disabled.

ucarp --interface=eth0 --srcip=10.1.1.1 --vhid=1 --pass=mypassword \
      --addr=10.1.1.252 \
      --upscript=/etc/vip-up.sh --downscript=/etc/vip-down.sh

Please report bugs to bugs@ucarp.org.

下面是一個比較好的安裝例子。
例如你有一個郵件伺服器IP地址是10.1.1.252,每個使用者都在自己的郵件客戶端配置了主機IP地址
如果你的郵件伺服器不能正常運行,客戶端將不能使用這個服務。
現在需要兩台伺服器,並且配製相同,兩台伺服器上分別設置地址10.1.1.1 和 10.1.1.2 讓兩個伺服器
都能響應同一IP地址(10.1.1.252)的請求,當一個伺服器出現問題停止,另一個伺服器接管這個服務。
首先我們來創建一個腳本,用來添加實IP地址,我們先把腳本存放到/etc/vip-up.sh
#! /bin/sh
/sbin/ip addr add 10.1.1.252/24 dev eth0
我們在創建一個用來關閉實IP的腳本,我們先把腳本存放到/etc/vip-down.sh
#! /bin/sh
/sbin/ip addr del 10.1.1.252/24 dev eth0
當然,你可以使用任何方法實現和修改這些腳本,例如你想要添加一個路由,想給你的操作做一個日誌,或者直接給你發郵件。
最後別忘了連接你的交換接刷新ARP的緩存,用戶都會很快的更新他們的MAC地址列表。
如果你的網卡使用的不是ETH0請根據你的實際情況修改上面腳本中的內容。
別忘了加一個可執行的許可權
chmod +x /etc/vip-up.sh /etc/vip-down.sh
現在. 我們現在需要的是一個虛擬的IP標識,我們就使用"42"。我們也需要一個口令,讓我們使用"LOVE".
現在我們在第一個主機上添加實IP地址並作相應的標示 10.1.1.1 。(MASTER)
/usr/local/sbin/ucarp -v 42 -p love -a 10.1.1.252 -s 10.1.1.1 &
現在我們在第二個主機上添加實IP地址並作相應的標示 10.1.1.2 。(SLAVE)
/usr/local/sbin/ucarp -v 42 -p love -a 10.1.1.252 -s 10.1.1.2 &
現在你應該能看見一個已經成為了主伺服器,另一個成為了從伺服器,根據你的實際情況修改你的腳本。
現在我們斷開主伺服器,你會發現從伺服器在幾秒種以後接管了主伺服器。
你也可以通過"-b"這個參數修改廣播時間,多個從伺服器當中,廣播時間較小的那個將成為主伺服器。。
請注意:當一個伺服器從另一個伺服器接管服務以後將自動變成主伺服器,而且不會因為另一個伺服器從新使用而降為從伺服器
如果你想讓一個伺服器立即變成主伺服器請在這個主機上使用一個"-p"的選項,使另一台變成從伺服器。
當UCARP在伺服器上第一次會以從伺服器方式運行,並且看是否需要變成主伺服器。
當進入從伺服器狀態的時候,正常情況下將運行關閉腳本,這有利於刪除舊的臨時文件或清除網卡的未知的狀態.
然而在一些特殊的環境里,如果ucarp服務還沒有啟動起來的時候,不需要運行downscript.如果是那樣的話,使用-n參數。
當它第一次進入從伺服器狀態時,不運行關閉腳本在運行以後,發生所有改變從主伺服器狀態到備份伺服器狀態
使用"-z"的選項伺服器將會先切換到從伺服器的狀態然後再推出,如果伺服器本身工作在從的狀態將直接推出。
使用"-r"的選項是讓從伺服器在主伺服器丟失響應多常時間后,把自己升級成為新的主伺服器。如果沒有指定的話這個值默認是3。
如果UCARP運行在daemon狀態(-B 後台運行),他的標準輸出和標準錯誤日誌默認將發送給系統的syslog進程,如果你不想記錄他可以使用

--syslog=none 或這也可以使用你自己的日誌工具。

如何獲得這個軟體:
軟體主頁:        http://www.ucarp.org/ .
郵件地址列表:        http://www.ucarp.org/ml/
其他的資料:        http://www.xiaojb.com/archives/it/freebsd_carp_vrrp.shtml
謝謝你的支持

                                         -Frank DENIS "Jedi/Sector One" <j@ucarp.org> .

[ 本帖最後由 xinyv 於 2007-3-28 16:21 編輯 ]
《解決方案》

英文原版:


                                 .:. UCARP .:.
                         Documentation for version 1.2


           ------------------------ BLURB ------------------------


UCARP allows a couple of hosts to share common virtual IP addresses in order
to provide automatic failover. It is a portable userland implementation of the
secure and patent-free Common Address Redundancy Protocol (CARP, OpenBSD's
alternative to the patents-bloated VRRP).

Strong points of the CARP protocol are: very low overhead, cryptographically
signed messages, interoperability between different operating systems and no
need for any dedicated extra network link between redundant hosts.

Home page is http://www.ucarp.org/


        ------------------------ COMPILATION ------------------------


libpcap (http://www.tcpdump.org/) must be installed on your system, with
development files (headers).

Then, follow the boring traditional procedure:

./configure
make install-strip

For details, have a look at the INSTALL file.

The software has been successfully tested on Linux 2.4, Linux 2.6, MacOS X,
OpenBSD, MirBSD and NetBSD.


        ------------------------ REQUIREMENTS ------------------------


A couple of virtual hosts must be given:

- A shared virtual IP, which will be dynamically answered by one alive host.
Services that need high availability need to be assigned to that virtual IP.

- A real IP address for each host.

- A shared identifier for the virtual IP address, which is a number between 1
and 255.

- For each host : an advertisement time base and skew, which is the frequency
the host will tell the other one that it's still alive. By default, base is 1
and skew is 0, which basically means one advertisement a second. The protocol
is very light, a tiny packet every second won't have any noticeable impact on
your network.

- A shared password (that will never go plaintext to the network).

- A script to bring the virtual address up when a host becomes the master.

- Another script to bring the virtual address down when a host is no more the
master.


            ------------------------ USAGE ------------------------


The server will usually be installed as : /usr/local/sbin/ucarp
Everything is driven through command-line options.
In order to see the list of available options, try : /usr/local/sbin/ucarp -h

Better than a long technical discussion, here's a real-life setup example.

Your company has an internal mail relay whose IP address is 10.1.1.252. Every
user has configured his mail client with that host or IP address and the
service must always be up and running without reconfiguring mail clients in
case of a failure.

It's why you set up two mail servers hosts with an identical configuration.
Their real IP addresses are 10.1.1.1 and 10.1.1.2.
Let's see how to assign the same additionnal IP address (10.1.1.252) to both
servers, so that when one goes down, the other one goes up.

First, we will create a script that brings the virtual IP address up. Let's
save that file as /etc/vip-up.sh :

#! /bin/sh
/sbin/ip addr add 10.1.1.252/24 dev eth0

Now another script to bring it down, /etc/vip-down.sh :

#! /bin/sh
/sbin/ip addr del 10.1.1.252/24 dev eth0

Of course, anything can go in these scripts. For instance, you may want to add
routes, to add something to log files or to send mail. And last, but not
least, you can use a script that will connect to your switches and flush their
ARP cache. Some users reported that transitions were way faster when also
switching MAC addresses.
The interface name is passed as an argument to the called scripts, so feel
free to replace "eth0" with "$1" in the previous examples.

Don't forget to make those files executable :

chmod +x /etc/vip-up.sh /etc/vip-down.sh

Right. What we need now is an identifier for the virtual IP. Let's take "42".
And we also need a password. Let's take "love".

Now, on the first host (whoose real IP is 10.1.1.1), run :

/usr/local/sbin/ucarp -v 42 -p love -a 10.1.1.252 -s 10.1.1.1 &

On the second host, whose real IP is 10.1.1.2, run :

/usr/local/sbin/ucarp -v 42 -p love -a 10.1.1.252 -s 10.1.1.2 &

You should see that one of those hosts quickly becomes the master, and the
other one the backup. Related scripts are spawned on change.

Now unplug the master. After a few seconds, the other host becomes the new
master.

By changing the base (the -b switch) you can have a "preferred" master. The
lower the value is, the more likely it's going to be a master.

Please note that by default, and if everything's ok, a master will stay a
master as long as possible. If you want a "preferred" master to immediately
become a master even if another host is already the master:
- add the --preempt (or -P) switch to *all* hosts
- use a lower skew or a lower base for the "preferred" one.

When ucarp first runs, it starts as a backup and listens to the network to
determine if it should become the master. When it enters the backup state, it
normally runs the downscript. That can be useful to remove old temporary files
or clean up an interface that is in an unknown state. In some circumstances,
however, it is undesirable to run the downscript if the service was not
already up. In that case, use the --neutral (-n) switch for ucarp to not run
the downscript when it enters the backup state the first time. All changes
from the master state to the backup state after that will run the downscript.

--shutdown (-z) will run the downscript at exit, unless ucarp is already in
the backup state.

The "dead ratio" (--deadratio=...) knob basically changes how long a backup
server will wait for an unresponsive master before considering it as dead, and
becoming the new master. In the original protocol, the ratio is 3. This is
also the default when this command-line switch is missing.

Notices are sent both to stderr/stdout and to the syslog daemon (with the
"daemon" facility) by default. stderr/stdout are bypassed if the daemon is
started in background (--daemonize). Facilities can be changed with the
--syslog switch. Use --syslog=none to disable syslog logging, for instance if
prefer using something like multilog.


        ------------------------ TRANSLATIONS ------------------------


UCARP can speak your native language through gettext / libintl.
If you want to translate the software, have a look at the po/ directory.
Copy the ucarp.pot file to <your locale name>.po and use software like Kbabel
or Emacs to update the file.
Better use use your local charset than UTF-8.


      ------------------------ DOWNLOADING UCARP ------------------------


UCARP home page is: http://www.ucarp.org/ .

UCARP mailing-list: http://www.ucarp.org/ml/


Thank you,

                        -Frank DENIS "Jedi/Sector One" <j@ucarp.org> .

ucarp 1.2 - Mar 27 2007

--interface=<if> (-i <if>): bind interface <if>
--srcip=<ip> (-s <ip>): source (real) IP address of that host
--vhid=<id> (-v <id>): virtual IP identifier (1-255)
--pass=<pass> (-p <pass>): password
--preempt (-P): becomes a master as soon as possible
--neutral (-n): don't run downscript at start if backup
--addr=<ip> (-a <ip>): virtual shared IP address
--help (-h): summary of command-line options
--advbase=<seconds> (-b <seconds>): advertisement frequency
--advskew=<skew> (-k <skew>): advertisement skew (0-255)
--upscript=<file> (-u <file>): run <file> to become a master
--downscript=<file> (-d <file>): run <file> to become a backup
--deadratio=<ratio> (-r <ratio>): ratio to consider a host as dead
--shutdown (-z): call shutdown script at exit
--daemonize (-B): run in background
--facility=<facility> (-f): set syslog facility (default=daemon)

Sample usage:

Manage the 10.1.1.252 shared virtual address on interface eth0, with
1 as a virtual address idenfitier, mypassword as a password, and
10.1.1.1 as a real permanent address for this host.
Call /etc/vip-up.sh when the host becomes a master, and
/etc/vip-down.sh when the virtual IP address has to be disabled.

ucarp --interface=eth0 --srcip=10.1.1.1 --vhid=1 --pass=mypassword \
      --addr=10.1.1.252 \
      --upscript=/etc/vip-up.sh --downscript=/etc/vip-down.sh


Please report bugs to bugs@ucarp.org.
《解決方案》

由於本人水平有限,上面不正確的地方請自行修改。
還有個問題請教高手指點。
就是哪個-k的選項沒看明白。
請問上面提到的skew和base有啥區別??
《解決方案》

好好,nntp,你倆咋都不理我呢,好歹給個思路吧,昨天有看了一遍,感覺對-n -b 都有點不理解了。
《解決方案》

原帖由 xinyv 於 2007-3-29 09:20 發表於 4樓  
好好,nntp,你倆咋都不理我呢,好歹給個思路吧,昨天有看了一遍,感覺對-n -b 都有點不理解了。

哥們兒,我沒有用過這東東。沒有用過的東東沒有發言權啊。。:oops:
《解決方案》

cu里根本沒有ucarp的中文貼子。google上也沒找到,好不容易找到一個還是法文的。
翻譯了一下午好歹也給點鼓勵啊.
《解決方案》

樓主學習精神可嘉!

謝謝共享,偶也沒有玩過這個,有空試試!
《解決方案》

有那些功能?
支持多台機器嗎?
支持啟動指定服務?
支持WATCH DOG?
.......
《解決方案》

看起來不錯!

去網站上看看
《解決方案》

那個好像只能監控到機器的存活,不能監控到服務,如果服務停了,但機器還是正常運轉,它就沒有辦法拉




[火星人 ] UCARP一個簡單的HA軟體已經有829次圍觀

http://coctec.com/docs/service/show-post-8453.html