Centos7部署HAproxy实现Nginx群集
前言
Haproxy是一个使用C语言编写的自由及开放代码软件,其提供高可用性、负载均衡,以及基于TCP和HTTP的应用程序代理,可以运行于大部分主流的Linux操作系统上。
本次实验用到四台服务器,一台服务器安装haproxy实现调度,另三台服务器搭建nginx提供web服务,一台客户机测试访问。
环境介绍
主机 | 系统 | ip地址 | 功能 |
HAproxy | Centos7 | 192.168.128.130 | haproxy |
node1 | Centos7 | 192.168.128.131 | Openresty |
node2 | Centos7 | 192.168.128.132 | Openresty |
node3 | Centos7 | 192.168.128.133 | Openresty |
Client | Windows10 | 192.168.3.2 | chrome |
nginx部署
PS:由于有3台nginx web服务器,我这边使用ansible统一安装了。
wget https://openresty.org/download/openresty-1.13.6.1.tar.gz
tar -zxvf openresty-1.13.6.1.tar.gz
cd openresty-1.13.6.1.tar
useradd -m www
yum -y install gcc gcc-c++ zlib-devel pcre-devel openssl-devel
./configure --prefix=/usr/local --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module
gmake && gmake install
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx
测试一下nginx安装是否正确
[root@node1 html]# curl -I http://192.168.128.131
HTTP/1.1 200 OK
Server: openresty/1.13.6.1
Date: Mon, 18 Mar 2019 14:06:59 GMT
Content-Type: text/html
Content-Length: 556
Last-Modified: Mon, 18 Mar 2019 13:39:22 GMT
Connection: keep-alive
ETag: "5c8f9f8a-22c"
Accept-Ranges: bytes
[root@node2 openresty-1.13.6.1]# curl -I http://192.168.128.132
HTTP/1.1 200 OK
Server: openresty/1.13.6.1
Date: Mon, 18 Mar 2019 14:07:28 GMT
Content-Type: text/html
Content-Length: 556
Last-Modified: Mon, 18 Mar 2019 13:38:49 GMT
Connection: keep-alive
ETag: "5c8f9f69-22c"
Accept-Ranges: bytes
[root@node3 openresty-1.13.6.1]# curl -I http://192.168.128.133
HTTP/1.1 200 OK
Server: openresty/1.13.6.1
Date: Mon, 18 Mar 2019 14:07:32 GMT
Content-Type: text/html
Content-Length: 556
Last-Modified: Mon, 18 Mar 2019 13:39:08 GMT
Connection: keep-alive
ETag: "5c8f9f7c-22c"
Accept-Ranges: bytes
haproxy部署
nginx部署成功后,我们开始部署haproxy,这里选择1.8.19版本安装。
编译安装
wget http://www.haproxy.org/download/1.8/src/haproxy-1.8.19.tar.gz
tar -zxvf haproxy-1.8.19.tar.gz
cd haproxy-1.8.19
make TARGET=linux310 ARCH=x86_64 PREFIX=/usr/local/haproxy
make install PREFIX=/usr/local/haproxy
[root@haproxy conf]# /usr/local/haproxy/sbin/haproxy -v
HA-Proxy version 1.8.19 2019/02/11
Copyright 2000-2019 Willy Tarreau <willy@haproxy.org>
- TARGET=linux310,内核版本,使用uname-r查看内核,如:3.10.0-514.el7,此时该参数就为linux310;kernel 大于2.6.28的可以用:TARGET=linux2628;
- ARCH=x86_64,系统位数;
- PREFIX=/usr/local/haprpxy #/usr/local/haprpxy,为haprpxy安装路径。
配置
由于新版本的haproxy已经没有在examples目录里面保存配置文件了,我就招了一个老版本的配置文件来使用。
添加配置文件
global
log 127.0.0.1 local0 #日志
maxconn 1000 # 最大连接数
daemon
defaults
log global # 采用全局定义的日志
mode http # 默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK
option httplog # 日志类型为http日志
option dontlognull # 不记录健康检查的日志信息
retries 3 # 3次连接失败就认为服务不可用,也可以在下面设置
timeout connect 5000 # 连接超时时间
timeout client 50000 # 客户端连接超时时间
timeout server 50000 # 服务器连接超时时间
listen admin_stats
bind 0.0.0.0:1080 # 监听端口
mode http
option httplog
maxconn 10
stats refresh 30s # 统计页面刷新时间
stats uri /stats # 统计页面url
stats realm XingCloud\ Haproxy # 统计页面密码框的提示文本
stats auth admin:admin # 统计页面的验证信息
stats auth Frank:Frank
stats hide-version
stats admin if TRUE
listen webcluster
bind 0.0.0.0:80
option httpchk GET /index.html
balance roundrobin # 负载均衡模式轮询
server inst1 192.168.128.131:80 check inter 2000 fall 3 # 定义后端服务器,每2000毫秒检查一次
server inst2 192.168.128.132:80 check inter 2000 fall 3
server inst3 192.168.128.133:80 check inter 2000 fall 3
处理日志
默认haproxy不带日志,我们需要rsyslog来配合生成日志,如果你的系统中没有rsyslog,可以使用yum 安装一下。
在/etc/rsyslog.conf最末尾添加如下两行:
# haproxy.log
local0.* /var/log/haproxy.log
local3.* /var/log/haproxy.log
修改/etc/sysconfig/rsyslog 文件中配置为如下配置:
# SYSLOGD_OPTIONS=""
SYSLOGD_OPTIONS="-r -m 0"
重启rsyslog
systemctl restart rsyslog
测试
- 上一篇: ELK日志系统 - Logstash篇
- 下一篇: 解决TeamViewer连接要等待伙伴确认