![](/img/trans.png)
[英]serving Django static files with nginx and gunicorn on kubernetes
[英]Django, nginx and gunicorn in kubernetes
我正在尝试在 kubernetes 中运行我的 django 应用程序。我使用 docker-compose 构建应用程序和 nginx。我能够在 docker compose 中使用代理服务器 nginx 运行应用程序。
docker-compose build - 成功
docker-compose up - 成功
http://aggre.mabh.io - 成功(能够加载应用程序)
当我尝试部署应用程序和 nginx 的图像时,我在 kubernetes 仪表板中收到错误提示
kubernetes 中的错误
pod 具有未绑定的 PersistentVolumeClaims(重复 2 次)(对于 nginx 和应用程序)
我正在使用kompose up来构建和部署应用程序。
如何在 kubernetes 中部署应用程序并通过 nginx kubernetes 外部端点访问应用程序?
docker-compose.yml
version: '3'
services:
djangoapp:
build: .
image: sigmaaggregator/djangoapp
labels:
- kompose.service.type=NodePort
volumes:
- .:/djangoapp
- static_volume:/djangoapp/static
ports:
- 4000:4000
networks:
- nginx_network
nginx:
image: nginx:1.13
ports:
- 80:80
volumes:
- ./config/nginx/conf.d:/etc/nginx/conf.d
- static_volume:/djangoapp/static
depends_on:
- djangoapp
networks:
- nginx_network
networks:
nginx_network:
driver: bridge
volumes:
static_volume:
文件
FROM python:3.6
RUN mkdir -p /djangoapp
WORKDIR /djangoapp
COPY . /djangoapp
RUN pip install -r requirements.txt
# copy our project code
RUN python manage.py collectstatic --no-input
CMD exec /bin/bash -c "trap : TERM INT; sleep infinity & wait"
# expose the port 4000
EXPOSE 4000
# define the default command to run when starting the container
CMD ["gunicorn", "--bind", ":4000", "aggre.wsgi:application"]
配置/nginx/conf.d/local.conf
upstream hello_server {
server djangoapp:4000;
}
server {
listen 80;
server_name aggre.mabh.io;
location /static/ {
alias /djangoapp/static/;
}
location / {
proxy_pass http://hello_server;
proxy_set_header Host $host;
#proxy_ssl_server_name on;
#proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_set_header X-Forwarded-Proto $scheme;
proxy_redirect off;
}
}
对于kubernetes上django的静态文件,可以考虑whitenoise http://whitenoise.evans.io/en/stable/这里提供解决方案。
这是一个直截了当的建议,但在我发现它被引用之前,我花了很多时间四处搜索。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.