• Keepalived+Nginx实现高可用和双主节点负载均衡


    简介

    Nginx可以实现高并发反向代理,lvs集群可以实现负载均衡,但是他们都有一个共同的弊端,就是Nginx,lvs架构中Director是单点故障,有没有一个好的方案解决这个问题呢?答案是有。通过Keepalived就可以实现,前端Nginx,lvs中Director服务器的高可用和负载均衡,通过shell命令或者脚本可以实现对服务器状态和服务的监控!

    一、环境介绍
    1、系统环境及软件版本
    操作系统:CentOS6.4-i386
    软件版本:Nginx-1.4.2
    keepalived-1.2.7
    yum源:
    # vim /etc/yum.repos.d/centos6.repo
    [base]
    name=centos-base
    baseurl=http://mirrors.sohu.com/centos/$releasever/os/$basearch
    gpgcheck=1
    enable=1
    gpgkey=http://mirrors.sohu.com/centos/RPM-GPG-KEY-CentOS-6
    [epel]
    name=Fedora-epel
    baseurl=http://mirrors.sohu.com/fedora-epel/$releasever/$basearch/
    enable=1
    gpgcheck=0

    提示:如果你的系统是centos,系统默认的yum源是可以用的
    2、拓扑图

    Keepalived+Nginx实现高可用和双主节点负载均衡

    3、IP地址规划
    Client: 172.16.254.28
    Keepalived+Nginx1: 172.16.3.3 Vip: 172.16.3.100
    Keepalived+Nginx2: 172.16.3.4 Vip: 172.16.3.200

    二、安装
    1、安装keepalived
    [root@node1 ~]# yum install keepalived

    2、编译安装Nginx

    [root@node1 ~]#useradd -r nginx
    [root@node1 ~]#yum -y groupinstall “Development tools” “Server  Platform Development”
    [root@node1 ~]#yum -y install pcre-devel
    [root@node1 ~]#tar xf nginx-1.4.2.tar.gz
    [root@node1 ~]#cd nginx-1.4.2
    [root@node1 nginx-1.4.2]# ./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

    提示:在两台服务器上都要安装Nginx
    3、提供Nginx服务启动脚本

    [root@node1 nginx-1.4.2]# vim /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"] && exit0
    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:’`
    foropt 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
    fi
    done
    }
    start() {
    [ -x $nginx ] || exit5
    [ -f $NGINX_CONF_FILE ] || exit6
    make_dirs
    echo-n $”Starting $prog: “
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq0 ] && touch$lockfile
    return$retval
    }
    stop() {
    echo-n $”Stopping $prog: “
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq0 ] && rm-f $lockfile
    return$retval
    }
    restart() {
    configtest || return$?
    stop
    sleep1
    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/null2>&1
    }
    case”$1″in
    start)
    rh_status_q && exit0
    $1
    ;;
    stop)
    rh_status_q || exit0
    $1
    ;;
    restart|configtest)
    $1
    ;;
    reload)
    rh_status_q || exit7
    $1
    ;;
    force-reload)
    force_reload
    ;;
    status)
    rh_status
    ;;
    condrestart|try-restart)
    rh_status_q || exit0
    ;;
    *)
    echo$”Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}”
    exit2
    esac
    [root@node1 nginx-1.4.2]# chmod +x /etc/rc.d/init.d/nginx
    [root@node1 nginx-1.4.2]# chkconfig –add nginx
    [root@node1 nginx-1.4.2]# chkconfig nginx on
    [root@node1 nginx-1.4.2]# service nginx start
    Starting nginx:                                            [  OK  ]

    提示:服务脚本在两台Nginx服务器上都要提供

    接下来请看第2页精彩内容http://www.linuxidc.com/Linux/2013-10/90809p2.htm

    Nginx 的详细介绍请点这里
    Nginx 的下载地址请点这里

    相关阅读:

    Nginx反向代理+负载均衡+健康探测+缓存 http://www.linuxidc.com/Linux/2013-09/89774.htm

    Nginx Tomcat 集群负载均衡解决笔记 http://www.linuxidc.com/Linux/2013-07/86827.htm

    Nginx 配置轮询分流-实现负载均衡【测试通过】 http://www.linuxidc.com/Linux/2013-06/86692.htm

    Nginx负载均衡引起的网站不可用 http://www.linuxidc.com/Linux/2013-05/84063.htm

    在Linux上使用Nginx为Solr集群做负载均衡 http://www.linuxidc.com/Linux/2012-12/75257.htm

  • 相关阅读:
    eclipse 远程debug tomcat web项目
    阿里巴巴fastjson的使用
    STS 3.6.4 SpringMVC 4.1.6 Hibernate 4.3.8 MySQL
    Ubuntu su 认证失败
    mysql mha高可用架构的安装
    Swift开发教程--关于Existing instance variable '_delegate'...的解决的方法
    设计模式-适配器模式(Go语言描写叙述)
    Xcode6.3 怎样使用Leaks查看内存泄露
    java中的subString具体解释及应用
    出走三上海篇
  • 原文地址:https://www.cnblogs.com/likehua/p/3568517.html
Copyright © 2020-2023  润新知