Skip to content

tracypholmes/terraform-k8s-demo

Repository files navigation

Automating Deployments with Terraform Walkthrough

Introduction

Terraform uses providers to interface with various APIs and exposing resources. A provider is versioned and included as part of a configuration file.

provider "kubernetes" {
  config_path    = "~/.kube/config"
  config_context = "docker-desktop"
}

The Kubernetes Provider allows Terraform to make calls to the Kubernetes API.

Getting Started

In this section, we'll make sure you have Terraform and Docker Desktop installed.

Prerequisites

  • Terraform 0.14
  • Docker Desktop for Windows/MacOS
  • Your favorite terminal or IDE

If you don't already have Terraform installed, you can get instructions from here.

I use Windows and WSL on a daily basis, so have Docker Desktop installed to make it easier on me as Kubernetes integration is included. You can find the download and instructions here. Note: You are more than welcome to use your own install of Kubernetes. If you do so, make sure you update the path in the provider.tf file.

Initialize Terraform

Terraform requires initialization before any configuration can be applied. Initialization:

  1. Installs any required providers.
  2. Creates the local state directory or connects to the remote state store.

Review the provider.tf file and examine the provider configuration. The Kubernetes provider configuration includes version and authentication parameters.

Next, initialize Terraform terminal.

terraform init

This installs the Terraform Kubernetes provider and creates a .terraform directory for local state management.

When the provider has completed installation.

Terraform allows a dry-run of changes and displays the differences. This is done through executing terraform plan.

For a full reference, see CLI documentation.

Plan the Deployment

In the terminal, run:

terraform plan -out=nginx.tfplan

It should output two changes and create a binary file called nginx.tfplan.

Terraform uses graph theory to create and modify resources in the correct order.

For more information about the Terraform resource graph, see this documentation.

View the Graph

Terraform uses graph theory to determine the order by which changes and resources can be applied and created.

If you would like to generate the graph locally, make sure you have Graphviz installed and then run this command:

terraform graph | dot -Tsvg > graph.svg

Alternately, you can run terraform graph. Copy the output and paste it into the browser at GraphivizOnline.

What do you notice about the graph?

To apply the changes, use terraform apply. This command will execute on plan.

See the CLI documentation for more information.

Apply the Deployment

In the terminal, run:

terraform apply

This will apply the changes by deploying the nginx web server to the Kubernetes cluster.

In the terminal, execute:

kubectl get pods

This should have a single Nginx pod running. To check the Nginx service, run:

kubectl get service

We are going to change the state backend to demonstrate the importance of configuring and maintaining a state file.

For more information about backend types, see Terraform documentation.

Change the Backend

Maintaining state for Terraform is critical to demonstrate and evaluate the differences in infrastructure configuration.

Let's change the backend state to demonstrate how we might configure a new backend. Before this step, we destroyed all of the previous infrastructure and state, so we start completely anew.

Rename backend.remote to backend.tf.

In the terminal, run:

terraform init terraform apply

We're pointing to a remote backend to be used as a bucket. This remote backend is a workspace that lives in Terraform Cloud.

Update Variables

In this example, variables will be updated in the variables.tf file.

In the variables.tf file, update the and replicas= to the following:

replicas=3

Note that the name is a string and must be in quotes.

Save the file by using Ctrl+S.

Next, in your terminal run:

terraform apply

Type "yes" to apply the changes. You should see it recreate many of the resources, including the name of the application and the number of replicas.

You can destroy resources with Terraform as well. As long as they are in Terraform state, Terraform will clean up the resources.

For more information about Terraform and the destroy command, see Terraform documentation.

Destroy Resources

Finally, destroy the resources in the Kubernetes cluster.

You want to destroy the resources in order to clean up any unused infrastructure.

In the terminal, type:

$ terraform destroy

Enter "yes" on the prompt to destroy the resources you've created in the Kubernetes cluster.

In the terminal, you can type:

$ kubectl get pods
No resources found in default namespace.
$ kubectl get service
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   xx.xx.xx.xx   <none>        xx/TCP   xx

To see that the nginx pod and service has been removed.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages