码迷,mamicode.com
首页 > 其他好文 > 详细

Docker Compose实践

时间:2018-08-05 16:49:14      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:direct   add   host   http服务   soc   proxy   ==   links   handle   

目录

项目说明

这是一个典型的web项目,由一个haproxy容器加三个web容器组成。haproxy在前端充当负载均衡器,反向代理到后台三个服务服务。

基本目录结构

首先创建一个compose-haproxy-web的目录,然后在目录里面,创建两个子目录:haproxy和web。

在web目录里包含三个文件: Dockerfile、index.py、index.html

在haproxy目录里包含一个文件: haproxy.cfg

目录结构如下:

compose-haproxy-web/
├── docker-compose.yml
├── haproxy
│   └── haproxy.cfg
└── web
    ├── Dockerfile
    ├── index.html
    └── index.py

2 directories, 5 files

web目录下的index.py提供一个简单的http服务,打印出访问者的ip和实际的本地IP,内容如下:

#!/usr/bin/python
#authors: yeasy.github.com
#date: 2013-07-05
import sys
import BaseHTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler
import socket
import fcntl
import struct
import pickle
from datetime import datetime
from collections import OrderedDict

class HandlerClass(SimpleHTTPRequestHandler):
  def get_ip_address(self,ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    return socket.inet_ntoa(fcntl.ioctl(
      s.fileno(),
      0x8915, # SIOCGIFADDR
      struct.pack(‘256s‘, ifname[:15])
    )[20:24])
  def log_message(self, format, *args):
    if len(args) < 3 or "200" not in args[1]:
      return
    try:
      request = pickle.load(open("pickle_data.txt","r"))
    except:
      request=OrderedDict()
    time_now = datetime.now()
    ts = time_now.strftime(‘%Y-%m-%d %H:%M:%S‘)
    server = self.get_ip_address(‘eth0‘)
    host=self.address_string()
    addr_pair = (host,server)
    if addr_pair not in request:
      request[addr_pair]=[1,ts]
    else:
      num = request[addr_pair][0]+1
      del request[addr_pair]
      request[addr_pair]=[num,ts]
    file=open("index.html", "w")
    file.write("<!DOCTYPE html> <html> <body><center><h1><font color=\"blue\" face=\"Georgia, Arial\" size=8><em>HA</em></font> Webpage Visit Results</h1></center>");
    for pair in request:
      if pair[0] == host:
        guest = "LOCAL: "+pair[0]
      else:
        guest = pair[0]
      if (time_now-datetime.strptime(request[pair][1],‘%Y-%m-%d %H:%M:%S‘)).seconds < 3:
        file.write("<p style=\"font-size:150%\" >#"+ str(request[pair][1]) +": <font color=\"red\">"+str(request[pair][0])+ "</font> requests " + "from &lt<font color=\"blue\">"+guest+"</font>&gt to WebServer &lt<font color=\"blue\">"+pair[1]+"</font>&gt</p>")
      else:
        file.write("<p style=\"font-size:150%\" >#"+ str(request[pair][1]) +": <font color=\"maroon\">"+str(request[pair][0])+ "</font> requests " + "from &lt<font color=\"navy\">"+guest+"</font>&gt to WebServer &lt<font color=\"navy\">"+pair[1]+"</font>&gt</p>")
    file.write("</body> </html>");
    file.close()
    pickle.dump(request,open("pickle_data.txt","w"))

if __name__ == ‘__main__‘:
  try:
    ServerClass = BaseHTTPServer.HTTPServer
    Protocol = "HTTP/1.0"
    addr = len(sys.argv) < 2 and "0.0.0.0" or sys.argv[1]
    port = len(sys.argv) < 3 and 80 or int(sys.argv[2])
    HandlerClass.protocol_version = Protocol
    httpd = ServerClass((addr, port), HandlerClass)
    sa = httpd.socket.getsockname()
    print "Serving HTTP on", sa[0], "port", sa[1], "..."
    httpd.serve_forever()
  except:
    exit()

web目录下index.html文件是一个空文件,在程序启动之后会用到。

web目录下Dockerfile内容如下:

FROM python:2.7
WORKDIR /code
ADD . /code
EXPOSE 80
CMD python index.py

haproxy目录下的haproxy.cfg内容如下:

global
    log 127.0.0.1 local0
    log 127.0.0.1 local1 notice

defaults
    log global
    mode http
    option httplog
    option dontlognull
    timeout connect 5000ms
    timeout client 50000ms
    timeout server 50000ms

listen stats
    bind 0.0.0.0:70
    stats enable
    stats uri /

frontend balancer
    bind 0.0.0.0:80
    mode http
    default_backend web_backends

backend web_backends
    mode http
    option forwardfor
    balance roundrobin
    server weba weba:80 check
    server webb webb:80 check
    server webc webc:80 check
    option httpchk GET /
    http-check expect status 200

配置docker-compose

docker-compose.yml内容如下:

version: ‘3‘
services:
  weba:
    build: ./web
    expose:
    - 80
  
  webb:
    build: ./web
    expose:
    - 80

  webc:
    build: ./web
    expose:
    - 80

  haproxy:
    image: haproxy:latest
    volumes:
    - ./haproxy:/haproxy-override
    - ./haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
    links:
    - weba
    - webb
    - webc
    ports:
    - "80:80"
    - "70:70"
    expose:
    - "80"
    - "70"

启动docker-compose:

docker-compose up -d

Docker Compose实践

标签:direct   add   host   http服务   soc   proxy   ==   links   handle   

原文地址:https://www.cnblogs.com/breezey/p/9426085.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!