Kubernetes IngressClass | error="ingress does not contain a valid IngressClass"

If you have come across this error after updating Kubernetes to the latest version, this solution should work. IngressClass is ingress resource that was added in Kubernetes 1.18 helps to target a specific Ingress controller while running multiple ingress controller in a single cluster. Earlier kubernetes.io/ingress.class annotation was used to specify the ingress controller which is deprecated by 1.18. Here we are directly mentioning the name of the controller rather than creating a the resource.

Solution

Setup 1 – Create IngressClass resource

First we have to create IngressClass ingress resource. (Check if there is any kubectl get ingressclass -A -o yaml)

apiVersion: networking.k8s.io/v1
kind: IngressClass
metadata:
  name: nginx
spec:
  # your controller name.
  controller: k8s.io/ingress-nginx

Setup 2 – Create/Update Ingress

Update ingress resource spec.ingressClassName with ingress class name nginx. This should resolve the problem.

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: minimal-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  ingressClassName: nginx
  rules:
  - http:
      paths:
      - path: /testpath
        pathType: Prefix
        backend:
          service:
            name: test
            port:
              number: 80

Set Default IngressClass

Instead of assigning ingressClassName inside ingress resource, we can set a specific ingress controller as default by adding annotation ingressclass.kubernetes.io/is-default-class to "true".

apiVersion: networking.k8s.io/v1
kind: IngressClass
metadata:
  name: nginx
  annotations:
    ingressclass.kubernetes.io/is-default-class: "true"
spec:
  # use your controller name.
  controller: k8s.io/ingress-nginx

One of the above solution should fix the invalid ingress class error. Please note that both Kubernetes and Ingress controller should be updated to the latest version to make sure this works.

References

Date: 2022-07-11