skip to Main Content

I am new to K8S and Terraform. I installed ingress_nginx on K8S Cluster running on Bare-metal.

[root@control02 ~]# kubectl get svc -n ingress-nginx
    NAME                                 TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                      AGE
    docker-hello-world-svc               NodePort    10.xx.xx.121     <none>        8086:30333/TCP               13d
    ingress-nginx-controller             NodePort    10.xx.xx.124     <none>        80:31545/TCP,443:30198/TCP   13d
    ingress-nginx-controller-admission   ClusterIP   10.xx.xx.85      <none>        443/TCP                      13d

I created Deployment, Service and Ingress and am able to access the docker-hello-world-svc from browser successfully. Ingress.yaml is given below

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: hello-world-ing
  annotations:
    kubernetes.io/ingress.class: "nginx"
  namespace: ingress-nginx
spec:
  #ingressClassName : nginx
  rules:
  - host: foo.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
           name: docker-hello-world-svc
           port:
            number: 8086

My requirement is to containerize our PHP based applications on K8S Cluster.

  1. Creating ingress via Terraform’s resource "kubernetes_ingress" "web" and ingress.yaml:kubernetes.io/ingress.class are same (or) are they different?

  2. How can I create ‘ingress’ on K8S Cluster machine using Terraform ?

    For example, when I trigger a job from GitLab, Terraform should create a new "resource kubernetes_ingress" on K8S Cluster or Control-Plane machine. Is this possible ?

Kindly clarify on the queries mentioned above and let me know if my understanding is wrong

3

Answers


  1. Chosen as BEST ANSWER

    I was able to create the service on existing K8S Cluster (Bare metal) using the following code

    K8S Cluster was setup on 192.168.xxx.xxx on which I created a service example. We need to mention the 'host' parameter inside 'kubernetes' block

    provider "kubernetes" {
        **host = "https://192.168.xxx.xxx:6443"**
        cluster_ca_certificate = "${base64decode(var.cluster_ca_certificate)}"
        client_certificate = "${base64decode(var.client_certificate)}"
        client_key = "${base64decode(var.client_key)}"
    }
      
      resource "kubernetes_service" "example" {
        metadata {
          name = "example"
        }
        spec {
          port {
            port = 8585
            target_port = 80
          }
          type = "ClusterIP"
        }
    }
    

  2. The ingress.class is needed to let the nginx ingress controller understand thathe need to handle this resource.

    To create an ingress with terraform you can use the following

    resource "kubernetes_ingress" "ingress" {
      metadata {
        name      = "ingress-name"
        namespace = "ingress-namespace"
        labels = {
          app = "some-label-app"
        }
        annotations = {
          "kubernetes.io/ingress.class" : "nginx"
        }
      }
    
      spec {
        rule {
          host = "foo.com"
          http {
            path {
              backend {
                service_name = "svc"
                service_port = "http"
              }
            }
          }
        }
      }
    }
    
    Login or Signup to reply.
  3. for,

    resource "kubernetes_ingress"
    

    this,

    metadata {
       annotations = {
         "kubernetes.io/ingress.class" : "nginx"
       }
    }
    

    should now be,

    spec {
      ingress_class_name = "nginx"
    }
    

    see,

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search