192.168.1.150 VOIP 虛擬IP
192.168.1.151 CNSHANK01 負載均衡1
192.168.1.152 CNSHANK02 負載均衡2
系統:Centos7.4php
1、nginx配置(兩臺nginx配置如出一轍,主頁設置不一樣):css
include mime.types;
default_type application/octet-stream;
limit_conn_zone $binary_remote_addr zone=addr:10m; #防IP***html
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types; #協助部分瀏覽器(如firefox)識別網頁文件的類型
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;node
client_body_buffer_size 1k; #指定客戶端請求主體緩衝區大小
client_header_buffer_size 1k; #指定來自客戶端請求頭的headerbuffer大小
large_client_header_buffers 2 1k; #客戶端請求中較大的消息頭指定的緩存最大數量和大小
server_tokens off; #禁止在錯誤頁面上顯示nginx版本號
client_body_timeout 20; #設置客戶端請求主體讀取超時時間
client_header_timeout 20; #設置客戶端請求頭讀取超時時間
gzip on;nginx
upstream dynamic { #負載服務器名dynamic
ip hash; #採用ip_hash方式
server 192.168.1.154:80;
server 192.168.1.155:80;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name blog.dollarphp.com;
root /usr/share/nginx/html;
include /etc/nginx/default.d/*.conf;
location / {vim
limit_conn addr 100; #指定每一個ip最多隻容許創建100個鏈接
limit_rate 500k; #每一個ip最大帶寬是500k
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_404;瀏覽器
index index.html index.htm; autoindex on; autoindex_localtime on; } location ~* ^.+.(js|css|htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ { root html; } location ~* ^.+.(?![js|css|htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma])$ { proxy_pass http://dynamic; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { }
}
}緩存
設置配置主頁測試文件
cd /usr/share/nginx/html
cp ./index.html ./index.html.bak
echo 「CNSHANK01-192.168.1.151」>./index.html #在主機192.1681.151上
echo 「CNSHANK02-192.168.1.152」>./index.html #在主機192.1681.152上bash
2、keepalived配置:
I、主服務器配置(192.168.1.151):服務器
配置:
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
}
vrrp_script chk_nginx {
script "/etc/keepalived/check.sh"
interval 1
weight -15
}
vrrp_instance VI_1 { # 實例,一個vip一個實例
state MASTER #標識爲主服務
interface ens33
virtual_router_id 51 #與從機保持一致便可
priority 100 #權重 ,高於從機
advert_int 1
authentication { # 認證受權,設置驗證信息,兩個節點必須一致
auth_type PASS
auth_pass 123456
}
virtual_ipaddress { #虛擬ip地址
192.168.1.150
}
track_script { #nginx監控服務
chk_nginx
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
添加服務檢查腳本(check.sh):
vim /etc/keepalived/check.sh
#!/bin/bash
if [ "pgrep nginx | wc -l
" == "0" ] ; then
exit 1
else
exit 0
fi
date '+%F %T'
:切換到主機模式" >> /var/log/keepalived/keepalived.logdate '+%F %T'
:切換到備機模式" >> /var/log/keepalived/keepalived.logdate '+%F %T'
:宕機" >> /var/log/keepalived/keepalived.log給腳本添加可執行權限:
chmod +x /etc/keepalived/check.sh
chmod +x /etc/keepalived/notify.sh
建立日誌目錄:
mkdir /var/log/keepalived
II、備服務器配置(192.168.1.152):
安裝:
yum -y install keepalived
配置:
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
}
vrrp_script chk_nginx {
script "/etc/keepalived/check.sh"
interval 1
weight -15
}
vrrp_instance VI_1 {
state BACKUP #BACKUP模式
interface ens33
virtual_router_id 51 #與主機一致
priority 99 #比主機小
advert_int 1
authentication { #與主機一致
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.1.150
}
track_script {
chk_nginx
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
pgrep nginx | wc -l
" == "0" ] ; thendate '+%F %T'
:切換到主機模式" >> /var/log/keepalived/keepalived.logdate '+%F %T'
:切換到備機模式" >> /var/log/keepalived/keepalived.logdate '+%F %T'
:宕機" >> /var/log/keepalived/keepalived.logII、中止主服務器:192.168.1.151後測試
查看日誌:
tail /var/log/keepalived/keepalived.log
主從機日誌都顯示切到備機模式
測試完成