zoukankan      html  css  js  c++  java
  • nginx+keeplived负载均衡配置

    一、nginx 编译安装
    1.依赖环境安装
         yum -y install gcc gcc-c++ zlib zlib-devel pcre pcre-devel openssl openssl-devel
         yum -y install kernel-devel Popt popt-devel
         yum -y groupinstall "Development Tools" "Development Libraries"

    2.编译安装
        [root@localhost src]# tar -xvf nginx-1.8.0.tar.gz
        [root@localhost nginx-1.8.0]#cd nginx-1.8.0

    #--with-http_stub_status_module :可显示nginx连接数

    [root@localhost nginx-1.8.0]# ./configure --with-http_stub_status_module --with-http_dav_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-http_ssl_module
    [root@localhost nginx-1.8.0]# make && make install

    3.添加脚本
    [root@localhost]# 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

    #---------------------------------------------------------------------------------------------------------------------------
    [root@localhost]# chmod +x /etc/init.d/nginx

    二、配置upstream 模块
    配置nginx.conf配置文件
    1.在http{}中添加一个upstream.conf配置文件
    include /usr/local/nginx/conf/upstream.conf;
         upstream www_test {
              #ip_hash; #如要看到测轮循的效果,这里需注释掉
              server 192.168.0.116:80;
              server 192.168.0.40:80;
    }

    2.将http{}中的server模块单独提取出来
    include /usr/local/nginx/vhost/*.conf;

    配置test 服务 /usr/local/nginx/vhost/test.conf
         server {
              listen 80;
              server_name localhost;

         location / {
              proxy_pass http://www_test;
              proxy_set_header Host $host;
              proxy_set_header X-Forwarded-Proto $scheme;
              proxy_set_header X-Real-IP $remote_addr;
              proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    三、安装配置keeplived服务
    1.安装
    两台前端nginx 开启upstream的服务器安装keepalived服务

         [root@localhost src]# tar -xvf keepalived-1.1.15.tar.gz
         [root@localhost src]# cd keepalived-1.1.15
         [root@localhost keepalived-1.1.15]# ./configure --prefix=/usr/local/keepalived
         [root@localhost keepalived-1.1.15]# make && make install

         [root@localhost keepalived-1.1.15]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
         [root@localhost keepalived-1.1.15]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
         [root@localhost keepalived-1.1.15]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
         [root@localhost keepalived-1.1.15]# cp -r /usr/local/keepalived/etc/keepalived/ /etc/

         [root@localhost keepalived-1.1.15]# /etc/init.d/keepalived start

    2.修改配置文件
    [root@localhost keepalived-1.1.15]# vim /etc/keepalived/keepalived.conf
    global_defs {
        notification_email {
       localhost@qq.com
    }
        notification_email_from Alexandre.Cassen@firewall.loc
        smtp_server 127.0.0.1
        smtp_connect_timeout 30
        router_id node1
    }

    vrrp_instance VI_1 {
         state MASTER #主设置MASTER 从设置为BACKUP
         interface eth0 #eth0 为自己的网卡,要通过ifconfig 查看己的网卡名
         virtual_router_id 51
         priority 100 #备机要小于主
         advert_int 1
         authentication {
         auth_type PASS
         auth_pass 1111
    }
    virtual_ipaddress {
         192.168.0.200 #虚拟IP
    }
    }

    virtual_server 192.168.0.200 80 {
         delay_loop 6
         lb_algo rr
         lb_kind DR
         nat_mask 255.255.255.0
         persistence_timeout 50
         protocol TCP

    real_server 192.168.0.116 80 {
    weight 1
    TCP_CHECK {
          connect_timeout 3
          nb_get_retry 3
          delay_before_retry 3
          connect_port 80
       }
    }
    real_server 192.168.0.40 80 {
    weight 1
    TCP_CHECK {
         connect_timeout 3
         nb_get_retry 3
         delay_before_retry 3
         connect_port 80
    }
    }
    }

    [root@localhost ]# /etc/init.d/keepalived start    #keepalived 服务启动命令


    四、后端可拿两台web 服务器来做测试即可

  • 相关阅读:
    【shell脚本】批量修改扩展名===modifyExtension.sh
    【shell脚本】打印九九乘法表
    【shell脚本】显示进度条
    【Linux命令】Linux压缩及解压命令
    【Linux命令】ulimit设置最大文件打开数
    【mysql】修改mysql数据库密码
    【raid级别】RAID级别工作模式
    MyBatis之#{} and ${}
    MyBatis之foreach
    强制修改常量的值
  • 原文地址:https://www.cnblogs.com/free-long/p/10671161.html
Copyright © 2011-2022 走看看