GKE Autopilot Kubernetes Cluster with Pulumi Infrastructure as Code
This is the first part of articles to use Pulumi as infrastructure as Code (IaC) to create and maintain the infrastructure and deploy applications on Kubernetes cluster on the Google Cloud Platform GKE Autopilot and pipelines to automated everything with GitLab Pipelines.
Pulumi is one of best tool to use as infrastructure as code. This has been empowering and help many DevOps and Engineers teams around the world turn our lives better, less time on provisioning and operations toil, and provide to platform team delivery self-service infra products in efficient matters.
Installing required tools
- Google Cloud SDK https://cloud.google.com/sdk
- Pulumi https://www.pulumi.com/docs/get-started/install/
- Kubectl https://kubernetes.io/docs/tasks/tools/install-kubectl/
- Node https://nodejs.org/en/download/ (we will use Typescript as language on Pulumi)
Start with Pulumi
Let's start with a gcp template
pulumi new gcp-typescript
Follow the steps in the cli for name of project, description, the stack and GCP project name
Pulumi will install all dependencies we need to handle GCP resources, but still we need Kubernetes modules, let's install.
npm install --save @pulumi/kubernetes @pulumi/kubernetesx
The index.ts file is the example to create a bucket, let's play with that to familiarize with Pulumi.
Run the command in our terminal:
pulumi up
Just run remove the test
pulumi destroy
Organize directory structure
Now we will to organize the directories to separate GCP resources and kubernetes resources.
Looks like this:
├── Pulumi.dev.yaml
├── Pulumi.yaml
├── gcp
│ └── gke
│ └── index.ts
├── index.ts
├── k8s
│ └── guestbook
│ ├── config.ts
│ ├── index.ts
│ ├── namespace.ts
│ └── redis.ts
├── package-lock.json
├── package.json
└── tsconfig.json
Configure the GKE Autopilot Cluster
The Autopilot we have the benefits of Google will handle our nodes, it's a fully managed Kubernetes cluster.
Of course there some trade-offs, for example you are not allowed to use Istio as service mesh, cert-manager and others that need mutatingwebhookconfigurations, you can check all features and limitations here: https://cloud.google.com/kubernetes-engine/docs/concepts/autopilot-overview
First, we will setup variables on the file Pulumi.dev.yaml, the variable for the min gke kubernetes version in your case 1.19 (this is the stable version on the date of the article created).
Pay attention to the name infrastructure: this is must be the same name as your Pulumi project name in the file Pulumi.yaml
config:
gcp:project: {REPLACE_FOR_YOUR_GCP_PROJECT}
infrastructure:gke-min-version: '1.19'
So let's edit your gcp/gke/index.ts file
And the root index.ts
export * from './gcp/gke';
and run the command up
pulumi up
This process could take a few minutes to create a GKE cluster.
Configure the application
For the exemplification, we will use the guestbook application (in the next episodes we will adding more complex applications).
Configure variables:
As the same variable for gke-min-version for the cluster, we will create just one, the number of replicas for the application.
pulumi config -s dev set --path guestbook.replicas 1
To simplify the process of write manifests files we will use the package pulumi/kubernetesx.
Remember the structure of directories, the files are on the k8s/guestbook
Change the file index.ts to add the entry for the application
export * from './gcp/gke';
export * from './k8s/guestbook';
And run up command:
pulumi up
The output response contains the IP to access the application.
To clean up just run the destroy command:
pulumi destroy
Conclusion
This is the first introduction of what we can able to do with Pulumi as infrastructure as code.
In the next episodes, we will explore more functionalities of Pulumi combine the pipeline on GitLab.
Here you can see a free and good Pulumi Crash Course on Youtube: https://www.youtube.com/channel/UCSt5XpbeUuRQ-CndGtYFL8w