keepalived應用部署

keepalived應用部署

1、keepalived概述;
2、設計原理;
3、案例:Keepalived實現雙機熱備;html

1、keepalived概述:

概述:keepalived起初爲Lvs設計的一款強大的輔助工具,保證Lvs負載調度器的故障切換以及web節點的健康狀態檢查,後續被應用到不少須要容錯的場景下,keepalived自身基於VRRP協議--虛擬路由冗餘協議,思科公有協議;node

2、設計原理:

應用場景:
keepalived應用部署
設計模塊:
1.core模塊:爲keepalived的核心組件,負責主進程的啓動、維護以及全局配置文件的加載和解析;
2.check模塊:負責real server 節點池內的節點的健康檢測;
3.VRRP模塊:在master與backup之間執行心跳檢測;
熱備實現過程:將多個主機以軟件的方式組成一個熱備組,經過共有的虛擬ip(VIP)地址對外提供服務,同一時刻,熱備組中只有一臺主機在工做,別的主機冗餘狀態,噹噹前在線的主機失效時,其餘冗餘的主機會自動接替虛擬ip地址,繼續提供服務,以保證架構的穩定性;web

3、案例:Keepalived實現雙機熱備;
案例拓撲:
keepalived應用部署算法

keepalived應用部署

案例步驟:
安裝node1節點上的httpd的服務;
安裝node2節點上的httpd的服務;
在兩臺node節點上安裝keepalived軟件程序(兩臺安裝步驟一致,在此只列出一臺);
配置node1上master主節點;
配置node2上backup從節點;
客戶端訪問測試雙機熱備的效果;bash

安裝node1節點上的httpd的服務;服務器

[root@node1 ~]# yum -y install httpd
[root@node1 ~]#cat <<END >>/var/www/html/index.html
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節點上的httpd的服務;
[root@node2 ~]# yum -y install httpd
[root@node2 ~]#cat <<END >>/var/www/html/index.html
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節點上安裝keepalived軟件程序(兩臺安裝步驟一致,在此只列出一臺);
[root@node1 ~]# yum -y install kernel-devel openssl-devel popt-devel  ##安裝內核開發包,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/    ##複製主配置文件
[root@node1 ~]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/       ##複製啓動時須要加載的配置文件
[root@node1 ~]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/        ##複製服務的控制腳本
[root@node1 ~]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/         ##複製keepalived的命令
[root@node1 ~]# chmod 755 /etc/init.d/keepalived                ##爲控制腳本指定權限

配置node1上master主節點;
[root@node1 ~]# vi /etc/keepalived/keepalived.conf
global_defs {
    router_id HA_TEST_R1        ##本服務器的名稱,若環境中有多個keepalived時,此名稱不能一致
}
vrrp_instance VI_1 {            ##定義VRRP熱備實例,每個keep組都不一樣
    state MASTER                ##MASTER表示主服務器
    interface eth0              ##承載VIP地址的物理接口
    virtual_router_id 1         ##虛擬路由器的ID號,每個keep組都不一樣
    priority 100                ##優先級,數值越大優先級越高
    advert_int 1                ##通告檢查間隔秒數(心跳頻率)
    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狀態
    lb_algo wrr             ##指定lvs的調度算法
    lb_kind DR              ##lvs集羣模式
    persistence_timeout 60  ##會話保持時間
    protocol TCP                ##選擇協議
    real_server 192.168.100.101 80 {         ##本機地址
        weight :3               ##服務器的權重
        notify_down /etc/keepalived/check.sh        ##指定節點失效後,採用的腳本,notify_up表示節點正常後,採用的腳本
        ##健康檢查方式一共有HTTP_GET|SSL_GET|TCP_CHECK|SMTP_CHECK|MISC_CHECK這些
        TCP_CHECK {
            connect_timeout 10      ##鏈接超時時間
            nb_get_retry 3      ##重連次數
            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從節點;
[root@node2 ~]# vi /etc/keepalived/keepalived.conf
global_defs {
    router_id HA_TEST_R2            ##本服務器的名稱
}
vrrp_instance VI_1 {
    state BACKUP                    ##BACKUP表示從服務器
    interface eth0
    virtual_router_id 1
    priority 99                 ##優先級,低於主服務器
    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狀態
    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          ##重連次數
            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

客戶端訪問測試雙機熱備的效果;
當node1與node2均正常狀態:
keepalived應用部署
當node1的httpd服務關閉:
keepalived應用部署
keepalived應用部署架構

keepalived應用部署
keepalived應用部署
keepalived應用部署
將node1節點的http和keepalived服務從新啓動:
keepalived應用部署
keepalived應用部署tcp

相關文章
相關標籤/搜索