• nginx+负载均衡+keepalive+双机热备


    最近搭建keepalived双机热备, 在网上博采众之所长, 整理一下自己的笔记!

    在新搭建的centos6.x下最好下载一下阿里云的源配置:

      centos:http://mirrors.aliyun.com/help/centos

    1、备份

    mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

    2、下载新的CentOS-Base.repo 到/etc/yum.repos.d/

    CentOS 5

    wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo

    或者

    curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo

    CentOS 6

    wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo

    或者

    curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo

    CentOS 7

    wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

    或者

    curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

    3、之后运行yum makecache生成缓存

    ===============================================================================================================================

      准备2台电脑来做这个实验:


      192.168.232.137        web服务器  keepalived nginx
      192.168.232.138        web服务器  keepalived nginx

      虚拟IP (VIP):192.168.232.17

      虚拟IP (VIP):192.168.232.18

    nginx下载地址:  http://nginx.org/en/download.html

    keepallived下载地址:   http://www.keepalived.org/download.html

    在137上:(ngnix和keepalived需要的依赖环境)

      安装依赖:

          su - root

           yum -y install gcc gcc+ gcc-c++

      OpenSSL: yum install -y openssl openssl-*

      pcre库:  yum install -y pcre pcre-devel

      zlib库:   yum install -y zlib zlib-devel

           yum -y install lrzsz

              yum -y install openssh-clients

           yum -y install libssl-dev libnl libnl-devel

           yum -y install popt popt-devel

      安装内核:   

           yum -y install kernel kernel-devel*

    ===================================================================================================================================

    1.  安装keepalived

    /usr/local/下:  wget http://www.keepalived.org/software/keepalived-1.2.2.tar.gz

              tar -zxvf keepalived-1.2.2.tar.gz  

            cd keepalived-1.2.2  
            ./configure  --prefix=/usr/local/keepalived
            make  
            make install   

    拷贝相应的文件

    1.拷贝执行文件

      cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

    2.将init.d文件拷贝到etc下,加入开机启动项

      cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived

    3.将keepalived文件拷贝到etc下,加入网卡配置

      cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

    4.创建keepalived文件夹

      mkdir -p /etc/keepalived

    5.将keepalived配置文件拷贝到etc下

      cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf

    6.添加可执行权限

      chmod +x /etc/init.d/keepalived

    2.启动keepalived

    启动:service keepalived start

    停止:service keepalived stop

    重启:service keepalived restart

    3.  配置日志文件

    1.将keepalived日志输出到local0:

      vim /etc/sysconfig/keepalived

      KEEPALIVED_OPTIONS="-D -d -S 0"

    2.在/etc/rsyslog.conf里添加:

      local0.*  /var/log/keepalived.log

    3.重新启动keepalived和rsyslog服务:

      service rsyslog restart

      service keepalived restart

    4. 打开防火墙的通讯地址

    iptables -A INPUT -d 224.0.0.18 -j ACCEPT

    /etc/rc.d/init.d/iptables save

     ===================================================================================================================================

    1.  安装nginx

    /usr/local/下:   wget http://nginx.org/download/nginx-1.10.3.tar.gz

            tar -zxvf linux-nginx-1.10.2.tar.gz   

            mkdir -p /var/temp/nginx    

    ./configure
    --prefix=/usr/local/nginx
    --pid-path=/var/run/nginx/nginx.pid
    --lock-path=/var/lock/nginx.lock
    --error-log-path=/var/log/nginx/error.log
    --http-log-path=/var/log/nginx/access.log
    --with-http_gzip_static_module
    --http-client-body-temp-path=/var/temp/nginx/client
    --http-proxy-temp-path=/var/temp/nginx/proxy
    --http-fastcgi-temp-path=/var/temp/nginx/fastcgi
    --http-uwsgi-temp-path=/var/temp/nginx/uwsgi
    --http-scgi-temp-path=/var/temp/nginx/scgi

            make
            make install
            whereis nginx

    2.启动nginx

    cd /usr/local/nginx/sbin/
    ./nginx

    ./nginx -s quit

    查看进程: ps -ef|grep nginx 

    3.测试nginx

    service iptables stop

    chkconfig iptables off

    访问: 192.168.200.137

    4.配置nginx

    配置虚拟主机
    反向代理
    负载均衡
    upstream tomcat_server_pool{
    server 192.168.101.5:8080 weight=10;
    server 192.168.101.6:8080 weight=10;
    }

    server {
    listen 80;
    server_name aaa.test.com;
    location / {
    proxy_pass http://tomcat_server_pool;
    index index.jsp index.html index.htm;
    }
    }

    负载均衡特点:
    转发
    故障移除
    恢复添加
    高可用(keepalived + nginx实现主备)

    配置高可用

    ! Configuration File for keepalived

    global_defs {
    notification_email {
    acassen@firewall.loc
    failover@firewall.loc
    sysadmin@firewall.loc
    }
    notification_email_from Alexandre.Cassen@firewall.loc
    #smtp_server 192.168.200.1 #指定smtp服务器地址
    #smtp_connect_timeout 30 #指定smtp连接超时时间
    router_id LVS_DEVEL #运行keepalived机器的一个标识
    }

    vrrp_script check_nginx {
    script "/etc/keepalived/check_nginx.sh" ##监控脚本
    interval 2 ##时间间隔,2秒
    weight 2 ##权重
    }

    vrrp_instance VI_1 {
    state MASTER #标示状态为MASTER 备份机为BACKUP
    interface eth0 #设置实例绑定的网卡
    virtual_router_id 51 #同一实例下virtual_router_id必须相同
    priority 100 #MASTER权重要高于BACKUP 比如BACKUP为99
    advert_int 1 #MASTER与BACKUP负载均衡器之间同步检查的时间间隔,单位是秒
    authentication { #设置认证
    auth_type PASS #主从服务器验证方式
    auth_pass 1111
    }
    track_script {
    check_nginx ##监控脚本
    }
    virtual_ipaddress { #设置vip
    #192.168.200.16 #可以多个虚拟IP,换行即可
    192.168.200.17
    }
    }

    vrrp_instance VI_2 {
    state BACKUP
    interface eth1
    virtual_router_id 52
    priority 99
    advert_int 1
    authentication {
    auth_type PASS
    auth_pass 1111
    }
    virtual_ipaddress {
    192.168.200.18
    }
    }

    ---------------------------------

    vim /etc/keepalived/check_nginx.sh

    #!/bin/bash
    # 如果进程中没有nginx则将keepalived进程kill掉
    A=`ps -C nginx --no-header |wc -l` ## 查看是否有 nginx进程 把值赋给变量A
    if [ $A -eq 0 ];then ## 如果没有进程值得为 零
    service keepalived stop ## 则结束 keepalived 进程
    fi

    chmod a+x /etc/keepalived/check_nginx.sh

    配置nginx和keepalived开机启动:

    https://www.cnblogs.com/whatmiss/p/7091220.html

    vim /etc/init.d/nginx

    #!/bin/bash

    # nginx Startup script for the Nginx HTTP Server

    # it is v.0.0.2 version.

    # chkconfig: - 85 15

    # description: Nginx is a high-performance web and proxy server.

    #              It has a lot of features, but it's not for everyone.

    # processname: nginx

    # pidfile: /var/run/nginx.pid

    # config: /usr/local/nginx/conf/nginx.conf

    nginxd=/usr/local/nginx/sbin/nginx

    nginx_config=/usr/local/nginx/conf/nginx.conf

    nginx_pid=/var/run/nginx.pid

    RETVAL=0

    prog="nginx"

    # Source function library.

    . /etc/rc.d/init.d/functions

    # Source networking configuration.

    . /etc/sysconfig/network

    # Check that networking is up.

    [ ${NETWORKING} = "no" ] && exit 0

    [ -x $nginxd ] || exit 0

    # Start nginx daemons functions.

    start() {

    if [ -e $nginx_pid ];then

       echo "nginx already running...."

       exit 1

    fi

       echo -n $"Starting $prog: "

       daemon $nginxd -c ${nginx_config}

       RETVAL=$?

       echo

       [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx

       return $RETVAL

    }

    # Stop nginx daemons functions.

    stop() {

            echo -n $"Stopping $prog: "

            killproc $nginxd

            RETVAL=$?

            echo

            [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid

    }

    # reload nginx service functions.

    reload() {

        echo -n $"Reloading $prog: "

        #kill -HUP `cat ${nginx_pid}`

        killproc $nginxd -HUP

        RETVAL=$?

        echo

    }

    # See how we were called.

    case "$1" in

    start)

            start

            ;;

    stop)

            stop

            ;;

    reload)

            reload

            ;;

    restart)

            stop

            start

            ;;

    status)

            status $prog

            RETVAL=$?

            ;;

    *)

            echo $"Usage: $prog {start|stop|restart|reload|status|help}"

            exit 1

    esac

    exit $RETVAL

    -----------------------------------------------------------------

    chmod a+x /etc/init.d/nginx

    chmod a+x /etc/init.d/keepalived

    [root@localhost keepalived]# vim /etc/rc.local
    #!/bin/sh
    #
    # This script will be executed *after* all the other init scripts.
    # You can put your own initialization stuff in here if you don't
    # want to do the full Sys V style init stuff.

    touch /var/lock/subsys/local
    /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart

    /usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart

    /etc/init.d/nginx start

    sleep 1

    /etc/init.d/keepalived start

    高可用测试:

    先启动两台tomcat --->再启动两台nginx-->后启动两台keepalived

    1.初始状态

    vip192.168.200.17绑定在主nginx的eth0上(同时是vip192.168.200.18的备nginx)

    /sbin/ip add show eth0

    vip192.168.200.18绑定在主nginx的eth1上(同时是vip192.168.200.17的备nginx)。

    /sbin/ip add show eth1

    从而实现双主热备....

    ======================================================================================================================================

    附录:

    加入开机启动:

    chkconfig --add keepalived       #添加时必须保证/etc/init.d/keepalived存在

    chkconfig keepalived on

    添加完可查询系统服务是否存在:chkconfig --list

  • 相关阅读:
    Django学习之文件下载
    Django学习之文件上传
    Django基础十一之认证系统
    Django基础之jQuery操作
    Django基础之django分页
    CVE-2019-17671:wrodpress 未授权访问漏洞-复现
    CVE-2020-1938:Apache-Tomcat-Ajp漏洞-复现
    Xray安装与使用
    内网渗透扫描器详解
    数据库注入详解
  • 原文地址:https://www.cnblogs.com/liuzhipeng/p/7243712.html
Copyright © 2020-2023  润新知