Docker 讓每個人都能夠從 Docker Registry 啟動一個打包好的 Docker 應用。Docker-Compose在Docker基礎上解決了多容器應用之間的依賴啟動問題。
Docker Compose 借助 yaml 格式的描述文件來定義一個多容器應用,然后就可以用一個簡單的 docker-compose up來啟動這一應用中的多個容器。然而,Compose 只能夠在本地或者 Docker Swarm 集群中運行。
如果我們需要在 Swarm 之外運行怎么辦?比如 Kubernetes?Compose 格式並非為分布式而誕生的,所以我們只能為選擇的容器編排工具重新編寫應用描述文件。
現在,在 Kubernetes Incubator 可以找到 Kompose。有了 Kompose,我們能夠簡單實現從 Docker Swarm 到 Kubernetes 的轉換過程,這樣就為 Docker 用戶敞開了 Kubernetes 的大門。
Kompose 目前支持 Docker-compose v2 格式,最近還加入了持久卷所有權(PVC)、以及多容器 Pod 的支持。除了缺省的 Kubernetes 之外,我們還支持 Openshift 的發布能力。Kompose 現在還出現在了 Fedora 包中,未來也會進入 CentOS 中去。
Kompose 是一個 Golang 應用,可以從 Github 上獲取。下面讓我們跳過 Build 環節直接進入實例。
Kompose工具能夠自動把 Docker Compose 應用轉換為 Kubernetes 描述文件。利用簡單的 kompose up 命令,可以在 Kubernetes 集群上啟動 Compose 應用。
Docker 的留言板應用
留言板應用是 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 主節點;
- 一組能夠橫向擴展並借助 DNS 找到 Master 的 Redis 從節點;
- 暴露於 80 端口的 PHP 前端。
這些組合在一起,讓用戶可以發表留言,並保存在 Redis 集群中。
要啟動這個應用:
$ 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 的留言板應用
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 <none> 80/TCP 1m redis-master 10.0.0.219 <none> 6379/TCP 1m redis-slave 10.0.0.84 <none> 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 集群應用。