大网数据广告活动
服务器运维问答

Kubernetes - Nginx Frontend & Django Backend

· 52CE 我爱测

I'm following along this doc: https://kubernetes.io/docs/tasks/access-application-cluster/connecting-frontend-backend/ the mainly difference is that my app is a Djago app running on port 8000. The frontend pod keeps crashing: 202…

问题原文

原帖链接 · Alexandre Cardoso · CC BY-SA 4.0

I'm following along this doc: https://kubernetes.io/docs/tasks/access-application-cluster/connecting-frontend-backend/ the mainly difference is that my app is a Djago app running on port 8000.

The frontend pod keeps crashing:

2021/03/15 00:15:52 [emerg] 1#1: host not found in upstream "posi:8000" in /etc/nginx/conf.d/posi.conf:2
nginx: [emerg] host not found in upstream "posi:8000" in /etc/nginx/conf.d/posi.conf:2

Could someone point out my mistakes, please.

deploy_backend.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: posi-backend
spec:
  selector:
    matchLabels:
      app: posi
      tier: backend
      track: stable
  replicas: 2
  template:
    metadata:
      labels:
        app: posi
        tier: backend
        track: stable
    spec:
      containers:
      - name: posi-backend
        image: xxxxxxxxxx.yyy.ecr.us-east-1.amazonaws.com/posi/posi:uwsgi
        command: ["uwsgi"]
        args: ["--ini", "config/uwsgi.ini"]
        ports:
        - name: http
          containerPort: 8000
      imagePullSecrets:
        - name: regcred

service_backend.yaml

 apiVersion: v1
 kind: Service
 metadata:
   name: posi-backend
 spec:
   selector:
     app: posi
     tier: backend
   ports:
   - protocol: TCP
     port: 8000
     targetPort: 8000

deploy_frontend.yaml

 apiVersion: apps/v1
 kind: Deployment
 metadata:
   name: posi-frontend
 spec:
   selector:
     matchLabels:
       app: posi
       tier: frontend
       track: stable
   replicas: 1
   template:
     metadata:
       labels:
         app: posi
         tier: frontend
         track: stable
     spec:
       containers:
         - name: posi-frontend
           image: alx/urn_front:1.0
           lifecycle:
             preStop:
               exec:
                 command: ["/usr/sbin/nginx", "-s", "quit"]

service_frontend.yaml

 apiVersion: v1
 kind: Service
 metadata: 
   name: posi-frontend
 spec: 
   selector:
     app: posi
     tier: frontend
   ports:
   - protocol: "TCP"
     port: 80
     targetPort: 80
   type: NodePort

nginx.conf

upstream posi_backend {
    server posi:8000;
}
server {
    listen 80;
    server_name qa-posi;
     location /static/ {
         alias /code/static/;
     }
    location / {
        proxy_pass http://posi_backend;
        include     /etc/nginx/uwsgi_params;
    }
}
  • Cluster information:

  • Kubernetes version: 1.20
  • Cloud being used: bare-metal
  • Installation method: Kubeadm
  • Host OS: Centos 7.9
  • CNI and version: Weave 0.3.0
  • CRI and version: Docker 19.03.11

已采纳解答

原帖链接 · Michael Hampton · CC BY-SA 4.0

You named your Service (and your Pod) posi-backend, but your nginx configuration tries to connect to:

upstream posi_backend {
    server posi:8000;
}

It should rather connect to posi-backend.

upstream posi_backend {
    server posi-backend:8000;
}

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2021-03-15)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。