Kubernetes集群中Django应用的Nginx配置 [英] Nginx config for django app in kubernetes cluster

查看:285
本文介绍了Kubernetes集群中Django应用的Nginx配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难为部署在kubernetes中的django应用程序创建nginx配置文件. Nginx和app是同一集群中的两个单独的容器.据我了解,容器可以通过127.0.0.1:XX和主机名相互通信.我为此使用minikube. 我的应用容器是通过以下文件构建的:

I'm having difficulty creating an nginx configuration file for a django app deployed in kubernetes. Nginx and app are two separate containers within the same cluster. From what I understand containers can communicate with each other via 127.0.0.1:XX and via hostnames. I'm using minikube for this. My app container is built from this file:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: website
  labels:
    name: website
spec:
  template:
    metadata:
      labels:
        name: website
    spec:
      containers:
        - name: website
          image: killabien/web
          ports:
            - containerPort: 8000
---
apiVersion: v1
kind: Service
metadata:
  name: website
  labels:
    name: website
spec:
  type: LoadBalancer
  ports:
    - port: 8000
      targetPort: 8000
  selector:
    name: website

然后是nginx:

apiVersion: v1
kind: Service
metadata:
  name: frontend
spec:
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  selector:
    app: website
    tier: frontend
  type: LoadBalancer
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: frontend
spec:
  template:
    metadata:
      labels:
        app: website
        tier: frontend
    spec:
      containers:
        - image: killabien/nginx
          name: nginx

我可以访问应用程序本身(在没有静态文件的情况下提供),但是当我尝试访问nginx时,出现502 Bad Gateway错误.这是Nginx的配置文件:

I can access the application itself(served without static files) but when I try to reach nginx I get 502 Bad Gateway error. Here is the nginx config file:

upstream website {
    server 127.0.0.1:8000;

}
server {
    listen 80;
    location /static {
        alias /www/davidbien/static;
    }

   location / {
        proxy_pass http://website;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     }
}

我尝试将127.0.0.1:8000替换为容器名称(网站)和localhost:8000,但没有任何更改.我在做什么错了?

I tried replacing 127.0.0.1:8000 with the container name(website) and localhost:8000 but nothing changed. What am I doing wrong?

推荐答案

对于您的特定问题:

  • 容器只有在同一个容器中是两个容器时才可以通过localhost引用(127.0.0.1)相互通信,这不是您的情况,因此是503.
  • 位于不同吊舱(如您的示例)中但共享相同名称空间(例如,默认)的容器可以使用服务名称(在您的情况下为:网站)相互通信
  • 位于不同名称空间的容器可以使用FQDN进行通信,如service-name.namespace-name.svc.cluster.local(在您的情况下为website.default.svc.cluster.local)中一样.

现在,k8s的理念是:吊舱是部署的单位,而容器是包装的单位.如果您的nginx和网站始终缩放在一起,那么就需要将它们放在同一个pod中并通过localhost进行通信,就好像它们是同一台计算机上的两个进程一样.如果您的前端要处理多个网站,并且它们分别扩展,则可以选择选项2或3(通过服务名称或FQDN进行通信).

Now, philosophy of k8s is that pod is unit of deployment and container is unit of packaging. IF your nginx and website are always scaled together, than it comes to reason to put them both in same pod and communicate over localhost as if they are two processes on same machine. IF your frontend is going to handle multiple websites and they scale separately then option 2 or 3 (communication over service name or FQDN) is in order.

这篇关于Kubernetes集群中Django应用的Nginx配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