随便准备了个配置文件,关于配置文件如何写,还得看其余相关文章javascript
#--------------------------------------------------------------------- # 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 # # 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 # chroot /var/lib/haproxy pidfile /var/run/haproxy.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 log global option httplog option dontlognull option http-server-close option forwardfor except 127.0.0.0/8 option redispatch retries 3 timeout http-request 10s 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 #--------------------------------------------------------------------- #此处配置HAProxy的监听端口 # 2条访问控制列表,一个是若是以/static等开头的,都属于url_static这个列表 # 另外一个是若是以.jpg等结尾的,也属于url_static这个列表 # 若是要使用static这个名字的backend,那要符合url_static,这就是倒数第二句话的意思 # 默认的backend使用名字叫app的backend frontend main bind :5000 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 default_backend app #--------------------------------------------------------------------- # static backend for serving up images, stylesheets and such #--------------------------------------------------------------------- # 定义一个backend,名字叫static,用来搭配上面的frontend backend static balance roundrobin server static 127.0.0.1:4331 check #--------------------------------------------------------------------- # round robin balancing between the various backends #--------------------------------------------------------------------- # 定义一个backend,名字叫app,用来搭配上面的frontend # 这个app是session轮询的 # 将cookie名字为WEBSRV插入,返回给客户端 # 定义一个server叫app1,它的cookie的key就是WEBSRV,值就是app1 # 3台轮询,一旦肯定某一台后,之后全部请求都会转发到那一台,session持久保留 backend app balance roundrobin cookie WEBSRV insert nocache indirect server app1 10.1.58.216:4001 check cookie app1 server app2 10.1.58.216:4002 check cookie app2 server app3 10.1.58.216:4003 check cookie app3
先cd到有上述配置文件的目录,而后运行下面命令,用来检查配置文件是否合法css
docker run -it --rm --name haproxy-syntax-check \ -v ${PWD}/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg \ haproxy haproxy -c -f /usr/local/etc/haproxy/haproxy.cfg
如发现有不合法的部分,会[alert]出来,根据提示修正配置文件
配置文件经过则提示 Configuration file is valid
java
而后运行一下,先使用-it使之在前台运行,若是有运行时的错误会提示你,而且haproxy会崩溃退出
另外端口号5000是搭配配置文件里的bind :5000
的,根据实际状况改写web
docker run -it --name haproxy-running \ -p "5000:5000" \ -v ${PWD}/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg \ haproxy
没有问题后,切换到后台运行redis
# 先杀掉前面运行的 docker rm -f haproxy-running # 从新运行,将-it改成-d docker run -d --name haproxy-running \ -p "5000:5000" \ -v ${PWD}/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg \ haproxy
在浏览器里访问ip+5000端口能够看到被代理的画面,被代理的服务器从配置文件上能够看出是10.1.58.216:4001-4003docker