码迷,mamicode.com
首页 > 其他好文 > 详细

NginX+keepalived实现高可用

时间:2016-12-16 23:35:22      阅读:330      评论:0      收藏:0      [点我收藏+]

标签:nginx+keepalived 高可用

NginX+keepalived实现高可用

 

 

yum install zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel libxslt-devel

 

技术分享

 

技术分享

 

技术分享

安装缺少组件后编辑成功。

 

技术分享

技术分享执行 make && make install

   编译安装成功:

技术分享

 

默认编译安装目录 :

 

技术分享

编辑脚本:vim /etc/init.d/nginx

 

内容如下:

#!/bin/sh

#

# nginx - this script starts and stops the nginx daemin

#

# chkconfig: - 85 15

# description: Nginx is an HTTP(S) server, HTTP(S) reverse \

# proxy and IMAP/POP3 proxy server

# processname: nginx

# config: /usr/local/nginx/conf/nginx.conf

# pidfile: /usr/local/nginx/logs/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"

lockfile=/var/lock/subsys/nginx start() {

[ -x $nginx ] || exit 5

[ -f $NGINX_CONF_FILE ] || exit 6

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=$?

echo

[ $retval -eq 0 ] && rm -f $lockfile return $retval

}

restart() {

configtest || return $?

stop 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/null 2>&1

}

 

case "$1" in start)

rh_status_q && exit 0

$1

;;

stop)

rh_status_q || exit 0

$1

;;

restart|configtest)

$1

;;

reload)

rh_status_q || exit 7

$1

;;

force-reload)

force_reload

;;

status)

rh_status

;;

condrestart|try-restart)

rh_status_q || exit 0

;;

*)

echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"

exit 2

esac

保存退出添加执行权限

chmod +x /etc/init.d/nginx

启动脚本观察监听端口是否有80

 

技术分享

 

编辑一个测试页:

vim /usr/local/nginx/html/index.html

 

 

 

 

 

技术分享保存退出测试能否访问

 

 

 

 

 

 

 

 

 

 

 

 

 

技术分享同样26.210也测试下

 

技术分享

26.210 keepalived 配置文件:vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {

notification_email {

jwenshan@hysec.com

jwenshan@gmail.com

}

notification_email_from jwenshan@163.com

smtp_server smtp.hysec.com smtp_connect_timeout 30 router_id nginx

}

 

vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111

}

virtual_ipaddress {

192.168.27.210

}

}

virtual_server 192.168.27.210 80 {

delay_loop 2 lb_algo rr lb_kind DR

persistence_timeout 60

protocol TCP

real_server 192.168.26.210 80 {

weight 1

notify_down /etc/keepalived/chk_nginx.sh

TCP_CHECK { connect_port 80 connect_timeout 3 nb_get_retry 2 delay_before_retry 1

}

}

}

chk_nginx.sh脚本:vim /etc/keepalived/chk_nginx.sh

#!/bin/bash

A=`ps -C nginx --no-header |wc -l`

if [ $A -eq 0 ];then

echo ‘nginx server is died‘

/etc/init.d/keepalived stop

fi

别忘记加执行权限:chmod +x /etc/keepalived/chk_nginx.sh

26.211 keepalived 配置文件:vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {

notification_email {

jwenshan@hysec.com

jwenshan@gmail.com

}

notification_email_from jwenshan@163.com

smtp_server smtp.hysec.com smtp_connect_timeout 30 router_id nginx

}

 

vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 1111

}

virtual_ipaddress {

192.168.27.210

}

}

virtual_server 192.168.27.210 80 {

delay_loop 2 lb_algo rr lb_kind DR

persistence_timeout 60

protocol TCP

real_server 192.168.26.211 80 {

weight 1

notify_down /etc/keepalived/chk_nginx.sh

TCP_CHECK { connect_port 80 connect_timeout 3 nb_get_retry 2 delay_before_retry 1

}

}

}

chk_nginx.sh脚本:vim /etc/keepalived/chk_nginx.sh

#!/bin/bash

A=`ps -C nginx --no-header |wc -l`

if [ $A -eq 0 ];then

echo ‘nginx server is died‘

/etc/init.d/keepalived stop

fi

 

 

 

技术分享

 

 

技术分享

 

技术分享

 

技术分享

 

技术分享


本文出自 “在路上” 博客,请务必保留此出处http://jdonghong.blog.51cto.com/3473478/1883414

NginX+keepalived实现高可用

标签:nginx+keepalived 高可用

原文地址:http://jdonghong.blog.51cto.com/3473478/1883414

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!