1

我正在尝试在 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 外部端点访问应用程序?

码头工人-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:

Dockerfile

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;
  }
}
4

1 回答 1

0

对于 kubernetes 上使用 django 的静态文件,请考虑 whitenoise http://whitenoise.evans.io/en/stable/在此处提供解决方案。

这是一个直截了当的建议,但在我发现它被引用之前,我花了很多时间搜索。

于 2021-02-26T01:01:01.577 回答