千家信息网

haproxy 的安装

发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,1.基础安装yum install pcre pcre-devel openssl openssl-develtar xf haproxy-1.7.1.tar.gzcd haproxy-1.7.1
千家信息网最后更新 2026年11月03日haproxy 的安装

1.基础安装

yum install pcre pcre-devel openssl  openssl-develtar xf  haproxy-1.7.1.tar.gzcd haproxy-1.7.1make TARGET=linux2628 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1  PREFIX=/usr/local/haproxy make install PREFIX=/usr/local/haproxycd examples/cp  haproxy.init   /etc/init.d/haproxy chmod +x  /etc/init.d/haproxy

2.本机IP:192.168.56.15

[root@bogon examples]# cat /etc/haproxy/haproxy.cfg globalmaxconn 100000chroot /usr/local/haproxyuid 99gid 99daemonnbproc 1pidfile /usr/local/haproxy/logs/haproxy.pidlog 127.0.0.1 local0 infodefaultsoption http-keep-alivemaxconn 100000mode httptimeout connect 300000mstimeout client  300000mstimeout server  300000mslisten stats mode http bind 0.0.0.0:9999 stats enable log global stats uri     /haproxy-status stats auth    haadmin:123456listen  web_port   bind 0.0.0.0:80 mode tcp log global[root@bogon examples]# [root@bogon examples]# /etc/init.d/haproxy  restart Restarting haproxy (via systemctl):                        [  OK  ][root@bogon examples]#

http://192.168.56.15:9999/haproxy-status

3.对后端代理

[root@bogon examples]# cat /etc/haproxy/haproxy.cfg globalmaxconn 100000chroot /usr/local/haproxyuid 99gid 99daemonnbproc 1pidfile /usr/local/haproxy/logs/haproxy.pidlog 127.0.0.1 local0 infodefaultsoption http-keep-alivemaxconn 100000mode httptimeout connect 300000mstimeout client  300000mstimeout server  300000mslisten stats mode http bind 0.0.0.0:9999 stats enable log global stats uri     /haproxy-status stats auth    haadmin:123456listen  web_port   bind 0.0.0.0:80 mode tcp  #tcp的方式 log global server web1  192.168.56.12:80  check inter 2000 fall 15 rise 10 server web2  192.168.56.16:80    check inter 2000 fall 15 rise 10[root@bogon examples]#  /etc/init.d/haproxy  restart  #inter是监控检查时间间隔,即每间隔2秒进行一次检查, rise是连续检查10次失败后将服务器从负载删除, fall是连续15次监控检查成功后重新添加至负载,一般fall大于rise几次,

[root@bogon html]# curl 192.168.56.15192.168.56.12 web1[root@bogon html]# curl 192.168.56.15192.168.56.16 web2[root@bogon html]# curl 192.168.56.15192.168.56.12 web1[root@bogon html]# curl 192.168.56.15192.168.56.16 web2[root@bogon html]# curl 192.168.56.15192.168.56.12 web1[root@bogon html]# curl 192.168.56.15192.168.56.16 web2[root@bogon html]# curl 192.168.56.15192.168.56.12 web1[root@bogon html]# curl 192.168.56.15192.168.56.16 web2[root@bogon html]# curl 192.168.56.15192.168.56.12 web1[root@bogon html]# curl 192.168.56.15192.168.56.16 web2[root@bogon html]#

查看负载状况


################################################

haproxy调度算法

HAProxy的算法有如下8种:roundrobin,表示简单的轮询,static-rr,表示根据权重,建议关注;leastconn,表示最少连接者先处理,建议关注;source,表示根据请求源IP,建议关注;uri,表示根据请求的URI;url_param,表示根据请求的URl参数'balanceurl_param' requires an URL parameter namehdr(name),表示根据HTTP请求头来锁定每一次HTTP请求;rdp-cookie(name),表示根据据cookie(name)来锁定并哈希每一次TCP请求。

haproxy的摘除节点的 yum安装的

globalmaxconn 100000chroot /var/lib/haproxyuser haproxygroup haproxydaemonnbproc 1 pidfile /usr/local/haproxy/logs/haproxy.pid stats socket /var/lib/haproxy.sock mode 600 level adminlog 127.0.0.1 local10 infodefaultsoption http-keep-alivemaxconn 100000mode httptimeout connect 5000mstimeout client  50000mstimeout server 50000mslisten statsmode httpbind 0.0.0.0:8888stats enablestats uri     /haproxy-status stats auth    haproxy:saltstackfrontend frontend_www_example_combind 192.168.56.21:80mode httpoption httploglog global    default_backend backend_www_example_combackend backend_www_example_comoption forwardfor header X-REAL-IPoption httpchk HEAD / HTTP/1.0balance   roundrobin server web-node1  192.168.56.11:8080 check inter 2000 rise 30 fall 15server web-node2  192.168.56.12:8080 check inter 2000 rise 30 fall 15[root@web-node1 ~]#

这样就能吧haproxy的节点删除或者增加

echo "disable server backend_www_example_com/web-node1" | socat /var/lib/haproxy.sock stdioecho "enable server backend_www_example_com/web-node1" | socat /var/lib/haproxy.sock stdio

