准备工作:
需要安装docker,本次docker用于安装httpd,用于模拟两台服务器。
最终用于验证haproxy是否安装成功
centos stream 8安装docker的教程地址:
https://blog.csdn.net/qq_39009944/article/details/123632823?spm=1001.2014.3001.5501
1.docker安装httpd
[root@localhost ~]# docker pull httpd:2.4.37-alpine
2.4.37-alpine: Pulling from library/httpd
cd784148e348: Pull complete
3c1a9ba32ed5: Pull complete
ad2301f3cf66: Pull complete
bdb52bc2ac77: Pull complete
d5b4d0f09975: Pull complete
Digest: sha256:b48b1fd7d417477f69b6cba46fd7c83a7644ebbff600c0a89e3a95b1ab2cef7e
Status: Downloaded newer image for httpd:2.4.37-alpine
docker.io/library/httpd:2.4.37-alpine
1.docker运行两个httpd
[root@localhost ~]# docker run --name web1 -d httpd:2.4.37-alpine
6e205fe7a310ca93423da22db5ab4d175cb624f5fc0814433f71a85fab312cbb
[root@localhost ~]# docker run --name web2 -d httpd:2.4.37-alpine
71709b22733db3eefc593be56c4387b2f8fea6e47d919ee8c7917b3a4611857b
2.查看两个httpd模拟服务器的ip,用于后面测试haproxy的反向代理
[root@localhost ~]# docker exec -it web1 /bin/bash
bash-4.4# ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:AC:11:00:02
inet addr:172.17.0.2 Bcast:172.17.255.255 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:18 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1532 (1.4 KiB) TX bytes:0 (0.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
[root@localhost ~]# docker exec -it web1 /bin/bash
bash-4.4# ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:AC:11:00:02
inet addr:172.17.0.2 Bcast:172.17.255.255 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:18 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1532 (1.4 KiB) TX bytes:0 (0.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
bash-4.4# cd htdocs/
bash-4.4# ls
index.html
bash-4.4# echo 'web1' > index.html
bash-exit
[root@localhost ~]# docker exec -it web2 /bin/bash
bash-4.4# ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:AC:11:00:03
inet addr:172.17.0.3 Bcast:172.17.255.255 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:12 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:936 (936.0 B) TX bytes:0 (0.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
/usr/local/apache2
bash-4.4# cd htdocs/
bash-4.4# ls
index.html
bash-4.4# echo 'web2' > index.html
bash-4.4# exit
3.访问容器中的两个站点
[root@localhost ~]# curl 172.17.0.2
web1
[root@localhost ~]# curl 172.17.0.3
web2
4.安装haproxy
[root@localhost ~]# yum -y install haproxy
5.配置haproxy的配置文件
[root@localhost ~]# cd /etc/haproxy
[root@localhost haproxy]# ls
haproxy.cfg
[root@localhost haproxy]# cp haproxy.cfg haproxy.cfg.bk
[root@localhost haproxy]# vi haproxy.cfg
6.配置haproxy 主机的80端口来代理 两台站点的url
frontend myweb
bind *:80
default_backend websrvs
backend websrvs
balance roundrobin
server web1 172.17.0.2:80 check
server web2 172.17.0.3:80 check
7.启动haproxy
[root@localhost haproxy]# systemctl start haproxy
[root@localhost haproxy]# systemctl status haproxy
● haproxy.service - HAProxy Load Balancer
Loaded: loaded (/usr/lib/systemd/system/haproxy.service; disabled; vendor preset: disabled)
Active: active (running) since Mon 2022-03-21 14:05:53 CST; 5s ago
Process: 4892 ExecStartPre=/usr/sbin/haproxy -f $CONFIG -c -q $OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 4894 (haproxy)
Tasks: 2 (limit: 23634)
Memory: 2.4M
CGroup: /system.slice/haproxy.service
├─4894 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid
└─4897 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid
Mar 21 14:05:53 localhost.localdomain systemd[1]: Starting HAProxy Load Balancer...
Mar 21 14:05:53 localhost.localdomain systemd[1]: Started HAProxy Load Balancer.
[root@localhost haproxy]# netstat -nptl | grep -w 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4897/haproxy
7.验证haproxy是否代理成功,这里我就不在用其他虚拟机来curl了,直接在windows 的cmd窗口里面验证了
C:\Users\admin>curl 192.168.0.120
web1
C:\Users\admin>curl 192.168.0.120
web2