美文网首页
HAProxy搭建及使用

HAProxy搭建及使用

作者: 王哈哈就很棒 | 来源:发表于2020-05-27 16:27 被阅读0次

安装部分

安装依赖

yum -y install vim wget gcc make

下载

wget https://mirrors.huaweicloud.com/haproxy/1.7/src/haproxy-1.7.0.tar.gz

解压

tar -xvf haproxy-1.7.0.tar.gz

编译安装

make TARGET=linux2628 PREFIX=/usr/local/haproxy
make install PREFIX=/usr/local/haproxy

配置服务相关

cp /usr/local/haproxy/sbin/haproxy /usr/sbin/
cp ./examples/haproxy.init /etc/init.d/haproxy
chmod 755 /etc/init.d/haproxy

配置文件(tcp)
mkdir /etc/haproxy
vim /etc/haproxy/haproxy.cfg

global
    daemon          # 以后台形式运行harpoxy
    maxconn 20480   # 最大连接数
    nbproc 1        # 设置进程数量
    pidfile /usr/local/haproxy/conf/haproxy.pid # 进程PID文件


defaults
    mode tcp
    retries 2
    option redispatch       # 当serverId对应的服务器挂掉后,强制定向到其他健康的服务器
    option abortonclose     # 当服务器负载很高的时候,自动结束掉当前队列处理比较久的连接
    maxconn 4096            # 默认的最大连接数
    timeout connect 5000ms  # 连接超时
    timeout client 30000ms  # 客户端超时
    timeout server 30000ms  # 服务器超时
    timeout check 2000ms    # 心跳检测超时
    log 127.0.0.1 local0 err

listen servers
    bind 0.0.0.0:9980       # 定义haproxy前端部分监听的端口
    mode tcp                # tcp模式
    balance roundrobin      # 设置负载算法为:轮询算法rr
    #maxconn 4086
    #log 127.0.0.1 local0 debug
    # server 定义多台后端真实服务器
    server s1 192.168.101.62:22 check
    server s2 192.168.101.53:22 check

listen stats  
    bind 0.0.0.0:1080 
    mode http
    option httplog
    log 127.0.0.1 local0 err 
    stats refresh 30s
    maxconn 10               # 最大连接数  
    stats uri /admin         # 状态页面 http//ip:1080/admin访问  
    stats realm Haproxy\ Statistics
    stats auth admin:admin   # 用户和密码:admin
    stats hide-version       # 隐藏版本信息  
    stats admin if TRUE      # 设置手工启动/禁用

http

global
    daemon          # 以后台形式运行harpoxy
    maxconn 20480   # 最大连接数
    nbproc 1        # 设置进程数量
    pidfile /usr/local/haproxy/conf/haproxy.pid # 进程PID文件


defaults
    mode http
    retries 2
    option redispatch       # 当serverId对应的服务器挂掉后,强制定向到其他健康的服务器
    option abortonclose     # 当服务器负载很高的时候,自动结束掉当前队列处理比较久的连接
    maxconn 4096            # 默认的最大连接数
    timeout connect 5000ms  # 连接超时
    timeout client 30000ms  # 客户端超时
    timeout server 30000ms  # 服务器超时
    timeout check 2000ms    # 心跳检测超时
    log 127.0.0.1 local0 err

listen servers
    bind 0.0.0.0:9980       # 定义haproxy前端部分监听的端口
    mode http               # http模式
    balance roundrobin      # 设置负载算法为:轮询算法rr
    #maxconn 4086
    #log 127.0.0.1 local0 debug
    # server 定义多台后端真实服务器
    server s1 192.168.101.62:80 check
    server s2 192.168.101.53:80 check

listen stats  
    bind 0.0.0.0:1080 
    mode http
    option httplog
    log 127.0.0.1 local0 err 
    stats refresh 30s
    maxconn 10               # 最大连接数  
    stats uri /admin         # 状态页面 http//ip:1080/admin访问  
    stats realm Haproxy\ Statistics
    stats auth admin:admin   # 用户和密码:admin
    stats hide-version       # 隐藏版本信息  
    stats admin if TRUE      # 设置手工启动/禁用

启动,停止,重启

service haproxy start 
service haproxy stop
service haproxy restart 

相关文章

网友评论

      本文标题:HAProxy搭建及使用

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