Linux搭建HAproxy

该博客详细介绍了如何在Linux系统上搭建HAProxy,包括安装Apache、配置HAProxy和设置负载均衡策略,以便实现对PHP应用的高可用性和性能优化。

操作步骤:
105php
在web安装apache
yum -y install httpd
[root@kvm122105 ~]# echo 122105 > /var/www/html/index.html
[root@kvm122105 ~]# echo 122105php > /var/www/html/index.php
systemctl start httpd
106php
在web安装apache
yum -y install httpd
[root@kvm122106 ~]# echo 122106 > /var/www/html/index.html
[root@kvm122106 ~]# echo 122106php > /var/www/html/index.php
systemctl start httpd

[root@kvm122101 haproxy-1.7]# tar xf haproxy-1.7.5.tar.gz

[root@kvm122101 haproxy-1.7]# cd haproxy-1.7.5/

[root@kvm122101 haproxy-1.7.5]# make TARGET=linux2628 PREFIX=/usr/local/haproxy/

[root@kvm122101 haproxy-1.7.5]# make TARGET=linux2628 PREFIX=/usr/local/haproxy/ install

[root@kvm122101 haproxy-1.7.5]# cd /usr/local/haproxy/

[root@kvm122101 haproxy]# vim haproxy.cfg

global #参数是进程级的,通常是和操作系统相关
log 127.0.0.1 local3
maxconn 4096
user nobody
group nobody
daemon
pidfile /usr/local/haproxy/haproxy.pid

defaults #配置默认参数,这些参数可以被用用到frontend,backend,Listen组件
log global
mode http
maxconn 2048
timeout connect 10s
timeout client 1m
timeout server 1m
retries 3
option redispatch

listen haproxy-monitor #Fronted和backend的组合体
bind *:8080
mode http
option forwardfor
option httpclose
stats enable
stats show-legends
stats refresh 5s
stats uri /stats
stats auth admin:admin

frontend main
#接收请求的前端虚拟节点,Frontend指定具体使用用后端的backend
bind *:8008
log global
option forwardfor
option httpclose
default_backend html-server

backend html-server
#后端服务集群的配置,真实服务器器,一一个Backend对应一一个或者多个实体服务器器
balance roundrobin
option httpchk GET /index.html
server html-A 192.168.122.105:80 check
server html-B 192.168.122.106:80 check

php操作

global
#参数是进程级的,通常是和操作系统相关
log 127.0.0.1 local3
maxconn 4096
user nobody
group nobody
daemon
pidfile /usr/local/haproxy/haproxy.pid

defaults
#配置默认参数,这些参数可以被用用到frontend,backend,Listen组件
log global
mode http
maxconn 2048
timeout connect 10s
timeout client 1m
timeout server 1m
retries 3
option redispatch

listen haproxy-monitor
#Fronted和backend的组合体
bind *:8080
mode http
option forwardfor
option httpclose
stats enable
stats show-legends
stats refresh 5s
stats uri /stats
stats auth admin:admin

frontend main
#接收请求的前端虚拟节点,Frontend指定具体使用用后端的backend
bind *:80
log global
option forwardfor
option httpclose

acl html url_reg -i .html$
acl php url_reg -i .php$
use_backend html-server if html
use_backend php-server if php

default_backend html-server

backend html-server
#后端服务集群的配置,真实服务器器,一一个Backend对应一一个或者多个实体服务器器
balance roundrobin
option httpchk GET /index.html

retries 3
option redispatch

cookie SERVERID insert indirect nocache
server html-A 192.168.122.105:80 cookie A check weight 1 inter 2000 rise 2 fall 5
server html-B 192.168.122.106:80 cookie B check weight 1 inter 2000 rise 2 fall 5

backend php-server
#后端服务集群的配置,真实服务器器,一一个Backend对应一一个或者多个实体服务器器
balance roundrobin
option httpchk GET /index.php

retries 3
option redispatch

cookie SERVERID insert indirect nocache
server html-A 192.168.122.105:80 cookie A check weight 1 inter 2000 rise 2 fall 5
server html-B 192.168.122.106:80 cookie B check weight 1 inter 2000 rise 2 fall 5

[root@kvm122101 haproxy]# ./sbin/haproxy -f haproxy.cfg

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值