一、拓撲圖
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiInBnaucDO0cHeIVXV5lHVHFUQRhDOmJVLNpEZRFTbvl2S39CXGR0LclDNvwVMw00LcJDMzZWe39CXt92Yu8GdjFTNuMzcvw1LcpDc0RHaiojIsJye.jpg)
Nginx有很強代理功能,但是一台nginx就形成了單點,現在使用keepalived來解決這個問題,keepalived的故障轉移時間很短.
Nginx+keepalived雙機實作nginx反向代理服務的高可用,一台nginx挂掉之後不影響應用也不影響内網通路外網.
Master和Backup兩邊都開啟nginx服務,無論Master還是Backup,當其中的一個keepalived服務停止後,vip都會漂移到keepalived服務還在的節點上。
二、安裝Nginx和keepalived服務
1、分别在nginx-master安裝nginx
[root@90sec src]# yum -y install gcc gcc-c++ openssl-devel pcre-devel automake autoconf
[root@90sec src]# groupadd -r nginx
[root@90sec src]# useradd -r -g nginx nginx
[root@90sec src]# tar xf nginx-1.7.5.tar.gz
[root@90sec src]# cd nginx-1.7.5
[root@90sec nginx-1.7.5]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --with-http_gzip_static_module --with-http_flv_module --with-http_mp4_module | tee /tmp/nginx.txt ###可以把nginx安裝的路徑儲存到nginx.txt中
[root@90sec nginx-1.7.5]# make && make install
[root@90sec nginx-1.7.5]# vim /etc/rc.d/init.d/nginx
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
[ $retval -eq 0 ] && rm -f $lockfile
restart() {
configtest || return $?
stop
sleep 1
start
reload() {
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
force_reload() {
restart
configtest() {
$nginx -t -c $NGINX_CONF_FILE
rh_status() {
status $prog
rh_status_q() {
rh_status >/dev/null 2>&1
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
restart|configtest)
reload)
rh_status_q || exit 7
force-reload)
force_reload
status)
rh_status
condrestart|try-restart)
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
[root@90sec nginx-1.7.5]# chmod +x /etc/rc.d/init.d/nginx
[root@90sec nginx-1.7.5]# chkconfig --add nginx
[root@90sec nginx-1.7.5]# chkconfig nginx on
[root@90sec nginx-1.7.5]# service nginx restart
[root@90sec nginx-1.7.5]# ss -anptl | grep "nginx"
LISTEN 0 128 *:80 *:* users:(("nginx",38972,6),("nginx",38973,6))
[root@90sec nginx-1.7.5]# service nginx status
nginx (pid 38973 38972) is running...
2、分别建立網站首頁
[root@90sec nginx-1.7.5]# echo "Welcom to nginx-master" > /usr/local/nginx/html/index.html
[root@90sec nginx-1.7.5]# curl http://192.168.83.132
Welcom to nginx-master
[root@80sec nginx-1.7.5]# echo "Welcome to nginx-backup" > /usr/local/nginx/html/index.html
[root@80sec nginx-1.7.5]# curl http://192.168.83.133
Welcome to nginx-backup
3、安裝keepalived
[root@90sec src]# tar xf keepalived-1.2.12.tar.gz
[root@90sec src]# cd keepalived-1.2.12
[root@90sec keepalived-1.2.12]# ./configure --prefix=/usr/local/keepalived --with-dir=/usr/src/kernels/*/
[root@90sec keepalived-1.2.12]# make && make install
[root@90sec keepalived-1.2.12]# mkdir /etc/keepalived
[root@90sec keepalived-1.2.12]# cp -f keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
[root@90sec keepalived-1.2.12]# cp -f keepalived/etc/init.d/keepalived.init /etc/init.d/keepalived[root@90sec keepalived-1.2.12]# cp -f keepalived/etc/init.d/keepalived.sysconfig /etc/sysconfig/keepalived
[root@90sec keepalived-1.2.12]# cp -f /usr/local/keepalived/sbin/keepalived /sbin/
修改keepalived的配置檔案
## nginx-master###
[root@90sec keepalived-1.2.12]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived #全局定義
global_defs {
notification_email {
[email protected] #發生故障時給誰發送郵件
}
notification_email_from root@localhost #故障用那個郵箱發送郵件
smtp_server 127.0.0.1 #指定發送email的smtp伺服器
smtp_connect_timeout 30 #逾時時間
router_id LVS_DEVEL #運作keepalived的機器的一個辨別
}
vrrp_script chk_nginx { #檢測nginx服務是否在運作有很多方式,比如程序,用腳本檢測等等
script "killall -0 nginx" #用shell指令檢查nginx服務是否存在
interval 1 #時間間隔為1秒檢測一次
weight -2 #當nginx的服務不存在了,就把目前的權重-2
fall 2 #測試失敗的次數
rise 1 #測試成功的次數
}
vrrp_instance VI_1 {
state MASTER #為主伺服器
interface eth0 #監聽的本地網卡接口
virtual_router_id 51 #主輔virtual_router_id号必須相同
mcast_src_ip=192.168.83.132 #主nginx的IP位址
priority 100 #優先級
advert_int 1 ###同步間隔時長
authentication { #認證
auth_type PASS #認證方式
auth_pass 1111 #密鑰
}
virtual_ipaddress {
192.168.83.131/24 dev eth1 #VIP
##後面的就都用不到了删除就可以
####nginx-backup####
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from root@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
mcast_src_ip=192.168.83.133 #修改為133
priority 90 #修改為90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.83.131/24 dev eth1
}
啟動keepalived服務(主輔兩台)
[root@90sec keepalived-1.2.12]# /etc/init.d/keepalived start
Starting keepalived: [ OK ]
[root@90sec keepalived-1.2.12]# chkconfig --add keepalived
[root@90sec keepalived-1.2.12]# chkconfig keepalived on
檢視vip是否主動切換
nginx-maser上keepalived關閉之前
nginx-master上keepalived關閉之後
四、修改nginx配置檔案做反向代理
1、分别在web1,web2安裝httpd服務
[root@70sec ~]# yum -y install httpd
[root@70sec ~]# echo "<h1>Welcome to WEB1</h1>" > /var/www/html/index.html
[root@70sec ~]# service httpd restart
Stopping httpd: [FAILED]
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 70sec.com for ServerName
[ OK ]
[root@70sec ~]# curl http://192.168.83.134
<h1>Welcome to WEB1</h1>
[root@10sec ~]# yum -y install httpd
[root@10sec ~]# echo "<h1>Welcome to WEB2</h1>" > /var/www/html/index.html
[root@10sec ~]# service httpd start
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 10sec.com for ServerName
[root@10sec ~]# curl httpd://192.168.83.135
<h1>Welcome to WEB2</h1>
2、修改nginx的配置檔案做反向代理(主輔配置檔案相同)
user nginx nginx; ##運作nginx的使用者群組
worker_processes 2; ##啟動程序數
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
use epoll; ##工作模型
worker_connections 1024; ##單程序最大連接配接數
http { ##http子產品
include mime.types; ##包含進來
default_type application/octet-stream; ##預設類型
#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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65; ##長連接配接時長
#gzip on;
upstream web { ##用upstream定義叢集與RS
server 192.168.83.134:80 weight=1 max_fails=3 fail_timeout=10s; ##RS位址,錯誤請求與逾時時長
server 192.168.83.135:80 weight=1 max_fails=3 fail_timeout=10s;
server {
listen 80; ##監聽端口
server_name 192.168.83.131; ##ServerName
root html;
index index.html index.htm;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://web; ##反向代理
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For Proxy_add_x_forwarded_for;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
3、拷貝到nginx-backup上
[root@90sec sbin]# scp /usr/local/nginx/conf/nginx.conf 192.168.83.133:/usr/local/nginx/conf/
測試代理
測試負載
現在關掉web1
[root@70sec ~]# service httpd stop
Stopping httpd: [ OK ]
再開啟web1,關閉web2
[root@70sec ~]# service httpd start
[root@10sec ~]# service httpd stop
upstream 支援的負載均衡算法
Nginx的負載均衡子產品目前支援4種排程算法,下面進行分别介紹,其中後兩項屬于第三方排程算法。
輪詢(預設)。每個請求按時間順序逐一配置設定到不同的後端伺服器,如果後端某台伺服器當機,故障系統被自動剔除,使使用者通路不受影響。Weight 指定輪詢權值,Weight值越大,配置設定到的通路機率越高,主要用于後端每個伺服器性能不均的情況下。
ip_hash。每個請求按通路IP的hash結果配置設定,這樣來自同一個IP的訪客固定通路一個後端伺服器,有效解決了動态網頁存在的session共享問題。
fair。這是比上面兩個更加智能的負載均衡算法。此種算法可以依據頁面大小和加載時間長短智能地進行負載均衡,也就是根據後端伺服器的響應時間來配置設定請求,響應時間短的優先配置設定。Nginx本身是不支援fair的,如果需要使用這種排程算法,必須下載下傳Nginx的upstream_fair子產品。