App 2.0开发模式的行业看法
692
2022-09-11
haproxy
haproxy
文章目录
haproxy
1.介绍2.haproxy安装3.配置各个负载的内核参数4.提供配置文件5.haproxy.service文件编写6.启用日志7.启动服务
1.介绍
HAProxy是一个使用C语言编写的自由及开放源代码软件,其提供高可用性、负载均衡,以及基于TCP和HTTP的应用程序代理。
HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。
HAProxy实现了一种事件驱动, 单一进程模型,此模型支持非常大的并发连接数。多进程或多线程模型受内存限制 、系统调度器限制以及无处不在的锁限制,很少能处理数千并发连接。事件驱动模型因为在有更好的资源和时间管理的用户空间(User-Space) 实现所有这些任务,所以没有这些问题。此模型的弊端是,在多核系统上,这些程序通常扩展性较差。这就是为什么他们必须进行优化以 使每个CPU时间片(Cycle)做更多的工作。
包括 GitHub、Bitbucket、Stack Overflow、Reddit、Tumblr、Twitter和 Tuenti在内的知名网站,及亚马逊网络服务系统都使用了HAProxy。
2.haproxy安装
yum -y install make gcc pcre-devel bzip2-devel openssl-devel systemd-develuseradd -r -M -s /sbin/nologin haproxytar xf haproxy-2.1.3.tar.gzcd haproxy-2.1.3make cleanmake -j $(grep 'processor' /proc/cpuinfo |wc -l) \TARGET=linux-glibc \USE_OPENSSL=1 \USE_ZLIB=1 \USE_PCRE=1 \USE_SYSTEMD=1make install PREFIX=/usr/local/haproxycp haproxy /usr/sbin/
3.配置各个负载的内核参数
echo 'net.ipv4.ip_nonlocal_bind = 1' >> /etc/sysctl.confecho 'net.ipv4.ip_forward = 1' >> /etc/sysctl.confsysctl -p
4.提供配置文件
mkdir /etc/haproxycat > /etc/haproxy/haproxy.cfg <#--------------全局配置----------------global log 127.0.0.1 local0 info #log loghost local0 info maxconn 20480#chroot /usr/local/haproxy pidfile /var/run/haproxy.pid #maxconn 4000 user haproxy group haproxy daemon#---------------------------------------------------------------------#common defaults that all the 'listen' and 'backend' sections will#use if not designated in their block#---------------------------------------------------------------------defaults mode log global option dontlognull option option #option forwardfor option redispatch balance roundrobin timeout connect 10s timeout client 10s timeout server 10s timeout check 10s maxconn 60000 retries 3#--------------统计页面配置------------------listen admin_stats bind 0.0.0.0:8189 stats enable mode log global stats uri /haproxy_stats stats realm Haproxy\ Statistics stats auth admin:admin #stats hide-version stats admin if TRUE stats refresh 30s#---------------web设置-----------------------listen webcluster bind 0.0.0.0:80 mode #option GET /index.html log global maxconn 3000 balance roundrobin cookie SESSION_COOKIE insert indirect nocache server web01 172.16.103.130:80 check inter 2000 fall 5 #server web01 192.168.80.102:80 cookie web01 check inter 2000 fall 5EOF
5.haproxy.service文件编写
cat > /usr/lib/systemd/system/haproxy.service <[Unit]Description=HAProxy Load BalancerAfter=syslog.target network.target[Service]ExecStartPre=/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -qExecStart=/usr/local/haproxy/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pidExecReload=/bin/kill -USR2 $MAINPID[Install]WantedBy=multi-user.targetEOFsystemctl daemon-reload
6.启用日志
# vim /etc/rsyslog.conflocal0.* /var/log/haproxy.logsystemctl restart rsyslog
7.启动服务
systemctl restart haproxyss -antl
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。