測試環境如下:
系統:Ceentos 6.4 64位
nginx伺服器 no1:172.16.4.7 vip:172.16.4.55
nginx伺服器 no2:172.16.4.11 vip:172.16.4.56
No1、no2雙機互信
#vim /etc/hosts
172.16.4.7 no1.test.com no1
172.16.4.11 no2.test.com no2
No1:
#ssh-keygen -t rsa -P ‘’
No2:
還要確定兩個節點時間同步
一、Nginx+keepalived 安裝
nginx-1.4.2.tar.gz
#yum -y install keepalived
1、安裝編譯所依賴的包pcre-devel openssl-devel
# yum -y install pcre-devel openssl-devel
2、解壓并添加使用者nginx,以nginx的身份運作服務
#tar xf nginx-1.4.2.tar.gz -C /usr/local/
#cd /usr/local/nginx-1.4.2
# groupadd -r nginx
# useradd -r -g nginx nginx
3、編譯和安裝
# ./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
編譯過程中如果提示我們還需要依賴某個包時,我們要安裝對應的devel包
# make && make install
4、為nginx提供SysV init腳本
腳本存放位置/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/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:'`
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
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
而後為此腳本賦予執行權限:
# chmod +x /etc/rc.d/init.d/nginx
添加至服務管理清單,并讓其開機自動啟動:
# chkconfig --add nginx
# chkconfig nginx on
而後就可以啟動服務并測試了:
# service nginx start
二、no1 配置
# mkdir -p /web/htdocs
# echo "172.16.4.7" > /web/htdocs/index.html
#vim /etc/nginx/nginx.conf
server {
listen 80;
server_name localhost;
location / {
root /web/htdocs;
index index.html index.htm;
配置完成後重新加載服務
#service nginx reload
測試使用curl指令請求或在浏覽器通路
# curl 172.16.4.7
172.16.4.7
三、no1的Keepalived配置
# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
notification_email_from keepadmin@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_script chk_nginx {
script "killall -0 nginx"
interval 2
weight -2
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 55
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
virtual_ipaddress {
172.16.4.55
track_script {
chk_nginx
vrrp_instance VI_2 {
state BACKUP
virtual_router_id 56
priority 99
172.16.4.56
四、no2的nginx配置以及keepalived配置
可以将no1的配置發送到no2上
#cd /etc/keepalived
#scp keepalived.conf no2:/etc/keepalived/
然後在其keepalived配置中修改下面選項:
state MASTER
priority100
五、測試
分别在2台nginx上重新開機啟動nginx和keepalived服務,然後停掉其中一台的服務,檢視資源能否轉移到另一台nginx上,
檢視方式:
#ip addr show
#ip a
檢視no1:
<a href="http://blog.51cto.com/attachment/201309/191211863.png" target="_blank"></a>
檢視no2:
<a href="http://blog.51cto.com/attachment/201309/191241477.png" target="_blank"></a>
将no1節點服務停掉,然後檢視資源轉移情況
<a href="http://blog.51cto.com/attachment/201309/191317306.png" target="_blank"></a>
<a href="http://blog.51cto.com/attachment/201309/191317309.png" target="_blank"></a>
再将no1的服務開啟檢視資源轉移
<a href="http://blog.51cto.com/attachment/201309/191347244.png" target="_blank"></a>
#tail /var/log/messages
#tail -f /var/log/messages #實時檢視,不會退出日志
或者在浏覽器通路我們定義的vip檢視,這裡我們就不示範了
本文轉自 宋鵬超 51CTO部落格,原文連結:http://blog.51cto.com/qidian510/1301781,如需轉載請自行聯系原作者