使用HAproxy搭建web群集原来这么简单(宇宙真的好谦虚啊,明明拥有一切,却叫太空)(二)

简介: 使用HAproxy搭建web群集原来这么简单(宇宙真的好谦虚啊,明明拥有一切,却叫太空)(二)

七、部署负载均衡web群集


主机

操作系统

IP地址

haproxy服务器 centos7

192.168.109.131

nginx服务器1

centos7

192.168.109.134

nginx服务器2

centos7

192.168.109.135

客户端

Windows7

192.168.109.200

关闭所有机子防火墙,将包传入haproxy服务器/opt下

systemctl stop firewalld
systemctl disable firewalld
setenforce 0
haproxy-1.5.19.tar.gz
#所需软件包
链接:https://pan.baidu.com/s/1TMPMVWCrvsyh6p6zAn1AGA
提取码:hde6

7.1 nginx节点服务器(两台相同)

#yum安装 nginx
vim /etc/yum.repos.d/nginx.repo
    [nginx-stable]
    name=nginx stable repo
    baseurl=http://nginx.org/packages/centos/7/$basearch/
    gpgcheck=0
    enabled=1
yum install nginx -y 
systemctl start nginx
systemctl enable nginx
#写入不同的网页内容以便测试的时候区分服务器
#服务器1
[root@localhost ~]# cd /usr/share/nginx/html/
[root@localhost html]# ls
50x.html  index.html
[root@localhost html]# echo "<h1>nginx test1</h1>" >index.html 
#服务器2
[root@localhost ~]# cd /usr/share/nginx/html/
[root@localhost html]# ls
50x.html  index.html
[root@localhost html]# echo "<h1>nginx test2</h1>" >index.html




7.2 haproxy服务器配置

[root@localhost haproxy-1.5.19]# ls
CHANGELOG     doc       haproxy                  LICENSE   ROADMAP  tests
contrib       ebtree    haproxy-systemd-wrapper  Makefile  src      VERDATE
CONTRIBUTING  examples  include                  README    SUBVERS  VERSION
[root@localhost haproxy-1.5.19]# cd examples/
[root@localhost examples]# mkdir /etc/haproxy
[root@localhost examples]# cp haproxy.cfg /etc/haproxy/
[root@localhost examples]# cd /etc/haproxy/
[root@localhost haproxy]# ls
haproxy.cfg
[root@localhost haproxy]# vim haproxy.cfg 
#修改后的文件
# this config needs haproxy-1.1.28 or haproxy-1.2.1
global
        log /dev/log   local0 info
        log /dev/log   local0 notice
        #log loghost    local0 info
        maxconn 4096
        #chroot /usr/share/haproxy
        uid 99
        gid 99
        daemon
        nbproc 1
        #debug
        #quiet
defaults
        log     global
        mode    http
        option  httplog
        option  dontlognull
        retries 3
        redispatch
        maxconn 2000
        #contimeout     5000
        #clitimeout     50000
        #srvtimeout     50000
        timeout http-request 10s
        timeout queue 1m
        timeout connect 10s
        timeout client 1m
        timeout server 1m
        timeout http-keep-alive 10s
        timeout check 10s
listen  nginx_cluster  0.0.0.0:80
        option httpchk GET /index.html
        balance roundrobin
        server  inst1 192.168.109.134:80 check inter 2000  fall 3
        server  inst2 192.168.109.135:80 check inter 2000  fall 3



7.3 添加haproxy系统服务

#将haproxy.init 启动文件拷贝到/etc/init.d中并改名
[root@localhost haproxy]# cd /opt/haproxy-1.5.19/examples/
[root@localhost examples]# cp haproxy.init /etc/init.d/haproxy
#添加执行权限
[root@localhost examples]# cd /etc/init.d/
[root@localhost init.d]# ls
functions  haproxy  netconsole  network  README
[root@localhost init.d]# chmod +x haproxy
#添加到chkconfig中管理
[root@localhost init.d]# chkconfig --add /etc/init.d/haproxy 
cd
#优化路径
[root@localhost init.d]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
#开启服务
[root@localhost init.d]# service haproxy start


