设为首页 收藏本站
查看: 1589|回复: 0

[经验分享] nginx+Keepalived双机主备方案实现(经线上测试)

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-6-10 10:14:22 | 显示全部楼层 |阅读模式
                                                                   wKioL1UUzs3ARaEaAAFXczTTAhU617.jpg


  • nginx的编译安装

1)编译工具(基本可以不用)
    yum -y install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre-devel gd-*
2)建立nginx 组
    groupadd -r nginx
    useradd -s /sbin/nologin -g nginx -r nginx
    id nginx
3)wget http://nginx.org/download/nginx-1.6.2.tar.gz
    tar xf /usr/local/src/nginx-1.6.2.tar.gz
    cd /usr/local/src/nginx-1.6.2.tar.gz
./configure --prefix=/usr/local/nginx --lock-path=/usr/local/nginx/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=/usr/local/nginx/client/ --http-proxy-temp-path=/usr/local/nginx/proxy/ --http-fastcgi-temp-path=/usr/local/nginx/fcgi/ --http-uwsgi-temp-path=/usr/local/nginx/uwsgi --http-scgi-temp-
path=/usr/local/nginx/scgi --with-pcre --with-file-aio --with-http_image_filter_module

   make && make install
4)配置nginx启动脚本
   ===============================================
vim /etc/init.d/nginx   chmod +x /etc/init.d/nginx  
#!/bin/bash
#
# Startup script for 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:      /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"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
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
    sleep 1
    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

==============================================================
5)nginx配置
vim /usr/local/nginx/conf/nginx.conf
====================================================
upstream MCM {
       server 10.124.156.236; #使用nginx的默认分发模式rr
       server 10.124.156.231; #两个分发服务器ip
     }

server {
        listen       80;
        server_name  localhost;
        location /mcm/ {
limit_req zone=allips burst=10 ;
proxy_pass http://MCM/mcm/;
proxy_redirect  off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For

$proxy_add_x_forwarded_for;
proxy_set_header Cookie $http_cookie;
chunked_transfer_encoding  off;
        }
         error_page   500 502 503 504  /50x.html;
         location = /50x.html {
             root   html;
         }
  =========================================================
2.Keepalived的安装配置
1)Keepalived的编译安装
  cd /usr/local/src/
  wget http://www.keepalived.org/software/keepalived-1.1.20.tar.gz
  tar xf keepalived-1.2.15.tar.gz
  cd keepalived-1.1.20
  ./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.32-279.el6.x86_64/
  make && make install
  将Keepalived做成服务:
   cd /usr/local/keepalived/ && cp etc/rc.d/init.d/keepalived /etc/rc.d/init.d/ && cp etc/sysconfig/keepalived /etc/sysconfig/ && mkdir /etc/keepalived && cp etc/keepalived/keepalived.conf /etc/keepalived/ && cp sbin/keepalived /usr/sbin/

2)配置Keepalived.conf文件
   ====================================================
   TestNginx1:10.124.194.244
   vim /etc/keepalived/keepalived.conf

   ==========================================
! Configuration File for keepalived

global_defs {
   notification_email {
       459202366@139.com  
}
   notification_email_from Keepalived@chtopnet.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id Nginx_MB
}

vrrp_script chk_http_port {
    script "/etc/keepalived/chk_nginx.sh"
    interval 2
    weight 2
}

vrrp_instance VI_3 {
    state MASTER
    interface eth0
    virtual_router_id 57      #VI_2 为 58
    priority 200              #备用是150
    mcast_src_ip 10.124.194.244 #本机ip
    advert_int 1

authentication {
        auth_type PASS
        auth_pass yzkj
    }
    track_script {
        chk_http_port
    }
    virtual_ipaddress {
        10.124.194.4         #VIP 1
    }
}
vrrp_instance VI_4 {
    state BACKUP
    interface eth0
    virtual_router_id 58     # VI_1 是57
    priority 150            #主是200
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass yzkj
    }
    track_script {
        chk_http_port
    }
    virtual_ipaddress {
        10.124.194.5       #VIP 2
    }
}
========================================
TestNginx2

! Configuration File for keepalived

global_defs {
   notification_email {
       459202366@139.com  
}
   notification_email_from Keepalived@chtopnet.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id Nginx_MB
}

vrrp_script chk_http_port {
    script "/etc/keepalived/chk_nginx.sh"
    interval 2
    weight 2
}

