将 Docker Compose 文件转换为 Kubernetes 资源

Kompose 是什么?它是个转换工具,可将 compose(即 Docker Compose)所组装的所有内容 转换成容器编排器(Kubernetes 或 OpenShift)可识别的形式。

更多信息请参考 Kompose 官网 http://kompose.io

准备开始

你必须拥有一个 Kubernetes 的集群,同时你的 Kubernetes 集群必须带有 kubectl 命令行工具。 如果你还没有集群,你可以通过 Minikube 构建一 个你自己的集群,或者你可以使用下面任意一个 Kubernetes 工具构建:

要获知版本信息,请输入 kubectl version.

安装 Kompose

我们有很多种方式安装 Kompose。首选方式是从最新的 GitHub 发布页面下载二进制文件。

Kompose 通过 GitHub 发布,发布周期为三星期。 你可以在 GitHub 发布页面 上看到所有当前版本。

  1. # Linux
  2. curl -L https://github.com/kubernetes/kompose/releases/download/v1.22.0/kompose-linux-amd64 -o kompose
  3. # macOS
  4. curl -L https://github.com/kubernetes/kompose/releases/download/v1.22.0/kompose-darwin-amd64 -o kompose
  5. # Windows
  6. curl -L https://github.com/kubernetes/kompose/releases/download/v1.22.0/kompose-windows-amd64.exe -o kompose.exe
  7. chmod +x kompose
  8. sudo mv ./kompose /usr/local/bin/kompose

或者,你可以下载 tar 包

go get 命令从主分支拉取最新的开发变更的方法安装 Kompose。

  1. go get -u github.com/kubernetes/kompose

Kompose 位于 EPEL CentOS 代码仓库。 如果你还没有安装启用 EPEL 代码仓库, 请运行命令 sudo yum install epel-release

如果你的系统中已经启用了 EPEL, 你就可以像安装其他软件包一样安装 Kompose。

  1. sudo yum -y install kompose

Kompose 位于 Fedora 24、25 和 26 的代码仓库。你可以像安装其他软件包一样安装 Kompose。

  1. sudo dnf -y install kompose

在 macOS 上你可以通过 Homebrew 安装 Kompose 的最新版本:

  1. brew install kompose

使用 Kompose

再需几步,我们就把你从 Docker Compose 带到 Kubernetes。 你只需要一个现有的 docker-compose.yml 文件。

  1. 进入 docker-compose.yml 文件所在的目录。如果没有,请使用下面这个进行测试。

    1. version: "2"
    2. services:
    3. redis-master:
    4. image: k8s.gcr.io/redis:e2e
    5. ports:
    6. - "6379"
    7. redis-slave:
    8. image: gcr.io/google_samples/gb-redisslave:v3
    9. ports:
    10. - "6379"
    11. environment:
    12. - GET_HOSTS_FROM=dns
    13. frontend:
    14. image: gcr.io/google-samples/gb-frontend:v4
    15. ports:
    16. - "80:80"
    17. environment:
    18. - GET_HOSTS_FROM=dns
    19. labels:
    20. kompose.service.type: LoadBalancer
  2. 要将 docker-compose.yml 转换为 kubectl 可用的文件,请运行 kompose convert 命令进行转换,然后运行 kubectl create -f <output file> 进行创建。

    1. kompose convert
    1. INFO Kubernetes file "frontend-service.yaml" created
    2. INFO Kubernetes file "frontend-service.yaml" created
    3. INFO Kubernetes file "frontend-service.yaml" created
    4. INFO Kubernetes file "redis-master-service.yaml" created
    5. INFO Kubernetes file "redis-master-service.yaml" created
    6. INFO Kubernetes file "redis-master-service.yaml" created
    7. INFO Kubernetes file "redis-slave-service.yaml" created
    8. INFO Kubernetes file "redis-slave-service.yaml" created
    9. INFO Kubernetes file "redis-slave-service.yaml" created
    10. INFO Kubernetes file "frontend-deployment.yaml" created
    11. INFO Kubernetes file "frontend-deployment.yaml" created
    12. INFO Kubernetes file "frontend-deployment.yaml" created
    13. INFO Kubernetes file "redis-master-deployment.yaml" created
    14. INFO Kubernetes file "redis-master-deployment.yaml" created
    15. INFO Kubernetes file "redis-master-deployment.yaml" created
    16. INFO Kubernetes file "redis-slave-deployment.yaml" created
    17. INFO Kubernetes file "redis-slave-deployment.yaml" created
    18. INFO Kubernetes file "redis-slave-deployment.yaml" created
    1. kubectl apply -f frontend-service.yaml,redis-master-service.yaml,redis-slave-service.yaml,frontend-deployment.yaml,

    输出类似于:

    1. service/frontend created
    2. service/redis-master created
    3. service/redis-slave created
    4. deployment.apps/frontend created
    5. deployment.apps/redis-master created
    6. deployment.apps/redis-slave created

    你部署的应用在 Kubernetes 中运行起来了。

  3. 访问你的应用

    如果你在开发过程中使用 minikube,请执行:

    1. minikube service frontend

    否则,我们要查看一下你的服务使用了什么 IP!

    1. kubectl describe svc frontend
    1. Name: frontend
    2. Namespace: default
    3. Labels: service=frontend
    4. Selector: service=frontend
    5. Type: LoadBalancer
    6. IP: 10.0.0.183
    7. LoadBalancer Ingress: 192.0.2.89
    8. Port: 80 80/TCP
    9. NodePort: 80 31144/TCP
    10. Endpoints: 172.17.0.4:80
    11. Session Affinity: None
    12. No events.

    如果你使用的是云提供商,你的 IP 将在 LoadBalancer Ingress 字段给出。

    1. curl http://192.0.2.89

用户指南

Kompose 支持两种驱动:OpenShift 和 Kubernetes。 你可以通过全局选项 --provider 选择驱动。如果没有指定, 会将 Kubernetes 作为默认驱动。

kompose convert

Kompose 支持将 V1、V2 和 V3 版本的 Docker Compose 文件转换为 Kubernetes 和 OpenShift 资源对象。

Kubernetes kompose convert 示例

  1. kompose --file docker-voting.yml convert
  1. WARN Unsupported key networks - ignoring
  2. WARN Unsupported key build - ignoring
  3. INFO Kubernetes file "worker-svc.yaml" created
  4. INFO Kubernetes file "db-svc.yaml" created
  5. INFO Kubernetes file "redis-svc.yaml" created
  6. INFO Kubernetes file "result-svc.yaml" created
  7. INFO Kubernetes file "vote-svc.yaml" created
  8. INFO Kubernetes file "redis-deployment.yaml" created
  9. INFO Kubernetes file "result-deployment.yaml" created
  10. INFO Kubernetes file "vote-deployment.yaml" created
  11. INFO Kubernetes file "worker-deployment.yaml" created
  12. INFO Kubernetes file "db-deployment.yaml" created
  1. ls
  1. db-deployment.yaml docker-compose.yml docker-gitlab.yml redis-deployment.yaml result-deployment.yaml vote-deployment.yaml worker-deployment.yaml
  2. db-svc.yaml docker-voting.yml redis-svc.yaml result-svc.yaml vote-svc.yaml worker-svc.yaml

你也可以同时提供多个 docker-compose 文件进行转换:

  1. kompose -f docker-compose.yml -f docker-guestbook.yml convert
  1. INFO Kubernetes file "frontend-service.yaml" created
  2. INFO Kubernetes file "mlbparks-service.yaml" created
  3. INFO Kubernetes file "mongodb-service.yaml" created
  4. INFO Kubernetes file "redis-master-service.yaml" created
  5. INFO Kubernetes file "redis-slave-service.yaml" created
  6. INFO Kubernetes file "frontend-deployment.yaml" created
  7. INFO Kubernetes file "mlbparks-deployment.yaml" created
  8. INFO Kubernetes file "mongodb-deployment.yaml" created
  9. INFO Kubernetes file "mongodb-claim0-persistentvolumeclaim.yaml" created
  10. INFO Kubernetes file "redis-master-deployment.yaml" created
  11. INFO Kubernetes file "redis-slave-deployment.yaml" created
  1. ls
  1. mlbparks-deployment.yaml mongodb-service.yaml redis-slave-service.jsonmlbparks-service.yaml
  2. frontend-deployment.yaml mongodb-claim0-persistentvolumeclaim.yaml redis-master-service.yaml
  3. frontend-service.yaml mongodb-deployment.yaml redis-slave-deployment.yaml
  4. redis-master-deployment.yaml

当提供多个 docker-compose 文件时,配置将会合并。任何通用的配置都将被后续文件覆盖。

OpenShift kompose convert 示例

  1. kompose --provider openshift --file docker-voting.yml convert
  1. WARN [worker] Service cannot be created because of missing port.
  2. INFO OpenShift file "vote-service.yaml" created
  3. INFO OpenShift file "db-service.yaml" created
  4. INFO OpenShift file "redis-service.yaml" created
  5. INFO OpenShift file "result-service.yaml" created
  6. INFO OpenShift file "vote-deploymentconfig.yaml" created
  7. INFO OpenShift file "vote-imagestream.yaml" created
  8. INFO OpenShift file "worker-deploymentconfig.yaml" created
  9. INFO OpenShift file "worker-imagestream.yaml" created
  10. INFO OpenShift file "db-deploymentconfig.yaml" created
  11. INFO OpenShift file "db-imagestream.yaml" created
  12. INFO OpenShift file "redis-deploymentconfig.yaml" created
  13. INFO OpenShift file "redis-imagestream.yaml" created
  14. INFO OpenShift file "result-deploymentconfig.yaml" created
  15. INFO OpenShift file "result-imagestream.yaml" created

kompose 还支持为服务中的构建指令创建 buildconfig。 默认情况下,它使用当前 git 分支的 remote 仓库作为源仓库,使用当前分支作为构建的源分支。 你可以分别使用 --build-repo--build-branch 选项指定不同的源仓库和分支。

  1. kompose --provider openshift --file buildconfig/docker-compose.yml convert
  1. WARN [foo] Service cannot be created because of missing port.
  2. INFO OpenShift Buildconfig using git@github.com:rtnpro/kompose.git::master as source.
  3. INFO OpenShift file "foo-deploymentconfig.yaml" created
  4. INFO OpenShift file "foo-imagestream.yaml" created
  5. INFO OpenShift file "foo-buildconfig.yaml" created

说明: 如果使用 oc create -f 手动推送 Openshift 工件,则需要确保在构建配置工件之前推送 imagestream 工件,以解决 Openshift 的这个问题:https://github.com/openshift/origin/issues/4518

kompose up

Kompose 支持通过 kompose up 直接将你的”复合的(composed)” 应用程序 部署到 Kubernetes 或 OpenShift。

Kubernetes kompose up 示例

  1. kompose --file ./examples/docker-guestbook.yml up
  1. We are going to create Kubernetes deployments and services for your Dockerized application.
  2. If you need different kind of resources, use the 'kompose convert' and 'kubectl create -f' commands instead.
  3. INFO Successfully created service: redis-master
  4. INFO Successfully created service: redis-slave
  5. INFO Successfully created service: frontend
  6. INFO Successfully created deployment: redis-master
  7. INFO Successfully created deployment: redis-slave
  8. INFO Successfully created deployment: frontend
  9. Your application has been deployed to Kubernetes. You can run 'kubectl get deployment,svc,pods' for details.
  1. kubectl get deployment,svc,pods
  1. NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
  2. deployment.extensions/frontend 1 1 1 1 4m
  3. deployment.extensions/redis-master 1 1 1 1 4m
  4. deployment.extensions/redis-slave 1 1 1 1 4m
  5. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  6. service/frontend ClusterIP 10.0.174.12 <none> 80/TCP 4m
  7. service/kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 13d
  8. service/redis-master ClusterIP 10.0.202.43 <none> 6379/TCP 4m
  9. service/redis-slave ClusterIP 10.0.1.85 <none> 6379/TCP 4m
  10. NAME READY STATUS RESTARTS AGE
  11. pod/frontend-2768218532-cs5t5 1/1 Running 0 4m
  12. pod/redis-master-1432129712-63jn8 1/1 Running 0 4m
  13. pod/redis-slave-2504961300-nve7b 1/1 Running 0 4m

说明:

  • 你必须有一个运行正常的 Kubernetes 集群,该集群具有预先配置的 kubectl 上下文。
  • 此操作仅生成 Deployment 和 Service 对象并将其部署到 Kubernetes。 如果需要部署其他不同类型的资源,请使用 kompose convertkubectl create -f 命令。