7.4 客户端测试



八、Haproxy集群日志定义


默认haproxy的日志是输出到系统的syslog中(/var/log/messages),查看起来不是非常方便,为了更好的管理haproxy的日志,我们在生产环境中一般单独定义出来。需要将haproxy的info及notice日志分别记录到不同的日志文件中。

vim /etc/haproxy/haproxy.cfg   #修改配置文件
global
        log /dev/log    local0 info
        log /dev/log    local0 notice
service haproxy start


需要修改rsyslog配置,为了便于管理。将haproxy相关的配置独立定义到haproxy.conf,并放到/etc/rsyslog.d/下,rsyslog启动时会自动加载此目录下的所有配置文件

vim /etc/rsyslog.d/haproxy.conf
if ($programname == 'haproxy' and $syslogseverity-text == 'info')
then -/var/log/haproxy/haproxy-info.log
&~
if ($programname == 'haproxy' and $syslogseverity-text == 'notice')
then -/var/log/haproxy/haproxy-notice.log
&~
#说明:这部分配置是将haproxy的info日志文件记录到/var/log/haproxy/haproxy-info.log下,将notice日志记录到/var/log/haproxy/haproxy-notice.log下。
&~表示当日志写到日志文件后,rsyslog停止处理这个信息
systemctl restart rsyslog.service 
tail -f /var/log/haproxy/haproxy-info.log #查看haproxy的请求日志信息






总结


haproxy的使用还是比较简单的,比lvs的配置简单很多

相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
目录
相关文章
|
8月前
|
负载均衡 网络协议 应用服务中间件
Haproxy搭建Web群集
Haproxy搭建Web群集
70 0
|
8天前
|
负载均衡 算法 前端开发
Haproxy搭建Web群集
这只是一个基本的HAProxy配置示例,你可以根据需要进行更高级的配置,包括SSL终端、健康检查、ACL(Access Control List)等。确保HAProxy的配置文件中的语法正确,以避免错误。在生产环境中,还应该考虑安全性和高可用性等因素。
27 7
|
6月前
|
负载均衡 监控 算法
百度搜索:蓝易云【HAProxy搭建web集群教程。】
这是一个简单的HAProxy搭建Web集群的教程。在实际应用中,还可以进行更多的配置和优化,以满足你的需求。建议在搭建之前查阅官方文档或参考其他详细的教程以获取更全面的指导。
88 3
百度搜索:蓝易云【HAProxy搭建web集群教程。】
|
9月前
|
应用服务中间件 Apache 开发工具
|
11月前
|
负载均衡 算法 应用服务中间件
案例:使用keepalived+Haproxy搭建Web群集
案例:使用keepalived+Haproxy搭建Web群集
|
10天前
|
关系型数据库 MySQL
web简易开发(二){html5+php实现文件上传及通过关键字搜索已上传图片)}
web简易开发(二){html5+php实现文件上传及通过关键字搜索已上传图片)}
|
18小时前
|
安全 测试技术 持续交付
在Python Web开发中,测试是一个至关重要的环节
【5月更文挑战第12天】在Python Web开发中,测试至关重要,包括单元测试(unittest模块)、集成测试、功能测试、系统测试、验收测试、性能测试、安全测试和端到端测试。常用的测试工具有unittest、pytest、selenium、requests和coverage。遵循“测试先行”和“持续集成”原则,确保代码质量与稳定性。
8 3
|
1天前
|
编解码 数据库 计算机视觉
LabVIEW开发基于Web数字图像处理
LabVIEW开发基于Web数字图像处理
|
4天前
|
前端开发 JavaScript Java
Java与Web开发的结合:JSP与Servlet
Java与Web开发的结合:JSP与Servlet
8 0
|
4天前
|
存储 程序员 API
python web开发示例详解
python web开发示例详解
13 0