Creating a GKE Cluster with GitHub Actions
By: Pavan Kumar
Tags: Kubernetes, Github, DevOps, Cloud Computing, Gcp

Automating Kubernetes Cluster creation and Bootstrapping using GitHub Actions

GitHub Actions allows you to design your CI and CD workflows directly in your GitHub repositories. GitHub actions are entirely integrated with GitHub. Build, Test, and Deploy can be done directly from GitHub. So your CI/CD workflows can be at the same place where your source code exists. The CI/CD Pipeline can be triggered with events like pull requests or merging of branches etc. In this article, we will be using the GitHub actions to create a GKE Cluster with terraform and bootstrap the cluster with some tools like Istio and flagger.

What is the entire story all about? (TLDR)

GitHub Actions allows you to design your CI and CD workflows directly in your GitHub repositories. Build, Test, and Deploy can be done directly from GitHub. We will create a GKE Cluster ( Using terraform ) with GitHub actions and bootstrap the cluster by installing components like Istio, Flagger, etc.

Prerequisites

GCP Account GitHub Account Any remote backend to store your state ( Optional )

DEMO

In this demo, we are going to provision a GKE cluster first and then add a set of Preemptible nodes to the same provisioned cluster.

You can fork my repo that has both the terraform and GitHub actions file.

This is a sample snippet of my terraform code. This would create a GKE cluster and then add a node pool to the cluster created through our terraform. But how do we automate this? How do we ensure that our terraform code doesn't have any errors? How do we ensure that our terraform code present in the master branch doesn't have any formatting issues? How do we ensure that any pull request raised to the master branch are properly tested before merging? And finally, how do we automate the provisioning of the GKE cluster and the node pool? The solution is GitHub Actions.

Let us now take a peek at what our GitHub action is doing here

name: The name of the GitHub Action. on: The name of the GitHub event that triggers this workflow. push.paths: The workflow would be triggered if any changes are made to this path. branches: Trigger the workflow on Push only for the main branch. pull_request: Trigger the workflow on a pull request event. jobs: A workflow is made of one or more jobs ( These define the set of steps to be performed ). jobs.terraform: The id of the job. jobs.terraform.env: A map of environment variables that are available to all steps in the job. runs-on: The type of machine on which the job should be running on. strategy.matrix: A matrix allows you to create multiple jobs by performing variable substitution in a single job definition. Here we tell our workflow to run in both gke_tf and worker_nodes folder. steps: A job contains a series of tasks called steps. steps.uses: It specifies the name of the prebuilt actions to be used. You can find a lot of them from GitHub Marketplace here ( https://github.com/marketplace?type=actions ). steps.run: Specifies the command to be run in the shell. steps.if: Prevents a job from running until a condition is met. bootstrap.needs: By default, all the jobs run in parallel. This needs section identifies the jobs that should be completed successfully before this job can run.

So our GitHub action does the following steps

Whenever a pull request is raised

a) Checks out the code from the master branch into the ubuntu-latest machine. ( name: Checkout )

b) Checks if our terraform code is formatted properly. ( name: Terraform Format )

c) Initializes the terraform working directory. ( name: Terraform Init )

d) Generates a speculative execution plan, showing what actions Terraform would take to apply the current configuration. ( name: Terraform Plan )

2. Whenever the pull request is merged

a) Creates or updates infrastructure according to Terraform configuration files in the current directory ( name: Terraform Apply )

b) Bootstrapping the cluster by Installing flagger and Istio operator ( name: bootstrap-cluster ). This job waits for the terraform job to be completed before it starts.

Let us now raise a Pull request by increasing the worker node count from 1 to 2 in either gke_tf or worker_nodes folder.

PR showing the status of our checks.

Terraform plan and format

Since the pull request is not yet merged, it would only check for the formatting issues and executes terraform plan.

Now the pull request has to be merged ( Manually ). How about we automate this too? Yes, you can now enable auto merging of pull requests from the GitHub UI.

Auto-merging of a Pull request.

As soon as our Pull request is merged, our GitHub action will start creating a cluster in your GCP account.

Creation of the cluster in GCP

If you login into the GCP console you should now be able to see your cluster in the GKE section with an additional node group too.

Conclusion

Thanks for reading my article. Hope you have liked it. Here are some of my other articles that may interest you.

Recommended