Automate Kubernetes Deployment using Terraform and Github Actions

GitHub Action + Dev 2023 Submission

What I built

I’ve created Terraform code that will create a namespace and deploys the Nginx server in the minikube cluster and TF code verification and deployment has been automated using GitHu…


This content originally appeared on DEV Community and was authored by Saravanan Gnanaguru

GitHub Action + Dev 2023 Submission

What I built

  • I've created Terraform code that will create a namespace and deploys the Nginx server in the minikube cluster and TF code verification and deployment has been automated using GitHub actions

How I built

  • Created a GitHub Actions workflow using the Marketplace Github actions plugins,
    • actions/checkout@v2.5.0 -> to Checkout the code
    • medyagh/setup-minikube@v0.0.13 -> to setup minikube
    • Azure/setup-kubectl@v3 -> to setup kubectl
    • hashicorp/setup-terraform@v2.0.2 -> to setup terraform
  • This workflow can be used in development environments, in which an Infra developer can create the Terraform code to deploy kubernetes workload. Once after creating the tf code, the developer can trigger the Terraform workflow, that will do the CI for Terraform code, and deploy the infra in minikube.
  • The kube config context has been created as a variable in Terraform, so it can be overridden with other Kubernetes Cluster config and contexts from Cloud providers like Amazon EKS or Azure AKS or GCP GKE Clusters.

Category Submission

DIY Deployments

App Link

Source code for the Repo is available here

Github Action Workflow Yaml

Screenshots

tf k8s workflow

Description

Triggering the Workflow

This workflow can be triggered from the actions tab, by providing the Terraform code directory as an input (Refer the screenshot above).
So it will run the below steps in the directory provided as input,

  1. Workflow installs, minikube, kubectl and terraform CLI executables needed to be used by the rest of workflow
  2. It runs terraform init command to download the kubernetes provider
  3. Then runs terraform validate command to check the tf code is valid or not
  4. After that it runs, terraform plan and terraform apply commands and performs the Kubernetes namespace creation and deploys the nginx server.
  5. Workflow also has terraform destroy command, that deletes the kubernetes infra created in the workflow

Workflow Dispatch

  • This workflow uses, workflow_displatch Github syntax - It is the sub block inside the on event triggering block, in which we can specify what are the inputs needed to trigger the workflow.
  • In the on event block workflow_dispatch section, we will be adding the inputs directory path, on which we are going to run our Terraform code validation
  • It is defaulted to the 'kubernetes' directory present in the repo for the ease of demo purpose.

Link to Source Code

Source code for the Repo is available here

Github Action Workflow Log

Github Action Workflow Yaml

GH Action workflow run

Permissive License

MIT License

Background

As mentioned earlier,
This workflow (or pipeline) can be configured for testing the terraform code pushed by DevOps engineers/SREs/Developers, and can be triggered whenever there is new tf code is pushed into a specific branch for Kubernetes workload management

Additional Resources/Info


This content originally appeared on DEV Community and was authored by Saravanan Gnanaguru


Print Share Comment Cite Upload Translate Updates
APA

Saravanan Gnanaguru | Sciencx (2023-05-22T19:03:40+00:00) Automate Kubernetes Deployment using Terraform and Github Actions. Retrieved from https://www.scien.cx/2023/05/22/automate-kubernetes-deployment-using-terraform-and-github-actions/

MLA
" » Automate Kubernetes Deployment using Terraform and Github Actions." Saravanan Gnanaguru | Sciencx - Monday May 22, 2023, https://www.scien.cx/2023/05/22/automate-kubernetes-deployment-using-terraform-and-github-actions/
HARVARD
Saravanan Gnanaguru | Sciencx Monday May 22, 2023 » Automate Kubernetes Deployment using Terraform and Github Actions., viewed ,<https://www.scien.cx/2023/05/22/automate-kubernetes-deployment-using-terraform-and-github-actions/>
VANCOUVER
Saravanan Gnanaguru | Sciencx - » Automate Kubernetes Deployment using Terraform and Github Actions. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/05/22/automate-kubernetes-deployment-using-terraform-and-github-actions/
CHICAGO
" » Automate Kubernetes Deployment using Terraform and Github Actions." Saravanan Gnanaguru | Sciencx - Accessed . https://www.scien.cx/2023/05/22/automate-kubernetes-deployment-using-terraform-and-github-actions/
IEEE
" » Automate Kubernetes Deployment using Terraform and Github Actions." Saravanan Gnanaguru | Sciencx [Online]. Available: https://www.scien.cx/2023/05/22/automate-kubernetes-deployment-using-terraform-and-github-actions/. [Accessed: ]
rf:citation
» Automate Kubernetes Deployment using Terraform and Github Actions | Saravanan Gnanaguru | Sciencx | https://www.scien.cx/2023/05/22/automate-kubernetes-deployment-using-terraform-and-github-actions/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.