nui9 发表于 2013-7-3 08:39:46

Nginx+keepalived 脚本安装主从双机热备自动切换解决方案

Nginx+keepalived主从双机热备自动切换解决方案
测试环境如下:系统:Ceentos 6.4 64位主nginx服务器:192.168.122.5备nginx服务器:192.168.122.6VIP:192.168.122.15
一、Nginx+keepalived 安装—脚本安装
#!/bin/bash# author: kuangl# mail: kuangl@orient-media.com# description: The installation of Nginx files.# -------------------------------------------------------- ### Nginx_install# -------------------------------------------------------- ## Nginx installation#CURRENT_PATH=$(pwd)for i in $(rpm -q gcc gcc-c++ kernel-devel openssl-devel zlib-devel popt-devel popt-static libnl-devel wget make |grep 'not installed' | awk '{print $2}')doyum -y install $idone[ -d /root/software ][ "$?" != 0 ] && mkdir /root/softwarecd /root/software[ !-e pcre-8.33.tar.gz ] && wget ftp://ftp.csx.cam.ac.uk/pub/soft ... re/pcre-8.33.tar.gztar -zxvf pcre-8.33.tar.gzcd pcre-8.33./configuremake && make installecho $? || [ $? != 0] || echo" installation pcrefailed" || exit 1cd /root/software[ ! -e nginx-1.2.9.tar.gz ] && wget http://nginx.org/download/nginx-1.2.9.tar.gztar -zxvf nginx-1.2.9.tar.gzcd nginx-1.2.9./configure--prefix=/usr/local/nginx --with-http_ssl_module --with-http_sub_module --with-http_stub_status_module--with-http_gzip_static_modulemake && make installecho $? || [ $? != 0] || echo" installationnginxfailed" || exit 1# -------------------------------------------------------- ### Keepalived_intsall# -------------------------------------------------------- ## Keepalived installationcd /root/softwarae[ ! -e keepalived-1.2.4.tar.gz ] &&wget http://www.keepalived.org/software/keepalived-1.2.4.tar.gztar -zxvf keepalived-1.2.4.tar.gzcd keepalived-1.2.4ln -s /usr/src/kernels/$(uname -r) /usr/src/kernels/linux./configure --prefix=/usr--bindir=/usr/bin--sbindir=/usr/bin--libexecdir=/usr/libexec --localstatedir=/var --libdir=/lib64--infodir=/usr/share/info--sysconfdir=/etc --mandir=/usr/local/share/man--with-kernel-dir=/usr/src/kernels/linuxmake && make installecho $? || [ $? != 0] || print " installation keepalivedfailed" || exit 1chkconfig --add keepalivedchkconfig --level 345 keepalived on
二、主Nginx 配置
# mkdir -p /var/www/html# cat "192.168.122.5" > /var/www/html/index.html# vim nginx.conf# ../sbin/nginx -s reload# curl http://192.168.122.5192.168.122.5

三、主Keepalived配置
# vim /etc/keepalived/keepalived.conf! Configuration File for keepalivedglobal_defs {notification_email {404060@qq.com138162@139.com}notification_email_from 404060@qq.comsmtp_server 127.0.0.1smtp_connect_timeout 30router_id LVS_DEVEL}vrrp_script chk_nginx {script "/etc/keepalived/chk_nginx.keepalived.sh"interval 2weight 2}vrrp_instance VI_1 {state MASTERinterface eth0virtual_router_id 51priority 200advert_int 1authentication {auth_type PASSauth_pass kuangling}track_script {chk_nginx.keepalived}virtual_ipaddress {192.168.122.15}}

四、备nginx配置
# mkdir -p /var/www/html# cat "192.168.122.6" > /var/www/html/index.html# vim nginx.conf# ../sbin/nginx -s reload# curl http://192.168.122.6192.168.122.6
五、备keepalived配置
# vim /etc/keepalived/keepalived.conf! Configuration File for keepalivedglobal_defs {notification_email {404060@qq.com138162@139.com}notification_email_from 404060@qq.comsmtp_server 127.0.0.1smtp_connect_timeout 30router_id LVS_DEVEL}vrrp_script chk_nginx {script "/etc/keepalived/chk_nginx.keepalived.sh"interval 2weight 2}vrrp_instance VI_1 {state BACKUPinterface eth0virtual_router_id 51priority 150advert_int 1authentication {auth_type PASSauth_pass kuangling}track_script {chk_nginx.keepalived}virtual_ipaddress {192.168.122.15}}
六、分别在2台nginx服务器上添加检测脚本:
# vim /etc/keepalived/chk_nginx.keepalived.sh#!/bin/bash# description: # 定时查看Nginx是否存在,如果不存在则启动Nginx# 如果启动失败,则停止keepalivedstatus=`ps -C nginx --no-header |wc -l`if [ $status -eq 0 ];then/usr/local/nginx/sbin/nginxsleep 3if [ `ps -C nginx --no-header |wc -l` -eq 0 ];thenkillall keepalivedfifi#chmod +x /etc/keepalived/chk_nginx.keepalived.sh
七、测试分别在2台nginx上启动nginx和keepalived服务,然后分别用ip a 查看ip

帝王 发表于 2013-7-3 14:13:54

我在马路边丢了一分钱

水莹儿 发表于 2013-7-4 01:07:11

勃起不是万能的,但不能勃起却是万万都不能的!

remington_young 发表于 2013-7-4 04:48:15

走,MM,咱们化蝶去……

llcong 发表于 2013-7-4 15:10:30

沙发!沙发!

liujjun 发表于 2013-7-5 05:51:58

自从我变成了狗屎,就再也没有人踩在我头上了。

nihaogirl 发表于 2013-7-5 12:43:40

关羽五绺长髯,风度翩翩,手提青龙偃月刀,江湖人送绰号——刀郎。
页: [1]
查看完整版本: Nginx+keepalived 脚本安装主从双机热备自动切换解决方案