Kubernetes Cloud Autoscalar in Terraform – Part 1

Cluster Autoscaler - It is a component that automatically adjusts the size of a Kubernetes Cluster so that all pods have a place to run and there are no unneeded nodes. It can be created by using the YAML file, Helm Chart, or Terraform. The following example creates the Cloud Autoscalar deployment using Terraform dynamic Modules.

Corresponding to https://github.com/kubernetes/autoscaler/blob/master/cluster-autoscaler/cloudprovider/aws/examples/cluster-autoscaler-autodiscover.yaml
Resource kubernetes_deployment:-
Plain Text
 




xxxxxxxxxx
1
75


 
1
cluster-autoscaler.tf:-
2
resource "kubernetes_deployment" "cluster-autoscaler" {
3
  metadata {
4
    name      = var.cluster-autoscaler-data["cluster-autoscaler-name"]
5
    namespace = var.cluster-autoscaler-data["namespace"]
6
    labels = {
7
      "app" = var.cluster-autoscaler-data["cluster-autoscaler-label"]
8
    }
9
  }
10
 
          
11
  spec {
12
    replicas = 1
13
    selector {
14
      match_labels = {
15
        "app" = var.cluster-autoscaler-data["cluster-autoscaler-label"]
16
      }
17
    }
18
 
          
19
    template {
20
      metadata {
21
        labels = {
22
          "app" = var.cluster-autoscaler-data["cluster-autoscaler-label"]
23
        }
24
        annotations = {
25
          "prometheus.io/port"   = "8085"
26
          "prometheus.io/scrape" = "true"
27
        }
28
      }
29
 
          
30
      spec {
31
        automount_service_account_token  = true
32
        termination_grace_period_seconds = 300
33
        service_account_name             = var.service-account-name
34
 
          
35
        container {
36
          image = var.cluster-autoscaler-data["image_name"]
37
          name  = "cluster-autoscaler"
38
          command = ["./cluster-autoscaler",
39
            "--v=4",
40
            " --stderrthreshold=info",
41
            "--cloud-provider=aws",
42
            "--skip-nodes-with-local-storage=false",
43
            "--expander=least-waste",
44
            "--node-group-auto-discovery=asg:tag=k8s.io/cluster-autoscaler/enabled,k8s.io/cluster-autoscaler/${var.eks_cluster_name}"
45
          ]
46
          resources {
47
            limits {
48
              cpu    = "100m"
49
              memory = "300Mi"
50
            }
51
            requests {
52
              cpu    = "100m"
53
              memory = "300Mi"
54
            }
55
          }
56
          volume_mount {
57
            name       = "ssl-certs"
58
            mount_path = "/etc/ssl/certs/ca-certificates.crt"
59
            read_only  = "true"
60
          }
61
        }
62
        volume {
63
          name = "ssl-certs"
64
          host_path {
65
            path = "/etc/ssl/certs/ca-bundle.crt"
66
          }
67
        }
68
      }
69
    }
70
  }
71
}
72
variable.tf :-
73
variable "cluster-autoscaler-data" {}
74
variable "service-account-name" {}
75
variable "eks_cluster_name" {}


Service Account Module:-