Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Run kubectl apply commands in terraform

Tags:

I have developed a terraform script to create a k8 cluster on GKE.

Post successful creation of cluster, I have set of yaml files to be applied on k8 cluster.

How can I invoke the below command in my terraform script?

kubectl create <.yaml> 
like image 694
Sunil Gajula Avatar asked Jan 08 '19 15:01

Sunil Gajula


People also ask

How do you run a kubectl command from Terraform?

You can use the Terraform resources template_file and null_resource. Notice that I'm using the trigger to run the kubectl command always you modify the template (you may want to replace create with apply). But maybe the best way is to use the Kubernetes provider.

What is kubectl apply command?

The command set kubectl apply is used at a terminal's command-line window to create or modify Kubernetes resources defined in a manifest file. This is called a declarative usage. The state of the resource is declared in the manifest file, then kubectl apply is used to implement that state.

Does Terraform use kubectl?

Kubectl Provider. This provider is the best way of managing Kubernetes resources in Terraform, by allowing you to use the thing Kubernetes loves best - yaml! This core of this provider is the kubectl_manifest resource, allowing free-form yaml to be processed and applied against Kubernetes.

How do Terraform and Kubernetes work together?

Terraform can be used to manage Kubernetes infrastructure, helping you to orchestrate your applications and run them at scale. This alleviates some of the challenges of running Kubernetes, including problems like detecting configuration drift, that is, planned or unplanned changes.


1 Answers

You can use the Terraform kubectl third party provider. Follow the installation instructions here: Kubectl Terraform Provider

Then simply define a kubectl_manifest pointing to your YAML file like:

# Get your cluster-info data "google_container_cluster" "my_cluster" {   name     = "my-cluster"   location = "us-east1-a" }  # Same parameters as kubernetes provider provider "kubectl" {   load_config_file       = false   host                   = "https://${data.google_container_cluster.my_cluster.endpoint}"   token                  = "${data.google_container_cluster.my_cluster.access_token}"   cluster_ca_certificate = "${base64decode(data.google_container_cluster.my_cluster.master_auth.0.cluster_ca_certificate)}" }  resource "kubectl_manifest" "my_service" {     yaml_body = file("${path.module}/my_service.yaml") } 

This approach has the big advantage that everything is obtained dynamically and does not rely on any local config file (very important if you run Terraform in a CI/CD server or to manage a multicluster environment).

Multi-object manifest files

The kubectl provider also offers data sources that help to handle files with multiple objest very easily. From the docs kubectl_filename_list:

data "kubectl_filename_list" "manifests" {     pattern = "./manifests/*.yaml" }  resource "kubectl_manifest" "test" {     count = length(data.kubectl_filename_list.manifests.matches)     yaml_body = file(element(data.kubectl_filename_list.manifests.matches, count.index)) } 

Extra points: You can templatize your yaml files. I interpolate the cluster name in the multi-resource autoscaler yaml file as follows:

resource "kubectl_manifest" "autoscaler" {   yaml_body = templatefile("${path.module}/autoscaler.yaml", {cluster_name = var.cluster_name }) } 
like image 123
david_g Avatar answered Oct 28 '22 23:10

david_g