haproxy+httpd网站架构,实现负载均衡实验笔记

发布于:2025-02-10 ⋅ 阅读:(36) ⋅ 点赞:(0)

前提准备:

  1. 两台httpd,一台haproxy,NFS部署在任意一台httpd上
  2. http1:192.168.180.110
  3. http2:192.168.180.120,NFS
  4. haproxy:192.168.180.100

http(两台httpd的操作是一样的):

1. 安装httpd

yum install -y httpd

2. 分别编写网页

echo "server1 192.168.180.110" > /var/www/html/index.html
echo "server2 192.168.180.120" > /var/www/html/index.html

3. 开启httpd

systemctl start httpd

4. 关闭防火墙

systemctl stop firewalld
setenforce 0

haproxy:

1. 安装haproxy(通过源码包安装)

yum install -y gcc gcc-c++ make lrzsz
tar zxf haproxy-2.9.9.tar.gz
cd haproxy-2.9.9
make TARGET=linux-glibc && make install

2. 移动主配置文件

mkdir /etc/haproxy
cp addons/ot/test/sa/haproxy.cfg /etc/haproxy/

3. 修改主配置文件

vim /etc/haproxy/haproxy.cfg
将端口改为8080

注释

添加内容
frontend http_front
        bind *:80
                 default_backend servers-backend

backend servers-backend
    mode http
    server inst1 192.168.180.110:80 check inter 80 fall 3
    server inst2 192.168.180.120:80 check inter 80 fall 3 backup

4. 创建自启动脚本

cp ~/haproxy-2.9.9/examples/haproxy.init /etc/init.d/haproxy
ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
chmod +x /etc/init.d/haproxy
chkconfig --add /etc/init.d/haproxy
/etc/init.d/haproxy start

5. 关闭防火墙

systemctl stop firewalld
setenforce 0

NFS-192.168.180.120:

1. 在httpd上均安装

yum install -y nfs-utils rpcbind

2. 创建共享目录

mkdir -p /opt/wwwroot
vim /etc/exports
/opt/wwwroot    192.168.180.0/24(rw,sync,no_root_squash)

3. 分别启动

systemctl start nfs
systemctl start rpcbind

4. 查看NFS共享了什么目录

showmount -e 192.168.180.120

5. http均挂载NFS共享目录

mount 192.168.180.120:/opt/wwwroot /var/www/html/

6. 创建测试页面

echo "http-NFS" > /opt/wwwroot/index.html

7. 访问测试


网站公告

今日签到

点亮在社区的每一天
去签到