Docker-Kubernetes 找不到manage.py
Docker-Kubernets can't find manage.py
我在 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
我认为您的问题出在您的 kubernetes 配置的 volumes
部分。
在您的 docker-compose 配置中,您有一个卷安装
volumes:
- ./app:/app
这对本地开发非常有用,因为它会在您的机器上复制代码并将其覆盖在 docker 图像上,这样您在机器上所做的更改就会反映在您的 运行 docker 容器,允许 runserver
查看文件更改并根据需要重新加载 django 服务器。
这在 kubernetes 中不太好 - 当你在生产中 运行 时,你想要使用已经嵌入到图像中的代码。由于这是当前配置的,我认为您正在创建一个 empty 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 deployment guide 关于您应该配置的其他一些设置 - 即,查看 gunicorn; runserver
不是您进行生产部署的最佳途径。
我在 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
我认为您的问题出在您的 kubernetes 配置的 volumes
部分。
在您的 docker-compose 配置中,您有一个卷安装
volumes:
- ./app:/app
这对本地开发非常有用,因为它会在您的机器上复制代码并将其覆盖在 docker 图像上,这样您在机器上所做的更改就会反映在您的 运行 docker 容器,允许 runserver
查看文件更改并根据需要重新加载 django 服务器。
这在 kubernetes 中不太好 - 当你在生产中 运行 时,你想要使用已经嵌入到图像中的代码。由于这是当前配置的,我认为您正在创建一个 empty 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 deployment guide 关于您应该配置的其他一些设置 - 即,查看 gunicorn; runserver
不是您进行生产部署的最佳途径。