Setup custom default backend for nginx ingress controller in Kubernetes

2.1k views Asked by At

I am stuck with custom default backend for error page from Nginx Ingress Controller. By default, Nginx Ingress Controller pods return Nginx's default page with errors such as 404, 50x and I want to modify them.

I have installed a DaemonSet of Nginx Ingress Controller following this tutorial. I use kubectl command to apply daemonset yaml file.

I found a tutorial from NGINX Annntations that show me setup a service that catching 404 error from ingress controller. I created a service following this Github repo. My Nginx Ingress Controller DaemonSet yaml file is:

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: nginx-ingress
  namespace: nginx-ingress
spec:
  selector:
    matchLabels:
      app: nginx-ingress
  template:
    metadata:
      labels:
        app: nginx-ingress
      annotations:
        nginx.ingress.kubernetes.io/custom-http-errors: "404,415"
        nginx.ingress.kubernetes.io/default-backend: nginx-ingress/custom-default-backend
        #prometheus.io/scrape: "true"
        #prometheus.io/port: "9113"
        #prometheus.io/scheme: http
    spec:
      serviceAccountName: nginx-ingress
      containers:
      - image: 10.207.149.80:80/smas/nginx/nginx-ingress:1.12.0
        imagePullPolicy: IfNotPresent
        name: nginx-ingress
        ports:
        - name: http
          containerPort: 80
          hostPort: 80
        - name: https
          containerPort: 443
          hostPort: 443
        - name: readiness-port
          containerPort: 8081
        - name: prometheus
          containerPort: 9113
        readinessProbe:
         httpGet:
           path: /nginx-ready
           port: readiness-port
         periodSeconds: 1
        securityContext:
          allowPrivilegeEscalation: true
          runAsUser: 101 #nginx
          capabilities:
            drop:
            - ALL
            add:
            - NET_BIND_SERVICE
        env:
        - name: POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        - name: POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        args:
          - -nginx-configmaps=$(POD_NAMESPACE)/nginx-config
          - -default-server-tls-secret=$(POD_NAMESPACE)/default-server-secret

It does not work :(

I also read Command-line Arguments tutorials and ConfigMap Resource tutorial but I do not see any argument/ config that allowing me to specify my custom defautl backend service.

Could you show me how to setup custom defautl backend service for my DaemonSet ?

Many thanks.

0

There are 0 answers