Kubernetes – Nginx Frontend & Django Backend

Alexandre Cardoso asked:

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: alxndr3/urnas_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

My answer:


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

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.