1、haproxy服务
在调度器里下载并开启haproxy.server
dnf install haproxy -y
systemctl enable --now haproxy
修改haproxy的配置文件
vim /etc/haproxy/haproxy.cfg
62 #---------------------------------------------------------------------
63 # main frontend which proxys to the backends
64 #---------------------------------------------------------------------
65 frontend main *:80 #修改端口为80
66 #acl blacklist src 172.25.6.250
67 #block if blacklist
68 #errorloc 403 http://172.25.6.5:8000
69 #acl testfie path /test.html
70 #http-request deny if testfile blacklist
71 #redirect location http://www.baidu.com if backlist
72 #acl url_static path_beg -i /images
73 #acl url_static path_end -i .jpg .gif .png .css .js
74 #acl read_request method GET
75 #acl read_request method HEAD
76 #acl write_request method PUT
77 #acl write_request method POST
78 #use_backend static if read_request
79 #use_backend app if write_request
80 default_backend app
81
82 #---------------------------------------------------------------------
83 # static backend for serving up images, stylesheets and such
84 #---------------------------------------------------------------------
85 #backend static
86 # balance roundrobin
87 # server static 172.25.6.6:80 check
88
89 #---------------------------------------------------------------------
90 # round robin balancing between the various backends
91 #---------------------------------------------------------------------
92 backend app
93 balance roundrobin
94 #balance source
95 #balance static-rr
96 server app1 172.25.7.4:80 check
97 server app2 172.25.6.5:80 check
98 #server backup 127.0.0.1:8000 backup做监控以及用户认证
2、Apache服务
在服务器1里下载Apache并开启Apache
dnf install httpd -y
systemctl enable --now httpd
在服务器2里下载Apache并开启Apache
dnf install httpd -y
systemctl enable --now httpd
在两个服务器的Apache默认发布目录下写index.html文件
echo this is vm3 > /var/www/html/index.html
echo this is vm4 > /var/www/html/index.html
3、真机下访问server2
[root@westos ~]# curl 172.25.7.3
this is vm3
[root@westos ~]# curl 172.25.7.3
this is vm4
[root@westos ~]# curl 172.25.7.3
this is vm3
[root@westos ~]# curl 172.25.7.3
this is vm4
[root@westos ~]# curl 172.25.7.3
this is vm3
[root@westos ~]# curl 172.25.7.3
this is vm4
4、判断负载均衡是否实现
关闭server3的http服务后重新访问server2
显示只访问到server4
[root@westos ~]# curl 172.25.7.3
this is vm4
[root@westos ~]# curl 172.25.7.3
this is vm4
[root@westos ~]# curl 172.25.7.3
this is vm4
[root@westos ~]# curl 172.25.7.3
this is vm4
[root@westos ~]# curl 172.25.7.3
this is vm4
[root@westos ~]# curl 172.25.7.3
this is vm4
5、添加监控
vim /etc/haproxy/haproxy.cfg
59 stats uri /status #添加监控
60 stats auth han:westos #设置用户名为han密码为westos
61 stats refresh 5s #设置为5秒刷新一次监控