Tuesday, August 3, 2021

Getting started with Kubernetes dev environment using Docker Desktop

This is 3rd or 4th time, I am learning Kubernetes (hereafter mostly refer as K8s short form) hands-on sessions. Every time I learn the kubectl command and its options, I forget as there were no chances to apply in the day job. Another mistake I did all those times was missing to post learning to this blog.

Hope this time I will get a chance to use it in the day job and not miss posting the Kubernetes learning on to this blog.

This post is very basic. The aim is to get started with the Kubernetes development environment using Docker Desktop. Below are the steps at a high level to get started. Detailed steps with videos are available on the internet.

Install Docker with Kubernetes support

The first step is to download the Docker Desktop and install it. It's straightforward as installing any other software in Windows. It is free and requires virtualization to be turned on. 
In order to run the Linux containers, it is better to enable the WSL2 backend. There are detailed instructions available to get it done including how to enable WSL2 on Windows.

Kubernetes support is only available when we use the Linux container mode. Note that Docker Desktop supports both Windows and Linux modes.

Points to note

  • Better don't take the experimental build. Stay one version below the current stable version.
  • This installation will modify our hosts file in Windows. Better not change anything.
  • It is better to have an 8 core 16GB machine to work smoothly with container workloads.

Install Kubernetes dashboard

For beginners, it is easy to understand what is going on in the K8s cluster by looking at a UI dashboard application. K8s don't install a UI dashboard by default. That dashboard can be installed by using the below PowerShell commands.


#Apply the dashboard
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.2.0/aio/deploy/recommended.yaml
#Generate token and store into C:\Users\<user name>\.kube. File name config (no extensions)
$TOKEN=((kubectl -n kube-system describe secret default | Select-String "token:") -split " +")[1]
kubectl config set-credentials docker-desktop --token="${TOKEN}"
#run below command
kubectl proxy
# Navigate to http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/#/workloads?namespace=default
# When prompted select auth from file and browse to the config file updated in previous step.

It first applies a deployment into the K8s cluster using a file present in the URL. Then Generate a credential file into the location indicated in the snippet. Then set those credentials. Finally, it opens the UI dashboard in the browser. In the UI dashboard application, select the config file from the mentioned location to authenticate.

Have a look around the dashboard to familiarize yourself with the concepts.

Test with our first deployment

Now let us try to push our first deployment to the K8s cluster. Below goes the PowerShell to push the deployment and to test whether the container worked or not.


kubectl apply -f https://k8s.io/examples/controllers/job.yaml

kubectl describe jobs/pi
$pods=$(kubectl get pods --selector=job-name=pi --output=jsonpath='{.items[*].metadata.name}')
echo $pods
kubectl logs $pods


What this script does is deploy the K8s definition file from the online source of k8s.io. Then lists the job. Then it gets the corresponding pod, to show the logs from it. If all goes well, we will see the value of pi with a large number of decimal points.

Points to note

  • We are not using our own K8s definition file. It is getting downloaded from the internet. Make sure the location is accessible especially if on the enterprise network.
  • Sometimes the pod may take some more time to start. In that scenario, the kubectl logs $pods will error out. Just wait for some time to get the pod to start. Else run kubectl get pods command to see whether a pod with the prefix of 'pi-' exists or not.

References

No comments: