Keepalived+Nginx實現高可用和雙主節點負載均衡

簡介前端

Nginx能夠實現高併發反向代理,lvs集羣能夠實現負載均衡,可是他們都有一個共同的弊端,就是Nginx,lvs架構中Director是單點故障,有沒有一個好的方案解決這個問題呢?答案是有。經過Keepalived就能夠實現,前端Nginx,lvs中Director服務器的高可用和負載均衡,經過shell命令或者腳本能夠實現對服務器狀態和服務的監控!node

1、環境介紹
一、系統環境及軟件版本
操做系統:CentOS6.4-i386
軟件版本:Nginx-1.4.2
keepalived-1.2.7
yum源:
# vim /etc/yum.repos.d/centos6.repo
[base]
name=centos-base
baseurl=http://mirrors.sohu.com/centos/$releasever/os/$basearch
gpgcheck=1
enable=1
gpgkey=http://mirrors.sohu.com/centos/RPM-GPG-KEY-CentOS-6
[epel]
name=Fedora-epel
baseurl=http://mirrors.sohu.com/fedora-epel/$releasever/$basearch/
enable=1
gpgcheck=0linux

提示:若是你的系統是centos,系統默認的yum源是能夠用的
二、拓撲圖nginx

Keepalived+Nginx實現高可用和雙主節點負載均衡

三、IP地址規劃
Client: 172.16.254.28
Keepalived+Nginx1: 172.16.3.3 Vip: 172.16.3.100
Keepalived+Nginx2: 172.16.3.4 Vip: 172.16.3.200shell

2、安裝
一、安裝keepalived
[root@node1 ~]# yum install keepalivedvim

二、編譯安裝Nginxcentos

[root@node1 ~]#useradd -r nginx
[root@node1 ~]#yum -y groupinstall 「Development tools」 「Server  Platform Development」
[root@node1 ~]#yum -y install pcre-devel
[root@node1 ~]#tar xf nginx-1.4.2.tar.gz
[root@node1 ~]#cd nginx-1.4.2
[root@node1 nginx-1.4.2]# ./configure \
–prefix=/usr\
–sbin-path=/usr/sbin/nginx\
–conf-path=/etc/nginx/nginx.conf \
–error-log-path=/var/log/nginx/error.log \
–http-log-path=/var/log/nginx/access.log \
–pid-path=/var/run/nginx/nginx.pid  \
–lock-path=/var/lock/nginx.lock \
–user=nginx \
–group=nginx \
–with-http_ssl_module \
–with-http_flv_module \
–with-http_stub_status_module \
–with-http_gzip_static_module \
–http-client-body-temp-path=/var/tmp/nginx/client/\
–http-proxy-temp-path=/var/tmp/nginx/proxy/\
–http-fastcgi-temp-path=/var/tmp/nginx/fcgi/\
–http-uwsgi-temp-path=/var/tmp/nginx/uwsgi\
–http-scgi-temp-path=/var/tmp/nginx/scgi\
–with-pcre緩存

提示:在兩臺服務器上都要安裝Nginx
三、提供Nginx服務啓動腳本服務器

[root@node1 nginx-1.4.2]# 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"] && exit0
nginx=」/usr/sbin/nginx」
prog=$(basename$nginx)
NGINX_CONF_FILE=」/etc/nginx/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:’`
foropt 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 ] || exit5
[ -f $NGINX_CONF_FILE ] || exit6
make_dirs
echo-n $」Starting $prog: 「
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq0 ] && touch$lockfile
return$retval
}
stop() {
echo-n $」Stopping $prog: 「
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq0 ] && rm-f $lockfile
return$retval
}
restart() {
configtest || return$?
stop
sleep1
start
}
reload() {
configtest || return$?
echo-n $」Reloading $prog: 「
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null2>&1
}
case」$1″in
start)
rh_status_q && exit0
$1
;;
stop)
rh_status_q || exit0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit0
;;
*)
echo$」Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}」
exit2
esac
[root@node1 nginx-1.4.2]# chmod +x /etc/rc.d/init.d/nginx
[root@node1 nginx-1.4.2]# chkconfig –add nginx
[root@node1 nginx-1.4.2]# chkconfig nginx on
[root@node1 nginx-1.4.2]# service nginx start
Starting nginx:                                            [  OK  ]架構

提示:服務腳本在兩臺Nginx服務器上都要提供

接下來請看第2頁精彩內容http://www.linuxidc.com/Linux/2013-10/90809p2.htm

Nginx 的詳細介紹請點這裏
Nginx 的下載地址請點這裏

相關閱讀:

Nginx反向代理+負載均衡+健康探測+緩存 http://www.linuxidc.com/Linux/2013-09/89774.htm

Nginx Tomcat 集羣負載均衡解決筆記 http://www.linuxidc.com/Linux/2013-07/86827.htm

Nginx 配置輪詢分流-實現負載均衡【測試經過】 http://www.linuxidc.com/Linux/2013-06/86692.htm

Nginx負載均衡引發的網站不可用 http://www.linuxidc.com/Linux/2013-05/84063.htm

在Linux上使用Nginx爲Solr集羣作負載均衡 http://www.linuxidc.com/Linux/2012-12/75257.htm

相關文章
相關標籤/搜索