Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform fails to create ingress (could not find the requested resource ingresses.extensions)

I'm using minikube locally. The following is the .tf file I use to create my kubernetes cluster:

provider "kubernetes" {
  config_path = "~/.kube/config"
}

resource "kubernetes_namespace" "tfs" {
  metadata {
    name = "tfs" # terraform-sandbox
  }
}

resource "kubernetes_deployment" "golang_webapp" {
  metadata {
    name      = "golang-webapp"
    namespace = "tfs"
    labels = {
      app = "webapp"
    }
  }
  spec {
    replicas = 3
    selector {
      match_labels = {
        app = "webapp"
      }
    }
    template {
      metadata {
        labels = {
          app = "webapp"
        }
      }
      spec {
        container {
          image             = "golang-docker-example"
          name              = "golang-webapp"
          image_pull_policy = "Never" # this is set so that kuberenetes wont try to download the image but use the localy built one
          liveness_probe {
            http_get {
              path = "/"
              port = 8080
            }
            initial_delay_seconds = 15
            period_seconds        = 15
          }

          readiness_probe {
            http_get {
              path = "/"
              port = 8080
            }
            initial_delay_seconds = 3
            period_seconds        = 3
          }
        }
      }
    }
  }
}


resource "kubernetes_service" "golang_webapp" {
  metadata {
    name      = "golang-webapp"
    namespace = "tfs"
    labels = {
      app = "webapp_ingress"
    }
  }
  spec {
    selector = {
      app = kubernetes_deployment.golang_webapp.metadata.0.labels.app
    }
    port {
      port        = 8080
      target_port = 8080
      protocol    = "TCP"
    }
    # type = "ClusterIP"
    type = "NodePort"
  }
}

resource "kubernetes_ingress" "main_ingress" {
  metadata {
    name      = "main-ingress"
    namespace = "tfs"
  }

  spec {
    rule {
      http {
        path {
          backend {
            service_name = "golang-webapp"
            service_port = 8080
          }
          path = "/golang-webapp"
        }
      }
    }
  }
}

When executing terraform apply, I am successfully able to create all of the resources except for the ingress.

The error is:

Error: Failed to create Ingress 'tfs/main-ingress' because: the server could not find the requested resource (post ingresses.extensions)

with kubernetes_ingress.main_ingress,
   on main.tf line 86, in resource "kubernetes_ingress" "main_ingress":
   86: resource "kubernetes_ingress" "main_ingress" {

When I try to create an ingress service with kubectl using the same configuration as the one above (only in .yaml and using the kubectl apply command) it works, so it seems that kubectl & minikube are able to create this type of ingress, but terraform cant for some reason...

Thanks in advance for any help!

Edit 1:

adding the .yaml that I'm able to create the ingress with

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example-ingress
  namespace: tfs
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
  rules:
    - http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: golang-webapp
                port:
                  number: 8080
like image 443
Max Avatar asked Jan 25 '26 15:01

Max


1 Answers

The kubernetes_ingress resource generate an ingress with an apiVersion which is not supported by your kubernetes cluster. You have to use [kubernetes_ingress_v1][1] resource which looks similar to kubernetes_ingress resource with some diferences. For your example, it will be like this :

resource "kubernetes_ingress_v1" "jenkins-ingress" {
   metadata {
      name        = "example-ingress"
      namespace   = "tfs"
      annotations = {
        "nginx.ingress.kubernetes.io/rewrite-target" = "/$1"
      }
   }
   spec {
      rule {
        http {
         path {
           path = "/"
           backend {
             service {
               name = "golang-webapp"
               port {
                 number = 8080
               }
             }
           }
        }
      }
    }
  }
}
like image 177
andolsi zied Avatar answered Jan 29 '26 13:01

andolsi zied



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!