Nginx+keepalived主从双机热备自动切换解决方案
测试环境如下:系统:Ceentos 6.4 64位
主nginx服务器:192.168.122.5
备nginx服务器:192.168.122.6
VIP:192.168.122.15
一、Nginx+keepalived 安装—脚本安装
12345678910111213141516171819202122232425262728293031323334353637383940414243#!/bin/bash# author: kuangl# mail: kuangl@orient-media.com# description: The installation of Nginx files.# -------------------------------------------------------- ### Nginx_install# -------------------------------------------------------- ## Nginx installation#CURRENT_PATH=$(pwd)fori 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/software/programming/pcre/pcre-8.33.tar.gztar-zxvf pcre-8.33.tar.gzcdpcre-8.33./configuremake&& makeinstallecho$? || [ $? != 0] || echo" installation pcrefailed"|| exit1cd/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.gzcdnginx-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&& makeinstallecho$? || [ $? != 0] || echo" installationnginxfailed"|| exit1# -------------------------------------------------------- ### 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.gzcdkeepalived-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&& makeinstallecho$? || [ $? != 0] || print " installation keepalivedfailed"|| exit1chkconfig --add keepalivedchkconfig --level 345 keepalived on二、主Nginx 配置
123# mkdir -p /var/www/html# cat "192.168.122.5" > /var/www/html/index.html# vim nginx.confhttp://img1.运维网.com/attachment/201307/192322489.jpg
123# ../sbin/nginx -s># curl http://192.168.122.5192.168.122.5三、主Keepalived配置
12345678910111213141516171819202122232425262728293031323334# vim /etc/keepalived/keepalived.conf! Configuration File forkeepalivedglobal_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配置
123# mkdir -p /var/www/html# cat "192.168.122.6" > /var/www/html/index.html# vim nginx.confhttp://img1.运维网.com/attachment/201307/192343441.jpg
123# ../sbin/nginx -s># curl http://192.168.122.6192.168.122.6五、备keepalived配置
12345678910111213141516171819202122232425262728293031323334# vim /etc/keepalived/keepalived.conf! Configuration File forkeepalivedglobal_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服务器上添加检测脚本:
1234567891011121314# vim /etc/keepalived/chk_nginx.keepalived.sh#!/bin/bash# description:# 定时查看Nginx是否存在,如果不存在则启动Nginx# 如果启动失败,则停止keepalivedstatus=`ps-C nginx --no-header |wc-l`if[ $status -eq0 ];then/usr/local/nginx/sbin/nginxsleep3if[ `ps-C nginx --no-header |wc-l` -eq0 ];thenkillall keepalivedfifi#chmod +x /etc/keepalived/chk_nginx.keepalived.sh七、测试
分别在2台nginx上启动nginx和keepalived服务,然后分别用ip a 查看ip
本文出自 “&思远晨曦” 博客,请务必保留此出处http://kling.blog.运维网.com/3320545/1240359
页:
[1]