OpenShift kompose up 示例

  1. kompose --file ./examples/docker-guestbook.yml --provider openshift up
  1. We are going to create OpenShift DeploymentConfigs and Services for your Dockerized application.
  2. If you need different kind of resources, use the 'kompose convert' and 'oc create -f' commands instead.
  3. INFO Successfully created service: redis-slave
  4. INFO Successfully created service: frontend
  5. INFO Successfully created service: redis-master
  6. INFO Successfully created deployment: redis-slave
  7. INFO Successfully created ImageStream: redis-slave
  8. INFO Successfully created deployment: frontend
  9. INFO Successfully created ImageStream: frontend
  10. INFO Successfully created deployment: redis-master
  11. INFO Successfully created ImageStream: redis-master
  12. Your application has been deployed to OpenShift. You can run 'oc get dc,svc,is' for details.
  1. oc get dc,svc,is
  1. NAME REVISION DESIRED CURRENT TRIGGERED BY
  2. dc/frontend 0 1 0 config,image(frontend:v4)
  3. dc/redis-master 0 1 0 config,image(redis-master:e2e)
  4. dc/redis-slave 0 1 0 config,image(redis-slave:v1)
  5. NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  6. svc/frontend 172.30.46.64 <none> 80/TCP 8s
  7. svc/redis-master 172.30.144.56 <none> 6379/TCP 8s
  8. svc/redis-slave 172.30.75.245 <none> 6379/TCP 8s
  9. NAME DOCKER REPO TAGS UPDATED
  10. is/frontend 172.30.12.200:5000/fff/frontend
  11. is/redis-master 172.30.12.200:5000/fff/redis-master
  12. is/redis-slave 172.30.12.200:5000/fff/redis-slave v1

说明:

你必须有一个运行正常的 OpenShift 集群,该集群具有预先配置的 oc 上下文 (oc login)。

kompose down

你一旦将”复合(composed)” 应用部署到 Kubernetes,kompose down 命令将能帮你通过删除 Deployment 和 Service 对象来删除应用。 如果需要删除其他资源,请使用 ‘kubectl’ 命令。

  1. kompose --file docker-guestbook.yml down
  1. INFO Successfully deleted service: redis-master
  2. INFO Successfully deleted deployment: redis-master
  3. INFO Successfully deleted service: redis-slave
  4. INFO Successfully deleted deployment: redis-slave
  5. INFO Successfully deleted service: frontend
  6. INFO Successfully deleted deployment: frontend

说明:

  • 你必须有一个运行正常的 Kubernetes 集群,该集群具有预先配置的 kubectl 上下文。

构建和推送 Docker 镜像

Kompose 支持构建和推送 Docker 镜像。如果 Docker Compose 文件中使用了 build 关键字,你的镜像将会:

  • 使用文档中指定的 image 键自动构建 Docker 镜像
  • 使用本地凭据推送到正确的 Docker 仓库

使用 Docker Compose 文件示例

  1. version: "2"
  2. services:
  3. foo:
  4. build: "./build"
  5. image: docker.io/foo/bar

使用带有 build 键的 kompose up 命令:

  1. kompose up
  1. INFO Build key detected. Attempting to build and push image 'docker.io/foo/bar'
  2. INFO Building image 'docker.io/foo/bar' from directory 'build'
  3. INFO Image 'docker.io/foo/bar' from directory 'build' built successfully
  4. INFO Pushing image 'foo/bar:latest' to registry 'docker.io'
  5. INFO Attempting authentication credentials 'https://index.docker.io/v1/
  6. INFO Successfully pushed image 'foo/bar:latest' to registry 'docker.io'
  7. INFO We are going to create Kubernetes Deployments, Services and PersistentVolumeClaims for your Dockerized application. If you need different kind of resources, use the 'kompose convert' and 'kubectl create -f' commands instead.
  8. INFO Deploying application in "default" namespace
  9. INFO Successfully created Service: foo
  10. INFO Successfully created Deployment: foo
  11. Your application has been deployed to Kubernetes. You can run 'kubectl get deployment,svc,pods,pvc' for details.

要想禁用该功能,或者使用 BuildConfig 中的版本(在 OpenShift 中), 可以通过传递 --build (local|build-config|none) 参数来实现。

  1. # 禁止构造和推送 Docker 镜像
  2. kompose up --build none
  3. # 为 OpenShift 生成 Build Config 工件
  4. kompose up --provider openshift --build build-config

其他转换方式

默认的 kompose 转换会生成 yaml 格式的 Kubernetes DeploymentService 对象。 你可以选择通过 -j 参数生成 json 格式的对象。 你也可以替换生成 Replication Controllers 对象、 Daemon SetsHelm charts。

  1. kompose convert -j
  1. INFO Kubernetes file "redis-svc.json" created
  2. INFO Kubernetes file "web-svc.json" created
  3. INFO Kubernetes file "redis-deployment.json" created
  4. INFO Kubernetes file "web-deployment.json" created

*-deployment.json 文件中包含 Deployment 对象。

  1. kompose convert --replication-controller
  1. INFO Kubernetes file "redis-svc.yaml" created
  2. INFO Kubernetes file "web-svc.yaml" created
  3. INFO Kubernetes file "redis-replicationcontroller.yaml" created
  4. INFO Kubernetes file "web-replicationcontroller.yaml" created

