Hello-world in Kubernetes with Terraform, k3d and kpt

Ben Fraser
Feb 23 · 3 min read
Image for post
Image for post

Short, high-level tutorial for setting up a Kubernetes cluster locally, using Terraform and k3d, then deploying a simple hello-world application using kpt.


Create a k3d cluster with Terraform:

git clone https://gitlab.com/bengordonfraser/terraform-k3d.git ; cd terraform-k3dcp terraform.tfvars.example terraform.tfvarsterraform initterraform apply

Input “yes” when prompted, to accept the terraform apply

Take note of the outputs k3d_cluster_ip and k3d_host_lb_port, as these will be used to access the cluster later on.

Image for post
Image for post

Use kpt to download the hello-world package and deploy it to the k3d cluster:

mkdir hello-world-workspace; cd $_export SRC_REPO=https://gitlab.com/bengordonfraser/hello-world-kpt.gitkpt pkg get $SRC_REPO@v0.1.0 helloworld                                                                                                     kpt live apply helloworld
Image for post
Image for post

Curl it!

curl localhost:8043

Image for post
Image for post

note - the ingress can take a few seconds to instantiate, so retry if curl returns an empty response at first.


kubectl get nodeskubectl get podskubectl get svckubectl get ingress

Experiment with kpt to change the deployment configuration:

kpt cfg tree helloworldkpt cfg list-setters helloworldkpt cfg set helloworld replicas 5kpt cfg tree helloworld  --replicaskpt live apply helloworld

Explore the terraform-k3d repo to gain an understanding of how a simple infrastructure configuration can be put together using input variables and a local-exec provisioner.

Clean up:

kpt live destroy helloworld

cd .. to return to the terraform-k3d directory, then remove k3d cluster with:

terraform destroy

Nerd For Tech

From Confusion to Clarification

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store