1. 程式人生 > >十一.keepalived高可用服務實踐部署

十一.keepalived高可用服務實踐部署

-s ash app The bind vim bin shel 全局

期中集群架構-第十一章-keepalived高可用集群章節
======================================================================
01. 作業題一說明:
先進行企業案例需求梳理:
? 當用戶請求www.etiantian.org/upload/xx 地址時,實現由upload上傳服務器池處理請求。
? 當用戶請求www.etiantian.org/static/xx 地址時,實現由靜態服務器池處理請求。
? 除此以外,對於其他訪問請求,全都由默認的動態服務器池處理請求。
用戶請求(URI) 處理請求服務器 站點目錄 功能


/upload 10.0.0.8:80 html/www/upload upload服務器
/static 10.0.0.7:80 html/www/static static靜態服務器
/ 10.0.0.9:80 html/www 默認

解題方法:
1)完成nginx網站服務器配置
第一個裏程:創建測試環境
# 10.0.0.8 主機上創建upload目錄,然後生成網站測試頁面文件
cd /application/nginx/html/www/
mkdir upload
cp oldboy.html upload/

# 10.0.0.7 主機上創建static目錄,然後生成網站測試頁面文件
cd /application/nginx/html/www/
mkdir static
cp oldboy.html static/

# 10.0.0.9 主機上創建默認測試頁面文件即可

第二個裏程:利用lb01進行訪問測試
# 測試10.0.0.8訪問是否正常
curl -H host:www.etiantian.org 10.0.0.8/upload/oldboy.html
web02 www.etiantian.org

# 測試10.0.0.7訪問是否正常
curl -H host:www.etiantian.org 10.0.0.7/static/oldboy.html


web01 www.etiantian.org

# 測試10.0.0.9訪問是否正常
curl -H host:www.etiantian.org 10.0.0.9/oldboy.html
web03 www.etiantian.org

2)完成nginx反向代理服務器配置
第一個裏程:配置upstream模塊信息
upstream upload {
server 10.0.0.8:80;
}
upstream static {
server 10.0.0.7:80;
}
upstream default {
server 10.0.0.9:80;
}

第二個裏程:配置proxy_pass模塊信息
server {
listen 80;
server_name www.etiantian.org;
root html;
index index.html index.htm;
location / {
proxy_pass http://default;
proxy_set_header host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
location /upload {
proxy_pass http://upload;
proxy_set_header host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
location /static {
proxy_pass http://static;
proxy_set_header host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}

作業題二說明:
根據用戶請求客戶端軟件,顯示不同頁面信息

解決方法:
1)nginx網站服務器配置
第一個裏程:創建測試環境
# 10.0.0.8 主機上創建upload目錄,然後生成網站測試頁面文件
cd /application/nginx/html/www/
cat oldboy.html

# 10.0.0.7 主機上創建static目錄,然後生成網站測試頁面文件
cd /application/nginx/html/www/
cat oldboy.html

# 10.0.0.9 主機上創建默認測試頁面文件即可
cd /application/nginx/html/www/
cat oldboy.html

測試訪問:
[root@lb01 conf]# curl -H host:www.etiantian.org 10.0.0.7/oldboy.html
web01 www.etiantian.org
[root@lb01 conf]# curl -H host:www.etiantian.org 10.0.0.8/oldboy.html
web02 www.etiantian.org
[root@lb01 conf]# curl -H host:www.etiantian.org 10.0.0.9/oldboy.html
web03 www.etiantian.org

2)nginx反向代理服務器配置
第一個裏程:配置upstream模塊信息
upstream iphone {
server 10.0.0.8:80;
}
upstream android {
server 10.0.0.7:80;
}
upstream pc {
server 10.0.0.9:80;
}

第二個裏程:配置proxy_pass模塊信息
server {
listen 80;
server_name www.etiantian.org;
root html;
index index.html index.htm;
location / {
if ($http_user_agent ~* "iphone") {
proxy_pass http://iphone;
}
if ($http_user_agent ~* "android") {
proxy_pass http://android;
}
proxy_pass http://pc;
proxy_set_header host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}


02. keepalived服務概念說明
keepalived軟件能幹什麽?
Keepalived軟件起初是專為LVS負載均衡軟件設計的,
用來管理並監控LVS集群系統中各個服務節點的狀態,後來又加入了可以實現高可用的VRRP功能

Keepalived軟件主要是通過VRRP協議實現高可用功能的。
VRRP是Virtual Router Redundancy Protocol(虛擬路由器冗余協議)的縮寫,
VRRP出現的目的就是為了解決靜態路由單點故障問題的,它能夠保證當個別節點宕機時,
整個網絡可以不間斷地運行

keepalived軟件工作原理?(重點)
原理
1)VRRP協議,全稱Virtual Router Redundancy Protocol,中文名為虛擬路由冗余協議,
VRRP的出現是為了解決靜態路由的單點故障。
2)VRRP是用過IP多播的方式(默認多播地址(224.0.0.18))實現高可用對之間通信的。
3)工作時主節點發包,備節點接包,當備節點接收不到主節點發的數據包的時候,
就啟動接管程序接管主節點的資源。備節點可以有多個,通過優先級競選,
但一般Keepalived系統運維工作中都是一對。

