1 環境準備
系統需要的 LINUX 版本為:2.6.9‐5.ELsmp 需要的keepalived 版本為:1.1.20
? 檢查 LINUX版本
[root@test01 etc]# uname -r
2.6.9-5.ELsmp
? 檢查 keepalived 版本
[root@test01 keepalived]# pwd
/root/disk/keepalived
[root@test01 keepalived]# cat VERSION
1.1.20
2目标
兩台伺服器,一主一備。提供兩個虛拟 IP。
Server A: 10.10.0.41 (主伺服器)
Server B: 10.10.0.118 (備伺服器)
Virtual IP: 10.10.0.44/45
要求正常情況,主伺服器提供服務,主伺服器失效時,備伺服器接管。
3 安裝 keepalive
yum install openssl-devel
[root@test01 keepalived]# ./configure
……
[root@test01 keepalived]# make
[root@test01 keepalived]# make install
……
cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/mkdir /etc/keepalivedcp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/cp /usr/local/sbin/keepalived /usr/sbin/
做成系統啟動服務友善管理.
[root@slave init.d]# chkconfig keepalived on
[root@slave init.d]# chkconfig –list | grep keepalivd
[root@slave init.d]# chkconfig –list | grep keep
keepalived 0:off 1:off 2:on 3:on 4:on 5:on 6:off
負載均衡是一個服務的虛拟化是一個很好的解決方案,當你設計一個負載均衡的拓撲
必須關注:
1.使用健康檢查 檢查server真的可用
- 使用failover協定來實作負載均衡可用性
負載平衡服務,提供了一個全局的高度可用的虛拟服務。為了增加負載均衡服務的可用性,我們需要
監控每個正則的server節點,這個問題是主要處理 使用健康檢查架構操作一個真正的server pool.
換句話說,當使用負載director,我們引入一個單點故障
對于虛拟服務。是以負載均衡高可用必須被解決,
使用專用的路由
協定切換/虛拟化協定。
簡單的試圖解決這兩個問題,通過添加,一方面,一個強大的
健康檢查架構,另一方面,
實作熱備份協定.這兩個架構可以處理Linux Vritual Server(LVS)架構來操縱LVS real server pools
通過增加和去除真正的real server 基于健康檢查的決定。
LVS 代表 “Linux Virtual Server”.LVS是一個
patched Linux核心,添加了一個負載均衡的功能。
LVS表現為一個網橋(使用NAT)來負載均衡TCP/UDP LVS 路由器元件是:
1.WAN口, 以太網接口控制了所有通路的用戶端
2.LAN口,以太網控制所有的負載均衡server
在這篇文章中,我們使用下面的關鍵字:
LVS元件:
1.VIP Virtual IP是 被用戶端通路的位址,用戶端隻會通路這個位址
2.Real server.一個real server 主機被用戶端通路,
在我們的拓撲裡 是WEB SERVER 1和WEB SERVER 2
3.Server pool: 一個真正的伺服器農場
4.Virtual server:一個伺服器池的接入點
5.和VIP相關的TCP/UDP 服務
VRRP 元件:(虛拟路由備援協定(Virtual Router Redundancy Protocol,簡稱VRRP)
1.VRRP 主管failover/virtualization 實作的協定
- VRRP Instance: 一個thread 控制 VRRPv2 IP位址的具體設定,一個VRRP 執行個體
可能備份一個或者多個VRRP 執行個體。在我們的 “Case study:Failover”, 我們處理4個
VRRP 執行個體。 一個有(VIP1,VIP2),一個擁有(VIP3,VIP4),一個擁有(DIP1) 另一個擁有(DIP2)
可以參與一個或多個虛拟路由器.
3.IP Address owner. VRRP 執行個體 的IP位址 作為一個真正的接口位址。
這就是VRRP 執行個體, 當起來的時候
将響應資料包處理
一個IP位址(ES)對ICMP,TCP連接配接
4.MASTER state: VRRP 執行個體狀态,它是假設代理了轉發資料包到VRRP執行個體相關的位址。
5.BACKUP state: VRRP 執行個體狀态 它可以轉發資料庫 由于目前的VRRP執行個體MASTER 失敗
6.real load balance :一個LVS director 運作一個或者多個VRRP 執行個體
我們的軟體結構 處理4個Linux 核心的請求:
下面的圖說明了keepalived 内部的實作元件。keepalived 使用一個完全的多線程架構 基于
核心的I/O多路複用。 2個主要的元件是:
1.Health-checker worker threads.每個 health-check 是注冊到我們的全局架構。
這些workers 執行監控檢查使用keepalived health-check 架構。
health-check 架構目前實作3個檢查:
- TCP CHECK:執行一個3層的檢查
2.HTTP GET: 檢查HTTP server html 記憶體的完整性
3.MISC CHECK: 執行使用者定義的完整性檢查
2.VRRP packet 排程器: 多路複用I/O處理VRRP執行個體
相應的
2個主要的部件使用下面的低級别的原始的
SMTP 通知; 一個SMTP 包裝 使用異步的stream 程序。這種原始的
使簡單的發送郵件通知。
IPVS 架構: LVS 核心接口,真正的伺服器池的操作
Netlink: 核心路由VRRP部分接口,提供VRRP VIP 操縱。
Multicast(多點傳播): 用于發送VRRP廣告,我們使用多點傳播
目前的多層 health-check 架構實作了下面的檢查子產品:
- TCP_CHECK:工作在4層。為了確定這個check,我們使用TCP Vanilla check 使用非阻塞的/逾時的 TCP連接配接。
如果遠端的server 不回複請求,那麼測試是錯誤的,伺服器被從sereer pool上移除。
2.HTTP_GET 工作在第5層, 執行一個URL的 GET HTTP。 get的結果是用MD5計算的
如果sum值和預期的值不比對, 測試是失敗的 server 會從server pool移除。
這個子產品實作一個multi-URL的get check 在相同的service上。
SSL_GET: 和HTTP_GET 類似但是使用了SSL連接配接到遠端的webservers.
MISC_CHECK: 這個檢查允許一個使用者定義腳本來運作作為健康檢查,結果必須是0或者1.
keepalived 的目的是定義一個通用的架構容易擴充來增加新的checks modules.
如果你對checkers 開發感興趣,你可以讀一下HackingGuide
導演切換簡單實作VRRP協定。快速描述這個協定:
1.”VRRP 指定選舉協定動态配置設定reponsibility 為一個虛拟的路由器
VRRP 路由器 控制了和虛拟路由器叫做master的IP address,轉發的包發送到這些IP位址。
選擇的程序提供了動态的fail over 在轉發reposibility 可能是Master 變得不可用的時候
這允許任何的虛拟路由器IP位址
區域網路作為預設的第一跳路由器的主機
安裝keepalived
在安裝keepalived 前,任何的先前安裝的版本必須要删除
1.下載下傳最新的keepalived 源代碼
下載下傳OpenSSL
你也需要使用Linux kernel 源代碼
2.簡單的編譯
[root@lvs keepalived]# ./configure
[root@lvs keepalived]# make
[root@lvs keepalived]# make install
3.所有的2進制和臨時的配置檔案被安裝, 你需要建立一個call到keepalived daemon在你的rc 檔案裡
[root@lvs keepalived]# ln -s /etc/rc.d/init.d/keepalived.init /etc/rc.d/rc3.d/S99keepalived
keepalivd 配置簡介:
keepalivd 配置使用下面的簡介:
7.1 全局定義摘要:
global_defs {
notification_email {
}
notification_email_from email
smtp_server host
smtp_connect_timeout num
lvs_id string
}
關鍵字 定義 類型
global_defs identify the global def configuration block
确定全局定義的配置塊
notification_email email accounts that will receive the notification mail List
電子郵件帳戶,将收到通知郵件
notification_email_from email to use when processing “MAIL FROM:” SMTP command List
電子郵件使用時處理郵件:“SMTP指令
smtp_server remote SMTP server to use for sending mail notifications alphanum
伺服器用于發送郵件通知
smtp_connection_timeout specify a timeout for SMTP stream processing numerical
指定SMTP流處理逾時
lvs_id specify the name of the LVS director alphanum
指定LVS director的姓名
7.2 Virtual server definitions synopsis(定義虛拟伺服器簡介)
virtual_server (@IP PORT)|(fwmark num) {
delay_loop num
lb_algo rr|wrr|lc|wlc|sh|dh|lblc
lb_kind NAT|DR|TUN
(nat_mask @IP)
persistence_timeout num
persistence_granularity @IP
virtualhost string
protocol TCP|UDP
sorry_server @IP PORT
real_server @IP PORT {
weight num
TCP_CHECK {
connect_port num
connect_timeout num
MISC_CHECK {
misc_path /path_to_script/script.sh
(or misc_path “/path_to_script/script.sh ”)
HTTP_GET|SSL_GET {
url { # You can add multiple url block
path alphanum
digest alphanum
nb_get_retry num
delay_before_retry num
keepalived主要用作RealServer的健康狀态檢查以及LoadBalance主機和BackUP主機之間failover的實作。
keepalived主要目的在于,其自身啟動一個服務,能夠實作工作在雙節點或多個節點上,并且可以在核心生效的ipvs規
則
其中目前持有資源的節點被稱為活躍節點,另外的節點被稱為備節點
被稱為 Master/Backup 傳承了vrrp的特*
備節點如果收不到主節點傳遞過來的心跳資訊,之後于是将資源搶回并使用,并在本地生效規則
virtual_server identify a virtual server definition block
确定虛拟伺服器定義塊
fwmark specify that virtual server is a FWMARK
指定虛拟伺服器是一個fwmark
delay_loop specify in seconds the interval between checks numerical
指定檢查間隔(機關秒)
lb_algo select a specific scheduler (rr|wrr|lc|wlc…) string
選擇一個特定的排程算法(RR | WRR | LC | WLC……)
lb_kind select a specific forwarding method (NAT|DR|TUN) string
選擇一個特定的轉發方法(NAT |DR|TUN)
persistence_timeout specify a timeout value for persistent connections numerical
指定持久連接配接逾時值
persistence_granularity specify a granularity mask for persistent connections
指定一個持久連接配接的粒度
Virtualhost specify a HTTP virtualhost to use for HTTP|SSL_GET alphanum
指定一個HTTP 虛拟主機用于HTTP|SSL_GET
protocol specify the protocol kind (TCP|UDP) numerical
這種協定類型(TCP|UDP)
sorry_server server to be added to the pool if all real servers are down
如果所有的real servers down了 server 被增加到pool
real_server specify a real server member
指定一個real server
Weight specify the real server weight for load balancing decisions numerical
指定real server 衡量load balancing決定
TCP_CHECK check real server availability using TCP connect
檢查real server 可用性 使用TCP連接配接
MISC_CHECK check real server availability using user defined script
檢查real server 可用性使用定義的腳本
misc_path identify the script to run with full path path
定義腳本運作的路徑
HTTP_GET check real server availability using HTTP GET request
檢查real server 可用性 使用HTTP GET請求
SSL_GET check real server availability using SSL GET request
檢查real server 可用性使用SSL GET 請求
url identify a url definition block
确定一個URL定義塊
Path specify the url path alphanum
定義一個url 路徑
Digest specify the digest for a specific url path alphanum
指定一個摘要用于特定的路徑
connect_port connect remote server on specified TCP port numerical
連接配接到遠端server 在指定的TCP端口上
connect_timeout connect remote server using timeout numerical
連接配接到遠端server 使用逾時
Nb_get_retry maximum number of retries numerical
最大重試次數
delay_before_retry delay between two successive retries numerica
連續兩次重試之間的延遲
NB: nat_mask 關鍵字是過時的 如果你不使用LVS 這個标志給你定義的反向NAT粒度的能力。
NB:Currrently,Healthcheck 架構,隻實作TCP 協定用于服務監控
NB:Type “path” 指向一個完整的腳本執行的路徑。
7.3 VRRP Instance definitions synopsis
vrrp_sync_group string {
group {
string
notify_master /path_to_script/script_master.sh
(or notify_master “/path_to_script/script_master.sh ”)
notify_backup /path_to_script/script_backup.sh
(or notify_backup “/path_to_script/script_backup.sh ”)
notify_fault /path_to_script/script_fault.sh
(or notify_fault “/path_to_script/script_fault.sh ”)
vrrp_instance string {
state MASTER|BACKUP
interface string
mcast_src_ip @IP
lvs_sync_daemon_interface string
virtual_router_id num
priority num
advert_int num
smtp_alert
authentication {
auth_type PASS|AH
auth_pass string
virtual_ipaddress { # Block limited to 20 IP addresses
@IP
virtual_ipaddress_excluded { # Unlimited IP addresses number
vrrp_instance identify a VRRP instance definition block
确定一個VRRP執行個體定義塊
State specify the instance state in standard use
在标準使用指定執行個體的狀态
Interface specify the network interface for the instance to run on string
指定的執行個體運作在字元串的網絡接口
mcast_src_ip specify the src IP address value for VRRP adverts IP header
指定源IP位址值用于VRRP IP 報頭
lvs_sync_daemon_inteface specify the network interface for the LVS sync_daemon to run on
string
指定網絡接口用于LVS sync_daemon 運作
Virtual_router_id specify to which VRRP router id the instance belongs numerical
指定執行個體屬于 哪個VRRP 路由器 ID
Priority specify the instance priority in the VRRP router numerical
指定VRRP 路由器的 執行個體優先級
advert_int specify the advertisement interval in seconds (set to 1) numerical
指定公告時間間隔
smtp_alert Activate the SMTP notification for MASTER state transition
激活SMTP 通知用于MASTER 狀态轉換
authentication identify a VRRP authentication definition block
定義一個VRRP 認證定義子產品
auth_type specify which kind of authentication to use (PASS|AH)
指定哪種類型認證使用(PASS|AH)
auth_pass specify the password string to use string
指定 密碼字元竄來使用
virtual_ipaddress identify a VRRP VIP definition block
定義一個VRRP VIP 定義塊
virtual_ipaddress_excluded identify a VRRP VIP excluded definition block (not protocol VIPs)
定義一個VRRP VIP 排除定義塊(不是VIPs協定)
notify_master specify a shell script to be executed during transition to master state
path
指定一個shell腳本用于執行在轉換為master狀态時執行
notify_backup specify a shell script to be executed during transition to backup state
notify_fault specify a shell script to be executed during transition to fault state
vrrp_sync_group Identify the VRRP synchronization instances group string
Keepalived 程式簡介:
8.1 keepalived daemon
相關的指令行參數:
首先你需要一個配置好的LVS的拓撲結構,在本文檔的其餘部分,我們假設所有的系統配置都已經做好。
這種拓撲結構一般是DMZ中的實作
的結構。
9.1 主要部件
- LVS 路由器,有負載均衡IP位址(192.168.100.0/24)
2.Network Router.預設的路由器對于整個内部網絡,所有區域網路中的工作站
通過這個IP位址的處理
3.Network DNS Server :引用内部網絡IP拓撲
4.SMTP Server
5.SERVER POOL
9.2 Server pool specifications
在這個簡單配置裡,我們有2個server pools:
- Server pool 1:托管的HTTP和SSL服務。每個伺服器擁有兩個應用伺服器
2.Server pool 2:托管SMTP service
9.3 Keepalived configuration
你現在準備配置 keepalived daemon 根據你的LVS 拓撲。整個配置是在 /etc/keepalived/keepalived.conf file。
Configuration File for keepalived
notification_email_from [email protected]
smtp_server 192.168.200.20
smtp_connect_timeout 30
lvs_id LVS_MAIN
virtual_server 192.168.200.15 80 {
delay_loop 30
lb_algo wrr
lb_kind NAT
persistence_timeout 50
protocol TCP
sorry_server 192.168.100.100 80
real_server 192.168.100.2 80 {
weight 2
HTTP_GET {
url {
path /testurl/test.jsp
digest ec90a42b99ea9a2f5ecbe213ac9eba03
path /testurl2/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334c
connect_timeout 3
nb_get_retry 3
delay_before_retry 2
real_server 192.168.100.3 80 {
weight 1
virtual_server 192.168.200.15 443 {
delay_loop 20
lb_algo rr
persistence_timeout 360
real_server 192.168.100.2 443 {
real_server 192.168.100.3 443 {
virtual_server 192.168.200.15 25 {
delay_loop 15
lb_algo wlc
real_server 192.168.100.4 25 {
real_server 192.168.100.5 25 {
根據這種配置的例子,簡單的守護程序将驅動核心使用
以下資訊:
1.LVS server 擁有的名字:LVS_MAIN lvs_id LVS_MAIN
2.通知:
- SMTP server 是:192.168.200.20
- SMTP connection timeout 設定為30秒
3.通知郵件: [email protected] & [email protected]
Load balanced services:
HTTP: VIP 192.168.200.15 port 80
1.Load balancing:使用輪詢排程算法使用NAT 轉發,持久性連接配接是設定為50秒在每個TCP 服務上。
如果你使用Linux Kernel 2.2 你需要制定NAT mask 來定義IPFW 僞裝的粒度。延遲循環設定為30秒。
2.Sorry Server.如果所有的servers 從VS的server pool裡移除, 我們增加192.168.100.100 端口80
來服務用戶端的請求。
3.Real server 192.168.100.2 端口80 被權重到2, Failure 檢測會基于HTTP_GET 通過2 URLS.
服務連接配接逾時時間設定為3秒, real server 會被認為down了在3次嘗試後。daemon 會等待2秒在重試前
4.Real server 192.168.100.3 端口80 被權重為1.Failure 檢測會基于HTTP_GET 通過1 URL。
SSL VIP 192.168.200.15 端口443