Managing Kubernetes objects on GKE with Terraform
--
It is well known that Infrastructure as code help you to have a more reliable and reproducible environment.
Terraform has several providers to interact with infrastructure, and one of them is the Kubernetes provider.
Authentication
There are generally two ways to configure the Kubernetes provider.
By default, Terraform will try to load the credentials~/.kube/config
, so you will not need to set any other information in the provider:
provider "kubernetes" {
}
Depending on whether you have the current context set this may require config_context_auth_info
and/or config_context_cluster
and/or config_context
as:
provider "kubernetes" {
config_context_auth_info = "ops"
config_context_cluster = "mycluster"
}
The other way is when you statically define TLS certificate credentials:
provider "kubernetes" {
host = "https://104.196.242.174"
client_certificate = "${file("~/.kube/client-cert.pem")}"
client_key = "${file("~/.kube/client-key.pem")}"
cluster_ca_certificate = "${file("~/.kube/cluster-ca-cert.pem")}"
}
or with username and password
provider "kubernetes" {
host = "https://104.196.242.174"
username = "username"
password = "password"
}
Managing namespaces on GKE
For this example, we are going to create namespaces from Terraform:
First, you will need to fetch the credentials from your cluster.
gcloud beta container clusters get-credentials <cluster_name> --region <region> --project <project_id>
Let’s create our file provider.tf with the latest provider version
provider "kubernetes" {
version = "1.10"
}
And the namespaces.tf file:
resource "kubernetes_namespace" "app" {
metadata {
labels = {
name = "app"
}
name = "app"
}
}
That's it! really simple isn’t it?
Conclusion
It is really useful when you are already managing your K8s cluster with Terraform, so you can start managing the K8s configuration as well.
You can find more examples here and also other resources as Pod, Ingress, Role, Secret, etc
Let me know your thoughts!