使用華為云VPC搭建Keepalived+Nginx高可用WEB負載均衡
問題描述:
本文檔介紹如何使用華為云VPC在CentOS6.8系統(tǒng)環(huán)境實現(xiàn) Keepalived+Nginx高可用WEB負載均衡環(huán)境搭建。
1、物料準備
本文檔中的操作為實驗環(huán)境,所有資源均為“按需付費”,如果您自己在生產(chǎn)環(huán)境中使用,建議使用包年/包月模式。為確保各資源組件可連通性,一定確保所有資源均在“華東-上海二”購買,實際生產(chǎn)環(huán)境按業(yè)務實際需要選擇區(qū)域。
1.1、購買虛擬私有云(VPC)并指定子網(wǎng)
(1)、虛擬私有云vpc-HA和子網(wǎng)subnet-HA
登錄華為云官網(wǎng),按照網(wǎng)站導航——產(chǎn)品à網(wǎng)絡à?虛擬私有云 VPC找到虛擬私有云(VPC)產(chǎn)品。
點擊“立即使用”購買虛擬私有云 VPC進入“申請?zhí)摂M私有云”頁面。
(2)、購買完成的虛擬私有云(vpc-HA)和子網(wǎng)(subnet-HA)信息如下圖所示
1.2?、?購買彈性云服務器(ECS)
登錄華為云官網(wǎng),按照網(wǎng)站導航——產(chǎn)品à計算à彈性云服務器 ECS找到彈性云服務器 (ECS)產(chǎn)品。
點擊“立即購買”進入“購買彈性云服務器”頁面
進入購買頁面后,購買過程中注意指定“物料準備”中指定的如下相關信息:
(1)、ecs-HA1:
(2)、?ecs-HA2:
(3)、?購買完成的兩臺彈性云服務器配置信息
我們這里是實驗環(huán)境,所有一切從簡,并未選購數(shù)據(jù)盤,實際生產(chǎn)環(huán)境使用時請按業(yè)務需求選購數(shù)據(jù)盤,并切實考慮HA兩個節(jié)點之間業(yè)務數(shù)據(jù)一致性(同步)問題。
1.3、購買彈性公網(wǎng)IP(EIP)
登錄華為云官網(wǎng),按照網(wǎng)站導航——產(chǎn)品à網(wǎng)絡à?彈性公網(wǎng)IP(Elastic IP),找到彈性公網(wǎng)IP(Elastic IP)購買頁。
我們本次購買到的彈性公網(wǎng)IP為XXXXXXXXXXX
1.4、申請?zhí)摂MIP地址
登錄華為云官網(wǎng),按照網(wǎng)站導航——控制臺→服務列表→網(wǎng)絡→虛擬私有云→vpc-HA→子網(wǎng)→subnet-HA→虛擬IP→申請?zhí)摂MIP地址。
2?、?操作步驟
2.1、 配置彈性云服務器環(huán)境
配置彈性云服務器環(huán)境需要使用互聯(lián)網(wǎng)安裝相關軟件包,所以我們在配置彈性云服務器環(huán)境的過程中會臨時使用彈性公網(wǎng)IPXXXXXXXXXX。
(1)、ecs-HA1:
A.? 綁定彈性公網(wǎng)IP XXXXXXXXXXXX到ecs-HA1。
登錄華為云官網(wǎng),按照網(wǎng)站導航——控制臺à服務列表à計算à彈性云服務器,找到ecs-HA1。
B.??通過ssh連接ecs-HA1安裝nginx、keepalived 軟件包及相關依賴包。
# yum install nginx ? keepalived -y
C.??編輯nginx配置文件。
# vim ? /etc/nginx/nginx.conf
user root;
worker_processes 1;
#error_log ? logs/error.log;
#error_log ? logs/error.log notice;
#error_log ? logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections ? 1024;
}
http {
include mime.types;
default_type ? application/octet-stream;
#log_format main ? '$remote_addr - $remote_user [$time_local] "$request" '
# '$status ? $body_bytes_sent "$http_referer" '
# ? '"$http_user_agent" "$http_x_forwarded_for"';
#access_log ? logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log ? logs/host.access.log main;
location / {
root html;
index index.html ? index.htm;
}
#error_page 404 ? /404.html;
# redirect server error ? pages to the static page /50x.html
error_page 500 502 503 ? 504 /50x.html;
location = /50x.html {
root html;
}
}
}
D.? ?編輯index.html文件內容以演示訪問效果
# vim ? /usr/share/nginx/html/index.html
Welcome ? to ECS-HA1
E.??設置nginx服務開機自啟動并啟動服務
# systemctl ? enable nginx
# systemctl ? start nginx.service
F.??Nginx單節(jié)點訪問測試。
G.??編輯keepalived配置文件。
# vim ? /etc/keepalived/keepalived.conf
! Configuration File for ? keepalived
global_defs {
router_id master-node
}
vrrp_script ? chk_http_port {
script ? "/etc/keepalived/chk_nginx.sh"
interval 2
weight -5
fall 2
rise 1
}
vrrp_instance VI_1 {
state MASTER
interface eth0
mcast_src_ip ? 192.168.0.10
virtual_router_id 51
priority 101
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.0.100
}
track_script {
chk_http_port
}
}
H.??編輯nginx監(jiān)控腳本。
# vim ? /etc/keepalived/chk_nginx.sh
#!/bin/bash
counter=$(ps -C nginx ? --no-heading|wc -l)
if [ "${counter}" ? = "0" ]; then
systemctl start ? nginx.service
sleep 2
counter=$(ps -C nginx ? --no-heading|wc -l)
if [ ? "${counter}" = "0" ]; then
systemctl stop ? keepalived.service
fi
fi
chmod +x ? /etc/keepalived/chk_nginx.sh
I.??設置keepalived服務開機自啟動并啟動服務
# systemctl enable ? keepalived
# systemctl start ? keepalived.service
(2)、?ecs-HA2:
先將彈性公網(wǎng)IP XXXXXXXX從ecs-HA1解綁定。
A.???綁定彈性公網(wǎng)IP xxxxxxxx到ecs-HA2。
登錄華為云官網(wǎng),按照網(wǎng)站導航——控制臺à服務列表à計算à彈性云服務器,找到ecs-HA2。
B.??通過ssh連接ecs-HA1安裝nginx、keepalived 軟件包及相關依賴包。
# yum install nginx keepalived ? -y
C.???編輯nginx配置文件(也可以從ecs-HA1上復制一份)。
# ? vim /etc/nginx/nginx.conf
user root;
worker_processes 1;
#error_log ? logs/error.log;
#error_log ? logs/error.log notice;
#error_log ? logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections ? 1024;
}
http {
include mime.types;
default_type ? application/octet-stream;
#log_format main ? '$remote_addr - $remote_user [$time_local] "$request" '
# '$status ? $body_bytes_sent "$http_referer" '
# ? '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log ? main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log ? logs/host.access.log main;
location / {
root html;
index index.html ? index.htm;
}
#error_page 404 ? /404.html;
# redirect server error ? pages to the static page /50x.html
error_page 500 502 503 ? 504 /50x.html;
location = /50x.html {
root html;
}
}
}
D.???編輯index.html文件內容以演示訪問效果
# vim ? /usr/share/nginx/html/index.html
Welcome ? to ECS-HA2
E.???設置nginx服務開機自啟動并啟動服務
# systemctl ? enable nginx
# systemctl ? start nginx.service
F.? ?Nginx單節(jié)點訪問測試。
G.???編輯keepalived配置文件。
# ? vim /etc/keepalived/keepalived.conf
! Configuration File ? for keepalived
global_defs {
router_id master-node
}
vrrp_script ? chk_http_port {
script ? "/etc/keepalived/chk_nginx.sh"
interval 2
weight -5
fall 2
rise 1
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
mcast_src_ip ? 192.168.0.20
virtual_router_id 51
priority 101
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.0.100
}
track_script {
chk_http_port
}
}
H.???編輯nginx監(jiān)控腳本并添加執(zhí)行權限(也可以從ecs-HA1上復制一份)
# vim ? /etc/keepalived/chk_nginx.sh
#!/bin/bash
counter=$(ps -C nginx ? --no-heading|wc -l)
if [ ? "${counter}" = "0" ]; then
systemctl start ? nginx.service
sleep 2
counter=$(ps -C nginx ? --no-heading|wc -l)
if [ ? "${counter}" = "0" ]; then
systemctl stop ? keepalived.service
fi
fi
chmod +x ? /etc/keepalived/chk_nginx.sh
設置keepalived服務開機自啟動并啟動服務
# systemctl enable ? keepalived
# systemctl start ? keepalived
2.2?、綁定虛擬IP。
先將彈性公網(wǎng)IP xxxxxxxx從ecs-HA2解綁定。
登錄華為云官網(wǎng),按照網(wǎng)站導航——控制臺à服務列表à網(wǎng)絡à虛擬私有云àvpc-HAà子網(wǎng)àsubnet-HAà虛擬IP。
(1)、綁定虛擬IP到彈性云服務器ecs-HA1。
(2)、?綁定虛擬IP到彈性云服務器ecs-HA2。
(3)、?綁定虛擬IP到彈性公網(wǎng)IP xxxxxxx。
3?、結果驗證
分別重啟ecs-HA1和ecs-HA2,通過華為云官網(wǎng)用戶控制臺遠程登錄到ecs-HA1上按以下操作進行驗證。
3.1、查看虛IP是否有綁定到ecs-HA1的eth0網(wǎng)卡上
# ip addr show
3.2?、通過瀏覽器訪問彈性公網(wǎng)IP看是否可以訪問到ecs-HA1節(jié)點上的WEB測試頁。
3.3、手動停止主服務器(ecs-HA1)上的keepalived服務,然后查看從服務器(ecs-HA2)是否有接管VIP;通過瀏覽器訪問彈性公網(wǎng)IP看是否可以訪問到ecs-HA2節(jié)點上的WEB測試頁。
# systemctl stop ? keepalived.service
通過以上驗證信息可以看出我們的高可用WEB負載均衡功能正常。
虛擬私有云 VPC
版權聲明:本文內容由網(wǎng)絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內刪除侵權內容。