haproxy七层代理

一.haproxy的基本部署

1.RS上装nginx

[root@webserver1 ~]# dnf install nginx -y

2.再RS上写入测试信息

[root@webserver1 ~]# echo webserver1 - 172.25.254.10 > /usr/share/nginx/html/index.html 
[root@webserver1 ~]# systemctl enable --now nginx
[root@webserver2 ~]# echo webserver2 - 172.25.254.20 > /usr/share/nginx/html/index.html
[root@webserver2 ~]# systemctl enable --now nginx

3.测试

[root@haproxy ~]# curl 172.25.254.10
webserver1 - 172.25.254.10
[root@haproxy ~]# curl 172.25.254.20
webserver2 - 172.25.254.20

4.haproxy服务器下载haproxy软件并开启服务

[root@haproxy ~]# dnf install haproxy -y
[root@haproxy ~]# systemctl enable haproxy.service 

5.写入代理配置参数

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 

 以下两种写法均可

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------

frontend webcluster
     bind *:80
     mode http
     use_backend webcluster-host

backend webcluster-host
     balance roundrobin
     server web1 172.25.254.10:80
     server web2 172.25.254.20:80
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------

listen webcluster
     bind *:80
     mode http
     balance roundrobin
     server web1 172.25.254.10:80
     server web2 172.25.254.20:80

6.重启服务

[root@haproxy ~]# systemctl restart haproxy.service 

7.测试

webserver1 - 172.25.254.10
[root@client ~]# curl 172.25.254.100
webserver2 - 172.25.254.20
[root@client ~]# curl 172.25.254.100
webserver1 - 172.25.254.10
[root@client ~]# curl 172.25.254.100
webserver2 - 172.25.254.20
[root@client ~]# curl 172.25.254.100
webserver1 - 172.25.254.10
[root@client ~]# curl 172.25.254.100
webserver2 - 172.25.254.20
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值