Using Terraform to deploy a docker image on Google Kubernetes Engine
Introduction to Terraform
Terraform is an Infrastructure as Code (IaC) tool developed by HashiCorp. It allows you to build, change, and version your infrastructure safely and efficiently.- Human-Readable Configuration Files: Terraform lets you define both cloud and on-prem resources in human-readable configuration files that you can version, reuse, and share.
- Multi-Cloud Support: Terraform can manage infrastructure on multiple cloud platforms. Providers enable Terraform to work with virtually any platform or service with an accessible API.
- Lifecycle Management: The core Terraform workflow consists of three stages:
- Write: Define resources across multiple cloud providers and services.
- Plan: Terraform creates an execution plan describing what it will create, update, or destroy.
- Apply: On approval, Terraform performs the proposed operations in the correct order, respecting any resource dependencies.
- State Management: Terraform keeps track of your real infrastructure in a state file, which acts as a source of truth for your environment.
- Provider Ecosystem: HashiCorp and the Terraform community have written thousands of providers to manage many different types of resources and services.
Prerequisites
Before proceeding, you need to prepare the following:
- A Google Cloud account with billing enabled and necessary services such as Compute Engine, Kubernetes Engine enabled.
- Installed gcloud, kubectl.
- Understanding of Google Kubernetes Engine, clusters, and Docker. If you're unsure, you can refer to this article to gain some basic knowledge.
Deploying a Docker Image to GKE
1. Login to gcloud
In this step, you can use an existing project, or if you don't want to affect existing projects, it's better to create a new project. After completing the deployment, you can simply delete the project to release resources.
Once you've determined which project to work with, switch to that project, retrieve the project ID, and log in to use it with Terraform.
2. Terraform codebase
Create a Terraform project with the following file contents:
First, let's create a file named `provider.tf` to define the information of the Google Cloud Provider.
`variable.tf` file to define the variables we'll use in this project.
Create a file named `terraform.tfvars` to define the default variable values. You'll need to change these values to fit your needs.
If you want to learn about building a Docker image and publishing it to the GCP Container Registry for use in this article, you can find it here.
The `cluster.tf` file is used to initialize the Kubernetes cluster.
The `k8s.tf` file is used to create Pod Deployment and LoadBalancer Service.
Here, `kubernetes_deployment_v1` defines the Docker image to deploy, while the LoadBalancer Service is used to map ports between the Docker container for external access.
Next, create an additional file named `output.tf` to print out the necessary information after the execution process is successful.
3. Execute Terraform commands
Please execute each of the following commands to proceed with the deployment on Google Cloud:
After successfully executing, you will see the following results:
You can check if the cluster has been initialized as follows:
Check Pods, Deployments, and Docker container running:
View information about the LoadBalancer Service that has been created:
Please access the EXTERNAL-IP field to check if the NodeJS Server has been deployed successfully. After executing the `terraform apply` command, you will also see similar results in the `load_balancer_hostname` field.
4. Clean up resources
To delete the resources created by Terraform, please execute the following command:
Conclusion
If you have any suggestions or questions regarding the content of the article, please don't hesitate to leave a comment below!
Comments
Post a Comment