實驗前期準備:
5臺虛擬機:4臺當做服務器,1臺當做客戶機(當然,也可以使用主機的瀏覽器),4臺服務器中,2臺服務器當做后端真實訪問服務器;另外2臺服務器當做負載均衡服務器。
并且關閉所有防火墻及核心防護
systemctl stop firewalld
setenforce 0
后端真實服務器1:
yum -y install epel-release
yum -y install nginx
echo '自定義內容1'>/usr/share/nginx/html/index.html
systemctl start nginx
curl 127.1 #查看nginx內容
自定義訪問內容
后端真實服務器2:
yum -y install epel-release
yum -y install nginx
echo '自定義內容2'>/usr/share/nginx/html/index.html
systemctl start nginx
curl 127.1 #查看nginx內容
自定義訪問內容(建議2者不要一樣,方便可以觀察變化)
負載均衡器1:
yum -y install epel-release
yum -y install nginx
vim /etc/nginx/nginx.conf
[#http模塊下
upstream web {
server 真實服務器1;
server 真實服務器2;
}#server模塊下
location / {
proxy_pass http://web;
}
]
systemctl start nginx
先做好負載均衡可以使用 'curl 127.1' 命令,查看是否是真實服務1,2的自定義內容。
yum -y install keepalive
vim /etc/keepalive/keepalive.conf
[
global_defs {router_id LVS_01#vrrp_strict
}vrrp_script check_down {script "/etc/keepalived/ng.sh"interval 1weight -30fall 3rise 2timeout 2
}vrrp_instance VI_1 {state MASTERinterface ens33virtual_router_id 50priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.174.188}track_script {check_down}
}
]
vim /etc/keepalived/ng.sh
{
killall -0 nginx
}
systemctl start keepalived.service
systemctl start nginx.service
主配置內容:
全局配置:自定義當前路由器唯一標識符為“LVS_01” ;注釋掉嚴格模式檢查。
vrrp健康檢查:健康檢查路徑;每秒執行一次;當腳本檢測失敗優先級降低30;連續3次失敗,鑒定為故障;連續2次成功,鑒定為恢復正常;腳本執行等待時間為2秒。
vrrp配置:初始為主設備;使用ens33網卡;虛擬路由組為50(主從一致);起始優先級為100;vrrp通告間隔為1秒;認證設置為密碼認證,密碼為1111(主從一致);定義的虛擬IP地址為192.168.174.188;關聯的健康檢查腳本為check_down。
負載均衡器2:
yum -y install epel-release
yum -y install nginx
vim /etc/nginx/nginx.conf
[#http模塊下
upstream web {
server 真實服務器1;
server 真實服務器2;
}#server模塊下
location / {
proxy_pass http://web;
}
]
systemctl start nginx
先做好負載均衡可以使用 'curl 127.1' 命令,查看是否是真實服務1,2的自定義內容。
yum -y install keepalive
vim /etc/keepalive/keepalive.conf
[
global_defs {router_id LVS_02#vrrp_strict
}vrrp_script check_down {script "/etc/keepalived/ng.sh"interval 1weight -30fall 3rise 2timeout 2
}vrrp_instance VI_1 {state BACKUPinterface ens33virtual_router_id 50priority 99advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.174.188}track_script {check_down}
}]
vim /etc/keepalived/ng.sh
{
killall -0 nginx
}
systemctl start keepalived.service
systemctl start nginx.service
從配置內容:
全局配置:自定義當前路由器唯一標識符為“LVS_02” ;注釋掉嚴格模式檢查。
vrrp健康檢查:健康檢查路徑;每秒執行一次;當腳本檢測失敗優先級降低30;連續3次失敗,鑒定為故障;連續2次成功,鑒定為恢復正常;腳本執行等待時間為2秒。
vrrp配置:初始為從設備;使用ens33網卡;虛擬路由組為50(主從一致);起始優先級為99;vrrp通告間隔為1秒;認證設置為密碼認證,密碼為1111(主從一致);定義的虛擬IP地址為192.168.174.188;關聯的健康檢查腳本為check_down。
實驗驗證:
使用瀏覽器訪問192.168.174.188
關閉主配置,再來查看是否對訪問造成影響:
此時的從設備:獲得了虛擬IP地址
我們重新打開主設備來查看主從的變化: