3

I'm trying deploy kubernetes ingress with terraform. As described here link and my own variant:

resource "kubernetes_ingress" "node" {
  metadata {
    name = "node"
  }
  spec {
    ingress_class_name = "nginx"
    rule {
      host = "backend.io"
      http {
        path {
          path = "/"
          backend {
            service_name = kubernetes_service.node.metadata.0.name
            service_port = 3000
          }
        }
      }
    }
  }
}

error:

╷
│ Error: Failed to create Ingress 'default/node' because: the server could not find the requested resource (post ingresses.extensions)
│ 
│   with kubernetes_ingress.node,
│   on node.tf line 86, in resource "kubernetes_ingress" "node":
│   86: resource "kubernetes_ingress" "node" {
│ 
╵

it works:

kubectl apply -f file_below.yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: node
spec:
  ingressClassName: nginx
  rules:
  - host: backend.io
    http:
      paths:
      - path: /
        pathType: ImplementationSpecific
        backend:
            service:
              name: node
              port:
               number: 3000

Need some ideas about how to deploy kubernetes ingress with terraform.

1
  • Have you defined the provider block in terraform code and if so can you add that to the question as well? Commented Mar 16, 2022 at 20:15

1 Answer 1

8

The issue here is that the example in YML is using the proper API version, i.e., networking.k8s.io/v1, hence it works as you probably have a version of K8s higher than 1.19. It is available since that version, the extensions/v1beta1 that Ingress was a part of was deprecated in favor of networking.k8s.io/v1 in 1.22, as you can read here. As that is the case, your current Terraform code is using the old K8s API version for Ingress. You can see that on the left-hand side of the documentation menu:

extensionsv1beta1

If you look further down in the documentation, you will see networking/v1 and in the resource section kubernetes_ingress_v1. Changing the code you have in Terraform to use Ingress from the networking.k8s.io/v1, it becomes:

resource "kubernetes_ingress_v1" "node" {
  metadata {
    name = "node"
  }

  spec {
    ingress_class_name = "nginx"
    rule {
      host = "backend.io"
      http {
        path {
          path = "/*"
          path_type = "ImplementationSpecific"
          backend {
            service {
              name = kubernetes_service.node.metadata.0.name
              port {
                number = 3000
              }
            }
          }
        }
      }
    }
  }
}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.