使用云服务器五分钟部署高可用利器haproxy
2020-04-28 11:50:38 63
这期来讲一下如何使用云服务器部署haproxy软件的实战,翻看往期有一篇关于轻量化实现web高可用的简单介绍,读者朋友有兴趣的话也可以阅读下。
软件安装
安装haproxy
使用yum安装或者源码装都可以,这里我们使用源码安装为例,首先安装必要的库包。实际我们在使用的过程中,由于后期可能您的网站会集成https的功能,因此在编译安装的时候,可以选择ssl模块的加入,一部到位解决后续的证书支持的功能。
yum install -y gcc glibc gcc-c++ make openssl openssl-devel readline-devel pcre-devel libssl-dev libpcre3
make TARGET=linux2628 USE_OPENSSL=1 ADDLIB=-lz PREFIX=/usr/local/haproxy
make install PREFIX=/usr/local/haproxy
cd /usr/local
wget http://download.openpkg.org/components/cache/haproxy/haproxy-1.8.5.tar.gz
tar -zxvf haproxy-1.8.5.tar.gz
cd haproxy-1.8.5
make TARGET=linux2628 USE_OPENSSL=1 ADDLIB=-lz PREFIX=/usr/local/haproxy
make install PREFIX=/usr/local/haproxy
haproxy配置
[root@haproxy_master ~]# cat /etc/haproxy/haproxy.cfg
global
log 127.0.0.1:514 local6
pidfile /var/run/haproxy.pid
maxconn 65535
user haproxy
group haproxy
daemon #以后台程序运行;
nbproc 8
tune.ssl.default-dh-param 2048
defaults
mode http
log global
option httplog
option dontlognull
option httpclose
option dontlognull
option forwardfor except 127.0.0.0/8
option redispatch
option http-server-close
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 30m
timeout http-keep-alive 10s
timeout check 10s
maxconn 65535
balance source
# contimeout 300
# clitimeout 300
# srvtimeout 600
#管理员的页面配置账号密码
listen stats
mode http
bind 0.0.0.0:1111 # 统计页面绑定1090端口;
stats enable # 开启统计页面功能;
stats hide-version # 隐藏Haproxy版本号;
stats uri /haproxyhtml # 自定义统计页面的访问uri;
stats realm Haproxy\ Statistics # 统计页面密码验证时的提示信息;
stats auth admin:password # 为统计页面开启登录验证功能;
stats admin if TRUE # 若登录用户验证通过,则赋予管理功能;
#设置协议为http的访问前端
frontend http-in
mode http
maxconn 65535
bind :80
log global
option httplog
option httpclose
option logasap
option dontlognull
option forwardfor header Client-IP
capture request header Host len 64
capture request header User-Agent len 128
capture request header X-Forwarded-For len 100
capture request header Referer len 200
capture response header Server len 40
capture response header Server-ID len 40
acl http_server hdr_beg(host) -i green.test.com
use_backend green.test.com if http_server
#设置协议为https的访问前端
mode http
listen TEST_APP_SSL
bind *:443 ssl crt /usr/local/haproxy/ssl/test.com.pem #证书位置
reqadd X-Forwarded-Proto:\ https
acl https_server hdr_beg(host) -i green.test.com
use_backend green.test.com if https_server
#定义访问后端(https和https共同调度到此)
backend green.test.com
mode http
balance roundrobin
cookie SERVERID insert indirect nocache
option httpclose
option forwardfor
server web01 201.201.201.201:80 weight 1 cookie 3 check inter 2000 rise 2 fall 5
server web02 201.201.201.202:80 weight 1 cookie 3 check inter 2000 rise 2 fall 5
启动haproxy服务
/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
如果查看到haproxy的监听端口已经起来,那就表示成功了。如果无法启动,通常backend字段或者forward字段的配置不正确,注意查看启动时打印的日志即可,一般都可以解决。
其他相关的功能
haproxy也可以支持https证书的功能,笔者在实际的项目中也使用到,以上配置文件也可以查看到。
haproxy可以实现方向代理的功能,在反向代理软件的选择上有可以考虑haproxy方案,部署和配置都是非常方便的。
睿江官网链接:www.eflycloud.com