keepalived軟件主要功能?
①. 管理LVS負載均衡軟件
②. 實現對LVS集群節點健康檢查功能
③. 作為系統網絡服務的高可用功能


03. 部署keepalived高可用服務:
1)確認反向代理服務是否工作正常
第一個裏程:在lb01/lb02上測試web服務器是否可以正常
curl -H host:www.etiantian.org 10.0.0.7/oldboy.html
curl -H host:www.etiantian.org 10.0.0.8/oldboy.html
curl -H host:www.etiantian.org 10.0.0.9/oldboy.html
curl -H host:bbs.etiantian.org 10.0.0.7/oldboy.html
curl -H host:bbs.etiantian.org 10.0.0.8/oldboy.html
curl -H host:bbs.etiantian.org 10.0.0.9/oldboy.html

第二個裏程:在瀏覽器上測試訪問lb01/lb02
解析hosts文件,將域名解析為10.0.0.5,進行測試訪問
解析hosts文件,將域名解析為10.0.0.6,進行測試訪問
scp -rp /application/nginx/conf/nginx.conf 10.0.0.6:/application/nginx/conf/ ---測試前同步lb01和lb02配置文件

2)安裝部署高可用keepalived服務
第一個裏程:安裝keepalived服務軟件
yum install -y keepalived

第二個裏程:編寫keepalived配置文件
vim /etc/keepalived/keepalived.conf
man keepalived.conf --- 配置文件說明信息
配置文件結構:
GLOBAL CONFIGURATION --- 全局配置(*)
VRRPD CONFIGURATION --- vrrp配置(*)
LVS CONFIGURATION --- LVS服務相關配置

lb01主負載均衡器配置
global_defs {
router_id lb01
}

vrrp_instance gorup01 {
state MASTER
interface eth0
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3/24 dev eth0 label eth0:1
}
}
/etc/init.d/keepalived reload

lb02配置信息
global_defs {
router_id lb02
}

vrrp_instance group01 {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3/24 dev eth0 label eth0:1
}
}
/etc/init.d/keepalived reload

3)進行測試訪問

04. 部署keepalived高可用服務問題
同時在keepalived高可用集群中,出現了兩個虛擬IP地址信息,這種情況就稱為腦裂

腦裂情況出現原因:
1. 心跳線出現問題
網卡配置有問題
交換設備有問題
線纜連接有問題
2. 有防火墻軟件阻止問題
3. virtual_router_id配置數值不正確
總之:只要備服務器收不到組播包,就會成為主,而主資源沒有釋放,就會出現腦裂

利用shell腳本實現監控管理:
備用設備有VIP就是表示不正常
01. 真正實現主備切換
02. 出現腦裂情況了

#!/bin/bash
check_info=$(ip a|grep -c 10.0.0.3)
if [ $check_info -ne 0 ]
then
echo "keepalived server error!!!"
fi

05. 實現nginx反向代理監控虛擬IP地址
1)編寫nginx反向代理配置
server {
listen 10.0.0.3:80;
server_name www.etiantian.org;
root html;
index index.html index.htm;
location / {
proxy_pass http://oldboy;
proxy_set_header host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
server {
listen 10.0.0.3:80;
server_name bbs.etiantian.org;
root html;
index index.html index.htm;
location / {
proxy_pass http://oldboy;
proxy_set_header host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
/application/nginx/sbin/nginx -s stop
/application/nginx/sbin/nginx
netstat -lntup|grep nginx
tcp 0 0 10.0.0.3:80 0.0.0.0:* LISTEN 53334/nginx

實現監聽本地網卡上沒有的IP地址
echo ‘net.ipv4.ip_nonlocal_bind = 1‘ >>/etc/sysctl.conf
sysctl -p

06. 將keepalived服務和反向代理nginx服務建立聯系
nginx反向代理服務停止,keepalived服務也停止
1)編寫腳本
#!/bin/bash
web_info=$(ps -ef|grep [n]ginx|wc -l)
if [ $web_info -lt 2 ]
then
/etc/init.d/keepalived stop
fi

2)運行腳本,實現監控nginx服務
編輯keepalived服務配置文件
vrrp_script check_web {
#定義一個監控腳本,腳本必須有執行權限
script "/server/scripts/check_web.sh"
#指定腳本間隔時間
interval 2
#腳本執行完成,讓優先級值和權重值進行運算,從而實現主備切換
weight 2
}

track_script {
check_web
}

chmod +x check_web.sh --- 修改腳本可執行權限

07. 實現高可用集群架構中雙主配置(互為主備配置)
lb01
vrrp_instance gorup01 {
state MASTER
interface eth0
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3/24 dev eth0 label eth0:1
}
}
vrrp_instance gorup02 {
state BACKUP
interface eth0
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.4/24 dev eth0 label eth0:1
}
}

lb02
vrrp_instance gorup01 {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3/24 dev eth0 label eth0:1
}
}
vrrp_instance gorup02 {
state MASTER
interface eth0
virtual_router_id 52
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.4/24 dev eth0 label eth0:1
}
}

修改nginx反向代理監控地址信息

十一.keepalived高可用服務實踐部署