[英]Django in kubernetes with ingress-nginx is not serving static files
I'm new to Kubernetes and this is my first project using it.我是 Kubernetes 的新手,这是我使用它的第一个项目。
What I'm trying to do is basically this.我想要做的基本上就是这个。 I have a Django app and a react app.
我有一个 Django 应用程序和一个 React 应用程序。 I want to serve them together using a single ingress-nginx using kubernetes' own ingress nginx library (kubernetes/ingress-nginx) React app is served in root and Django is served in "/api" root.
我想使用 kubernetes 自己的入口 nginx 库 (kubernetes/ingress-nginx) 使用单个 ingress-nginx 为它们提供服务,React 应用程序在 root 中提供服务,而 Django 在“/api”根中提供服务。 I've defined a FORCE_SCRIPT_NAME in Django settings.
我在 Django 设置中定义了 FORCE_SCRIPT_NAME。
Also for static files, I've created a separate persistent volume claim and mounted it in Django deployment file.同样对于静态文件,我创建了一个单独的持久卷声明并将其安装在 Django 部署文件中。 But when I hit the Django's admin page, static files are not served.
但是当我点击 Django 的管理页面时,不提供静态文件。 And also media files are served in api/api/media... path which has an extra api.
并且媒体文件也在 api/api/media... 路径中提供,该路径具有额外的 api。 What is the proper way to serve static files in Kubernetes?
在 Kubernetes 中提供静态文件的正确方法是什么? I don't want to use an online static root like S3.
我不想使用像 S3 这样的在线静态根。 Here is my Django deployment yaml.
这是我的 Django 部署 yaml。
apiVersion: apps/v1
kind: Deployment
metadata:
name: django
labels:
deployment: django
spec:
replicas: 1
selector:
matchLabels:
pod: django
template:
metadata:
labels:
pod: django
spec:
volumes:
- name: django-configmap-volume
configMap:
name: django-configmap
- name: static-volume
persistentVolumeClaim:
claimName: static-volume
- name: media-volume
persistentVolumeClaim:
claimName: media-volume
containers:
- name: django
image: my_image:v1.0
ports:
- containerPort: 8182
envFrom:
- configMapRef:
name: django-configmap
env:
- name: DB_HOST
value: postgres-service
- name: DB_NAME
value: kubernetes_django
- name: DB_USER
valueFrom:
secretKeyRef:
name: postgres-credentials
key: user
- name: DB_PASS
valueFrom:
secretKeyRef:
name: postgres-credentials
key: password
- name: DB_PORT
value: "5432"
volumeMounts:
- name: django-configmap-volume
mountPath: /etc/config
- name: static-volume
mountPath: /app/static
- name: media-volume
mountPath: /app/media
Django settings.py Django设置.py
...
FORCE_SCRIPT_NAME = '/api/'
STATIC_ROOT = '/app/static'
MEDIA_ROOT = '/app/media'
STATIC_URL = '/api/static/'
MEDIA_URL = '/api/media/'
...
I've defined a configuration snippet in ingress for static files and media files location but I think it doesn't work.我已经在入口中为静态文件和媒体文件位置定义了一个配置片段,但我认为它不起作用。 ingress.yaml
入口.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: hoosthere-ingress
namespace: default
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: /$1
nginx.ingress.kubernetes.io/proxy-connect-timeout: '30'
nginx.ingress.kubernetes.io/proxy-send-timeout: '500'
nginx.ingress.kubernetes.io/configuration-snippet: |
location ~* "^/api/static?(.*)" {
alias /app/static/;
}
location ~* "^/api/media?(.*)" {
alias /app/media/;
}
spec:
rules:
- http:
paths:
- path: /?(.*)
backend:
serviceName: react-service
servicePort: 1220
- path: /api/?(.*)
backend:
serviceName: django-service
servicePort: 8000
If you can provide a good example of this, I'd be appreciated for any help.如果您能提供一个很好的例子,我将不胜感激。
It's mentioned in the comments above, but take a look at whitenoise http://whitenoise.evans.io/en/stable/ to provide a solution here.上面的评论中提到了这一点,但请查看 whitenoise http://whitenoise.evans.io/en/stable/以在此处提供解决方案。
This is a straightforward recommendation, but I spent much time searching around before I found it referenced in the comments here.这是一个简单的建议,但我花了很多时间四处搜索,然后才发现这里的评论中引用了它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.