Tasks

Step-by-step instructions for performing operations with Kubernetes.

Edit This Page

Translate a Docker Compose File to Kubernetes Resources

kompose 是一个将docker-compose迁移到kubernetes的工具,kompose会把Docker Compose文件翻译成Kubernetes资源文件。

更多关于Kompose信息,请访问官方网站http://kompose.io

kompose 是从本地Docker管理到使用Kubernetes管理您的应用程序的便利工具。 Docker的转换撰写格式到Kubernetes资源清单可能不是精确的,但会起到参考作用,尤其是初次在Kubernetes上部署应用程序。

用例说明

如果您有一个Docker Compose的docker-compose.yml文件或者一个Docker分布式应用捆绑包的docker-compose-bundle.dab文件,可以通过kompose命令将它们生成为Kubernetes的deplyment、service的资源文件,如下所示:

$ kompose -f docker-compose.yml convert
WARN: Unsupported key networks - ignoring
file "redis-svc.yaml" created
file "web-svc.yaml" created
file "web-deployment.yaml" created
file "redis-deployment.yaml" created

安装说明

安装Kompose有多种方法,我们推荐的方法是从GitHub下载最新的二进制文件。

GitHub版本

在GitHub上面,Kompose代码每三周更新出一个版本,您可以在GitHub release page看到最新的版本。

当前我们用的版本是0.5.0

# Linux
curl -L https://github.com/kubernetes-incubator/kompose/releases/download/v0.5.0/kompose-linux-amd64 -o kompose

# macOS
curl -L https://github.com/kubernetes-incubator/kompose/releases/download/v0.5.0/kompose-darwin-amd64 -o kompose

# Windows
curl -L https://github.com/kubernetes-incubator/kompose/releases/download/v0.5.0/kompose-windows-amd64.exe -o kompose.exe

增加可执行权限并将它移动到您指定的路径(如 /usr/local/bin)。

chmod +x kompose
sudo mv ./kompose /usr/local/bin/kompose

Kompose转换

目前,Kompose支持将Docker Compose文件(v1,v2版本)和分布式应用捆绑包转换成Kubernetes对象和OpenShift对象。 以下是一个在examples/路径下的测试例子,这个例子讲会通过kompose convert把compose和dab文件转换成相应的Kubernetes和OpenShift对象。

Kubernetes

$ cd examples/

$ ls
docker-compose.yml  docker-compose-bundle.dab  docker-gitlab.yml  docker-voting.yml

$ kompose -f docker-gitlab.yml convert
file "redisio-svc.yaml" created
file "gitlab-svc.yaml" created
file "postgresql-svc.yaml" created
file "gitlab-deployment.yaml" created
file "postgresql-deployment.yaml" created
file "redisio-deployment.yaml" created

$ ls *.yaml
gitlab-deployment.yaml  postgresql-deployment.yaml  redis-deployment.yaml    redisio-svc.yaml  web-deployment.yaml
gitlab-svc.yaml         postgresql-svc.yaml         redisio-deployment.yaml  redis-svc.yaml    web-svc.yaml

您可以尝试转换一个v2版本Docker Compose,如下:

$ kompose --file docker-voting.yml convert
WARN Unsupported key networks - ignoring
WARN Unsupported key build - ignoring
file "worker-svc.yaml" created
file "db-svc.yaml" created
file "redis-svc.yaml" created
file "result-svc.yaml" created
file "vote-svc.yaml" created
file "redis-deployment.yaml" created
file "result-deployment.yaml" created
file "vote-deployment.yaml" created
file "worker-deployment.yaml" created
file "db-deployment.yaml" created

$ ls
db-deployment.yaml  docker-compose.yml         docker-gitlab.yml  redis-deployment.yaml  result-deployment.yaml  vote-deployment.yaml  worker-deployment.yaml
db-svc.yaml         docker-compose-bundle.dab  docker-voting.yml  redis-svc.yaml         result-svc.yaml         vote-svc.yaml         worker-svc.yaml

您也可以一次性同时转换多个docker-compose文件,如下所示:

$ kompose -f docker-compose.yml -f docker-guestbook.yml convert
file "frontend-service.yaml" created
file "mlbparks-service.yaml" created
file "mongodb-service.yaml" created
file "redis-master-service.yaml" created
file "redis-slave-service.yaml" created
file "frontend-deployment.yaml" created
file "mlbparks-deployment.yaml" created
file "mongodb-deployment.yaml" created
file "mongodb-claim0-persistentvolumeclaim.yaml" created
file "redis-master-deployment.yaml" created
file "redis-slave-deployment.yaml" created

