美文网首页
keepalived+nginx+http省钱到家

keepalived+nginx+http省钱到家

作者: 六十三63 | 来源:发表于2019-04-07 11:46 被阅读0次

    不知道是不是架构师方面觉得服务器越少就越省钱,反正原先的4台服务器作一个服务的负载都觉得浪费。所以就出现了两台服务器作负载和服务的需求。
    研究做过几次尝试
    1.keepalived+ipvsadm+http,负载会失败。
    2.keepalived+http,只能作vip,主备形式提供,流量只会到keepalive主的服务器上。只有障碍切换才会跑到备的主机上。
    3.keepalived+nginx+http,这样可以提供轮询式的访问,但是要注意的是客户端无法再用http原来的端口访问,要在nginx上改变访问的端口。所以这种省钱方式其实还是不靠谱的。

    但如果是前端程序访问后端服务可以自定义访问端口的情况下,这种方式起码让自己的成本帐面上省了一半

    1.环境说明

    ip 作用
    192.168.20.99 vip
    192.168.20.11 keepalived(主)+nginx+http服务器
    192.168.20.12 keepalived(备)+nginx+http服务器

    2.keepalived,nginx,http的安装

    在有yum源的情况下,keepalived和httpd都可以通过yum安装

    yum install -y keepalived httpd
    

    nginx稍微麻烦一点,需要编译
    编译环境安装

    yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel
    

    下载nginx源码包并安装,这里要用到upstream这个模块,地址为http://nginx.org/en/download.html

    tar -xzvf nginx-1.15.6.tar.gz
    cd nginx-1.15.6
    ./configure --with-stream && make && make install
    ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
    nginx -v #正确显示版本号以后说明nginx可以正常使用
    

    keepalived的配置文件
    192.168.20.11(主)上的配置文件

    vip="192.168.20.99"
    cat <<EOF>/etc/keepalived/keepalived.conf
    vrrp_instance VI_1 {
        state MASTER
        interface enp0s8
        virtual_router_id 99
        priority 100
        advert_int 1
           authentication {
              auth_type PASS
              auth_pass 1111
           }
           virtual_ipaddress {
                              $vip
          }
    }
    EOF
    

    192.168.20.12(备)上的配置文件

    vip="192.168.20.99"
    cat <<EOF>/etc/keepalived/keepalived.conf
    vrrp_instance VI_1 {
        state BACKUP
        interface enp0s8
        virtual_router_id 99
        priority 80
        advert_int 1
           authentication {
              auth_type PASS
              auth_pass 1111
           }
           virtual_ipaddress {
                              $vip
          }
    }
    EOF
    

    启动keepalived并加入开机启动

    systemctl start keepalived && systemctl enable keepalived
    

    keepalived备机添加本地回路
    添加是为了备机的nginx有相应的ip可以监听,不然没有vip监听,nginx无法启动

    ip addr add 192.168.20.99/32 dev lo
    #可以加入开机启动
    chmod 755 /etc/rc.d/rc.local
    echo "ip addr add 192.168.20.99 dev lo">>/etc/rc.local
    

    httpd的主页文件(为了方便测试,把主机名写入index.html)

    echo "my name is `hostname`">/var/www/html/index.html
    

    启动httpd

    systemctl start httpd && systemctl enable httpd
    

    nginx的配置文件
    以下配置文件中前端访问端口变为了88,如果还是80的话,因为http 80 占用,nginx是无法绑定的。

    cat <<EOF>/usr/local/nginx/conf/nginx.conf
    worker_processes 1;
    worker_rlimit_nofile 200000;
    events {
        worker_connections 120000;
    }
    stream {
        include /etc/nginx/stream/*.stream;
    }
    EOF
    
    [ -d /etc/nginx/stream ] || mkdir -p /etc/nginx/stream
    
    cat <<EOF>/etc/nginx/stream/192.168.20.99_80.stream
    upstream 192.168.20.99_80 {
        server 192.168.20.11:80 weight=10;
        server 192.168.20.12:80 weight=10;
    }
    server {
        listen 192.168.20.99:88;
        proxy_connect_timeout 1s;
        proxy_timeout 30s;
        proxy_pass 192.168.20.99_80;
    }
    EOF
    

    语法检查,如果没有问题就启动nginx

    nginx -t
    nginx
    # 加入开机启动项
    chmod 755 /etc/rc.d/rc.local
    echo "nginx" >>/etc/rc.local
    

    3.测试

    注意访问的是88端口

    myname@mypc:~/testbox$ for i in `seq 1 5`;do curl 192.168.20.99:88;done
    my name is test1
    my name is test2
    my name is test1
    my name is test2
    my name is test1
    

    相关文章

      网友评论

          本文标题:keepalived+nginx+http省钱到家

          本文链接:https://www.haomeiwen.com/subject/ycnjiqtx.html