Jusene's Blog

使用Fig编排Docker

字数统计: 1.1k阅读时长: 6 min
2017/09/04 Share

编排

编排是一个没有严格定义的概念,大致描述的就是自动配置,协作和管理服务的过程,在Docker的世界里,编排用来描述一组实践过程,这个过程运行多个多个容器里的应用,通过将这些容器链接起来,快速构建出生产环境与开发环境。

Fig

简单的容器编排,目前Fig团队已经加入了Docker公司,Fig通过配置fig.yml来快速构建基于Docker的复杂应用,容器间通过某些方法指定一些运行的属性来和其他容器的产生交互。

安装Fig

Fig由python编写,所以我门可以通过PYPI源来安装fig。

1
~]# pip install fig

构建应用

  1. 构建镜像

    1
    ~]# mkdir figapp
    2
    ~]# cd figapp
    3
    ~]# vim Dockerfile
    4
    FROM python:latest
    5
    MAINTAINER Jusene
    6
    7
    RUN pip install flask redis
    8
    9
    ~]# docker build -t flask:v1 .
  2. 提供web服务程序

    1
    ~]# vim app.py
    2
    #-*- coding=utf8 -*-
    3
    4
    from flask import Flask
    5
    from redis import Redis
    6
    7
    app=Flask(__app__)
    8
    redis=Redis(host="db",port=6379)
    9
    10
    @app.route('/')
    11
    def hello():
    12
    	redis.incr('hits')
    13
    	return "Hello Docker Reader,You had Hit {0} times!".format(int(redis.get('hits')))
    14
    15
    if __name__ == "__main__":
    16
    	app.run(host="0.0.0.0",debug=True)
  3. 编写fig.yml

    1
    ~]# vim fig.yml
    2
    ---
    3
    web:
    4
    	image: flask:v1
    5
    	command: python /figapp/app.py
    6
    	ports:
    7
    		- "5000:5000"
    8
    	volumes:
    9
    		- .:/figapp
    10
    	links:
    11
    		- redis:db
    12
    redis:
    13
    	image: redis:latest
    14
    	environment:
  4. 启动fig

    1
    ~]# fig up                             注意必须在fig.yml下运行
    2
    Recreating figapp_redis_1...
    3
    Recreating figapp_web_1...
    4
    Attaching to figapp_redis_1, figapp_web_1
    5
    redis_1 | 1:C 31 Aug 16:22:04.209 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
    6
    redis_1 | 1:C 31 Aug 16:22:04.210 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=1, just started
    7
    redis_1 | 1:C 31 Aug 16:22:04.210 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
    8
    redis_1 | 1:M 31 Aug 16:22:04.214 * Running mode=standalone, port=6379.
    9
    redis_1 | 1:M 31 Aug 16:22:04.214 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
    10
    redis_1 | 1:M 31 Aug 16:22:04.214 # Server initialized
    11
    redis_1 | 1:M 31 Aug 16:22:04.214 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
    12
    redis_1 | 1:M 31 Aug 16:22:04.215 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
    13
    redis_1 | 1:M 31 Aug 16:22:04.215 * DB loaded from disk: 0.000 seconds
    14
    redis_1 | 1:M 31 Aug 16:22:04.215 * Ready to accept connections
    15
    web_1   |  * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
    16
    web_1   |  * Restarting with stat
    17
    web_1   |  * Debugger is active!
    18
    web_1   |  * Debugger PIN: 264-973-293
    19
    20
    ~]# fig up -d  #守护模式
  5. 测试应用

    1
    ~]# curl 10.211.55.6:5000
    2
    Hello Docker Reader,You had Hit 1 times!
    3
    ~]# curl 10.211.55.6:5000
    4
    Hello Docker Reader,You had Hit 2 times!

    因为在flask启动的时候启用debug,所以在修改代码后会自动重载代码,一个快速docker开发环境。

  6. 查看docker

    1
    ~]# docker ps
    2
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
    3
    745acf95b6cb        flask               "python /figapp/app.p"   4 minutes ago       Up 4 minutes        0.0.0.0:5000->5000/tcp   figapp_web_1
    4
    96cb74bd5f45        redis:latest        "docker-entrypoint.sh"   4 minutes ago       Up 4 minutes        6379/tcp                 figapp_redis_1
    5
    ~]# fig ps
    6
     Name                   Command               State           Ports          
    7
    --------------------------------------------------------------------------------
    8
    figapp_redis_1   docker-entrypoint.sh redis ...   Up      6379/tcp               
    9
    figapp_web_1     python /figapp/app.py            Up      0.0.0.0:5000->5000/tcp 
    10
    ~]# fig -h
    11
    Fast, isolated development environments using Docker.
    12
    13
    Usage:
    14
      fig [options] [COMMAND] [ARGS...]
    15
      fig -h|--help
    16
    17
    Options:
    18
      --verbose                 Show more output
    19
      --version                 Print version and exit
    20
      -f, --file FILE           Specify an alternate fig file (default: fig.yml)
    21
      -p, --project-name NAME   Specify an alternate project name (default: directory name)
    22
    23
    Commands:
    24
      build     Build or rebuild services
    25
      help      Get help on a command
    26
      kill      Kill containers
    27
      logs      View output from containers
    28
      port      Print the public port for a port binding
    29
      ps        List containers
    30
      pull      Pulls service images
    31
      rm        Remove stopped containers
    32
      run       Run a one-off command
    33
      scale     Set number of containers for a service
    34
      start     Start services
    35
      stop      Stop services
    36
      restart   Restart services
    37
      up        Create and start containers

整个过程类似:

1
~]# docker run -d --name figapp_redis_1 redis:latest
2
~]# docker run -d --name figapp_web_1 -v .:/figapp/ -p 5000:5000 --link redis:db flask:v1 python /figapp/app.py

还有fig.yml可以与Dockfile一起使用来构建应用:

1
~]# vim fig.yml
2
---
3
web:
4
	build: /root/figapp/              #指定Dockerfile所在的目录         
5
	command: python /figapp/app.py
6
	ports:
7
		- "5000:5000"
8
	volumes:
9
		- .:/figapp
10
	links:
11
		- redis:db
12
redis:
13
	image: redis:latest
14
	environment:                       #指定redis的环境变量

Fig官网链接:http://www.fig.sh/index.html

CATALOG
  1. 1. 编排
  2. 2. Fig
    1. 2.1. 安装Fig
    2. 2.2. 构建应用