![](/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.