美文网首页
HAProxy负载均衡

HAProxy负载均衡

作者: dongshixiao | 来源:发表于2018-03-24 09:11 被阅读0次

    环境:虚拟机VMware12

    三台实验机

    PL    192.168.30.100
    WEB1  192.168.30.101
    WEB2  192.168.30.102
    

    准备工作:

    安装ubuntu16.04 (LB)
    1.设置系统为中文
    2.删除libreoffice
    sudo apt-get remove libreoffice-common

    3.删除Amazon的链接
    sudo apt-get remove unity-webapps-common

    4.删掉基本不用的自带软件(用的时候再装也来得及,为了显示敲断了)

    sudo apt-get remove thunderbird totem rhythmbox
    empathy brasero simple-scan gnome-mahjongg 
    aisleriot gnome-mines cheese transmission-common
    gnome-orca webbrowser-app gnome-sudoku 
    landscape-client-ui-install
    sudo apt-get remove onboard deja-dup
    

    5.安装Vim
    sudo apt-get install vim

    克隆一份系统命名web1并设置ip:192.168.30.101

    为web1安装环境

    1.添加源

    sudo add-apt-repository ppa:ondrej/php
    sudo apt-get update
    

    2.安装nginx

    sudo apt-get install nginx
    --service nginx start
    --service nginx restart
    --service nginx stop
    

    3.安装php7
    sudo apt-get install php7.0 php7.0-fpm php7.0-mysql php7.0-mcrypt php7.0-cli

    3.配置/etc/nginx/sites-available/defalt (记得备份一份)
    解开或者添加

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        # With php7.0-cgi alone:
        #fastcgi_pass 127.0.0.1:9000;
        # With php7.0-fpm:
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        #include fastcgi_params;
    }
    

    克隆一份系统命名web2并设置ip:192.168.30.102

    为LB安装HAProxy(ubuntu16.04LTS HAProxy1.6.3)

    sudo apt-get update
    sudo apt-get install haproxy
    

    测试是否安装成功:

    haproxy -v

    修改配置文件(记得备份一份):

    cd /etc/haproxy/haproxy.cfg

    添加内容:

    listen haproxy_status
            # 绑定地址,每5s自动刷新,隐藏版本,状态访问页面,认证账号,密码,条件满>足进入管理界面
            bind *:1434
            stats enable
            stats refresh 100s
            stats hide-version
            stats uri /haproxy-status
            stats realm "HAProxy/ static"
            stats auth admin:admin123
            stats admin if TRUE
    frontend http
            bind *:80
            mode http
            default_backend web_backend
    backend web_backenda
            mode http
            balance roundrobin
            option forwardfor header X-REAL-IP
            # cookie中插入srv字串防止登录信息丢失
            cookie srv insert nocache
            server web1 192.168.30.101:80 check
            server web2 192.168.30.102:80 check
    

    一个比较完整的配置 静态资源单独分开
    如下配置原文连接:https://www.cnblogs.com/breg/p/6020012.html

    # 全局配置,日志,运行安装路径,
    global
            log 127.0.0.1 local3 info  # 日志存储到127.0.0.1,端口是514,
     
            chroot /var/lib/haproxy
            pidfile /var/run/haproxy.pid        #配置haproxy的sock文件,权限是600,等级是admin权限,超时2分钟
            stats socket /var/lib/haproxy/haproxy.sock mode 660 level admin
            stats timeout 2m
            user haproxy
            group haproxy
            daemon
     
    # 默认配置
    defaults
            log global
            mode http
            #option httplog         # 访问日志关闭
            option dontlognull      # 不记录空链接,如监控链接
            timeout connect 5000
            timeout client 50000
            timeout server 50000
            timeout check 10000
            maxconn 3000
     
    # 状态监控页面
    listen haproxy_status
            # 绑定地址,每5s自动刷新,隐藏版本,状态访问页面,认证账号,密码,条件满足进入管理界面
            bind 172.16.1.14:8888
            stats enable
            stats refresh 100s
            stats hide-version
            stats uri /haproxy-status
            stats realm "HAProxy/ static"
            stats auth admin:admin123
            stats admin if TRUE
            # 允许的网段,允许,拒绝
            #acl allow src 192.168.12.0/24
            #tcp-request content accept if allow
            #tcp-request content reject
     
    # 1.匹配到www.pinhui001.com域名,跳转到www_backend
    frontend ph_web
            bind 172.16.1.14:80
            acl www hdr_end(host) pinhui001.com        #ACL规则定义的方式有hdr_reg(host)、hdr_dom(host)、hdr_beg(host)、url_sub、url_dir、path_beg、path_end等,-i表示不匹配大小写
            acl www hdr_end(host) www.pinhui001.com
            use_backend www_backend if www
     
    # 2.匹配到目录static,images及jpg,png结尾的跳转到
    frontend ph_static
            bind 172.16.1.14:1802
            acl url_static path_beg -i /static /images /stylesheets
            #acl url_static path_end -i .jpg .gif .png .css .js
            acl static_reg url_reg /*.(css|jpg|js|jpeg|gif)$
            use_backend static_backend if url_static
     
    # test
    frontend test_web
            bind 172.16.1.14:8899
            acl test hdr_beg(host) -i test.pinhui001.cc
            use_backend test_backend if test
     
    backend test_backend
            mode http
            balance roundrobin
            option forwardfor header X-REAL-IP
            option httpchk GET /iisstart.htm HTTP/1.1\r\nHost:172.16.1.25:80
            server web-node1 172.16.1.25:80 check inter 2000 rise 3 fall 3 weight 1
     
    # 1.
    backend www_backend
            # 随机,2秒检测,2次成功认为服务可用,3次失败认为服务不可用,权重为1
            # option httpchk GET /index.html
            balance roundrobin
            option forwardfor header X-REAL-IP
            server web-node1 172.16.1.25:18201 check inter 2000 rise 3 fall 3 weight 1
            server web-node3 192.168.2.16:80 check inter 2000 rise 3 fall 3 weight 1
     
    # 2.
    backend static_backend
            balance roundrobin
            option forwardfor header X-REAL-IP
            # cookie中插入srv字串防止登录信息丢失
            cookie srv insert nocache
            server static01 172.16.1.110:80 check inter 2000 rise 2 fall 3 weight 1
            server static02 172.16.1.111:80 check inter 2000 rise 2 fall 3 weight 1
    

    相关文章

      网友评论

          本文标题:HAProxy负载均衡

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