Draft

Draft是微软Deis团队开源(见https://github.com/azure/draft)的容器应用开发辅助工具,它可以帮助开发人员简化容器应用程序的开发流程。

Draft主要由三个命令组成

  • draft init:初始化docker registry账号,并在Kubernetes集群中部署draftd(负责镜像构建、将镜像推送到docker registry以及部署应用等)
  • draft create:draft根据packs检测应用的开发语言,并自动生成Dockerfile和Kubernetes Helm Charts
  • draft up:根据Dockfile构建镜像,并使用Helm将应用部署到Kubernetes集群(支持本地或远端集群)。同时,还会在本地启动一个draft client,监控代码变化,并将更新过的代码推送给draftd。

Draft安装

由于Draft需要构建镜像并部署应用到Kubernetes集群,因而在安装Draft之前需要

  • 部署一个Kubernetes集群,部署方法可以参考kubernetes部署方法
  • 安装并初始化helm(需要v2.4.x版本,并且不要忘记运行helm init),具体步骤可以参考helm使用方法
  • 注册docker registry账号,比如Docker HubQuay.io
  • 配置Ingress Controller并在DNS中设置通配符域*的A记录(如*.draft.example.com)到Ingress IP地址。最简单的Ingress Controller创建方式是使用helm:
  1. # 部署nginx ingress controller
  2. $ helm install stable/nginx-ingress --namespace=kube-system --name=nginx-ingress
  3. # 等待ingress controller配置完成,并记下外网IP
  4. $ kubectl --namespace kube-system get services -w nginx-ingress-nginx-ingress-controller

[info] minikube Ingress Controller

minikube中配置和使用Ingress Controller的方法可以参考这里

初始化好Kubernetes集群和Helm后,可以在这里下载draft二进制文件,并配置draft

  1. # 注意修改用户名、密码和邮件
  2. $ token=$(echo '{"username":"feisky","password":"secret","email":"feisky@email.com"}' | base64)
  3. # 注意修改registry.org和basedomain
  4. $ draft init --set registry.url=docker.io,registry.org=feisky,registry.authtoken=${token},basedomain=app.feisky.xyz

Draft入门

draft源码中提供了很多应用的示例,我们来看一下怎么用draft来简化python应用的开发流程。

  1. $ git clone https://github.com/Azure/draft.git
  2. $ cd draft/examples/python
  3. $ ls
  4. app.py requirements.txt
  5. $ cat requirements.txt
  6. flask
  7. $ cat app.py
  8. from flask import Flask
  9. app = Flask(__name__)
  10. @app.route('/')
  11. def hello_world():
  12. return "Hello, World!\n"
  13. if __name__ == '__main__':
  14. app.run(host='0.0.0.0', port=8080)

Draft create生成Dockerfile和chart

  1. $ draft create
  2. --> Python app detected
  3. --> Ready to sail
  4. $ ls
  5. Dockerfile app.py chart draft.toml requirements.txt
  6. $ cat Dockerfile
  7. FROM python:onbuild
  8. EXPOSE 8080
  9. ENTRYPOINT ["python"]
  10. CMD ["app.py"]
  11. $ cat draft.toml
  12. [environments]
  13. [environments.development]
  14. name = "virulent-sheep"
  15. namespace = "default"
  16. watch = true
  17. watch_delay = 2

Draft Up构建镜像并部署应用

  1. $ draft up
  2. --> Building Dockerfile
  3. Step 1 : FROM python:onbuild
  4. onbuild: Pulling from library/python
  5. 10a267c67f42: Pulling fs layer
  6. ....
  7. Digest: sha256:5178d22192c2b8b4e1140a3bae9021ee0e808d754b4310014745c11f03fcc61b
  8. Status: Downloaded newer image for python:onbuild
  9. # Executing 3 build triggers...
  10. Step 1 : COPY requirements.txt /usr/src/app/
  11. Step 1 : RUN pip install --no-cache-dir -r requirements.txt
  12. ....
  13. Successfully built f742caba47ed
  14. --> Pushing docker.io/feisky/virulent-sheep:de7e97d0d889b4cdb81ae4b972097d759c59e06e
  15. ....
  16. de7e97d0d889b4cdb81ae4b972097d759c59e06e: digest: sha256:7ee10c1a56ced4f854e7934c9d4a1722d331d7e9bf8130c1a01d6adf7aed6238 size: 2840
  17. --> Deploying to Kubernetes
  18. Release "virulent-sheep" does not exist. Installing it now.
  19. --> Status: DEPLOYED
  20. --> Notes:
  21. http://virulent-sheep.app.feisky.xyzto access your application
  22. Watching local files for changes...

打开一个新的shell,就可以通过子域名来访问应用了

  1. $ curl virulent-sheep.app.feisky.xyz
  2. Hello, World!