haproxy增加日志的功能

mkdir /var/log/haproxychmod a+w /var/log/haproxy/etc/rsyslog.conf打开选项:$ModLoad imudp$UDPServerRun 514# Save haproxy loglocal0.*                       /var/log/haproxy/haproxy.logvim  /etc/sysconfig/rsyslog[root@web-node1 nginx]# cat /etc/sysconfig/rsyslog # Options for rsyslogd# Syslogd options are deprecated since rsyslog v3.# If you want to use them, switch to compatibility mode 2 by "-c 2"  See rsyslogd(8) for more detailsSYSLOGD_OPTIONS="-r -m 0 -c 2"   #修改此处[root@web-node1 nginx]#

haproxy日志里面的localhost 和配置文件有关 这个IP可以修改

log 127.0.0.1 local10 info

3.集群配置

192.168.56.11 nginx 反向代理 keepalived haproxy

192.168.56.12 nginx 反向代理 keepalived haproxy

192.168.56.13 nginx 模拟tomcat

192.168.56.14 nginx 模拟tomcat

keepalived的VIP为192.168.56.21


问题:haproxy 不会绑定不存在的VIP 所以启动不起来 so,这样可以绑定不存在的VIP

echo 1 > /proc/sys/net/ipv4/ip_nonlocal_bind

vi /etc/sysctl.conf sysctl -p

net.ipv4.ip_nonlocal_bind = 1
开启允许绑定非本机的IP


查看keepalived的配置文件192.168.56.11(主)

[root@web-node1 nginx]# cat  /etc/keepalived/keepalived.conf ! Configuration File for keepalivedglobal_defs {   notification_email {     saltstack@example.com   }   notification_email_from keepalived@example.com   smtp_server 127.0.0.1   smtp_connect_timeout 30   router_id haproxy_ha}vrrp_instance haproxy_ha {state MASTERinterface eth0    virtual_router_id 36priority 150    advert_int 1authentication {auth_type PASS        auth_pass 1111    }    virtual_ipaddress {       192.168.56.21    }}[root@web-node1 nginx]#

查看keepalived的配置文件192.168.56.12(备)

[root@web-node2 ~]# cat /etc/keepalived/keepalived.conf ! Configuration File for keepalivedglobal_defs {   notification_email {     saltstack@example.com   }   notification_email_from keepalived@example.com   smtp_server 127.0.0.1   smtp_connect_timeout 30   router_id haproxy_ha}vrrp_instance haproxy_ha {state BACKUPinterface eth0    virtual_router_id 36priority 100    advert_int 1authentication {auth_type PASS        auth_pass 1111    }    virtual_ipaddress {       192.168.56.21    }}[root@web-node2 ~]#

查看192.168.56.11 haproxy的配置文件

[root@web-node2 ~]# cat /etc/haproxy/haproxy.cfgglobalmaxconn 100000chroot /var/lib/haproxyuser haproxygroup haproxydaemonnbproc 1 pidfile /usr/local/haproxy/logs/haproxy.pid stats socket /var/lib/haproxy.sock mode 600 level adminlog 127.0.0.1 local3 infodefaultsoption http-keep-alivemaxconn 100000mode httptimeout connect 5000mstimeout client  50000mstimeout server 50000mslisten statsmode httpbind 0.0.0.0:8888stats enablestats uri     /haproxy-status stats auth    haproxy:saltstackfrontend frontend_www_example_combind 192.168.56.21:80mode tcpoption httploglog global    default_backend backend_www_example_combackend backend_www_example_comoption forwardfor header X-REAL-IPoption httpchk HEAD / HTTP/1.0balance   roundrobin server web-node1  192.168.56.11:8080 check inter 2000 rise 30 fall 15server web-node2  192.168.56.12:8080 check inter 2000 rise 30 fall 15[root@web-node2 ~]#

查看192.168.56.12 haproxy的配置文件

[root@web-node1 nginx]# cat /etc/haproxy/haproxy.cfg globalmaxconn 100000chroot /var/lib/haproxyuser haproxygroup haproxydaemonnbproc 1 pidfile /usr/local/haproxy/logs/haproxy.pid stats socket /var/lib/haproxy.sock mode 600 level admin#log 127.0.0.1 local3 infolog  192.168.56.21 local0 debugdefaultsoption http-keep-alivemaxconn 100000mode httptimeout connect 5000mstimeout client  50000mstimeout server 50000mslisten statsmode httpbind 0.0.0.0:8888stats enablestats uri     /haproxy-status stats auth    haproxy:saltstackfrontend frontend_www_example_combind 192.168.56.21:80mode httpoption httploglog global    default_backend backend_www_example_combackend backend_www_example_comoption forwardfor header X-REAL-IPoption httpchk HEAD / HTTP/1.0balance   roundrobin server web-node1  192.168.56.11:8080 check inter 2000 rise 30 fall 15server web-node2  192.168.56.12:8080 check inter 2000 rise 30 fall 15[root@web-node1 nginx]#

查看192.168.56.11的nginx反向代理的配置文件(最简单的配置 demo)

