服务器 linux 运维 详细讲解Haproxy负载均衡部署 - 重启服务后验证
目录
一、搭建环境
1.准备三台虚拟机
2.haproxy安装haproxy-1.7.2
3.建立haproxy配置文件
二、搭建四层负载haproxy
1.修改haproxy配置文件
2.web1、2写访问文件
3.启动服务
4.验证
三、搭载七层负载均衡 (基于四层负载环境的基础)
1.修改haproxy配置文件 拼多多帮助力入口
2.重启服务后验证
3.七层负载配置监控页面(四层负载不支持监控状态)
(1)修改haproxy配置文件
(2)重启服务并验证
一、搭建环境
1.准备三台虚拟机
虚拟机操作系统ip地址软件安装HAproxycentOS7192.168.115.130haproxy-1.7.2web1centOS7192.168.115.131httpd-2.4.6web2centOS7192.168.115.150httpd-2.4.6
2.haproxy安装haproxy-1.7.2
wget https://www.haproxy.org/download/1.7/src/haproxy-1.7.2.tar.gz
#网上下载
tar xf haproxy-1.7.2.tar.gz
#解压
make PREFIX=/usr/local/haproxy TARGET=linux2628
#可能会报失败错误,查询是否安装gcc
make install PREFIX=/usr/local/haproxy
#安装haproxy
3.建立haproxy配置文件
mkdir /etc/haproxy
touch /etc/haproxy/haproxy.cfg
二、搭建四层负载haproxy
1.修改haproxy配置文件
vim /etc/haproxy/haproxy.cfg
global
daemon
maxconn 256
pidfile /var/run/haproxy.pid
defaults
mode tcp
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
frontend http-in
bind *:80
default_backend servers
backend servers
server server1 192.168.115.131:80 maxconn 32
server server2 192.168.115.150:80 maxconn 32
balance roundrobin #轮询
2.web1、2写访问文件
#web1
echo web1 > /var/www/html/index.html
#web2
echo web2 > /var/www/html/index.html
3.启动服务
#haproxy虚拟机启动haproxy服务
systemctl daemon-reload
systemctl start haproxy
#web1、2启动apache服务
systemctl start httpd
4.验证
三、搭载七层负载均衡 (基于四层负载环境的基础)
1.修改haproxy配置文件
global
daemon
maxconn 256
pidfile /var/run/haproxy.pid
defaults
mode http
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
frontend http-in
bind *:80
default_backend servers
backend servers
server server1 192.168.115.131:80 maxconn 32
server server2 192.168.115.150:80 maxconn 32
2.重启服务后验证
3.七层负载配置监控页面(四层负载不支持监控状态)
(1)修改haproxy配置文件
#在haproxy配置文件中写入
listen stats #定义监控页面
bind *:1080 #绑定端口1080
stats refresh 30s #每30秒更新监控数据
stats uri /stats #访问监控页面的uri
stats realm HAProxy\ Stats #监控页面的认证提示
stats auth test:123 #监控页面的用户名和密码
(2)重启服务并验证
systemctl daemon-reload
systemctl restart haproxy