一.體系架構(gòu)
在Keepalived + Nginx高可用負(fù)載均衡架構(gòu)中,keepalived負(fù)責(zé)實現(xiàn)High-availability (HA) 功能控制前端機VIP(虛擬網(wǎng)絡(luò)地址)狰晚,當(dāng)有設(shè)備發(fā)生故障時,熱備服務(wù)器可以瞬間將VIP自動切換過來,實際運行中體驗只有2秒鐘切換時間,DNS服務(wù)可以負(fù)責(zé)前端VIP的負(fù)載均衡揪垄。
nginx負(fù)責(zé)控制后端web服務(wù)器的負(fù)載均衡,將客戶端的請求按照一定的算法轉(zhuǎn)發(fā)給后端Real Server處理逻翁,而Real Server將響應(yīng)直接返回給客戶端饥努。
二. 優(yōu)點
1.實現(xiàn)了可彈性化的架構(gòu),在壓力增大的時候可以臨時添加web服務(wù)器添加到這個架構(gòu)里面去;
2.upstream具有負(fù)載均衡能力卢未,可以自動判斷后端的機器肪凛,并且自動踢出不能正常提供服務(wù)的機器;
3.相對于lvs而言辽社,正則分發(fā)和重定向更為靈活伟墙。而Keepalvied可保證單個nginx負(fù)載均衡器的有效性,避免單點故障滴铅;
4.用nginx做負(fù)載均衡戳葵,無需對后端的機器做任何改動。
5.nginx部署在docker容器里汉匙,即大量地節(jié)約開發(fā)拱烁、測試、部署的時間噩翠,又可以在出現(xiàn)故障時通過鏡像快速恢復(fù)業(yè)務(wù)戏自。
三. 系統(tǒng)環(huán)境
兩臺負(fù)載機器安裝:centos7.2+docker+nginx+keepalived,分別命名為:NGINX_MASTER伤锚,NGINX_BACKUP擅笔。
后端web服務(wù)器,可以是提供web服務(wù)的任何架構(gòu)屯援,分別命名為:WEB_1猛们,WEB_2。
后端數(shù)據(jù)庫機器可任意架構(gòu)狞洋,只要能提供數(shù)據(jù)庫服務(wù)即可弯淘。
服務(wù)器 操作系統(tǒng) IP地址 安裝軟件
NGINX_MASTER Centos 7.2 64位 10.141.1.32 docker+nginx+keepalived
NGINX_BACKUP Centos 7.2 64位 10.141.9.2 docker+nginx+keepalived
WEB_1 Centos 7.2 64位 10.141.3.73 docker+web
WEB_2 Centos 7.2 64位 10.141.26.218 docker+web
虛擬IP 10.141.1.33
四. 搭建環(huán)境
1. 主機準(zhǔn)備
全部主機執(zhí)行命令
setenforce 0 #關(guān)閉selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
systemctl stop firewalld #關(guān)閉防火墻
systemctl stop iptables #關(guān)閉iptables
2. docker安裝(全部主機執(zhí)行命令)
a. 在線安裝
參考: (https://docs.docker.com/install/linux/docker-ce/centos/#uninstall-old-versions)
yum install docker
b. 離線二進(jìn)制安裝:
參考:(http://www.reibang.com/p/46b9a351f749)
3. 準(zhǔn)備web服務(wù)器
a. 啟動服務(wù)
web1和web2執(zhí)行,這里使用python啟動一個simplehttpserver
touch 123.txt #在web1執(zhí)行
touch 456.txt #在web2執(zhí)行
python -m SimpleHTTPserver #web1,web2都執(zhí)行
b. 檢查
curl 10.141.3.73:8000 #返回123.txt
curl 10.141.26.218:8000 #返回456.txt
4. 安裝nginx進(jìn)行負(fù)載均衡吉懊,在master和backup執(zhí)行
a. 拉鏡像
docker pull nginx
b. vim nginx.conf 庐橙,增加 upstream和server
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
upstream linuxidc {
server 10.141.3.73:8000;
server 10.141.26.218:8000;
}
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
proxy_pass http://linuxidc;
}
}
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
c. 啟動nginx
docker run -it -d -p 80:80 -v /${PWD}/nginx.conf:/etc/nginx/nginx.conf nginx
d. 驗證
curl localhost #返回123.txt 或者返回456.txt
5. 搭建keepalived進(jìn)行熱備(在master和backup執(zhí)行)
a. 安裝keepalived
yum install -y keepalived
systemctl start keepalived
systemctl enable keepalived
b. 修改配置文件/etc/keepalived/keepalived.conf
這里使用的是單播模式,解決腦裂問題借嗽,云主機(比如阿里云怕午,騰訊云。亞信云等)需要單獨申請VIP并綁定主機淹魄,否則不能訪問VIP
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
#vrrp_strict #單播模式要注釋掉
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script chk_port { #檢測服務(wù)是否在運行郁惜。有很多方式,比如進(jìn)程甲锡,用腳本檢測等等
script "/root/chk_server.sh" #這里通過腳本監(jiān)測
interval 2 #腳本執(zhí)行間隔兆蕉,每2s檢測一次
weight -10 #腳本結(jié)果導(dǎo)致的優(yōu)先級變更,檢測失旂吐佟(腳本返回非0)則優(yōu)先級 -10
fall 2 #檢測連續(xù)2次失敗才算確定是真失敗虎韵。會用weight減少優(yōu)先級(1-255之間)
rise 1 #檢測1次成功就算成功。但不修改優(yōu)先級
}
vrrp_instance VI_1 {
state MASTER #backup主機填寫B(tài)ACKUP
unicast_src_ip 10.141.1.32 #寫本機地址
unicast_peer {
10.141.9.2 #填寫另外一臺keepalived主機地址
}
interface eth0 #網(wǎng)卡
virtual_router_id 58 #默認(rèn)51缸废,可以換一個地址包蓝,避免沖突驶社,主備id要一樣
priority 100 #權(quán)重,backup修改為95,檢查失敗后優(yōu)先級變90测萎,低于95會將vip轉(zhuǎn)移到slave
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.141.1.33 #虛擬ip
}
track_script {
chk_port
}
}
chk_server.sh腳本
counter=$(netstat -na|grep "LISTEN"|grep "80"|wc -l)
if [ "${counter}" -eq 0 ]; then
exit 0
fi
c. 驗證
systemctl restart keepalived
ip a查看master中綁定VIP亡电,backup沒有綁定
master上執(zhí)行systemctl stop keepalived,可以發(fā)現(xiàn)VIP漂流到backup上
curl 10.141.1.33 #返回結(jié)果為123.txt或者456.txt