码迷,mamicode.com
首页 > 其他好文 > 详细

haproxy的负载均衡

时间:2019-01-08 16:41:10      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:pcr   systemctl   use   cluster   部分   http   提示信息   linux   tar.gz   

开启三台虚拟机:
192.168.80.103
192.168.80.104
192.168.80.105


在80.103里:
systemctl stop firewalld
setenforce 0
yum install lrz* -y
把haproxy拉入Xshell
tar xf haproxy-1.5.15.tar.gz -C /opt/ //解压包并且放入文件中
cd /opt/haproxy-1.5.15/
源码编译安装haproxy
yum 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/haproxy
mkdir /etc/haproxy
useradd -s /sbin/nologin -M haproxy
id 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/haproxy
vi /etc/init.d/haproxy
chmod +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 //启动haproxy
netstat -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 5
server web02 192.168.80.105:80 check inter 2000 fall 5
注:
后端服务器 web1 和 web2 ,IP 地址分别为 192.168.80.10 和 192.168.80.20
check:对当前server做健康状态检测
inter <delay>:检测之间的时间间隔,默认为2000ms
fall <count>:连续多少次检测结果为“失败”才标记为不可用;默认为3
rise <count>:连续多少次检测结果为“成功”才标记为可用;默认为2

service haproxy start //开启haproxy
netstat -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 "<h1>server aa</h1>" > index.html
systemctl 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 "<h1>server bb</h1>" > index.html
systemctl start httpd

在网页输入192.168.80.105
技术分享图片

haproxy的负载均衡

标签:pcr   systemctl   use   cluster   部分   http   提示信息   linux   tar.gz   

原文地址:http://blog.51cto.com/14158297/2340065

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!