Haproxy简介及配置文件详解

📅 发布时间:2026/7/13 22:47:48 👁️ 浏览次数:
Haproxy简介及配置文件详解
Haproxy简介及配置文件详解配置haproxy首先yum安装haproxyYum install -y haproxy编辑haproxy.cfg文件Vim /etc/haproxy/haproxy.cfg#---------------------------------------------------------------------# Example configuration for a possible web application. See the# full configuration options online.## https://www.haproxy.org/download/1.8/doc/configuration.txt##---------------------------------------------------------------------globallog 127.0.0.1 local2chroot /var/lib/haproxypidfile /var/run/haproxy.piduser haproxygroup haproxydaemonmaxconn 4000defaultsmode httplog globaloption httplogoption dontlognullretries 3timeout http-request 5stimeout queue 1mtimeout connect 5stimeout client 1mtimeout server 1mtimeout http-keep-alive 5stimeout check 5smaxconn 3000listen admin_statusbind 0.0.0.0:5990stats refresh 5sstats uri /admin?statusstats realm itnihao\ itnihaostats auth admin:adminstats admin if TRUEfrontend mainbind *:80acl itnihao_web hdr_reg(host) -i ^(www.itnihao.cn|ww1.itnihao.cn)$acl itnihao_blog hdr_dom(host) -i blog.itnihao.cnuse_backend server_web if itnihao_webuse_backend server_blog if itnihao_blogdefault_backend server_bbsbackend server_webbalance roundrobincookie SERVERIDoption httpchk GET /index.htmlserver web1 192.168.11.157:80 cookie web1 check inter 1500 rise 3 fall 3 weight 1backend server_blogbalance roundrobincookie SERVERIDoption httpchk GET /index.htmlserver web2 192.168.11.158:80 cookie web2 check inter 1500 rise 3 fall 3 weight 1backend server_bbsbalance roundrobincookie SERVERIDoption httpchk GET /index.htmlserver web3 192.168.11.159:80 cookie web3 check inter 1500 rise 3 fall 3 weight 1注意其中单词拼写不要出错在另开三台服务器编写vim /usr/share/nginx/html/index.html中的内容IP地址要与服务端配置文件中的IP相同访问网页192.168.11.1545990/admin?status可以得到利用压缩包编译安装首先在服务器下载lrzsz插件yum install -y lrzsz将准备好的压缩包拖入服务器并解压解压后进入软件包cd haproxy-2.9.15/编译文件make TARGETlinux-glibc PREFIX/usr/local/haproxy安装到指定位置make install PREFIX/usr/local/haproxy创建目录mkdir -p /usr/data/haproxy/编译文件vim /usr/local/haproxy/haproxy.cfg与之前yum安装的文件配置一样直接复制粘贴在下载好的软件包中复制文件examples/haproxy.Init到/etc/init.d/haproxycp -p haproxy.init /etc/init.d/haproxy编译脚本文件Vim /etc/init.d/haproxy注意注释掉第26行编辑好第36行主意好文件所在位置检查haproxy配置文件是否可执行systemctl check haproxy创建软连接ln -s /usr/local/haproxy/sbin/haproxy /usr/sbin/给脚本添加执行权限chmod x haproxy将 HAProxy 服务添加到系统开机启动项chkconfig --add /etc/init.d/haproxy创建用户useradd -s /sbin/nologin -d /var/lib/haproxy haproxy刷新systemd配置清单systemctl daemon-reload启动服务systemctl start haproxy操作两台服务器组成keepalived双机热备并添加脚本Yum下载keepalived服务Yum install -y keepalived编译文件Vim/etc/keepalived/keepalived.conf! Configuration File for keepalivedglobal_defs {router_id haproxy_01}vrrp_script check_haproxy {script /etc/keepalived/check_haproxy.shinterval 2}vrrp_instance VI_1 {state BACKUPinterface ens160virtual_router_id 50nopreemptpriority 90advert_int 1authentication {auth_type PASSahth_pass 123}virtual_ipaddress {192.168.11.222}track_script {check_haproxy}}复制文件到另一个keepalived服务端scp ./keepalived.conf 192.168.11.160:/etc/keepalived/编辑启停脚本vim /etc/keepalived/check_haproxy.shCount1netstat -antp |grep -v grep |grep haproxy |wc -lif [ $Count1 -eq 0 ]; thensystemctl start haproxysleep 2Count2netstat -antp |grep -v grep |grep haproxy |wc -lif [ $Count2 -eq 0 ]; thenservice keepalived keepalivedelseexit 0fielseexit 0fi添加执行权限chmod x check_haproxy.sh把启停脚本按权限复制到另一个keepalived服务端scp -p check_haproxy.sh 192.168.11.160:/etc/keepalived/在两个服务端启动keepalived服务systemctl start keepalived