原做者: Sebastien Goasguenhtml
原文: Kompose: a tool to go from Docker-compose to Kubernetes前端
Docker 给了开发者以巨大的帮助。让每一个人都可以从 Docker Registry 启动一个打包好的 Docker 应用。为了对付多容器应用, Docker 开发了 Docker-compose (也就是 Compose)。Compose 借助 yaml 格式的描述文件来定义一个多容器应用,而后就能够用一个简单的 docker-compose up来启动这一应用中的多个容器。然而,Compose 只可以在本地或者 Docker Swarm 集群中运行。linux
那若是咱们须要在 Swarm 以外运行怎么办?好比 Kubernetes?Compose 格式并不是为分布式而诞生的。因此,你只能为你选择的容器编排工具从新编写应用描述文件。redis
咱们利用 Kompose,可以简单的完成将应用从 Docker Swarm 到 Kubernetes 的转换过程,这样就为 Docker 用户敞开了 Kubernetes 的大门。docker
今年夏天,来自红帽的 Tomas Kral 和 Suraj Deshmukh,以及来自 Google 的 Janet Kuo,他们和 Kompose 的主要开发者 Nguyen An-Tu 一块儿为 Kompose 锦上添花。咱们把 Kompose 提交给 Kubernets Incubator,获得了 Kubernetes 社区的支持,如今能够在 Kubernetes Incubator 找到 Kompose。app
Kompose 目前支持 Docker-compose v2 格式,最近还加入了持久卷全部权(PVC)、以及多容器 Pod 的支持。除了缺省的 Kubernetes 以外,咱们还支持 Openshift 的发布能力。Kompose 如今还出如今了 Fedora 包中,将来也会进入 CentOS 中去。Kompose 是一个 Golang 应用,能够从 Github 上获取。下面让咱们跳过 Build 环节直接进入实例。frontend
留言板应用是 Kubernetes 的权威示例。若是要用 Docker Compose 来实现留言板,能够用下面的代码:分布式
version: "2" services: redis-master: image: gcr.io/google_containers/redis:e2e ports: - "6379" redis-slave: image: gcr.io/google_samples/gb-redisslave:v1 ports: - "6379" environment: - GET_HOSTS_FROM=dns frontend: image: gcr.io/google-samples/gb-frontend:v4 ports: - "80:80" environment: - GET_HOSTS_FROM=dns
其中包含了三个服务:工具
这些组合在一块儿,让用户能够发表留言,并保存在 Redis 集群中。ui
要启动这个应用:
$ docker-compose -f docker-guestbook.yml up -d Creating network "examples_default" with the default driver Creating examples_redis-slave_1 Creating examples_frontend_1 Creating examples_redis-master_1
这就是一个简单的 Docker 用法,下面我肯看看如何在不重写任何东西的状况下,让这些工做在 Kubernetes 上完成。
Kompose 目前有三个主要的命令:up、down 以及 convert。为了行文方便,咱们只简单说一下留言吧应用的启动。
跟 docker-compose 相似,咱们能够用 kompose up 命令处理 Docker compose 文件,来启动应用:
$ kompose -f ./examples/docker-guestbook.yml up We are going to create Kubernetes deployment and service for your dockerized application. If you need more kind of controllers, use 'kompose convert' and 'kubectl create -f' instead. INFO[0000] Successfully created service: redis-master INFO[0000] Successfully created service: redis-slave INFO[0000] Successfully created service: frontend INFO[0000] Successfully created deployment: redis-master INFO[0000] Successfully created deployment: redis-slave INFO[0000] Successfully created deployment: frontend Application has been deployed to Kubernetes. You can run 'kubectl get deployment,svc' for details.
Kompose 自动把 Docker-compose 文件转为 Kuberntes 对象。缺省状况下,他会为一个 Compose 服务建立一个 Deployment 以及一个服务。另外还能自动检测当前的 Kuberntes 端点,并在上面建立对象。能够经过一系列的选项来建立 Replication Controller、Replica Set 或者 Daemon Set。
就这样完成了自动转换,若是你了解一些 Kubernetes 的话,能够用 kubectl 命令来看看集群上运行的留言板。
$ kubectl get pods,svc,deployments NAME READY STATUS RESTARTS AGE frontend-3780173733-0ayyx 1/1 Running 0 1m redis-master-3028862641-8miqn 1/1 Running 0 1m redis-slave-3788432149-t3ejp 1/1 Running 0 1m NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE frontend 10.0.0.34 80/TCP 1m redis-master 10.0.0.219 6379/TCP 1m redis-slave 10.0.0.84 6379/TCP 1m NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE frontend 1 1 1 1 1m redis-master 1 1 1 1 1m redis-slave 1 1 1 1 1m
看到了三个服务、三个 Deployment 以及三个 Pod。能够经过 frontend 服务来访问留言板应用。只不过此次的留言板,是从 Docker-Compose 文件启动的。
以上给读者快速的介绍了一下 kompose。还有不少激动人心的特性,例如建立不一样类型的资源、建立 Helm Chars,甚至可使用试验性的 Docker bundle 格式进行输入(Lachlan Evenson 的博客:using a Docker bundle with Kubernetes)。能够在咱们的 KubeCon 上的视频 中看到完整的演示。
前往 Kubernetes incubator 获取 Kompose,能够帮助你轻松地把应用从 Docker Compose 迁移为 Kubernetes 集群应用。