Home > Software engineering >  How to create ingress-nginx for my kubernetes deployment and service?
How to create ingress-nginx for my kubernetes deployment and service?

Time:03-05

I am able to access my django app deployment using LoadBalancer service type but I'm trying to switch to ClusterIP service type and ingress-nginx but I am getting 503 Service Temporarily Unavailable when I try to access the site via the host url. Describing the ingress also shows error: endpoints "django-service" not found and error: endpoints "default-http-backend" not found. What am I doing wrong?

This is my service and ingress yaml:

---
apiVersion: v1
kind: Service
metadata:
  name: django-service
spec:
  type: ClusterIP
  ports:
  - name: http
    protocol: TCP
    port: 80
    targetPort: 8000
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: django-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/force-ssl-redirect: 'true'
    nginx.ingress.kubernetes.io/ssl-redirect: 'true'
spec:
  tls:
  - hosts:
    - django.example.com
  rules:
  - host: django.example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: django-service
            port:
              number: 80
  ingressClassName: nginx

kubectl get all

$ kubectl get all
NAME                                        READY   STATUS    RESTARTS   AGE
pod/django-app-5bdd8ffff9-79xzj             1/1     Running   0          7m44s
pod/postgres-58fffbb5cc-247x9               1/1     Running   0          7m44s

NAME                     TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
service/django-service   ClusterIP   10.233.29.58    <none>        80/TCP     7m44s
service/pg-service       ClusterIP   10.233.14.137   <none>        5432/TCP   7m44s

NAME                                   READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/django-app             1/1     1            1           7m44s
deployment.apps/postgres               1/1     1            1           7m44s

NAME                                              DESIRED   CURRENT   READY   AGE
replicaset.apps/django-app-5bdd8ffff9             1         1         1       7m44s
replicaset.apps/postgres-58fffbb5cc               1         1         1       7m44s

describe ingress

$ kubectl describe ing django-ingress
Name:             django-ingress
Labels:           <none>
Namespace:        django
Address:          10.10.30.50
Default backend:  default-http-backend:80 (<error: endpoints "default-http-backend" not found>)
TLS:
  SNI routes django.example.com
Rules:
  Host                      Path  Backends
  ----                      ----  --------
  django.example.com
                            /   django-service:80 (<error: endpoints "django-service" not found>)
Annotations:                nginx.ingress.kubernetes.io/force-ssl-redirect: true
                            nginx.ingress.kubernetes.io/rewrite-target: /
                            nginx.ingress.kubernetes.io/ssl-redirect: true
Events:
  Type    Reason  Age                   From                      Message
  ----    ------  ----                  ----                      -------
  Normal  Sync    5m28s (x2 over 6m5s)  nginx-ingress-controller  Scheduled for sync
  Normal  Sync    5m28s (x2 over 6m5s)  nginx-ingress-controller  Scheduled for sync

CodePudding user response:

I think you forgot to make the link with your deployment in your service.

apiVersion: v1
kind: Service
metadata:
  name: django-service
spec:
  type: ClusterIP
  ports:
  - name: http
    protocol: TCP
    port: 80
    targetPort: 8000
  selector:
    app: your-deployment-name

Your label must be set in your deployment as well:

spec:
  selector:
    matchLabels:
      app: your-deployment-name
  template:
    metadata:
      labels:
        app: your-deployment-name
  • Related