[root@web-node1 nginx]# cat  /etc/nginx/nginx.confworker_processes  1;events {    worker_connections  1024;}http {    include       mime.types;    default_type  application/octet-stream;    sendfile        on;    keepalive_timeout  65;     upstream server_pools {         server 192.168.56.13:8080  weight=1;         server 192.168.56.14:8080  weight=1;    }     server {        listen       192.168.56.11:8080;        server_name  blog.liuhaixiao.com;        location / {            proxy_pass http://server_pools;            proxy_set_header Host  $host;            proxy_set_header X-Forwarded-For $remote_addr;        }    }} [root@web-node1 nginx]#

查看192.168.56.12的nginx反向代理的配置文件(最简单的配置 demo)

[root@web-node2 ~]# cat /etc/nginx/nginx.confworker_processes  1;events {    worker_connections  1024;}http {    include       mime.types;    default_type  application/octet-stream;    sendfile        on;    keepalive_timeout  65;     upstream server_pools {         server 192.168.56.13:8080  weight=1;         server 192.168.56.14:8080  weight=1;    }     server {        listen       192.168.56.12:8080;        server_name blog.liuhaixiao.com;        location / {            proxy_pass http://server_pools;            proxy_set_header Host  $host;            proxy_set_header X-Forwarded-For $remote_addr;        }    }}[root@web-node2 ~]#
查看192.168.56.13的nginx web容器[root@web-node3 html]# cat /usr/share/nginx/html/index.html node-2查看192.168.56.14的nginx web容器[root@web-node4 html]# cat /usr/share/nginx/html/index.html node-1

检查VIP zai 192.168.56.11上面

[root@web-node1 nginx]# ip addr1: lo:  mtu 65536 qdisc noqueue state UNKNOWN     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00    inet 127.0.0.1/8 scope host lo       valid_lft forever preferred_lft forever    inet6 ::1/128 scope host        valid_lft forever preferred_lft forever2: eth0:  mtu 1500 qdisc pfifo_fast state UP qlen 1000    link/ether 00:0c:29:10:66:98 brd ff:ff:ff:ff:ff:ff    inet 192.168.56.11/24 brd 192.168.56.255 scope global eth0       valid_lft forever preferred_lft forever    inet 192.168.56.21/32 scope global eth0       valid_lft forever preferred_lft forever    inet6 fe80::20c:29ff:fe10:6698/64 scope link        valid_lft forever preferred_lft forever[root@web-node1 nginx]#


现在把本地的电脑blog.liuhaixiao.com 解析到192.168.56.21上面去


####################################################

新添加:192.168.56.15 增加一个nginx 域名

haproxy 监听80端口 nginx 监听的端口千万不能和后端tomcat一样 否则回包回不来

[root@web-node1 ~]# cat  /etc/nginx/nginx.confworker_processes  1;events {    worker_connections  1024;}http {    include       mime.types;    default_type  application/octet-stream;    sendfile        on;    keepalive_timeout  65;     upstream server_pools {         server 192.168.56.13:8080  weight=1;         server 192.168.56.14:8080  weight=1;    }      upstream server_tt {         server 192.168.56.15:8080  weight=1;    }    server {        listen       81;        server_name  blog.liuhaixiao.com;        location / {            proxy_pass http://server_pools;            proxy_set_header Host  $host;            proxy_set_header X-Forwarded-For $remote_addr;        }    }       server {        listen      81;        server_name  blog.sanlang.com;        location / {            proxy_pass http://server_tt;            proxy_set_header Host  $host;            proxy_set_header X-Forwarded-For $remote_addr;        }    }} [root@web-node1 ~]#

3是 另外一台服务器的192.168.56.15 tomcat/nginx 的页面


haproxy具体参数请参考:

http://www.cnblogs.com/zhang789/p/6057402.html

haproxy:多域名参考

基于域名负载均衡的Haproxy配置

http://blog.csdn.net/youyudehexie/article/details/7606504
















#####################

配置 文件 代理 检查 建议 参数 域名 容器 日志 服务器 端口 算法 节点 本机 参考 服务 监听 监控 均衡 成功 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 上海挚品互联网科技有限公司 科技类互联网 互联网 卓越科技 壕鑫互联网络科技 润盈互联网科技 滨湖区互联网智能科技产品供应 深圳市运趣互联网科技有限公司 恒银科技与互联网金融 茂名市互联网科技产业园 花神(南京)互联网科技有限公司 消费医药和互联网科技 广州新华互联网科技学校电竞方面 高科技互联网发展前景如何 长沙茂晟互联网科技有限公司 有人说阿里系是互联网科技企业 35岁以上互联网科技行业 昆明中奇联合互联网科技 艺电能源互联网科技有限公司招聘 陕西彩京云互联网科技有限公司 互联网高科技绘画 互联网科技资迅 致骋互联网科技 天扬科技互联网广告 甘肃拓世互联网科技有限公司 宝灵互联网科技有限公司 东莞新华互联网科技学校 互联网科技的全部知识 区块链结合互联网科技 深圳互联网科技的发展 互联网硬科技公司
0