千家信息网

haproxy的负载均衡是如何实现的

发表于:2024-09-22 作者:千家信息网编辑
千家信息网最后更新 2024年09月22日,下文给大家带来haproxy的负载均衡是如何实现的,希望能够给大家在实际运用中带来一定的帮助,负载均衡涉及的东西比较多,理论也不多,网上有很多书籍,今天我们就用在行业内累计的经验来做一个解答。开启三台
千家信息网最后更新 2024年09月22日haproxy的负载均衡是如何实现的

下文给大家带来haproxy的负载均衡是如何实现的,希望能够给大家在实际运用中带来一定的帮助,负载均衡涉及的东西比较多,理论也不多,网上有很多书籍,今天我们就用在行业内累计的经验做一个解答。

开启三台虚拟机:192.168.80.103    192.168.80.104   192.168.80.105  

在80.103里:systemctl stop firewalld       //关闭防火墙setenforce 0                        //关闭监控
yum install lrz* -y           //安装上传软件把haproxy包拉入

tar xf haproxy-1.5.15.tar.gz -C /opt/   //解压包并且放入/opt文件中
cd /opt/haproxy-1.5.15/ 源码编译安装haproxyyum install -y \pcre-devel \bzip2-devel \gcc \gcc-c++ \make
uname -r    //内核版本make TARGET=linux26  PREFIX=/usr/local/haproxy     //标识64为系统make install PREFIX=/usr/local/haproxymkdir /etc/haproxyuseradd -s /sbin/nologin -M  haproxyid haproxy

cd /usr/local/haproxy/cd /opt/haproxy-1.5.15/cd examples/cp haproxy.cfg /etc/haproxy/cd /etc/haproxy/

vi haproxy.cfg

修改后:

srvtimeout 50000 后面的内容全删了,再添加下面新的内容:
-----------------------------------------统计页面配置------------------------------------
listen admin_stats #为haproxy访问状态监控页面配置,取名为admin_stats
bind 0.0.0.0:8089 //监听端口
stats enable //启用监听端口
mode http #http的7层模式
log global # 继承global中log的定义
stats uri /stats #监控页面的url访问路径,即http://ip/stats 访问监控页面
stats realm Haproxy\ Statistics #监控页面的密码框提示信息
stats auth admin:admin #监控页面的用户和密码admin,可以设置多个用户名
#stats hide-version //隐藏统计页面上HAProxy的版本信息
stats admin if TRUE //当通过认证才可管理
stats refresh 30s //页面自动刷新时间30s
:wq //保存退出

cd -

cp haproxy.init /etc/init.d/haproxyvi /etc/init.d/haproxychmod +x /etc/init.d/haproxy    //给这个文件设置权限ll /etc/init.d/haproxy

chkconfig --add haproxy      //添加系统服务ln -s /usr/local/haproxy/sbin/haproxy  /usr/sbin/haproxy     //软链接service haproxy start      //启动haproxynetstat -anp | grep haproxy     //查看haproxy服务是否开启

在网页上输入IP地址:8089/stats

service haproxy stop    //关闭haproxy
vi /etc/haproxy/haproxy.cfg在里面添加以下内容:------------------------web设置--------------------------------listen webcluster     #定义webcluster云服务器组。        bind 0.0.0.0:80   #定义haproxy前端部分监听的端口。        mode http    #http的7层模式        option httpchk GET /index.html     #心跳检测        log global    #继承global中log的定义        maxconn 3000   #server进程可接受的最大并发连接数        balance roundrobin    #负载均衡的方式:轮询server web01 192.168.80.104:80 check inter 2000 fall 5server web02 192.168.80.105:80 check inter 2000 fall 5     :wq     //保存退出注:后端服务器 web1 和 web2 ,IP 地址分别为 192.168.80.10 和 192.168.80.20check:对当前server做健康状态检测inter :检测之间的时间间隔,默认为2000msfall :连续多少次检测结果为"失败"才标记为不可用;默认为3rise :连续多少次检测结果为"成功"才标记为可用;默认为2
service haproxy start     //开启haproxynetstat -anp | grep haproxy      //查看haproxy服务是否开启

在网页上输入http://192.168.80103:8089/stats    //统计页面

在网页上输入http://192.168.80103          //负载均衡


在80.104里:systemctl stop firewalld setenforce 0
yum install httpd -y     //安装httpd
vi /etc/httpd/conf/httpd.conf把ServerName www.example.com:80 前面的#去掉
cd /var/www/html/echo "

server aa

" > index.htmlsystemctl start httpd
在网页输入192.168.80.104

在80.105里:systemctl stop firewalld setenforce 0
yum install httpd -y   //安装httpd
vi /etc/httpd/conf/httpd.conf把ServerName www.example.com:80 前面的#去掉
cd /var/www/html/echo "

server bb

" > index.htmlsystemctl start httpd
在网页输入192.168.80.105

看了以上关于haproxy的负载均衡是如何实现的,如果大家还有什么地方需要了解的可以在行业资讯里查找自己感兴趣的或者找我们的专业技术工程师解答的,技术工程师在行业内拥有十几年的经验了。

0