If you're getting started with Kubernetes, it is a good idea to have a basic testing environment on a local machine that you can spin up quickly.
This can be helpful to have a learning environment for classes that may not provide a lab environment (or one that suggests an overcomplicated cloud environment), or simply for personal experimentation.
This guide will focus on using a bash terminal in Linux, but it will likely not be much different for the macOS terminal or Windows Subsystem for Linux environments.
Make sure you have kubectl installed, as the tools in this guide will make use of the cluster context set for kubectl:
Next, you will need to install KinD, the tool that we will use to create a local Kubernetes cluster:
Finally, install Telepresence. This will be used to expose the service objects running in your KinD cluster, much like a cloud-hosted solution would create external load balancer objects:
Once you have these tools installed and you've verified that they're working, you can start by creating a KinD cluster. For most Kubernetes test cases, it's a good idea to have multiple worker nodes. This might change depending on what you're testing, but here is a good example config file you can use for a cluster with one control plane node and three worker nodes.
Save this yaml as a file in your working directory, and call it something like 3worker-config.yaml:
- role: control-plane
- role: worker
- role: worker
- role: worker
Use the file we just saved to create a cluster:
kind create cluster --config 3worker-config.yaml
After a few minutes, KinD will finish creating the cluster, and should automatically set your kubectl context to use it. Verify that the cluster is created, kubectl is talking to it, and the nodes are ready:
kubectl get nodes
You should see a kind-control plane and three kind-worker nodes.
Next, to expose services on the host machine, you can run telepresence:
Telepresence will use the same context that kubectl is using to automatically set up a tunnel to access any exposed services in your cluster.
You now have a Kubernetes test cluster that is ready to go!
To test your new cluster, we'll install and expose a simple nginx container. First, create a pod:
kubectl run nginx --image=nginx
Wait a few minutes until the output of "kubectl get pods" shows that the new nginx pod is Ready.
We can then expose the pod with a Service endpoint:
kubectl expose pod nginx --port=80
Once you've verified that the new service is ready with "kubectl get svc", find the endpoint:
kubectl get ep
The output should look something like this:
$ kubectl get ep
NAME ENDPOINTS AGE
kubernetes 172.18.0.4:6443 11m
nginx 10.244.1.2:80 5m21s
We can then open a web browser on the host machine and navigate to the endpoint address for your nginx pod. In my case it will be 10.244.1.2. Navigating to "nginx.default" should also work for this example.
The "Welcome to nginx!" page should be displayed in your browser.
Once we're done with the cluster, we can first close the Telepresence connection:
Then we can delete the KinD cluster:
kind delete cluster