keepalived最初設計的目的是爲了實現lvs前端director的高可用,很是輕量級。主要的實現的vrrp協議。html
VRRP是一種容錯協議,它保證當主機的下一跳路由器出現故障時,由另外一臺路由器來代替出現故障的路由器進行工做,從而保持網絡通訊的連續性和可靠性。前端
簡化網絡管理:在具備多播或廣播能力的局域網(如以太網)中,藉助VRRP 能在某臺設備出現故障時仍然提供高可靠的缺省鏈路,有效避免單一鏈路發生故障後網絡中斷的問題,而無需修改動態路由協議、路由發現協議等配置信息,也無需修改主機的默認網關配置。後端
適應性強:VRRP 報文封裝在 IP 報文中,支持各類上層協議。bash
網絡開銷小:VRRP 只定義了一種報文——VRRP 通告報文,而且只有處於Master 狀態的路由器能夠發送 VRRP 報文。網絡
IPVS: 爲lvs生成ipvs規則的組件,是內核級別的。架構
NETLINK:Netlink是套接字家族中的一員,主要用內核與用戶空間的進程間、用戶進程間的通信。然而它並不像網絡套接字能夠用於主機間通信,Netlink只能用於同一主機上進程通信,並經過PID來標識它們。Netlink被設計爲在Linux內核與用戶空間進程傳送各類網絡信息。網絡工具iproute2利用 Netlink從用戶空間與內核進行通信。Netlink由一個在用戶空間的標準的Socket接口和內核模塊提供的內核API組成。Netlink的設計比ioctl更加靈活,Netlink使用了AF_NETLINK Socket 家族。(摘自維基百科)app
IPVS wrapper:藉助於Checkers實現後端lvs主機的健康狀態檢測。ide
VRRP Stack: 實現VRRP協議,實現虛擬IP地址的轉移。工具
更多詳細的介紹,參考官網地址:http://www.keepalived.org/documentation.htmloop
在CentOS6.4之後,keepalived直接收錄到內置的rpm倉庫中,能夠直接安裝使用。
yum install keepalived -y
keepalived配置文件:
/etc/keepalived/keepalived.conf
配置模式(主從模式): 主節點(Master):172.16.10.9 從節點(Backup):172.16.10.77 虛擬地址:172.16.10.68
配置過程:
### 172.16.10.9:/etc/keepalived/keepalived.conf,實例內容: vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 10 priority 100 advert_int 1 authentication { auth_type PASS auth_pass MasBac } virtual_ipaddress { 172.16.10.68 } } ### 172.16.10.77:/etc/keepalived/keepalived.conf,實例內容: vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 10 priority 98 advert_int 1 authentication { auth_type PASS auth_pass MasBac } virtual_ipaddress { 172.16.10.68 } }
配置完成後,先啓動172.16.10.77的keepalived服務,查看日誌信息:
此時,啓動Master端172.16.10.9的keepalived服務:
配置模式(雙主模式): 節點地址:172.16.10.9,172.16.10.77 虛擬地址:172.16.10.68 172.16.10.69 說明:這裏的雙主模型的實現其實是配置了2組vrrp的示例。 以下關係: 示例1: 主節點:172.16.10.9 從節點:172.16.10.77 示例2: 主節點:172.16.10.77 從節點:172.16.10.9
配置信息:
## 172.16.10.9的vrrp實例信息: vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 10 priority 100 advert_int 1 authentication { auth_type PASS auth_pass MasBac } virtual_ipaddress { 172.16.10.68 } } vrrp_instance VI_2 { state BACKUP interface eth0 virtual_router_id 15 priority 98 advert_int 1 authentication { auth_type PASS auth_pass DouMas } virtual_ipaddress { 172.16.10.69 } } ## 172.16.10.77的vrrp實例信息: vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 10 priority 98 advert_int 1 authentication { auth_type PASS auth_pass MasBac } virtual_ipaddress { 172.16.10.68 } } vrrp_instance VI_2 { state MASTER interface eth0 virtual_router_id 15 priority 100 advert_int 1 authentication { auth_type PASS auth_pass DouMas } virtual_ipaddress { 172.16.10.69 } }
從新啓動keepalived服務:
此時,若是 172.16.10.77 出現故障,172.16.10.9會擁有2個虛擬ip地址,實現真正的冗餘
環境說明: keepalived採用雙主模型:節點是172.16.10.9 172.16.10.77 後端的realserver:172.16.10.122 172.16.10.133
配置說明:
## 配置後端節點:172.16.10.122 172.16.10.133 ## ifconfig eth0 172.16.10.122/16 up ## ifconfig eth0 172.16.10.122/16 up # 這個也能夠寫到配置文件 echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce ifconfig lo:0 172.16.10.68 netmask 255.255.255.255 broadcast 172.16.10.68 route add -host 172.16.10.68 dev lo:0 ifconfig lo:1 172.16.10.69 netmask 255.255.255.255 broadcast 172.16.10.69 route add -host 172.16.10.69 dev lo:1
配置keepalived:
### 172.16.10.9:/etc/keepalived/keepalived.conf的配置: global_defs { notification_email { root@example.com } notification_email_from keepalived@example.com smtp_server 172.16.10.9 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 10 priority 100 advert_int 1 authentication { auth_type PASS auth_pass MasBac } virtual_ipaddress { 172.16.10.68 } } vrrp_instance VI_2 { state BACKUP interface eth0 virtual_router_id 15 priority 98 advert_int 1 authentication { auth_type PASS auth_pass DouMas } virtual_ipaddress { 172.16.10.69 } } virtual_server 172.16.10.68 80 { delay_loop 6 lb_algo rr lb_kind DR persistence_timeout 50 protocol TCP sorry_server 172.16.10.77 80 real_server 172.16.10.122 80 { weight 1 HTTP_GET { url { path / status_code 200 } connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } real_server 172.16.10.133 80 { weight 1 HTTP_GET { url { path / status_code 200 } connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } } virtual_server 172.16.10.69 80 { delay_loop 6 lb_algo rr lb_kind DR #persistence_timeout 50 protocol TCP sorry_server 172.16.10.77 80 real_server 172.16.10.122 80 { weight 1 HTTP_GET { url { path / status_code 200 } connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } real_server 172.16.10.133 80 { weight 1 HTTP_GET { url { path / status_code 200 } connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } } ### 172.16.10.77:/etc/keepalived/keepalived.conf的配置: ! Configuration File for keepalived global_defs { notification_email { root@example.com } notification_email_from keepalived@example.com smtp_server 172.16.10.9 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 10 priority 98 advert_int 1 authentication { auth_type PASS auth_pass MasBac } virtual_ipaddress { 172.16.10.68 } } vrrp_instance VI_2 { state MASTER interface eth0 virtual_router_id 15 priority 100 advert_int 1 authentication { auth_type PASS auth_pass DouMas } virtual_ipaddress { 172.16.10.69 } } virtual_server 172.16.10.68 80 { delay_loop 6 lb_algo rr lb_kind DR persistence_timeout 50 protocol TCP sorry_server 172.16.10.77 80 real_server 172.16.10.122 80 { weight 1 HTTP_GET { url { path / status_code 200 } connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } real_server 172.16.10.133 80 { weight 1 HTTP_GET { url { path / status_code 200 } connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } } virtual_server 172.16.10.69 80 { delay_loop 6 lb_algo rr lb_kind DR persistence_timeout 50 protocol TCP sorry_server 172.16.10.77 80 real_server 172.16.10.122 80 { weight 1 HTTP_GET { url { path / status_code 200 } connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } real_server 172.16.10.133 80 { weight 1 HTTP_GET { url { path / status_code 200 } connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } }
TCP_CHECK { connect_port 80 connect_timeout 3 }
測試:
測試前,重啓keepalived服務。
當後端realserver都出現故障時:
配置完成。