簡 介:html
集羣就是一組獨立的計算機,經過網絡鏈接組合成一個組合來共同完一個任務。nginx
集羣優勢:web
集羣種類:算法
Nginx複雜概念說明:後端
Nginx反向代理說明:緩存
Nginx反向代理所使用的模塊:bash
upstream模塊配置服務器
參數:網絡
調度算法session
####################### proxy upstream server_pools { server 192.168.10.249:80 weight=3 max_fails=3 fail_timeout=10; server 192.168.10.253:80 weight=5 max_fails=3 fail_timeout=10; server 192.168.10.252:80 weight=5 max_fails=3 fail_timeout=10 backup; }
ip_hash算法(不能有backup標識和weighe標識)
upstream server_pools { ip_hash; server 192.168.10.249:80 max_fails=3 fail_timeout=10; server 192.168.10.253:80 max_fails=3 fail_tim eout=10; server 192.168.10.252:80 max_fails=3 fail_timeout=10; }
動態調度算法
upstream server_pools { server 192.168.10.249:80 max_fails=3 fail_timeout=10; server 192.168.10.253:80 max_fails=3 fail_timeout=10; server 192.168.10.252:80 max_fails=3 fail_timeout=10; fair; }
參數詳解:
server { listen 80; server_name web.yan.com; location / { proxy_pass http://server_pools; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; proxy_connect_timeout 60; proxy_send_timeout 60; proxy_read_timeout 60; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_504;
}
}
服務器規劃:
/upload 10.0.0.8:80 html/www/upload upload服務器
/static 10.0.0.7:80 html/www/static static靜態服務器
/ 10.0.0.9:80 html/www 默認
建立upstream負載信息:
upstream upload_pools { server 192.168.10.56:80; } upstream static_pools { server 192.168.10.57:80; } upstream default_pools { server 192.168.10.58:80; }
調用upstream信息
server { listen 80; server_name www.yan.com; location /static/ { proxy_pass http://static_pools; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; } location /upload/ { proxy_pass http://upload_pools; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; } location / { proxy_pass http://default_pools; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; } access_log logs/access_www.log main; } }
手機用戶電腦用戶訪問不一樣網站實例配置:
upstream upload_pools { server 10.0.0.8:80; } upstream static_pools { server 10.0.0.7:80; } upstream default_pools { server 10.0.0.9:80; } server { listen 80; server_name www.yan.com; location / { if ($http_user_agent ~* "Iphome") { proxy_pass http://static_pools; } if ($http_user_agent ~* "Android") { proxy_pass http://upload_pools; } proxy_pass http://default_pools; } access_log logs/access_www.log main; } }
Nginx配置
配置listen 192.168.10.244:80; (域名解析到VIP)內核須要開啓容許綁定非本地的IP
echo 'net.ipv4.ip_nonlocal_bind = 1' >>/etc/sysctl.conf ##/etc/sysctl.conf 加上 sysctl -p
配置文件
upstream server_pools { server 192.168.10.56; server 192.168.10.57; server 192.168.10.58; } server { listen 192.168.10.244:80; server_name www.yan.com; location / { proxy_pass http://server_pools; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; } access_log logs/access_www.log main; } server { listen 192.168.10.245:80; server_name blog.yan.com; location / { proxy_pass http://server_pools; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; } access_log logs/access_blog.log main; }
keepalived配置
keepalived腦裂說明:
因爲某種緣由,致使兩臺高可用服務器對在指定的時間內,沒法檢測到對方心跳信息,各自取得資源服務權,而此時的兩臺高可用服務器都在正常運行,會致使同一個IP或者服務器在兩端同時存在而發生衝突,嚴重佔用同一個VIP使用戶寫入數據丟失。
心跳線老化,
高可用集羣同一個交換機故障
防火牆規則
網卡等信息配置不正確等
解決辦法,監控備用服務器需IP信息,若是有則,主服務器出現問題。
#!/bin/bash if [ `ip a s ens33|grep 192.168.10.244|wc -l` -ne 0 ] then echo "keepalived is error!!!" else echo "keepalived is ok!!!" fi
NGinx宕機實現,keepalived主備切換(配置文件標紅處就是執行腳本切換操做)
#!/bin/bash #name: check_web.sh #desc: check nginx and kill keepalived if [ `ps -ef |grep nginx |grep -v grep |wc -l` -lt 2 ];then /etc/init.d/keepalived stop fi
chmod +x /server/scripts/check_web.sh
配置文件
#lb01 global_defs { router_id LVS_01 #惟一標示 }
vrrp_script check_web {
script "/server/scripts/check_web.sh" #執行的腳本路徑
interval 2 #兩秒檢查一次
weight 2 #觸發腳本優先級減去定義的weight下降優先級改成備服務
}
vrrp_instance VI_1 { # state MASTER interface ens33 virtual_router_id 51 priority 150 advert_int 1 authentication { auth_type PASS auth_pass admin } virtual_ipaddress { 192.168.10.244/24 dev ens33 label ens33:1 }
track_script { #執行腳本
check_web
} } vrrp_instance VI_2 { state BACKUP interface ens33 virtual_router_id 52 priority 100 advert_int 1 authentication { auth_type PASS auth_pass root } virtual_ipaddress { 192.168.10.245/24 dev ens33 label ens33:2 }
} #lb02 global_defs { router_id LVS_02 } vrrp_instance VI_1 { state BACKUP interface ens33 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass admin } virtual_ipaddress { 192.168.10.244/24 dev ens33 label ens33:1 } } vrrp_instance VI_2 { state MASTER interface ens33 virtual_router_id 52 priority 150 advert_int 1 authentication { auth_type PASS auth_pass root } virtual_ipaddress { 192.168.10.245/24 dev ens33 label ens33:2 } }