一、keepalived概述; 二、設(shè)計原理; 三、案例:Keepalived實現(xiàn)雙機熱備; 一、keepalived概述: 概述:keepalived起初為Lvs設(shè)計的一款強大的輔助工具,保證Lvs負載調(diào)度器的故障切換以及web節(jié)點的健康狀態(tài)檢查,后續(xù)被應(yīng)用到很多需要容錯的場景下,keepalived自身基于VRRP協(xié)議--虛擬路由冗余協(xié)議,思科公有協(xié)議; 二、設(shè)計原理: 應(yīng)用場景: 設(shè)計模塊: 熱備實現(xiàn)過程:將多個主機以軟件的方式組成一個熱備組,通過共有的虛擬ip(VIP)地址對外提供服務(wù),同一時刻,熱備組中只有一臺主機在工作,別的主機冗余狀態(tài),當(dāng)當(dāng)前在線的主機失效時,其他冗余的主機會自動接替虛擬ip地址,繼續(xù)提供服務(wù),以保證架構(gòu)的穩(wěn)定性; 三、案例:Keepalived實現(xiàn)雙機熱備; 案例拓撲: 案例環(huán)境:
案例步驟: ?安裝node1節(jié)點上的httpd的服務(wù); ?安裝node2節(jié)點上的httpd的服務(wù); ?在兩臺node節(jié)點上安裝keepalived軟件程序(兩臺安裝步驟一致,在此只列出一臺); ?配置node1上master主節(jié)點; ?配置node2上backup從節(jié)點; ?客戶端訪問測試雙機熱備的效果; ?安裝node1節(jié)點上的httpd的服務(wù); [root@node1 ~]# yum -y install httpd [root@node1 ~]#cat < 192.168.100.101 END [root@node1 ~]# systemctl start httpd [root@node1 ~]# systemctl enable httpd [root@node1 ~]# netstat -utpln |grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 13891/httpd ?安裝node2節(jié)點上的httpd的服務(wù); [root@node2 ~]# yum -y install httpd [root@node2 ~]#cat < 192.168.100.101 END [root@node2 ~]# systemctl start httpd [root@node2 ~]# systemctl enable httpd [root@node2 ~]# netstat -utpln |grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 13891/httpd ?在兩臺node節(jié)點上安裝keepalived軟件程序(兩臺安裝步驟一致,在此只列出一臺); [root@node1 ~]# yum -y install kernel-devel openssl-devel popt-devel ##安裝內(nèi)核開發(fā)包,popt支持庫等工具; [root@node1 ~]# tar -zxvf keepalived-1.2.13.tar.gz -C /usr/src/ [root@node1 ~]# cd /usr/src/keepalived-1.2.13/ [root@node1 keepalived-1.2.13]# ./configure --prefix=/usr/local/keepalived [root@node1 keepalived-1.2.13]# make &&make install [root@node1 keepalived-1.2.13]# cd [root@node1 ~]# mkdir -p /etc/keepalived ##程序的主配置目錄 [root@node1 ~]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ ##復(fù)制主配置文件 [root@node1 ~]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ ##復(fù)制啟動時需要加載的配置文件 [root@node1 ~]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/ ##復(fù)制服務(wù)的控制腳本 [root@node1 ~]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/ ##復(fù)制keepalived的命令 [root@node1 ~]# chmod 755 /etc/init.d/keepalived ##為控制腳本指定權(quán)限 ?配置node1上master主節(jié)點; [root@node1 ~]# vi /etc/keepalived/keepalived.conf global_defs { router_id HA_TEST_R1 ##本服務(wù)器的名稱,若環(huán)境中有多個keepalived時,此名稱不能一致 } vrrp_instance VI_1 { ##定義VRRP熱備實例,每一個keep組都不同 state MASTER ##MASTER表示主服務(wù)器 interface eth0 ##承載VIP地址的物理接口 virtual_router_id 1 ##虛擬路由器的ID號,每一個keep組都不同 priority 100 ##優(yōu)先級,數(shù)值越大優(yōu)先級越高 advert_int 1 ##通告檢查間隔秒數(shù)(心跳頻率) authentication { ##認證信息 auth_type PASS ##認證類型 auth_pass 123456 ##密碼字串 } virtual_ipaddress { 192.168.100.95 ##指定漂移地址(VIP) } } virtual_server 192.168.100.95 80 { #vip配置 delay_loop 2 #每隔2秒檢查一次real_server狀態(tài) lb_algo wrr ##指定lvs的調(diào)度算法 lb_kind DR ##lvs集群模式 persistence_timeout 60 ##會話保持時間 protocol TCP ##選擇協(xié)議 real_server 192.168.100.101 80 { ##本機地址 weight :3 ##服務(wù)器的權(quán)重 notify_down /etc/keepalived/check.sh ##指定節(jié)點失效后,采用的腳本,notify_up表示節(jié)點正常后,采用的腳本 ##健康檢查方式一共有HTTP_GET|SSL_GET|TCP_CHECK|SMTP_CHECK|MISC_CHECK這些 TCP_CHECK { connect_timeout 10 ##連接超時時間 nb_get_retry 3 ##重連次數(shù) delay_before_retry 3 ##重連間隔時間 connect_port 80 ##健康檢查端口 } } } :wq [root@node1 ~]# vi /etc/keepalived/check.sh #!/bin/bash /etc/init.d/keepalived stop echo -e "$(ip a |grep eth0 |grep inet |awk '{print $2}'|awk -F'/' '{print $1}') (httpd) is down on $(date +%F-%T)" >>/root/check_httpd.log :wq [root@node1 ~]# chmod 777 /etc/keepalived/check.sh [root@node1 ~]# /etc/init.d/keepalived start Starting keepalived (via systemctl): [ 確定 ] [root@node1 ~]# ip a |grep 192.168.100.95 inet 192.168.100.95/32 scope global eth0 ?配置node2上backup從節(jié)點; [root@node2 ~]# vi /etc/keepalived/keepalived.conf global_defs { router_id HA_TEST_R2 ##本服務(wù)器的名稱 } vrrp_instance VI_1 { state BACKUP ##BACKUP表示從服務(wù)器 interface eth0 virtual_router_id 1 priority 99 ##優(yōu)先級,低于主服務(wù)器 advert_int 1 authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 192.168.100.95 } } virtual_server 192.168.100.95 80 { ##vip配置 delay_loop 2 ##每隔2秒檢查一次real_server狀態(tài) lb_algo wrr lb_kind DR persistence_timeout 60 ##會話保持時間 protocol TCP real_server 192.168.100.102 80 { ##本機地址 weight :3 notify_down /etc/keepalived/check.sh TCP_CHECK { connect_timeout 10 ##連接超時時間 nb_get_retry 3 ##重連次數(shù) delay_before_retry 3 ##重連間隔時間 connect_port 80 ##健康檢查端口 } } } [root@node2 ~]# vi /etc/keepalived/check.sh #!/bin/bash service keepalived stop echo -e "$(ip a |grep eth0 |grep inet |awk '{print $2}'|awk -F'/' '{print $1}') (httpd) is down on $(date +%F-%T)" >>/root/check_httpd.log :wq [root@node2 ~]# chmod 777 /etc/keepalived/check.sh [root@node2 ~]# /etc/init.d/keepalived start Starting keepalived (via systemctl): [ 確定 ] [root@node2~]# ip a |grep 192.168.100.95 ?客戶端訪問測試雙機熱備的效果; 當(dāng)node1與node2均正常狀態(tài): 當(dāng)node1的httpd服務(wù)關(guān)閉: 將node1節(jié)點的http和keepalived服務(wù)重新啟動:
|
|