Haproxy实验总结

一、haproxy的基本部署实验:

1、环境准备:

准备三台虚拟机:haproxy(172.25.254.100)、webserver1(172.25.254.10)、webserver2(172.25.254.20)

防火墙,selinux关闭

2、haproxy部分:

[root@haproxy ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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 forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:0c:29:cc:d6:59 brd ff:ff:ff:ff:ff:ff
    altname enp3s0
    altname ens160
    inet 172.25.254.100/24 brd 172.25.254.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::e9d9:e029:7f5a:84bf/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

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

[root@haproxy ~]# dnf install haproxy -y
Complete!
[root@haproxy ~]# rpm -qc haproxy 
/etc/haproxy/haproxy.cfg
/etc/logrotate.d/haproxy
/etc/sysconfig/haproxy
[root@haproxy ~]# 
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 
[root@haproxy ~]# systemctl enable haproxy.service 
Created symlink /etc/systemd/system/multi-user.target.wants/haproxy.service → /usr/lib/systemd/system/haproxy.service.
[root@haproxy ~]# systemctl restart haproxy.service 
[root@haproxy ~]# curl 172.25.254.100
webserver1 - 172.25.254.10
[root@haproxy ~]# curl 172.25.254.100
webserver2 - 172.25.254.20
[root@haproxy ~]# curl 172.25.254.100
webserver1 - 172.25.254.10

3、haproxy配置文件:vim /etc/haproxy/haproxy.cfg:

4、webserver1部分: 

[root@webserver1 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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 forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:0c:29:03:5f:47 brd ff:ff:ff:ff:ff:ff
    altname enp3s0
    altname ens160
    inet 172.25.254.10/24 brd 172.25.254.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::b947:4cf:357d:b67e/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@webserver1 ~]# dnf install nginx -y
Complete!
[root@webserver1 ~]# echo webserver1 - 172.25.254.10 > /usr/share/nginx/html/index.html
[root@webserver1 ~]# systemctl enable --now nginx.service 
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.

5、webserver2部分: 

[root@webserver2 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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 forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:0c:29:f6:d1:9e brd ff:ff:ff:ff:ff:ff
    altname enp3s0
    altname ens160
    inet 172.25.254.20/24 brd 172.25.254.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::d9bf:66c4:33ab:9efa/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@webserver2 ~]# dnf install nginx -y
Complete!
[root@webserver2 ~]# echo webserver2 - 172.25.254.20 > /usr/share/nginx/html/index.html
[root@webserver2 ~]# systemctl enable --now nginx.service 
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.

6、实验测试: 

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

二、haproxy的多进程与多线程实验: 

1、多进程实验:

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 
[root@haproxy ~]# systemctl restart haproxy.service 
[root@haproxy ~]# pstree -p | grep haproxy
           |-haproxy(1586)-+-haproxy(1588)
           |               `-haproxy(1589)

 

2、多线程实验: 

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 
[root@haproxy ~]# systemctl restart haproxy.service 
[root@haproxy ~]# pstree -p | grep haproxy
           |-haproxy(1569)---haproxy(1571)---{haproxy}(1572)

三、haproxy的global配置实验:

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 
[root@haproxy ~]# systemctl restart haproxy.service 
[root@haproxy ~]# vim /etc/rsyslog.conf 
[root@haproxy ~]# ll /var/log/haproxy.log 
-rw------- 1 root root 5436 Aug  7 16:34 /var/log/haproxy.log

 四、haproxy的proxies配置实验

 1、环境准备:

准备三台虚拟机:haproxy(172.25.254.100)、webserver1(172.25.254.10)、webserver2(172.25.254.20)

防火墙,selinux关闭

2、haproxy 部分:

[root@haproxy ~]# dnf install httpd -y
[root@haproxy ~]# vim /etc/httpd/conf/httpd.conf 		
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 			
[root@haproxy ~]# systemctl restart haproxy.service 
 
[root@haproxy ~]# curl 172.25.254.100
<html><body><h1>503 Service Unavailable</h1>
No server is available to handle this request.
</body></html>
[root@haproxy ~]# echo sorry, this is down > /var/www/html/index.html
[root@haproxy ~]# systemctl restart httpd
[root@haproxy ~]# curl 172.25.254.100
sorry, this is down
 
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
[root@haproxy ~]# systemctl restart haproxy.service 
[root@haproxy ~]# curl 172.25.254.100
sorry, this is down

[root@haproxy ~]# curl 172.25.254.100
webserver2 - 172.25.254.20
[root@haproxy ~]# curl 172.25.254.100
webserver2 - 172.25.254.20
[root@haproxy ~]# curl 172.25.254.100
webserver2 - 172.25.254.20
 
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 
[root@haproxy ~]# systemctl restart haproxy.service 
[root@haproxy ~]# curl 172.25.254.100
webserver1 - 172.25.254.10
[root@haproxy ~]# curl 172.25.254.100
webserver1 - 172.25.254.10
[root@haproxy ~]# curl 172.25.254.100
webserver2 - 172.25.254.20
[root@haproxy ~]# curl 172.25.254.100
webserver1 - 172.25.254.10
[root@haproxy ~]# curl 172.25.254.100
webserver1 - 172.25.254.10
[root@haproxy ~]# curl 172.25.254.100
webserver2 - 172.25.254.20
 
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 
[root@haproxy ~]# systemctl restart haproxy.service
 
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 
[root@haproxy ~]# systemctl restart haproxy.service 

3、webserver1 部分: 

[root@webserver1 ~]# systemctl stop nginx.service 
[root@webserver1 ~]# systemctl start nginx.service 
[root@webserver1 ~]# while true; do curl 172.25.254.100; sleep 0.1; done
webserver1 - 172.25.254.10
webserver1 - 172.25.254.10
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
webserver1 - 172.25.254.10
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
webserver1 - 172.25.254.10
...

4、webserver2 部分 : 

[root@webserver2 ~]# systemctl stop nginx.service 
[root@webserver2 ~]# systemctl start nginx.service 
[root@webserver2 ~]# while true
> do
> curl 172.25.254.100
> sleep 0.1
> done
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
webserver1 - 172.25.254.10
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
webserver1 - 172.25.254.10
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
...

5、实验网页测试:

五、 cookie实验和IP透传七层、四层实验

1、cookie实验:

(1)实验配置:
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 
listen webcluster
bind*:80
mode http
balance roundrobin
hash-type consistent
cookie WEBCOOKIE insert nocache indi rect			
server webl 172.25.254.10:80 cookie testl check inter 2 fall 3 rise 5 weight 1
server web2 172.25.254.20:80 cookie test2 check inter 2 fall 3 rise 5 weight 1
server web_sorry 172.25.254.100:8080 backup
 
[root@haproxy ~]# systemctl restart haproxy.service 
(2)实验测试: 

 

2、IP透传七层实验:

(1)实验配置:

(2)实验测试:

 3、IP透传四层实验:

(1)实验配置:

 

(2)实验测试:

六、haproxy对MYSQL的四层负载

1、环境准备(webserver1和webserver2)

[root@webserver1 ~]# dnf install mariadb-server -y
[root@webserver1 ~]# vim /etc/my.cnf.d/mariadb-server.cnf 
[root@webserver1 ~]# systemctl start mariadb.service 


[root@webserver2 ~]# dnf install mariadb-server -y
[root@webserver2 ~]# vim /etc/my.cnf.d/mariadb-server.cnf 
[root@webserver2 ~]# systemctl start mariadb.service 

2、haproxy部分: 

3、webserver1部分:

4、webserver2部分:

 5、实验测试:

 

七、haproxy的https实验

1、实验配置:

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 
[root@haproxy ~]# systemctl restart haproxy.service 
[root@haproxy ~]# netstat -antlupe | grep haproxy
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      0          5834670    4458/haproxy        
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      0          5834671    4458/haproxy        
udp        0      0 0.0.0.0:39695           0.0.0.0:*                           991        5834708    4458/haproxy        
udp        0      0 0.0.0.0:41817           0.0.0.0:*                           991        5835929    4458/haproxy        
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 
[root@haproxy ~]# systemctl restart haproxy.service 

2、实验测试:

  • 5
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值