說明:這次做了一個需要8臺虛擬機的項目 其架構如下圖
一、簡介
keepalived是一個類似於layer3, 4 & 5交換機制的軟件,也就是我們平時說的第3層、第4層和第5層交換。Keepalived是自動完成,不需人工干涉。
Layer3,4,5工作在IP/TCP協議棧的IP層,TCP層,及應用層,原理分別如下:
Layer3:Keepalived使用Layer3的方式工作式時,Keepalived會定期向服務器羣中的服務器發送一個ICMP的數據包(既我們平時用的Ping程序),如果發現某臺服務的IP地址沒有激活,Keepalived便報告這臺服務器失效,並將它從服務器羣中剔除,這種情況的典型例子是某臺服務器被非法關機。Layer3的方式是以服務器的IP地址是否有效作爲服務器工作正常與否的標準。
Layer4:如果理解了Layer3的方式,Layer4就容易了。Layer4主要以TCP端口的狀態來決定服務器工作正常與否。如web server的服務端口一般是80,如果Keepalived檢測到80端口沒有啓動,則Keepalived將把這臺服務器從服務器羣中剔除。
Layer5:Layer5對指定的URL執行HTTP GET。然後使用MD5算法對HTTP GET結果進行求和。如果這個總數與預期值不符,那麼測試是錯誤的,服務器將從服務器池中移除。該模塊對同一服務實施多URL獲取檢查。如果您使用承載多個應用程序服務器的服務器,則此功能很有用。此功能使您能夠檢查應用程序服務器是否正常工作。MD5摘要是使用genhash實用程序(包含在keepalived軟件包中)生成的。
Nginx作爲負載均衡服務:Nginx 既可以在內部直接支持 Rails 和 PHP 程序對外進行服務,也可以支持作爲 HTTP代理服務對外進行服務。Nginx採用C進行編寫,是一款高性能的反向代理服務器。
二、環境說明
虛擬機數量:四臺
操作系統:全部是centos7.3
keepalived版本:keepalived-2.0.7
nginx版本:nginx-1.14.0
主機名稱 | 安裝軟件 | 公網ip | 私網ip |
---|---|---|---|
keepalived_web_m | keepalived+nginx | 172.18.74.93 | 192.168.255.51 |
keepalived_web_s | keepalived+nginx | 172.18.74.80 | 192.168.255.54 |
web1 | apache | 172.18.74.92 | 192.168.255.50 |
web2 | apache | 172.18.74 .102 | 192.168.255.53 |
三、環境安裝
首先關閉所有機器的防火前和selinux,配置好之後我們開啓,然後添加策略。
systemctl stop firewalld
setenforce 0
1 安裝nginx
首先下載並解壓需要的包
wget http://www.zlib.net/zlib-1.2.11.tar.gz
tar -zxvf zlib-1.2.11.tar.gz
wget https://jaist.dl.sourceforge.net/project/pcre/pcre/8.41/pcre-8.41.tar.gz
tar -zxvf pcre-8.41.tar.gz
wget https://www.openssl.org/source/openssl-1.0.2o.tar.gz
tar -zxvf openssl-1.0.2o.tar.gz
wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
tar -zxvf ngx_cache_purge-2.3.tar.gz
wget http://nginx.org/download/nginx-1.14.0.tar.gz
tar -zxvf nginx-1.14.0.tar.gz
進入nginx目錄
cd nginx-1.14.0
配置安裝選項
/configure --add-module=../ngx_cache_purge-2.3 --prefix=/usr/local/nginx --with-http_ssl_module --with-stream --with-pcre=../pcre-8.41 --with-zlib=../zlib-1.2.11 --with-openssl=../openssl-1.0.2o
編譯並安裝
make&&make install
先備份一份nginx配置文件cd /usr/local/nginx/conf
cp nginx.conf ./nginx.conf.bak
修改nginx.conf的配置
vim nginx
修改紅框裏的內容
保存退出然後重新加載nginx
nginx -s reload
2 下載並安裝keepalived
yum install wget make gcc gcc-c++ openssl-devel
wget http://www.keepalived.org/software/keepalived-2.0.7.tar.gz
tar zxvf keepalived-2.0.7.tar.gz
cd keepalived-2.0.7
./configure --prefix=/data/keepalived
如果報以下警告:
WARNING - this build will not support IPVS with IPv6. Please install libnl/libnl-3 dev libraries to support IPv6 with IPVS.
不用擔心,我們只需要用到VRRP功能,不需要用IPVS功能,所以請確保以下三項是yes就行了。
編譯並安裝
make&&make install
2、將keepalived 以服務方式啓動
創建/etc/keepalived目錄並把keepalived的配置文件拷貝/etc/keepalived目錄下並加入到開機啓動。
mkdir /etc/keepalived
cp /data/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ \
systemctl enable keepalived
3、 修改keepalived配置文件
! Configuration File for keepalived
global_defs {
notification_email {
[email protected] #這裏是說keepalived有故障時給誰發送郵件
}
notification_email_from [email protected] #這裏是當keepalived有問題時郵件的發送方,用不到一般
smtp_server 127.0.0.1 #郵件服務器的地址
smtp_connect_timeout 30
router_id keepalived01 #路由的id 一般寫主機名
vrrp_skip_check_adv_addr
#vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh" # 檢查nginx狀態的腳本
interval 2
weight 3
}
vrrp_instance VI_1 {
state MASTER #注意這裏主上寫MASTER
interface ens192 //公網網卡
virtual_router_id 51 //主從服務器的這個id要一致
priority 100 #優先級越高,vip就在誰那
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.18.74.100 #虛擬ip,有多個vip可在下面繼續增加
}
track_script { ## 將 track_script 塊加入 instance 配置塊
chk_nginx
}
}
備主機裏的keepalived配置除了與上面註釋裏不同的其他一樣
4、添加檢查nginx狀態的腳本
#!/bin/bash
A=ps -C nginx --no-header |wc -l
if [ $A -eq 0 ];then
/usr/local/nginx/sbin
sleep 3
if [ps -C nginx --no-header |wc -l
-eq 0 ];then
systemctl stop keepalived
fi
fi
**這裏的ps -C nginx --no-header |wc -l是被反引號引起來的
重啓keepalived
systemctl restart keepalived
輸入命令ip a查看是否有vip
然後關閉master的keepalived
systemctl stop keepalived
查看backup上是否有vip
有說明配置成功
沒有說明keepalived.conf存在問題
5 安裝apache服務器
在兩臺web服務器上安裝apache服務
yum install -y httpd
編輯默認頁面
cd /var/www/html/
vim index.html
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>產品添加-JD產品管理系統</title>
</head>
<body>
<h3>JD產品管理</h3>
<form action="deal.php" method="post" enctype="multipart/form-data">
圖書名稱:<input type='text' name='title' />
<hr />
銷售價格:<input type="text" name='price' />
<hr />
市場價格:<input type="text" name='market_price' />
<hr />
<input type="submit" name="submit" value="添加" />
<input type="reset" name="reset" value="重置" />
</form>
</body>
</html>
同樣在web2裏安裝apache並修改index.html
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>產品添加-JD產品管理系統</title>
</head>
<body>
<h3>web2_JD產品管理</h3>
<form action="deal.php" method="post" enctype="multipart/form-data">
圖書名稱:<input type='text' name='title' />
<hr />
銷售價格:<input type="text" name='price' />
<hr />
市場價格:<input type="text" name='market_price' />
<hr />
<input type="submit" name="submit" value="添加" />
<input type="reset" name="reset" value="重置" />
</form>
</body>
</html>
然後開啓兩臺web服務器上的apache
systemctl start httpd
通過瀏覽器訪問vip
可以看到web1的頁面,再次刷新會出現web2的頁面
因爲在nginx的配置爲你教案中設置的web1和web2的權重爲100:80
所以每訪問180次會出現100次web1,80次web2
下一篇我們講《keepalived+nginx+mysql的高可用負載均衡配置》。