$ ls
mlbparks-deployment.yaml  mongodb-service.yaml                       redis-slave-service.jsonmlbparks-service.yaml
frontend-deployment.yaml  mongodb-claim0-persistentvolumeclaim.yaml  redis-master-service.yaml
frontend-service.yaml     mongodb-deployment.yaml                    redis-slave-deployment.yaml
redis-master-deployment.yaml

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

使用 --bundle, --dab去解析一个DAB文件,如下所示:

$ kompose --bundle docker-compose-bundle.dab convert
WARN: Unsupported key networks - ignoring
file "redis-svc.yaml" created
file "web-svc.yaml" created
file "web-deployment.yaml" created
file "redis-deployment.yaml" created

OpenShift

$ kompose --provider openshift --file docker-voting.yml convert
WARN [worker] Service cannot be created because of missing port.
INFO file "vote-service.yaml" created
INFO file "db-service.yaml" created
INFO file "redis-service.yaml" created
INFO file "result-service.yaml" created
INFO file "vote-deploymentconfig.yaml" created
INFO file "vote-imagestream.yaml" created
INFO file "worker-deploymentconfig.yaml" created
INFO file "worker-imagestream.yaml" created
INFO file "db-deploymentconfig.yaml" created
INFO file "db-imagestream.yaml" created
INFO file "redis-deploymentconfig.yaml" created
INFO file "redis-imagestream.yaml" created
INFO file "result-deploymentconfig.yaml" created
INFO file "result-imagestream.yaml" created

同样的方式,您可以将DAB文件转换成OpenShift。

$ kompose --bundle docker-compose-bundle.dab --provider openshift convert
WARN: Unsupported key networks - ignoring
INFO file "redis-svc.yaml" created
INFO file "web-svc.yaml" created
INFO file "web-deploymentconfig.yaml" created
INFO file "web-imagestream.yaml" created
INFO file "redis-deploymentconfig.yaml" created
INFO file "redis-imagestream.yaml" created

Kompose还支持在服务中为build指令创建buildconfig。默认情况下,它使用远程仓库分作为源仓库并获得当前git分支,当前分支作为构建的源分支。您可以使用“–build-repo”和“–build-branch”选项分别指定不同的源代码和分支。

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

注意:如果您使用oc create -f手动推送Openshift组件,则需要确保在buildconfig工件之前推送镜像组件,以解决这个Openshift问题: https://github.com/openshift/origin/issues/4518 。

Kompose安装

Kompose支持直接通过kompose up 命令完成 “composed” 应用部署。

Kubernetes

$ kompose --file ./examples/docker-guestbook.yml up

通过创建Kubernetes的deployment,service来容器化您的应用。
如果您需要多种类型的资源,使用 'kompose convert'命令和'kubectl create -f' 进行替换。

INFO Successfully created service: redis-master
INFO Successfully created service: redis-slave
INFO Successfully created service: frontend
INFO Successfully created deployment: redis-master
INFO Successfully created deployment: redis-slave
INFO Successfully created deployment: frontend


您的应用将会在Kubernetes部署,您可以通过运行'kubectl get deployment,svc,pods' 获得更详细的信息。
$ kubectl get deployment,svc,pods
NAME                               DESIRED       CURRENT       UP-TO-DATE   AVAILABLE   AGE
deploy/frontend                    1             1             1            1           4m
deploy/redis-master                1             1             1            1           4m
deploy/redis-slave                 1             1             1            1           4m

NAME                               CLUSTER-IP    EXTERNAL-IP   PORT(S)      AGE
svc/frontend                       10.0.174.12   <none>        80/TCP       4m
svc/kubernetes                     10.0.0.1      <none>        443/TCP      13d
svc/redis-master                   10.0.202.43   <none>        6379/TCP     4m
svc/redis-slave                    10.0.1.85     <none>        6379/TCP     4m

NAME                               READY         STATUS        RESTARTS     AGE
po/frontend-2768218532-cs5t5       1/1           Running       0            4m
po/redis-master-1432129712-63jn8   1/1           Running       0            4m
po/redis-slave-2504961300-nve7b    1/1           Running       0            4m

注意:

OpenShift

$kompose --file ./examples/docker-guestbook.yml --provider openshift up

通过创建Openshift的deployment,service来容器化您的应用。
如果您需要多种类型的资源,使用 'kompose convert'命令和'kubectl create -f' 进行替换。

INFO Successfully created service: redis-slave
INFO Successfully created service: frontend
INFO Successfully created service: redis-master
INFO Successfully created deployment: redis-slave
INFO Successfully created ImageStream: redis-slave
INFO Successfully created deployment: frontend
INFO Successfully created ImageStream: frontend
INFO Successfully created deployment: redis-master
INFO Successfully created ImageStream: redis-master


您的应用将会在Kubernetes部署,您可以通过运行'oc get dc,svc,is' 获得更详细的信息