vrrp_instance VI_3 {
    state BACKUP
    interface eth0
    virtual_router_id 57        #VI_4 为 58
    priority  150            #主是200
    advert_int 1

authentication {
        auth_type PASS
        auth_pass yzkj
    }
    track_script {
        chk_http_port
    }
    virtual_ipaddress {
        10.124.194.4         #VIP 1
    }
}
vrrp_instance VI_4 {
    state MASTER
    interface eth0
    virtual_router_id 58     #VI_3 是57
    priority 200             #备用是50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass yzkj
    }
    track_script {
        chk_http_port
    }
    virtual_ipaddress {
        10.124.194.5       #VIP 2
    }
}
=====================================
3)添加nginx状态自动检测脚本
  vim /etc/keepalived/chk_nginx.sh
=============  
#!/bin/bash
# description:
# 定时查看nginx是否存在,如果不存在则启动nginx
# 如果启动失败,则停止keepalived
status=$(ps -C nginx --no-heading|wc -l)
if [ "${status}" = "0" ]; then
        /usr/local/nginx/sbin/nginx
        status2=$(ps -C nginx --no-heading|wc -l)
        if [ "${status2}" = "0"  ]; then
                /etc/init.d/keepalived stop
        fi
fi
====================
4)keepalived nginx启动
service keepalived start
service nginx start  或者(/usr/local/nginx/sbin/nginx)

5)防火墙设置:
  在Lvs+Keepalived的设置中一般都将iptables关掉,这是一个很危险的操作。在测试nginx+Keepalived的时候发现iptables不关闭的话,会出现VIP同时绑定在主备nginx上。所以经过研究测试,要想开着iptables来使用Keepalived,就应该先明白Keepalived的工作原理。Keepalived工作主
要是通过vrrp协议进行报文互通,于是就必须在iptables里设置
-A INPUT -p vrrp -j ACCEPT #基于DR模式,当用户发出请求后,只有DR响应ARP广播包,允许vrrp虚拟路由器冗余协议。然后重启iptables服务,至此困扰已久的VIP同时绑定在多个服务器上的问题解决。
3.测试:
在测试过程中出现一个有趣的话题:就是当我进行停止nginx运行的时候,发现进程自动会起来,然后我就问、查,总是不以所以然,总以为是kill pkill命令不好使。后来经过查证,竟然是我们写的一个检测脚本在作怪。脚本记录如下:(修改完善版)
  ===================================
#!/bin/bash
# description:
# 定时查看nginx是否存在,如果不存在则启动nginx
# 如果启动失败,则停止keepalived
status=$(ps -C nginx --no-heading|wc -l)
if [ "${status}" = "0" ]; then
        /usr/local/nginx/sbin/nginx
        status2=$(ps -C nginx --no-heading|wc -l)
        status3=$(ps -C keepalived --no-heading|wc -l)
        if [ "${status2}" = "0"  ]; then
                /etc/init.d/keepalived stop
        elif [ "${status3}" = "0" ]; then
                /etc/init.d/keepalived start
             fi
        fi
fi
========================================
它的作用主要就是检测nginx的状态,如果nginx死掉,就尝试重启,如果重启不成功就把Keepalived服务停止,使两个vip同时漂到同一台nginx服务器上面去,如果nginx恢复就应该重启Keepalived服务。
  4.使用rsync使后端几台服务器文件同步
1)服务安装:yum install rsync -y
    服务端:10.124.151.246
    =============================
vim /etc/rsyncd.conf
uid = root
gid = root
use chroot = no
max connections = 5
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[web01]                     
path=/usr/local/nginx/html/         
comment = update         
ignore errors              
read only = no            
list = no                 
hosts allow = 192.168.122.0/24
auth users = root        
uid = root
gid = root
secrets file = /etc/rsyncd.secrets
    =================================
vim /etc/rsyncd.secrets
root:123456

chmod 0600 /etc/rsyncd.secrets
    =============================
  启动服务:rsync --daemon
            echo "rsync --daemon" >> /etc/rc.local #自启动

  客户端配置(也就是代码推送端)10.124.151.245
    vim /etc/rsyncd.secrets
    123456
    chmod 0600 /etc/rsyncd.secrets
2)使用命令行进行代码推送(根据代码的修改和增删情况进行代码同步操作)
   rsync -vzrtopg --delete --progress --password-file=/etc/rsyncd.secrets  * root@10.124.151.246::web01



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-75796-1-1.html 上篇帖子: nginx安装 下篇帖子: Nginx作为WEB服务相关配置(性能优化,网络连接,访问控制,虚拟主机,https等等)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表