1. 程式人生 > >compose 到 Kubernetes 的遷移工具_Kubernetes中文社群

compose 到 Kubernetes 的遷移工具_Kubernetes中文社群

在 skippbox,我們開發了 Kompose 這一工具,他能夠自動把 Docker Compose 應用轉換為 Kubernetes 描述檔案。利用一個簡單的 kompose up 命令,就可以在 Kubernetes 叢集上啟動 Compose 應用。我們非常樂於將其捐獻給 Kubernetes Incubator。下面介紹一下這一工具的開發動機和用法。

Docker 給了開發者以巨大的幫助。讓每個人都能夠從 Docker Registry 啟動一個打包好的 Docker 應用。為了對付多容器應用, Docker 開發了 Docker-compose (也就是 Compose)。Compose 藉助 yaml 格式的描述檔案來定義一個多容器應用,然後就可以用一個簡單的 docker-compose up來啟動這一應用中的多個容器。然而,Compose 只能夠在本地或者 Docker Swarm 叢集中執行。

那如果我們需要在 Swarm 之外執行怎麼辦?比如 Kubernetes?

Compose 格式並非為分散式而誕生的。所以,你只能為你選擇的容器編排工具重新編寫應用描述檔案。

我們利用 Kompose,能夠簡單的完成將應用從 Docker Swarm 到 Kubernetes 的轉換過程,這樣就為 Docker 使用者敞開了 Kubernetes 的大門。

今年夏天,來自紅帽子的 Tomas Kral 和 Suraj Deshmukh,以及來自 Google 的 Janet Kuo,他們和 Kompose 的主要開發者 Nguyen An-Tu 一起為 Kompose 錦上添花。我們把 Kompose 提交給 Kubernets Incubator,得到了 Kubernetes 社群的支援,現在可以在 Kubernetes Incubator 找到 Kompose。

Kompose 目前支援 Docker-compose v2 格式,最近還加入了持久卷所有權(PVC)、以及多容器 Pod 的支援。除了預設的 Kubernetes 之外,我們還支援 Openshift 的釋出能力。Kompose 現在還出現在了 Fedora 包中,未來也會進入 CentOS 中去。

Kompose 是一個 Golang 應用,可以從 Github 上獲取。下面讓我們跳過 Build 環節直接進入例項。

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 檔案啟動的。

20161228141423

以上給讀者快速的介紹了一下 kompose。還有很多激動人心的特性,例如建立不同型別的資源、建立 Helm Chars,甚至可以使用試驗性的 Docker bundle 格式進行輸入(Lachlan Evenson 的部落格:using a Docker bundle with Kubernetes)。可以在我們的 KubeCon 上的視訊 中看到完整的演示。

前往 Kubernetes incubator 獲取 Kompose,可以幫助你輕鬆地把應用從 Docker Compose 遷移為 Kubernetes 叢集應用。