*-replicationcontroller.yaml 文件包含 Replication Controller 对象。 如果你想指定副本数(默认为 1),可以使用 --replicas 参数: kompose convert --replication-controller --replicas 3

  1. kompose convert --daemon-set
  1. INFO Kubernetes file "redis-svc.yaml" created
  2. INFO Kubernetes file "web-svc.yaml" created
  3. INFO Kubernetes file "redis-daemonset.yaml" created
  4. INFO Kubernetes file "web-daemonset.yaml" created

*-daemonset.yaml 文件包含 DaemonSet 对象。

如果你想生成 Helm 可用的 Chart, 只需简单的执行下面的命令:

  1. kompose convert -c
  1. INFO Kubernetes file "web-svc.yaml" created
  2. INFO Kubernetes file "redis-svc.yaml" created
  3. INFO Kubernetes file "web-deployment.yaml" created
  4. INFO Kubernetes file "redis-deployment.yaml" created
  5. chart created in "./docker-compose/"
  1. tree docker-compose/
  1. docker-compose
  2. ├── Chart.yaml
  3. ├── README.md
  4. └── templates
  5. ├── redis-deployment.yaml
  6. ├── redis-svc.yaml
  7. ├── web-deployment.yaml
  8. └── web-svc.yaml

这个 Chart 结构旨在为构建 Helm Chart 提供框架。

标签

kompose 支持 docker-compose.yml 文件中用于 Kompose 的标签,以便 在转换时明确定义 Service 的行为。

  • kompose.service.type 定义要创建的 Service 类型。例如:

    1. version: "2"
    2. services:
    3. nginx:
    4. image: nginx
    5. dockerfile: foobar
    6. build: ./foobar
    7. cap_add:
    8. - ALL
    9. container_name: foobar
    10. labels:
    11. kompose.service.type: nodeport
  • kompose.service.expose 定义是否允许从集群外部访问 Service。 如果该值被设置为 “true”,提供程序将自动设置端点, 对于任何其他值,该值将被设置为主机名。 如果在 Service 中定义了多个端口,则选择第一个端口作为公开端口。

    • 如果使用 Kubernetes 驱动,会有一个 Ingress 资源被创建,并且假定 已经配置了相应的 Ingress 控制器。
    • 如果使用 OpenShift 驱动, 则会有一个 route 被创建。

    例如:

    1. version: "2"
    2. services:
    3. web:
    4. image: tuna/docker-counter23
    5. ports:
    6. - "5000:5000"
    7. links:
    8. - redis
    9. labels:
    10. kompose.service.expose: "counter.example.com"
    11. redis:
    12. image: redis:3.0
    13. ports:
    14. - "6379"

当前支持的选项有:

kompose.service.typenodeport / clusterip / loadbalancer
kompose.service.exposetrue / hostname

说明:

kompose.service.type 标签应该只用 ports 来定义,否则 kompose 会失败。

重启

如果你想创建没有控制器的普通 Pod,可以使用 docker-compose 的 restart 结构来指定这一行为。请参考下表了解 restart 的不同参数。

docker-compose restart创建的对象Pod restartPolicy
“”控制器对象Always
always控制器对象Always
on-failurePodOnFailure
noPodNever

说明: 控制器对象可以是 deploymentreplicationcontroller 等。

例如,pival Service 将在这里变成 Pod。这个容器计算 pi 的取值。

  1. version: '2'
  2. services:
  3. pival:
  4. image: perl
  5. command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
  6. restart: "on-failure"

关于 Deployment Config 的提醒

如果 Docker Compose 文件中为服务声明了卷,Deployment (Kubernetes) 或 DeploymentConfig (OpenShift) 策略会从 “RollingUpdate” (默认) 变为 “Recreate”。 这样做的目的是为了避免服务的多个实例同时访问卷。

如果 Docker Compose 文件中的服务名包含 _(例如 web_service), 那么将会被替换为 -,服务也相应的会重命名(例如 web-service)。 Kompose 这样做的原因是 “Kubernetes” 不允许对象名称中包含 _

请注意,更改服务名称可能会破坏一些 docker-compose 文件。

Docker Compose 版本

Kompose 支持的 Docker Compose 版本包括:1、2 和 3。 对 2.1 和 3.2 版本的支持还有限,因为它们还在实验阶段。

所有三个版本的兼容性列表请查看我们的 转换文档, 文档中列出了所有不兼容的 Docker Compose 关键字。

最后修改 May 18, 2021 at 2:19 PM PST : [zh]Resync tasks files[8] (a7e39ecdb)