使用 Kompose 转换 docker-compose 以在 GKE 上部署工作负载

Converting docker-compose using Kompose to deploy workloads on GKE

我有一个用 Django Restframework 编写的项目,Celery 用于执行长 运行ning 任务,Redis 作为代理,Flower 用于监视 Celery 任务。我写了一个 Dockerfile & docker-compose.yaml 来创建一个网络,运行 这个服务在容器中。

Dockerfile

FROM python:3.7-slim

ENV PYTHONUNBUFFERED 1

RUN apt-get update &&\
    apt-get install python3-dev default-libmysqlclient-dev gcc  -y &&\
    apt-get install -y libssl-dev libffi-dev &&\
    python -m pip install --upgrade pip &&\
    mkdir /ibdax

WORKDIR /ibdax

COPY ./requirements.txt /requirements.txt

COPY . /ibdax

EXPOSE 80

EXPOSE 5555

ENV ENVIRONMENT=LOCAL

#install dependencies
RUN pip install -r /requirements.txt

RUN pip install django-phonenumber-field[phonenumbers]
RUN pip install drf-yasg[validation]

docker-compose.yaml

version: "3"

services:
  redis:
    container_name: redis-service
    image: "redis:latest"
    ports:
      - "6379:6379"
    restart: always
    command: "redis-server"

  ibdax-backend:
    container_name: ibdax
    build:
      context: .
      dockerfile: Dockerfile
    image: "ibdax-django-service"
    volumes:
      - .:/ibdax
    ports:
      - "80:80"
    expose:
      - "80"
    restart: always
    env_file:
      - .env.staging
    command: >
      sh -c "daphne -b 0.0.0.0 -p 80 ibdax.asgi:application"
    links:
      - redis

  celery:
    container_name: celery-container
    image: "ibdax-django-service"
    command: "watchmedo auto-restart -d . -p '*.py' -- celery  -A ibdax worker -l INFO"
    volumes:
      - .:/ibdax
    restart: always
    env_file:
      - .env.staging
    links:
      - redis
    depends_on:
      - ibdax-backend

  flower:
    container_name: flower
    image: "ibdax-django-service"
    command: "flower -A ibdax --port=5555 --basic_auth=${FLOWER_USERNAME}:${FLOWER_PASSWORD}"
    volumes:
      - .:/ibdax
    ports:
      - "5555:5555"
    expose:
      - "5555"
    restart: always
    env_file:
      - .env
      - .env.staging
    links:
      - redis
    depends_on:
      - ibdax-backend

Dockerfile & docker-compose 工作正常,现在我想将此应用程序部署到 GKE。我遇到了 Kompose 它将 docker-compose 翻译成 kubernetes资源。我阅读了文档并开始按照步骤操作,第一步是 运行 kompose convert。这返回了一些警告并创建了一些文件,如下所示 -

WARN Service "celery" won't be created because 'ports' is not specified 
WARN Volume mount on the host "/Users/jeetpatel/Desktop/projects/ibdax" isn't supported - ignoring path on the host 
WARN Volume mount on the host "/Users/jeetpatel/Desktop/projects/ibdax" isn't supported - ignoring path on the host 
WARN Volume mount on the host "/Users/jeetpatel/Desktop/projects/ibdax" isn't supported - ignoring path on the host 
INFO Kubernetes file "flower-service.yaml" created 
INFO Kubernetes file "ibdax-backend-service.yaml" created 
INFO Kubernetes file "redis-service.yaml" created 
INFO Kubernetes file "celery-deployment.yaml" created 
INFO Kubernetes file "env-dev-configmap.yaml" created 
INFO Kubernetes file "celery-claim0-persistentvolumeclaim.yaml" created 
INFO Kubernetes file "flower-deployment.yaml" created 
INFO Kubernetes file "flower-claim0-persistentvolumeclaim.yaml" created 
INFO Kubernetes file "ibdax-backend-deployment.yaml" created 
INFO Kubernetes file "ibdax-backend-claim0-persistentvolumeclaim.yaml" created 
INFO Kubernetes file "redis-deployment.yaml" created 

我忽略了警告并转到下一步,即 运行ning 命令

kubectl apply -f flower-service.yaml, ibdax-backend-service.yaml, redis-service.yaml, celery-deployment.yaml

但是我得到这个错误 -

error: Unexpected args: [ibdax-backend-service.yaml, redis-service.yaml, celery-deployment.yaml]

所以我打算这样一个一个申请-

kubectl apply -f flower-service.yaml

但是我得到这个错误 -

The Service "flower" is invalid: spec.ports[1]: Duplicate value: core.ServicePort{Name:"", Protocol:"TCP", AppProtocol:(*string)(nil), Port:5555, TargetPort:intstr.IntOrString{Type:0, IntVal:0, StrVal:""}, NodePort:0}

不确定我哪里错了。

另外 Kompose 的先决条件是有一个 Kubernetes 集群所以我用 public 网络创建了一个 Autopilot 集群。现在我不确定这个应用命令将如何识别我创建的集群并在其上部署我的应用程序。

kompose convert 之后你的 flower-service.yaml 文件有重复的端口 - 这就是错误的意思。

...
  ports:
    - name: "5555"
      port: 5555
      targetPort: 5555
    - name: 5555-tcp
      port: 5555
      targetPort: 5555
...

您可以删除端口 name: "5555"name: 5555-tcp
例如,将端口块替换为

  ports:
    - name: 5555-tcp
      port: 5555
      targetPort: 5555

并再次部署服务。
我还建议将端口名称更改为更具描述性的名称。


ibdax-backend-service.yaml 文件也会发生同样的事情。

...
  ports:
    - name: "80"
      port: 80
      targetPort: 80
    - name: 80-tcp
      port: 80
      targetPort: 80
...

您可以删除其中一个定义,然后重新部署服务(也建议将端口名称更改为更具描述性的名称)。


kompose 不是一个完美的工具,它总能给你一个完美的结果。您应该检查生成的文件是否存在任何可能的冲突 and/or 缺失字段。