原做者: 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 集羣應用。