一桦卒、案例1:根據(jù)用戶的客戶端轉(zhuǎn)發(fā)請求
1.1 環(huán)境準(zhǔn)備
服務(wù)器名稱 | 內(nèi)網(wǎng)IP | 外網(wǎng)IP | |
---|---|---|---|
lb01 | 172.16.1.5 | 10.0.0.5 | 負(fù)載均衡 |
web01 | 172.16.1.7 | 10.0.0.7 | 存放PC端的頁面 |
web02 | 172.16.1.8 | 10.0.0.8 | 存放移動端的頁面 |
1.2 創(chuàng)建環(huán)境
\\web01
echo this is PC website>/app/www/lidao.html
\\web02
echo this is Mobile website>/app/www/lidao.html
1.3 lb01命令行測試
[root@lb01 ~]# curl 10.0.0.[7-8]/lidao.html
[1/2]: 10.0.0.7/lidao.html --> <stdout>
--_curl_--10.0.0.7/lidao.html
this is PC website
[2/2]: 10.0.0.8/lidao.html --> <stdout>
--_curl_--10.0.0.8/lidao.html
this is Mobile website
[root@lb01 ~]#
1.4 在lb01判斷客戶端類型,
[root@lb01 /etc/nginx]# cat nginx.conf
……
upstream default_pools{
server 10.0.0.7:80 weight=1 max_fails=1 fail_timeout=10s;
}
upstream mobile_pools{
server 10.0.0.8:80 weight=1 max_fails=1 fail_timeout=10s;
}
server{
listen 80;
server_name www.oldboy.com;
location / {
if ($http_user_agent ~* "Android|IOS") {
proxy_pass http://mobile_pools;
}
proxy_pass http://default_pools;
proxy_set_header Host $host;
proxy_set_header X_Forwarded-For $remote_addr;
}
}
……
[root@lb01 /etc/nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 /etc/nginx]# systemctl reload nginx
1>在命令行進(jìn)行測試
[root@lb01 /etc/nginx]# curl 10.0.0.5/lidao.html
this is PC website
[root@lb01 /etc/nginx]# curl -A ios 10.0.0.5/lidao.html
this is Mobile website
[root@lb01 /etc/nginx]#
2>在火狐瀏覽器進(jìn)行
首先匿又,下載安裝firefox方灾,安裝 user agent switch 插件
其次,打開火狐瀏覽器進(jìn)行測試
二琳省、案例2:動靜態(tài)分離
根據(jù)用戶的uri進(jìn)行轉(zhuǎn)發(fā)location
2.1 環(huán)境準(zhǔn)備
服務(wù)器名稱 | 內(nèi)網(wǎng)IP | 外網(wǎng)IP | |
---|---|---|---|
lb01 | 172.16.1.5 | 10.0.0.5 | 負(fù)載均衡 |
web01 | 172.16.1.7 | 10.0.0.7 | 模擬存放上傳/upload |
web02 | 172.16.1.8 | 10.0.0.8 | 模擬存放靜態(tài)網(wǎng)頁static |
web03 | 172.16.1.9 | 10.0.0.9 | 模擬存放動態(tài)網(wǎng)頁(默認(rèn)) |
2.2 創(chuàng)建環(huán)境
\\\\web01
[root@web01 ~]# mkdir -p /app/www/upload/
[root@web01 ~]# echo this is upload >/app/www/upload/guoav.html
[root@web01 ~]#
\\\\web02
[root@web02 ~]# mkdir -p /app/www/static/
[root@web02 ~]# echo this is static >/app/www/static/guoav.html
[root@web02 ~]#
\\\\web03
[root@web03 ~]# mkdir -p /app/www/
[root@web03 ~]# echo this is default >/app/www/guoav.html
[root@web03 ~]#
檢查模擬環(huán)境是否ok
[root@web01 ~]# curl 10.0.0.7/upload/guoav.html
this is upload
[root@web01 ~]# curl 10.0.0.8/static/guoav.html
this is static
[root@web01 ~]# curl 10.0.0.9/guoav.html
this is default
[root@web01 ~]#
2.3 配置lb01
[root@lb01 /etc/nginx]# cat nginx.conf
……
upstream upload{
server 10.0.0.7:80 weight=1 max_fails=1 fail_timeout=10s;
}
upstream static{
server 10.0.0.8:80 weight=1 max_fails=1 fail_timeout=10s;
}
upstream default{
server 10.0.0.9:80 weight=1 max_fails=1 fail_timeout=10s;
}
server{
listen 80;
server_name www.oldboy.com;
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;
}
location / {
proxy_pass http://default;
proxy_set_header Host $host;
proxy_set_header X_Forwarded-For $remote_addr;
}
}
……
[root@lb01 /etc/nginx]#
[root@lb01 /etc/nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 /etc/nginx]# systemctl reload nginx
2.4 瀏覽器測試
根據(jù)用戶請求文件類型進(jìn)行轉(zhuǎn)發(fā)
三迎吵、負(fù)載均衡的排查思路
四、負(fù)載均衡的輪詢算法
默認(rèn)輪詢(rr)
加權(quán)輪詢(wrr):weight
最小連接數(shù)(least conn):根據(jù)后端服務(wù)器連接數(shù)分配任務(wù)
ip_hash:只要客戶端ip地址相同就會被轉(zhuǎn)發(fā)到同一臺機(jī)器上※※
五针贬、會話保持
cookie和session
1>cookie和session共同點(diǎn):
存放用戶修改
key value類型击费,變量和變量內(nèi)容
2>cookie和session區(qū)別:
cookie:
存放在瀏覽器里面
存放簡單的信息或存放鑰匙
開發(fā)設(shè)置的
相應(yīng)的時(shí)候服務(wù)器給你設(shè)置
session:
存放在服務(wù)器中--redis中
存放敏感信息
鎖頭
六、高可用keepalived
Keepalived服務(wù)的工作原理※※※
1> Keepalived高可用對之間是通過VRRP進(jìn)行通信的桦他,VRRP是通過競選機(jī)制來確定主備的蔫巩,主的優(yōu)先高于備,因此快压,工作時(shí)主會優(yōu)先獲得所有的資源圆仔,備節(jié)點(diǎn)處于等待狀態(tài),當(dāng)主掛了的時(shí)候蔫劣,備節(jié)點(diǎn)就會接管主節(jié)點(diǎn)的資源坪郭,然后頂替主節(jié)點(diǎn)對外提供服務(wù)。
2> 在Keepalived服務(wù)隊(duì)之間脉幢,只有作為主的服務(wù)器會一直發(fā)送VRRP廣播包歪沃,告訴備他還活著,此時(shí)備不會搶占主嫌松,當(dāng)主不可用時(shí)沪曙,即備監(jiān)聽不到主發(fā)送的廣播包時(shí),就回啟動相關(guān)服務(wù)接管資源萎羔,保證業(yè)務(wù)的連續(xù)性液走。
3> 接管速度最快可以小于1秒。
6.1 環(huán)境準(zhǔn)備
服務(wù)器名稱 | 內(nèi)網(wǎng)IP | 外網(wǎng)IP |
---|---|---|
lb01 | 172.16.1.5 | 10.0.0.5 |
lb02 | 172.16.1.6 | 10.0.0.6 |
web01 | 172.16.1.7 | 10.0.0.7 |
web02 | 172.16.1.8 | 10.0.0.8 |
1>每臺機(jī)器安裝好nginx
2>在lb01和lb02安裝keepalived----yum install -y keepalived
3>啟動服務(wù)贾陷,并設(shè)置開機(jī)自啟動
??啟動服務(wù):systemctl start keepalived
??開機(jī)自啟動:systemctl enable keepalived
4>檢查服務(wù):rpm -qa keepalived
6.2 配置文件
分為三個部分:
GLOBAL CONFIGURATION:全局定義部分
VRRPD CONFIGURATION:vrrp實(shí)例(rsync模塊)
LVS CONFIGURATION:通過keepalived配置文件控制lvs
6.3 keepalived配置文件詳解
https://www.processon.com/view/link/5d034b4ee4b08ceab31b8e11
6.4 配置keepalived
對lb01(主)和lb02(備)進(jìn)行以下配置
注:配置時(shí)請注意lbo1主負(fù)載均衡與lb02備負(fù)載均衡有些不同
[root@lb01 /etc/keepalived]# cp keepalived.conf{,.bak}
[root@lb01 /etc/keepalived]# ll
total 8
-rw-r--r-- 1 root root 3598 Jan 6 16:47 keepalived.conf
-rw-r--r-- 1 root root 3598 Jun 14 15:56 keepalived.conf.bak
[root@lb01 /etc/keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb01
}
vrrp_instance VI_1 {
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
}
}
[root@lb01 /etc/keepalived]# systemctl restart keepalived.service