$ oc get dc,svc,is
NAME               REVISION                              DESIRED       CURRENT    TRIGGERED BY
dc/frontend        0                                     1             0          config,image(frontend:v4)
dc/redis-master    0                                     1             0          config,image(redis-master:e2e)
dc/redis-slave     0                                     1             0          config,image(redis-slave:v1)
NAME               CLUSTER-IP                            EXTERNAL-IP   PORT(S)    AGE
svc/frontend       172.30.46.64                          <none>        80/TCP     8s
svc/redis-master   172.30.144.56                         <none>        6379/TCP   8s
svc/redis-slave    172.30.75.245                         <none>        6379/TCP   8s
NAME               DOCKER REPO                           TAGS          UPDATED
is/frontend        172.30.12.200:5000/fff/frontend
is/redis-master    172.30.12.200:5000/fff/redis-master
is/redis-slave     172.30.12.200:5000/fff/redis-slave    v1

注意事项:

Kompose 卸载

一经在Kubernetes部署 “composed”应用,kompose down将会帮您删除应用相关的deployments和services。如果您需要删除其他资源类型,可使用’kubectl’命令。

$ kompose --file docker-guestbook.yml down
INFO Successfully deleted service: redis-master
INFO Successfully deleted deployment: redis-master
INFO Successfully deleted service: redis-slave
INFO Successfully deleted deployment: redis-slave
INFO Successfully deleted service: frontend
INFO Successfully deleted deployment: frontend

注意事项:

默认的kompose转换将会生成Kubernetes DeploymentsServices的在yaml文件,只要加上-j即可生成json格式文件,您也可以选择生成Replication Controllers 对象, Daemon Sets, 或者 Helm charts。

$ kompose convert -j
file "redis-svc.json" created
file "web-svc.json" created
file "redis-deployment.json" created
file "web-deployment.json" created

*-deployment.json文件包含部署对象

$ kompose convert --rc
file "redis-svc.yaml" created
file "web-svc.yaml" created
file "redis-rc.yaml" created
file "web-rc.yaml" created

*-rc.yaml文件包含Replication Controller对象。如果要指定副本(默认为1),请使用--replicas标志:$ kompose convert --rc --replicas 3

$ kompose convert --ds
file "redis-svc.yaml" created
file "web-svc.yaml" created
file "redis-daemonset.yaml" created
file "web-daemonset.yaml" created

*-daemonset.yaml文件包含Daemon Set对象。 如果要生成与[Helm](https://github.com/kubernetes/helm)一起使用的图表,只需执行以下操作: *-daemonset.yaml.

$ kompose convert -c
file "web-svc.yaml" created
file "redis-svc.yaml" created
file "web-deployment.yaml" created
file "redis-deployment.yaml" created
chart created in "./docker-compose/"

$ tree docker-compose/
docker-compose
├── Chart.yaml
├── README.md
└── templates
    ├── redis-deployment.yaml
    ├── redis-svc.yaml
    ├── web-deployment.yaml
    └── web-svc.yaml

图表结构旨在提供构建您的Helm图表的骨架。

尚不支持的docker-compose配置注意事项

目前kompose不支持一些Docker Compose选项,它们在[conversion](http://kompose.io/conversion/)文档中列出。 例如:

$ cat nginx.yml
nginx:
  image: nginx
  dockerfile: foobar
  build: ./foobar
  cap_add:
    - ALL
  container_name: foobar

$ kompose -f nginx.yml convert
WARN Unsupported key build - ignoring
WARN Unsupported key cap_add - ignoring
WARN Unsupported key dockerfile - ignoring

标签

kompose支持Kompose-specific,标签在docker-compose.yml文件中显式定义了在转换时的一个service的行为

version: "2"
services:
  nginx:
    image: nginx
    dockerfile: foobar
    build: ./foobar
    cap_add:
      - ALL
    container_name: foobar
    labels:
      kompose.service.type: nodeport

目前支持的选项如下:

Key Value
kompose.service.type nodeport / clusterip / loadbalancer
kompose.service.expose true / hostname

重启

如果您想在没有控制器的情况下创建pods,您可以使用docker-compose的restart命令。restart 参数如下表所示:

docker-compose restart object created Pod restartPolicy
"" controller object Always
always controller object Always
on-failure Pod OnFailure
no Pod Never

注意: controller object可以是Deployment 或者 ReplicationController

version: '2'

services:
  pival:
    image: perl
    command: ["perl",  "-Mbignum=bpi", "-wle", "print bpi(2000)"]
    restart: "on-failure"

####DeploymentConfig注意事项

如果Docker Compose文件有一个为服务指定的卷,那么部署(Kubernetes)或DeploymentConfig(OpenShift)策略将改为“重新创建”,而不是“RollingUpdate”(默认)。这样做是为了避免在同一时间访问卷的多个实例。

Analytics

Create an Issue Edit this Page