Kubernetes - Nginx 前端和 Django 后端

Kubernetes - Nginx 前端和 Django 后端

我正在关注这个文档:https://kubernetes.io/docs/tasks/access-application-cluster/connecting-frontend-backend/主要的区别在于我的应用程序是在端口 8000 上运行的 Djago 应用程序。

前端 pod 不断崩溃:

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

有人能指出我的错误吗?

部署后端.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

服务后端.yaml

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

部署_前端.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"]

服务前端.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;
    }
}
  • 集群信息:

  • Kubernetes 版本:1.20
  • 正在使用的云:裸机
  • 安装方法:Kubeadm
  • 主机操作系统:Centos 7.9
  • CNI 和版本:Weave 0.3.0
  • CRI 和版本:Docker 19.03.11

答案1

您命名了您的服务(和您的 Pod)posi-backend,但您的 nginx 配置尝试连接到:

upstream posi_backend {
    server posi:8000;
}

它应该连接到posi-backend

upstream posi_backend {
    server posi-backend:8000;
}

相关内容