00.課程介紹部分
1.實(shí)現(xiàn)負(fù)載均衡模塊的詳述
upstream 模塊: 負(fù)載均衡
proxy 模塊: 反向代理
2.企業(yè)實(shí)際應(yīng)用過程
實(shí)現(xiàn)企業(yè)網(wǎng)站動靜分離
實(shí)現(xiàn)不同客戶端訪問顯示不同頁面
3.高可用冗余服務(wù)介紹(keepalived--vrrp協(xié)議)
4.高可用服務(wù)部署過程
01.課程知識回顧
1 實(shí)現(xiàn)數(shù)據(jù)庫數(shù)據(jù)遷移
1) mysqldump -uroot -poldboy123 -A >/tmp/mysql.bak
2) scp /tmp/mysql.bak 172.16.1.5:/tmp/mysql.bak
3) mysql -uroot -poldboly123 </tmp/mysql.bak
4) grant all on wordpress.* to 'wordpress'@'172.16.1.%' identified by 'oldboy123'
vim/ /thml/blog/wp-config.php
2 實(shí)現(xiàn)網(wǎng)站數(shù)據(jù)共享存儲
1) showmount -e 172.16.1.31
2) wordpress cd/html/bolg/wp-content/uploads
3) mount -t nfs 172.16.1.31:/data/blog /html/bolg/wp-content/uploads
3 實(shí)現(xiàn)網(wǎng)站域名修改
1) 修改nginx配置文件信息
2) 修改代碼或數(shù)據(jù)庫信息
3) 重啟nginxfuwu
4) 修改本地DNS文件
4 負(fù)載均衡概念
負(fù)載作用
1)對網(wǎng)站用戶請求進(jìn)行調(diào)度處理
2)減少web網(wǎng)站壓力
負(fù)載均衡實(shí)現(xiàn)
硬件方式實(shí)現(xiàn) F5 A10 (廠商培訓(xùn))
軟件方式實(shí)現(xiàn)
nginx 利用網(wǎng)路4層或7層功能實(shí)現(xiàn)負(fù)載均衡
LVS 利用網(wǎng)路4層或7層功能實(shí)現(xiàn)負(fù)載均衡
haproxy 利用網(wǎng)路4層或7層功能實(shí)現(xiàn)負(fù)載均衡 (功能更強(qiáng)大\圖形界面)
集群 多臺服務(wù)器完成相同的工作
反向代理 外網(wǎng)用戶 可以訪問內(nèi)網(wǎng)服務(wù)器
正箱代理 內(nèi)網(wǎng)用戶 可以訪問外網(wǎng)服務(wù)器
5 實(shí)現(xiàn)負(fù)載均衡配置
vim /etc/yum/repos.dnginx.repo
yum -y install ngixn
1) 搭建web集群
2) 分發(fā) 測試
3) 負(fù)載君衡服務(wù)器上配置
02.負(fù)載均衡詳述部分
upstream 模塊 ---負(fù)載均衡
Syntax: upstream name { ... } ---指定可以分配用戶請求web節(jié)點(diǎn)信息
Default: —
Context: http ---需要寫在http模塊中
name:集群名
分配后端節(jié)點(diǎn)方式方法:
1.根據(jù)能力進(jìn)行分配用戶訪問請求
weight --權(quán)重 按比例進(jìn)行權(quán)重分配訪問資源
upstream yang {
server 10.0.0.7:80 weight=3;
server 10.0.0.8:80 weight=1;
server 10.0.0.9:80 weight=2;
}
2.根據(jù)后端節(jié)點(diǎn)健康狀態(tài)分配資源
max_fails=3; ---向有問題的節(jié)點(diǎn)發(fā)送請求的次數(shù)
fail_timeout=5s; ---指定多久以后給有問題節(jié)點(diǎn)再次發(fā)送請求
3.根據(jù)后端節(jié)點(diǎn)狀態(tài)進(jìn)行熱備節(jié)點(diǎn)用
backup ---所有節(jié)點(diǎn)都壞了才會啟用熱備節(jié)點(diǎn)
4.根據(jù)后端節(jié)點(diǎn)連接數(shù)進(jìn)行分配請求
least_conn ---連接數(shù)少的多分配請求
upstream yang {
least_conn;
server 10.0.0.7:80;
server 10.0.0.8:80;
server 10.0.0.9:80;
}
5.根據(jù)用戶源IP地址hash值分配請求 (哈希值)
ip_hash; 缺陷: 負(fù)載不均衡
配置后,一個(gè)IP訪問網(wǎng)頁時(shí),就不會經(jīng)過負(fù)載均衡 一個(gè)IP固定訪問一個(gè)web服務(wù)器
10.0.0.1 --- hash值 hash值范圍 web01
10.0.0.2 --- hash值 負(fù)載均衡 hash值范圍 web02
10.0.0.3 --- hash值 hash值范圍 web03
proxy 模塊 ---反向代理
Syntax: proxy_pass URL;
Default: —
Context: location, if in location, limit_except
第一個(gè)功能配置:經(jīng)過負(fù)載均衡訪問網(wǎng)站看到不同的頁面(解決)
proxy_set_header ---請求報(bào)文中的請求頭
proxy_set_header Host $host ---將請求頭中的Host設(shè)置變量 進(jìn)行調(diào)用
需要寫在http,server,location模塊中
配置信息:
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://yang;
proxy_set_header Host $http_host;
}
}
第二個(gè)功能配置:讓web節(jié)點(diǎn)日志中顯示真實(shí)用戶IP地址
proxy_set_header --可以在請求頭中添加信息
vim /etc/nginx/nginx.conf 添加
proxy_set_header X-Forwarded-For $remote_addr
配置信息:
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://yang;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
在日志最后一列會顯示真實(shí)用戶的IP地址,日志可以調(diào)取$http_x_forwarded_for
第三個(gè)功能配置:根據(jù)頁面顯示信息進(jìn)行健康檢查
Syntax: proxy_next_upstream error | timeout | invalid_header | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | http_429 | non_idempotent | off ...;
如果響應(yīng)報(bào)文出現(xiàn) error timeout 404 403 500等 就會找其他正常的節(jié)點(diǎn),就不會顯示在頁面上
配置信息:
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://yang;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_next_upstream error timeout invalid_header http_403 http_404;
}
}
說明: 給用戶顯示的頁面一定是正常的頁面信息
03.企業(yè)實(shí)際應(yīng)用過程
1.利用負(fù)載均衡實(shí)現(xiàn)動靜分離
準(zhǔn)備環(huán)境:
動態(tài)集群: web01 URI /dynamic ---顯示頁面 dynamic
靜態(tài)集群: web02 URI /static ---顯示頁面 static
上傳集群: web03 URI /upload ---顯示頁面 upload
web01:
echo dynamic.oldboy.com >/html/www/index.html
修改:
mkdir /html/www/dynamic
mv /html/www/index.html /html/www/dynamic
web02:
echo static.oldboy.com >/html/www/index.html
修改:
mkdir /html/www/static/
mv /html/www/index.html /html/www/static
web03:
echo upload.oldboy.com >/html/www/index.html
修改:
mkdir /html/www/upload
mv /html/www/index.html /html/www/upload
負(fù)載均衡服務(wù)器配置:
upstream dynamic {
server 10.0.0.7:80;
}
upstream static {
server 10.0.0.8:80;
}
upstream upload {
server 10.0.0.9:80;
}
server {
listen 80;
server_name localhost;
location /dynamic/ {
proxy_pass http://dynamic;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_next_upstream error timeout invalid_header http_404;
}
location /static/ {
proxy_pass http://static;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_next_upstream error timeout invalid_header http_404;
}
location /upload {
proxy_pass http://upload;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_next_upstream error timeout invalid_header http_404;
}
}
2.利用負(fù)載均衡根據(jù)客戶端顯示不同頁面信息
準(zhǔn)備環(huán)境:
iPhone iPhone web01 iPhone
chrome chrome web02 chrome
default default web03 default
web01
echo iphone > /html/bbs/index.html
web02
echo chrome > /html/bbs/index.html
web03
echo default > /html/bbs/index.html
配置負(fù)載均衡服務(wù)器:
location / {
if ($http_user_agent ~* Android) {
proxy_pass http://iphone;
}
if ($http_user_agent ~* chrome) {
proxy_pass http://chrome;
}
proxy_pass http://default;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_next_upstream error timeout invalid_header http_404;
}
04.課程知識總結(jié)
1.負(fù)載均衡服務(wù)模塊過程
upstream 模塊 如何進(jìn)行調(diào)度分配資源
proxy 模塊 proxy_set_header 無法顯示不同網(wǎng)站頁面/日志文件記錄真實(shí)IP地址
2.負(fù)載均衡企業(yè)應(yīng)用過程
1) 實(shí)現(xiàn)網(wǎng)站動靜分配配置
2) 實(shí)現(xiàn)不同客戶端訪問顯示不同頁面
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。