创建两个网站
先使用docker
创建两个php
网站。
- 创建目录
~/Program/test
- 添加文件
~/Program/test/index1.php
server11111
- 添加文件
~/Program/test/index2.php
server22222
- 创建容器
docker run -d -v ~/Program/test/index1.php:/webwww/public/index.php --name web1 -p 8081:80 ssslocal/nginx-php7
docker run -d -v ~/Program/test/index2.php:/webwww/public/index.php --name web2 -p 8082:80 ssslocal/nginx-php7
- 测试配置结果
$ curl localhost:8081 [23:57:29]
server11111
$ curl localhost:8082 [23:57:34]
server22222
配置haproxy反向代理集群
- 创建配置文件
~/Program/test/haproxy.cfg
#---------------------------------------------------------------------
# Example configuration for a possible web application. See the
# full configuration options online.
#
# http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global #全局配置文件
# to have these messages end up in /var/log/haproxy.log you will
# need to: #配置日志
#
# 1) configure syslog to accept network log events. This is done
# by adding the '-r' option to the SYSLOGD_OPTIONS in
# /etc/sysconfig/syslog #修改syslog配置文件
#
# 2) configure local2 events to go to the /var/log/haproxy.log
# file. A line like the following can be added to
# /etc/sysconfig/syslog #定义日志设备
#
# local2.* /var/log/haproxy.log
#
#log 127.0.0.1 local2 #日志配置,所有的日志都记录本地,通过local2输出
#chroot /var/lib/haproxy #改变haproxy的工作目录
#pidfile /var/run/haproxy.pid #指定pid文件的路径
maxconn 4000 #最大连接数的设定
#user haproxy #指定运行服务的用户
#group haproxy #指定运行服务的用户组
daemon
# turn on stats unix socket
#stats socket /var/lib/haproxy/stats
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http #默认使用协议,可以为{http|tcp|health} http:是七层协议 tcp:是四层 health:只返回OK
log global #全局日志记录
option httplog #详细记录http日志
option dontlognull #不记录空日志
option http-server-close #启用http-server-close
option forwardfor except 127.0.0.0/8 #来自这些信息的都不forwardfor
option redispatch #重新分发,ServerID对应的服务器宕机后,强制定向到其他运行正常的服务器
retries 3 #3次连接失败则认为服务不可用
timeout http-request 10s #默认http请求超时时间
timeout queue 1m #默认队列超时时间
timeout connect 10s #默认连接超时时间
timeout client 1m #默认客户端超时时间
timeout server 1m #默认服务器超时时间
timeout http-keep-alive 10s #默认持久连接超时时间
timeout check 10s #默认检查时间间隔
maxconn 3000 #最大连接数
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend web_80
bind :80
#定义ACL规则以如".html"结尾的文件;-i:忽略大小写
acl url_static path_beg -i /static /images /javascript /stylesheets
acl url_static path_end -i .jpg .gif .png .css .js
use_backend static if url_static #调用后端服务器并检查ACL规则是否被匹配
default_backend web #客户端访问时默认调用后端服务器地址池
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static #定义后端服务器
balance roundrobin #定义算法;基于权重进行轮询
server static 127.0.0.1:4331 check check #启动对后端server的健康状态检测
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend web
balance roundrobin
server web1 web1:80 check
server web2 web2:80 check
- 创建容器
docker run --name haproxy -e LANG=en_US.UTF-8 -p 80:80 -v ~/Program/test/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro --link web1:web1 --link web2:web2 --restart=always -d haproxy
- 预期的结果
$ curl localhost [23:58:30]
server11111
$ curl localhost [23:58:34]
server22222
可以看到同一个请求,访问到了不同的服务器。
- 可用性测试
# 先暂停web1
$ docker stop web1 [23:22:40]
web1
$ curl localhost [23:22:46]
server22222
$ curl localhost [23:23:00]
server22222
$ curl localhost [23:23:06]
server22222
# 开启web1 关闭web2
$ docker start web1 [23:23:14]
web1
$ docker stop web2 [23:23:57]
web2
$ curl localhost [23:24:03]
server11111
$ curl localhost [23:24:17]
server11111
可以观察到, 当一台web服务挂掉时, 自动访问到了另外一台。
haproxy
是通过健康监测来判断,服务是否可用.
总结
通过haproxy, 实现了集群部署, 可以通过添加服务器来增强网站的处理能力,
当其中一些服务器停止服务,并不会导致网站不可用,
但是, 如果haproxy
停止服务,那网站就会无法访问了。
网友评论