一、案例1:根據(jù)用戶的客戶端轉(zhuǎn)發(fā)請(qǐng)求
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端的頁(yè)面 |
web02 | 172.16.1.8 | 10.0.0.8 | 存放移動(dòng)端的頁(yè)面 |
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命令行測(cè)試
[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)行測(cè)試
[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 插件
其次纵寝,打開(kāi)火狐瀏覽器進(jìn)行測(cè)試
二、案例2:動(dòng)靜態(tài)分離
根據(jù)用戶的uri進(jìn)行轉(zhuǎn)發(fā)location
imageimage
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)頁(yè)static |
web03 | 172.16.1.9 | 10.0.0.9 | 模擬存放動(dòng)態(tài)網(wǎng)頁(yè)(默認(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 瀏覽器測(cè)試
根據(jù)用戶請(qǐng)求文件類型進(jìn)行轉(zhuǎn)發(fā)
三花吟、負(fù)載均衡的排查思路
四、負(fù)載均衡的輪詢算法
默認(rèn)輪詢(rr)
加權(quán)輪詢(wrr):weight
最小連接數(shù)(least conn):根據(jù)后端服務(wù)器連接數(shù)分配任務(wù)
ip_hash:只要客戶端ip地址相同就會(huì)被轉(zhuǎn)發(fā)到同一臺(tái)機(jī)器上※※
五厨姚、會(huì)話保持
cookie和session
1>cookie和session共同點(diǎn):
存放用戶修改
key value類型衅澈,變量和變量?jī)?nèi)容
2>cookie和session區(qū)別:
cookie:
存放在瀏覽器里面
存放簡(jiǎn)單的信息或存放鑰匙
開(kāi)發(fā)設(shè)置的
相應(yīng)的時(shí)候服務(wù)器給你設(shè)置
session:
存放在服務(wù)器中--redis中
存放敏感信息
鎖頭
六、高可用keepalived
Keepalived服務(wù)的工作原理※※※
1> Keepalived高可用對(duì)之間是通過(guò)VRRP進(jìn)行通信的谬墙,VRRP是通過(guò)競(jìng)選機(jī)制來(lái)確定主備的今布,主的優(yōu)先高于備,因此拭抬,工作時(shí)主會(huì)優(yōu)先獲得所有的資源部默,備節(jié)點(diǎn)處于等待狀態(tài),當(dāng)主掛了的時(shí)候造虎,備節(jié)點(diǎn)就會(huì)接管主節(jié)點(diǎn)的資源傅蹂,然后頂替主節(jié)點(diǎn)對(duì)外提供服務(wù)。
2> 在Keepalived服務(wù)隊(duì)之間算凿,只有作為主的服務(wù)器會(huì)一直發(fā)送VRRP廣播包份蝴,告訴備他還活著,此時(shí)備不會(huì)搶占主氓轰,當(dāng)主不可用時(shí)婚夫,即備監(jiān)聽(tīng)不到主發(fā)送的廣播包時(shí),就回啟動(dòng)相關(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>每臺(tái)機(jī)器安裝好nginx
2>在lb01和lb02安裝keepalived----yum install -y keepalived
3>啟動(dòng)服務(wù)储玫,并設(shè)置開(kāi)機(jī)自啟動(dòng)
??啟動(dòng)服務(wù):systemctl start keepalived
??開(kāi)機(jī)自啟動(dòng):systemctl enable keepalived
4>檢查服務(wù):rpm -qa keepalived
6.2 配置文件
分為三個(gè)部分:
GLOBAL CONFIGURATION:全局定義部分
VRRPD CONFIGURATION:vrrp實(shí)例(rsync模塊)
LVS CONFIGURATION:通過(guò)keepalived配置文件控制lvs
6.3 keepalived配置文件詳解
https://www.processon.com/view/link/5d034b4ee4b08ceab31b8e11
6.4 配置keepalived
對(duì)lb01(主)和lb02(備)進(jìn)行以下配置
注:配置時(shí)請(qǐng)注意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