您好,登錄后才能下訂單哦!
一、簡介
Keepalived是一個免費開源的,用C編寫的類似于layer3, 4 & 7交換機制軟件,具備我們平時說的第3層、第4層和第7層交換機的功能。主要提供loadbalancing(負載均衡)和 high-availability(高可用)功能,負載均衡實現需要依賴Linux的虛擬服務內核模塊(ipvs),而高可用是通過VRRP協議實現多臺機器之間的故障轉移服務。
上圖是Keepalived的功能體系結構,大致分兩層:用戶空間(user space)和內核空間(kernel space)。
內核空間:主要包括IPVS(IP虛擬服務器,用于實現網絡服務的負載均衡)和NETLINK(提供高級路由及其他相關的網絡功能)兩個部份。
用戶空間:
Keepalived的所有功能是配置keepalived.conf文件來實現的。
二、安裝與配置
從官網下載keepalived的最新版本,本文以當前最新版本1.2.24為例。
2.1 安裝
shell> cd /usr/local/src shell> wget http://www.keepalived.org/software/keepalived-1.2.24.tar.gz shell> tar -zxvf keepalived-1.2.24.tar.gz shell> cd keepalived-1.2.24 shell> ./configure --prefix=/usr/local/keepalived shell> make && make install
2.2 配置
安裝完成后,進入安裝目錄的etc目錄下,將keepalived相應的配置文件拷貝到系統相應的目錄當中。keepalived啟動時會從/etc/keepalived目錄下查找keepalived.conf配置文件,如果沒有找到則使用默認的配置。/etc/keepalived目錄安裝時默認是沒有安裝的,需要手動創建。配置文件目錄結構如下所示:
shell> tree -l /usr/local/keepalived/etc -- keepalived | |-- keepalived.conf | `-- samples | |-- keepalived.conf.status_code | |-- keepalived.conf.track_interface | |-- keepalived.conf.vrrp | |-- 。。。 |-- rc.d | `-- init.d | `-- keepalived `-- sysconfig `-- keepalived
分別對應系統目錄(忽略samples目錄):
/etc/keepalived/keepalived.conf /etc/rc.d/init.d/keepalived /etc/sysconfig/keepalived
將配置文件拷貝到系統對應的目錄下:
shell> mkdir /etc/keepalived shell> cp /usr/local/keepalived/etc/keepalived.conf /etc/keepalived/keepalived.conf shell> cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/keepalived shell> cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived
設置keepalived服務開機啟動:
shell> chkconfig keepalived on shell> service keepalived start #啟動服務 shell> service keepalived stop #停止服務 shell> service keepalived restart #重啟服務
另外需要注意的一點是,keepalived啟動時不會檢查配置文件的語法是否正確,所以我們在編寫配置文件時要特別小心,別寫錯了,否則會出現一些意想不到的現象。
使用service keepalived start
命令啟動服務時,默認會將/etc/sysconfig/keepalived
文件中KEEPALIVED_OPTIONS
參數作為keepalived
服務啟動時的參數,并從/etc/keepalived/
目錄下加載keepalived.conf配置文件,或用-f參數指定配置文件的位置。
# Options for keepalived. See `keepalived --help' output and keepalived(8) and # keepalived.conf(5) man pages for a list of all options. Here are the most # common ones : # # --vrrp -P Only run with VRRP subsystem. # --check -C Only run with Health-checker subsystem. # --dont-release-vrrp -V Dont remove VRRP VIPs & VROUTEs on daemon stop. # --dont-release-ipvs -I Dont remove IPVS topology on daemon stop. # --dump-conf -d Dump the configuration data. # --log-detail -D Detailed log messages.日志默認輸出在/var/log/message文件中 # --log-facility -S 0-7 Set local syslog facility (default=LOG_DAEMON) # KEEPALIVED_OPTIONS="-D"
運行keepalived --help
可以查看啟動時的可選參數,這些可選參數都可以配置在/etc/sysconfig/keepalived
文件中的KEEPALIVED_OPTIONS
選項中,作為服務啟動時的參數。
keepalived正常運行后,會啟動3個進程,其中一個是父進程,負責監控其子進程。一個是vrrp子進程,另外一個是checkers子進程。
shell> ps -ef | grep keepalived root 831 1 0 11:22 ? 00:00:00 keepalived -D root 840 831 0 11:22 ? 00:00:00 keepalived -D root 841 831 0 11:22 ? 00:00:00 keepalived -D
到此keepalived就安裝完成了。
三、keepalived.conf配置文件說明
keepalived服務安裝完成之后,后面的主要工作就是在keepalived.conf文件中配置HA和負載均衡。一個功能比較完整的常用的keepalived配置文件,主要包含三塊:全局定義塊、VRRP實例定義塊和虛擬服務器定義塊。全局定義塊是必須的,如果keepalived只用來做ha,虛擬服務器是可選的。下面是一個功能比較完整的配置文件模板:
#全局定義塊 global_defs { # 郵件通知配置 notification_email { email1 email2 } notification_email_from email smtp_server host smtp_connect_timeout num lvs_id string router_id string ## 標識本節點的字條串,通常為hostname } #VRRP 實例定義塊 vrrp_sync_group string { group { string string } } vrrp_instance string { state MASTER|BACKUP virtual_router_id num interface string mcast_src_ip @IP priority num advert_int num nopreempt smtp_alert lvs_sync_daemon_interface string authentication { auth_type PASS|AH auth_pass string } virtual_ipaddress { # Block limited to 20 IP addresses @IP @IP @IP } } #虛擬服務器定義塊 virtual_server (@IP PORT)|(fwmark num) { delay_loop num lb_algo rr|wrr|lc|wlc|sh|dh|lblc lb_kind NAT|DR|TUN persistence_timeout num protocol TCP|UDP real_server @IP PORT { weight num notify_down /path/script.sh TCP_CHECK { connect_port num connect_timeout num } } real_server @IP PORT { weight num MISC_CHECK { misc_path /path_to_script/script.sh(or misc_path “/path_to_script/script.sh <arg_list>”) } } real_server @IP PORT { weight num HTTP_GET|SSL_GET { url { # You can add multiple url block path alphanum digest alphanum } connect_port num connect_timeout num nb_get_retry num delay_before_retry num } } }
全局定義塊
1、email通知(notification_email、smtp_server、smtp_connect_timeout):用于服務有故障時發送郵件報警,可選項,不建議用。需要系統開啟sendmail服務,建議用第三獨立監控服務,如用nagios全面監控代替。
2、lvs_id:lvs負載均衡器標識,在一個網絡內,它的值應該是唯一的。
3、router_id:用戶標識本節點的名稱,通常為hostname
4、花括號{}:用來分隔定義塊,必須成對出現。如果寫漏了,keepalived運行時不會得到預期的結果。由于定義塊存在嵌套關系,因此很容易遺漏結尾處的花括號,這點需要特別注意。
VRRP實例定義塊
虛擬服務器virtual_server定義塊
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。