博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
负载均衡器之 Haproxy
阅读量:5970 次
发布时间:2019-06-19

本文共 4298 字,大约阅读时间需要 14 分钟。

1. 编译安装haproxy

官网: http://www.haproxy.org

1.1 下载haproxy

# wget http://www.haproxy.org/download/1.6/src/haproxy-1.6.14.tar.gz

1.2 解压haproxy

# tar xf haproxy-1.6.14.tar.gz

1.3 查看编译方法

# less README下面选自READMETo build haproxy, you have to choose your target OS amongst the following onesand assign it to the TARGET variable :  - linux22     for Linux 2.2  - linux24     for Linux 2.4 and above (default)  - linux24e    for Linux 2.4 with support for a working epoll (> 0.21)  - linux26     for Linux 2.6 and above  - linux2628   for Linux 2.6.28, 3.x, and above (enables splice and tproxy)  - solaris     for Solaris 8 or 10 (others untested)  - freebsd     for FreeBSD 5 to 10 (others untested)  - netbsd      for NetBSD  - osx         for Mac OS/X  - openbsd     for OpenBSD 3.1 and above  - aix51       for AIX 5.1  - aix52       for AIX 5.2  - cygwin      for Cygwin  - generic     for any other OS or version.  - custom      to manually adjust every settingBy default, the DEBUG variable is set to '-g' to enable debug symbols. It isnot wise to disable it on uncommon systems, because it's often the only way toget a complete core when you need one. Otherwise, you can set DEBUG to '-s' tostrip the binary.For example, I use this to build for Solaris 8 :    $ make TARGET=solaris CPU=ultrasparc USE_STATIC_PCRE=1And I build it this way on OpenBSD or FreeBSD :    $ gmake TARGET=freebsd USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1And on a classic Linux with SSL and ZLIB support (eg: Red Hat 5.x) :    $ make TARGET=linux26 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1And on a recent Linux >= 2.6.28 with SSL and ZLIB support :    $ make TARGET=linux2628 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1In order to build a 32-bit binary on an x86_64 Linux system with SSL supportwithout support for compression but when OpenSSL requires ZLIB anyway :    $ make TARGET=linux26 ARCH=i386 USE_OPENSSL=1 ADDLIB=-lz

1.4 安装:

# make TARGET=linux2628 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1安装方法是根据自己内核版本来了,具体查看内核的方法:# uname -a# make install PREFIX=/usr/local/haproxy

2. haproxy最简单的配置

2.1 拷贝配置文件 

# mkdir -p /usr/local/haproxy/conf/# cp -a examples/content-sw-sample.cfg /usr/local/haproxy/conf/# mv /usr/local/haproxy/conf/content-sw-sample.cfg /usr/local/haproxy/conf/http_haproxy.conf

配置配置文件如下

# cat http_haproxy.conf | grep -v "#"global        maxconn         100        log             127.0.0.1 local0        uid             501        gid             501        chroot          /usr/local/haproxy/var        daemonfrontend public        bind            192.168.31.243:81 name clear        mode            http        default_backend dynamic        timeout client  30sbackend dynamic        mode            http        timeout connect 5s        timeout server  30s        timeout queue   30s        balance         roundrobin        server          server_01 192.168.31.243:80 minconn 50 maxconn 500 check inter 1000        server          server_02 192.168.31.150:80 minconn 50 maxconn 500 check inter 1000#

2.2 检查语法

# ./haproxy -f /usr/local/haproxy/conf/http_haproxy.conf -c Configuration file is valid#

2.3 测试访问

# netstat -tulnp | grep haproxytcp        0      0 192.168.31.243:81           0.0.0.0:*                   LISTEN      2243/./haproxy      ## curl -I http://192.168.31.243:81/HTTP/1.1 200 OKServer: nginx/1.14.2Date: Mon, 07 Jan 2019 08:10:13 GMTContent-Type: text/htmlContent-Length: 636Last-Modified: Tue, 18 Dec 2018 06:51:02 GMTETag: "5c1898d6-27c"Accept-Ranges: bytes# curl -I http://192.168.31.243:80HTTP/1.1 200 OKServer: nginx/1.14.2Date: Mon, 07 Jan 2019 08:10:16 GMTContent-Type: text/htmlContent-Length: 636Last-Modified: Tue, 18 Dec 2018 06:51:02 GMTConnection: keep-aliveETag: "5c1898d6-27c"Accept-Ranges: bytes# 

3. 出现的问题和解决的办法

3.1 haproxy 默认log不输出的问题

在haproxy配置文件中,指定了 log 输出为 local0 之后,同时也在 /etc/rsyslog.conf 配置了

local0.*                                                /var/log/haproxy.log

发现还是不行,最后发现UDPServer未开启导致的,

# cat /etc/rsyslog.conf | grep "UDPServerRun"$UDPServerRun 514# UDPServerRun 514 需要打开
验证是否打开# netstat -tulnp | grep 514udp        0      0 0.0.0.0:514                 0.0.0.0:*                               2772/rsyslogd       udp        0      0 :::514                      :::*                                    2772/rsyslogd       #

 

 

 

转载于:https://www.cnblogs.com/wang-li/p/10259821.html

你可能感兴趣的文章
滑动窗口最大值的golang实现
查看>>
会计的思考(17):还原会计报表的企业个性之一
查看>>
java对象初始化顺序的简单验证
查看>>
[CF452E]Three strings
查看>>
获取指定进程所对应的可执行(EXE)文件全路径(代码)
查看>>
ORA-01722:无效数字
查看>>
搭建golang+vscode开发环境
查看>>
C#占位符
查看>>
java面试-JVM调优和参数配置
查看>>
常用的激活函数
查看>>
sqlcmd
查看>>
Excel 已经检测到"XXX.xsl"是SYLK文件,但是不能将其加载的问题
查看>>
(基础篇)PHP获取时间、时间戳的各种格式写法汇总
查看>>
浅析面向对象开发
查看>>
Hystrix降级逻辑中如何获取触发的异常
查看>>
【跃迁之路】【535天】程序员高效学习方法论探索系列(实验阶段292-2018.07.25)...
查看>>
mac系统下git、mysql、nginx、php的环境搭建
查看>>
JavaScript面向对象编程——Array类型
查看>>
让IE兼容background-size的方法_background-size ie下使用
查看>>
中国发布自主开发的域名系统基础软件 “红枫”
查看>>