0

我在 Django 中构建了一个 API,并使用 docker-compose 来编排 redis 和 celery 服务。现在,我想将 API 移动到 Kubernetes 集群 (AKS)。但是,python can't find manage.py当我将它运行到集群中时出现错误。我使用 Kompose 工具编写了 kubernets manifest.yaml。这是我的 Dockerfile、docker-compose.yml 和 kubernets.yaml 文件

# docker-compose.yml
version: '3'

services:
  app:
    build:
      context: .
    ports:
      - "8000:8000"        
    volumes:
      - ./app:/app
    command: >
      sh -c "python3 ./manage.py makemigrations &&
             python3 ./manage.py migrate &&
             python3 ./manage.py runserver 0.0.0.0:8000"

Dockerfile

# Dockerfile
FROM python:3.8 

ENV PYTHONUNBUFFERED 1
COPY ./requirements.txt /requirements.txt
RUN pip install -r /requirements.txt

RUN mkdir /app
COPY ./app /app
WORKDIR /app

并且 kubernets 表现出来

apiVersion: v1
items:
  - apiVersion: v1
    kind: Service
    metadata:
      annotations:
        kompose.cmd: kompose convert -f docker-compose.yml -o kb_manifests.yaml
        kompose.version: 1.22.0 (955b78124)
      creationTimestamp: null
      labels:
        io.kompose.service: app
      name: app
    spec:
      ports:
        - name: "8000"
          port: 8000
          targetPort: 8000
      selector:
        io.kompose.service: app
    status:
      loadBalancer: {}


  - apiVersion: apps/v1
    kind: Deployment
    metadata:
      annotations:
        kompose.cmd: kompose convert -f docker-compose.yml -o kb_manifests.yaml
        kompose.version: 1.22.0 (955b78124)
      creationTimestamp: null
      labels:
        io.kompose.service: app
      name: app
    spec:
      replicas: 1
      selector:
        matchLabels:
          io.kompose.service: app
      strategy:
        type: Recreate
      template:
        metadata:
          annotations:
            kompose.cmd: kompose convert -f docker-compose.yml -o kb_manifests.yaml
            kompose.version: 1.22.0 (955b78124)
          creationTimestamp: null
          labels:
            io.kompose.service: app
        spec:
          containers:
            - args:
                - sh
                - -c
                - |-
                  python manage.py makemigrations &&
                  python manage.py migrate &&
                  python manage.py runserver 0.0.0.0:8000
              image: <image pushed in a Azure Container Registry (ACR)>
              name: app
              ports:
                - containerPort: 8000
              resources: {}
              volumeMounts:
                - mountPath: /app
                  name: app-claim0
          restartPolicy: Always
          volumes:
            - name: app-claim0
              persistentVolumeClaim:
                claimName: app-claim0
    status: {}
  - apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      creationTimestamp: null
      labels:
        io.kompose.service: app-claim0
      name: app-claim0
    spec:
      accessModes:
        - ReadWriteOnce
      resources:
        requests:
          storage: 100Mi
    status: {}

日志错误

$ kubectl logs app-6fc488bf56-hb8g9 --previous
# python: can't open file 'manage.py': [Errno 2] No such file or director
4

1 回答 1

1

我认为您的问题出volumes在您的 kubernetes 配置部分。

在您的 docker-compose 配置中,您有一个卷安装

volumes:
 - ./app:/app

这对于本地开发非常有用,因为它将获取您机器上的代码副本并将其覆盖在 docker 映像上,因此您在机器上所做的更改将反映在正在运行的 docker 容器中,从而允许runserver查看文件更改并重新加载django 服务器根据需要。

这在 kubernetes 中不太好 - 当您在 prod 中运行时,您希望使用已烘焙到映像中的代码。由于这是当前配置的,我认为您正在创建一个 PersistentVolumeClaim文件,然后将其安装/app在正在运行的容器中的目录顶部。由于它是空的,因此找不到manage.py文件。

尝试使您的 kubernetes 配置如下所示:

apiVersion: v1
items:
  - apiVersion: v1
    kind: Service
    metadata:
      annotations:
        kompose.cmd: kompose convert -f docker-compose.yml -o kb_manifests.yaml
        kompose.version: 1.22.0 (955b78124)
      creationTimestamp: null
      labels:
        io.kompose.service: app
      name: app
    spec:
      ports:
        - name: "8000"
          port: 8000
          targetPort: 8000
      selector:
        io.kompose.service: app
    status:
      loadBalancer: {}


  - apiVersion: apps/v1
    kind: Deployment
    metadata:
      annotations:
        kompose.cmd: kompose convert -f docker-compose.yml -o kb_manifests.yaml
        kompose.version: 1.22.0 (955b78124)
      creationTimestamp: null
      labels:
        io.kompose.service: app
      name: app
    spec:
      replicas: 1
      selector:
        matchLabels:
          io.kompose.service: app
      strategy:
        type: Recreate
      template:
        metadata:
          annotations:
            kompose.cmd: kompose convert -f docker-compose.yml -o kb_manifests.yaml
            kompose.version: 1.22.0 (955b78124)
          creationTimestamp: null
          labels:
            io.kompose.service: app
        spec:
          containers:
            - args:
                - sh
                - -c
                - |-
                  python manage.py makemigrations &&
                  python manage.py migrate &&
                  python manage.py runserver 0.0.0.0:8000
              image: <image pushed in a Azure Container Registry (ACR)>
              name: app
              ports:
                - containerPort: 8000
              resources: {}
          restartPolicy: Always
    status: {}

这应该与您所拥有的相同,减去对卷的任何引用。

如果那行得通,那么您就可以参加比赛了。一旦启动并运行,请查看Django 部署指南,了解您应该配置的其他一些设置 - 即,查看 gunicorn;runserver不是您进行生产部署的最佳途径。

于 2021-04-10T00:42:44.153 回答