haproxy安装与配置

一、部分理论
  HAProxy是一个使用C语言编写的自由及开放源代码软件[1],其提供高可用性、负载均衡,以及基于TCP和HTTP的应用程序代理。
  HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。
HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。
  HAProxy实现了一种事件驱动, 单一进程模型,此模型支持非常大的并发连接数。多进程或多线程模型受内存限制 、系统调度器限制以及无处不在的锁限制,很少能处理数千并发连接。
事件驱动模型因为在有更好的资源和时间管理的用户空间(User-Space) 实现所有这些任务,所以没有这些问题。此模型的弊端是,在多核系统上,这些程序通常扩展性较差。
这就是为什么他们必须进行优化以 使每个CPU时间片(Cycle)做更多的工作。

二、安装与配置
1.tar zxvf haproxy-2.3.6.tar.gz
2.cd /root/haproxy-2.3.6
mkdir /root/haproxy
3.最小安装没有安装 gcc-c++,编译make需要先安装
yum install gcc-c++ -y
make TARGET=linux310 ARCH=x86_64 PREFIX=/root/haproxy
make install PREFIX=/root/haproxy
4.创建配置文件
mkdir /root/haproxy/conf -p
mkdir /root/haproxy/logs -p
vi /root/haproxy/conf/haproxy.cfg
#logging options
global
    log 127.0.0.1 local0 info #日志输出配置,所有日志都记录在本机,通过local0输出
    maxconn 5000
    chroot /root/haproxy #haproxy 安装路径
    uid 0 #所属运行的用户uid
    gid 0 #所属运行的用户组
    daemon  #后台运行
    quiet
    nbproc 20
    pidfile /root/haproxy/logs/haproxy.pid #指定PID文件路径

defaults
    log global
    #使用4层代理模式,"mode http"为7层代理模式
    mode tcp
    #if you set mode to tcp,then you nust change tcplog into httplog
    option tcplog
    option dontlognull
    retries 3
    option redispatch
    maxconn 2000
    #连接超时时间
    timeout connect 5s
    #客户端空闲超时时间为 60秒 则HA 发起重连机制
    timeout client 60s
    #服务器端连接超时时间为 15秒 则HA 发起重连机制
    timeout server 15s    
    
#配置haproxy web监控,查看统计信息
listen admin_stats
    bind 0.0.0.0:4001 #前端浏览器中查看统计的WEB界面地址
    mode http
    stats enable
    stats auth admin:yehaver  #设置查看统计的账号密码
    #设置haproxy监控地址为http://localhost:4001/test
    stats uri /test
    stats refresh 5s  #5s刷新一次

#front-end IP for consumers and producters
listen mycat_proxy
    bind 0.0.0.0:4002  #绑定协议端口
    #配置TCP模式
    #所处理的类别,默认采用http模式,可配置成tcp作4层消息转发
    mode tcp
    option tcplog
    #balance url_param userid
    #balance url_param session_id check_post 64
    #balance hdr(User-Agent)
    #balance hdr(host)
    #balance hdr(Host) use_domain_only
    #balance rdp-cookie
    #balance leastconn
    #balance source //ip
    #简单的轮询
    balance roundrobin
    #负载均衡策略
    #rabbitmq集群节点配置 #inter 每隔五秒对mq集群做健康检查, 2次正确证明服务器可用,2次失败证明服务器不可用,并且配置主备机制
    server mycat-01 192.168.100.101:8066 check inter 5000 rise 2 fall 2
    #server mycat-02 192.168.100.102:8066 check inter 5000 rise 2 fall 2  可以增加很多机器
    
5.启动haproxy
/root/haproxy/sbin/haproxy -f /root/haproxy/conf/haproxy.cfg

6.登录网址验证
http://192.168.100.101:4001/test
mysql模式登录同样要加上 --default-auth=mysql_native_password  
mysql -h 192.168.100.101 -P 4002 -u root -p --default-auth=mysql_native_password  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值