简体   繁体   中英

deploy kubernetes ingress with terraform

I'm trying deploy kube.netes 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 kube.netes ingress with terraform.

The issue here is that the example in YML is using the proper API version, ie, 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:

扩展v1beta1

If you look further down in the documentation, you will see networking/v1 and in the resource section kube.netes_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
              }
            }
          }
        }
      }
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM