linux—haproxy实现负载均衡

一.实验环境

haproxy服务器:server1:172.25.254.1/24

后端服务器:server2:172.25.254.2/24,server3:172.25.254.3

安装包:haproxy-1.6.11.tar.gz

所有服务器都关闭iptables与selinux

二.实验步骤

1.后端服务器开启httpd服务

yum install httpd -y   ##安装httpd

cd /var/www/html

vim index.html     ##编写发布内容

/etc/init.d/httpd start   ##开启httpd服务

检测:后端服务器的httpd服务开启成功

  

2.安装haproxy

[root@server1 ~]# yum install rpm-build -y     ##安装rpm编译软件
[root@server1 ~]# ls
anaconda-ks.cfg        install.log         rpmbuild
haproxy-1.6.11.tar.gz  install.log.syslog
[root@server1 ~]# rpmbuild -tb haproxy-1.6.11.tar.gz    ##编译安装包
error: Failed build dependencies:
	pcre-devel is needed by haproxy-1.6.11-1.x86_64
[root@server1 ~]# yum install pcre-devel -y            ##解决依赖性
[root@server1 ~]# rpmbuild -tb haproxy-1.6.11.tar.gz 
[root@server1 ~]# yum install gcc -y                   ##解决依赖性
[root@server1 ~]# rpmbuild -tb haproxy-1.6.11.tar.gz   ##编译安装包
[root@server1 ~]# ls
anaconda-ks.cfg        install.log         rpmbuild
haproxy-1.6.11.tar.gz  install.log.syslog
[root@server1 ~]# cd rpmbuild/         
[root@server1 rpmbuild]# ls
BUILD  BUILDROOT  RPMS  SOURCES  SPECS  SRPMS
[root@server1 rpmbuild]# cd RPMS/
[root@server1 RPMS]# ls
x86_64
[root@server1 RPMS]# cd x86_64/
[root@server1 x86_64]# ls
haproxy-1.6.11-1.x86_64.rpm           ##这是最终要下载的安装包
[root@server1 x86_64]# rpm -qpl haproxy-1.6.11-1.x86_64.rpm     ##查看安装后的文件
/etc/haproxy
/etc/rc.d/init.d/haproxy
/usr/sbin/haproxy
/usr/share/doc/haproxy-1.6.11
/usr/share/doc/haproxy-1.6.11/CHANGELOG
/usr/share/doc/haproxy-1.6.11/README
/usr/share/doc/haproxy-1.6.11/architecture.txt
/usr/share/doc/haproxy-1.6.11/configuration.txt
/usr/share/doc/haproxy-1.6.11/intro.txt
/usr/share/doc/haproxy-1.6.11/management.txt
/usr/share/doc/haproxy-1.6.11/proxy-protocol.txt
/usr/share/man/man1/haproxy.1.gz
[root@server1 x86_64]# rpm -ivh haproxy-1.6.11-1.x86_64.rpm     ##安装haproxy
Preparing...                                                   ########################################### [100%]
   1:haproxy                                                   ########################################### [100%]

 

3.配置haproxy,设置负载均衡

[root@server1 x86_64]# cd
[root@server1 ~]# ls
anaconda-ks.cfg        install.log         rpmbuild
haproxy-1.6.11.tar.gz  install.log.syslog
[root@server1 ~]# tar zxf haproxy-1.6.11.tar.gz    ##解压压缩包,压缩包在网上下载
[root@server1 ~]# ls
anaconda-ks.cfg  haproxy-1.6.11.tar.gz  install.log.syslog
haproxy-1.6.11   install.log            rpmbuild
[root@server1 ~]# cd haproxy-1.6.11
[root@server1 haproxy-1.6.11]# cd examples/
[root@server1 examples]# 
[root@server1 examples]# ls
acl-content-sw.cfg     debug2html    init.haproxy
auth.cfg               debugfind     option-http_proxy.cfg
check                  errorfiles    seamless_reload.txt
check.conf             haproxy.init  ssl.cfg
content-sw-sample.cfg  haproxy.spec  stats_haproxy.sh
debug2ansi             haproxy.vim   transparent_proxy.cfg
[root@server1 examples]# vim /etc/init.d/haproxy     ##查看启动脚本文件

 

 

因为启动脚本文件中指示默认读取的配置文件名称为/etc/haproxy/haproxy.cfg,所以之后要将配置模板拷贝为指定名称

[root@server1 examples]# cp content-sw-sample.cfg /etc/haproxy/haproxy.cfg
[root@server1 haproxy]# groupadd -g 200 haproxy    ##为haproxy创建指定用户
[root@server1 haproxy]# useradd -u 200 -g 200 haproxy
[root@server1 haproxy]# id haproxy
uid=200(haproxy) gid=200(haproxy) groups=200(haproxy)
[root@server1 haproxy]# vim /etc/security/limits.conf   ##修改系统限定文件

[root@server1 haproxy]# ls
haproxy.cfg
[root@server1 haproxy]# vim haproxy.cfg  
 10 global
 11         maxconn         10000    ##最大连接数
 12         stats socket    /var/run/haproxy.stat mode 600 level admin  
 13         log             127.0.0.1 local0   ##日志
 14         uid             200     ##默认用户uid
 15         gid             200     ##默认用户组gid
 16         chroot          /var/empty   ##安全设置,根目录切换。锁在/var/empty
 17         daemon
 18 defaults
 19         mode            http
 20         log             global
 21         option          httplog
 22         option          dontlognull
 23         monitor-uri     /monitoruri
 24         maxconn         8000
 25         timeout client  30s
 26 
 27         stats uri       /admin/stats
 28         option  prefer-last-server
 29         retries         2
 30         timeout connect 5s
 31         timeout server  5s
 32 # The public 'www' address in the DMZ
 33 frontend public
 34         bind             *:80 name clear
 35         #bind            192.168.1.10:443 ssl crt /etc/haproxy/haproxy.pem
 36         #use_backend     static if { hdr_beg(host) -i img }
 37         #use_backend     static if { path_beg /img /css   }
 38         default_backend  static    ##默认访问static
 39 
 40 # The static backend backend for 'Host: img', /img and /css.
 41 backend static
 42         balance         roundrobin  ##轮询算法
 43         server          statsrv1 172.25.254.2:80 check inter 1000   ##realserver1,默认1000浩渺检查一次
 44         server          statsrv2 172.25.254.3:80 check inter 1000   ##realserver2

4.开启服务

[root@server1 haproxy]# /etc/init.d/haproxy start

检测:

此时访问haproxy服务器,真实主机server2与server3轮询工作

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值