运维企业实战——HAProxy实现apache负载均衡

实验环境:
server1:haproxy
server2:apache
server3:apache
实验步骤:
1、下载parxy包
2、解压

tar zxf haproxy-1.7.3.tar.gz 

在这里插入图片描述
3、制作rpm包(有.spec文件,就可以把源码制作为rpm包)
(1)、生成rpmbuild的目录

yum install rpm-build  pcre-devel  gcc -y
	rpmbuild -tb haproxy-1.7.3.tar.gz

在这里插入图片描述
(2)、找到.spec文件、

cd haproxy-1.7.3
ls
find . -name *.spec

在这里插入图片描述
(3)、下载haproxy

cd /root/rpmbuild/RPMS/x86_64
rpm -ivh haproxy-1.7.3-1.x86_64.rpm

在这里插入图片描述
4、编辑配置文件

cp /mnt/haproxy-1.7.3/examples/content-sw-sample.cfg /etc/haproxy/haproxy.cfg
cd /etc/haproxy
vim haproxy.cfg

编辑内容:

global					# 全局配置
        maxconn         65535
        stats socket    /var/run/haproxy.stat mode 600 level admin
        log             127.0.0.1 local0	# 日志名local0,需要手动在日志配置文件中配置
        uid             200		#haproxy的用户
        gid             200		#haproxy用户组
        chroot          /var/empty		#切换根目录,安全
        daemon							#后台运行

defaults				#默认配置
        mode            http		
        log             global
        option          httplog
        option          dontlognull
        monitor-uri     /monitoruri
        maxconn         8000
        timeout client  30s			#客户端时间超过30秒超时
        retries         2			#重试两次失败认为服务器不可用
        option redispatch			#当client连接到挂掉的服务器后,会切换到健康的主机
        timeout connect 5s			#haproxy将请求转发给后端服务器等待时长
        timeout server  5s			#客户端与服务端建立连接,等待服务器的超时时长
        stats uri       /admin/stats	# haproxy监控

# The public 'www' address in the DMZ
frontend public
        bind            *:80 name clear	#haproxy服务器
        #bind            192.168.1.10:443 ssl crt /etc/haproxy/haproxy.pem
        #use_backend     static if { hdr_beg(host) -i img }
        #use_backend     static if { path_beg /img /css   }
        default_backend dynamic		#默认为动态页面

# The static backend backend for 'Host: img', /img and /css.
backend dynamic		#动态页面
        balance         roundrobin		#balance定义为负载均衡算法
        server          web1 172.25.60.2:80 check inter 1000	#指定或短服务器
        server          web2 172.25.60.3:80 check inter 1000

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
5、server2、server3(发布内容)

 /etc/init.d/httpd start
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 172.25.60.2 for ServerName
                                                           [  OK  ]
vim /var/www/html/index.html 

6、测试:

curl 172.25.60.1

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44321029/article/details/89517880