Hello-world in Kubernetes with Terraform, k3d and kpt

Ben Fraser
Feb 23 · 3 min read

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

Requirements:

Create a k3d cluster with Terraform:

Clone the source repo and navigate to it, initialise terraform, then apply the configuration:

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.

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

Curl it!

Use the ip and port from the Terraform outputs (in this case 127.0.0.1 aka localhost, and port 8043):

curl localhost:8043

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

Learning:

Explore the cluster and helloworld deployment with kubectl, or a kubernetes management tool such as k9s. Some example kubectl commands:

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:

Remove helloworld deployment with:

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

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/. Don’t forget to check out Ask-NFT, a mentorship ecosystem we’ve started

Ben Fraser

Written by

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/. Don’t forget to check out Ask-NFT, a mentorship ecosystem we’ve started

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