基於docker的haproxy

準備配置文件

隨便準備了個配置文件,關於配置文件如何寫,還得看其餘相關文章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 validjava

運行時檢驗 & 運行

而後運行一下,先使用-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

相關文章
相關標籤/搜索