Kubernetes is the industry standard tool for hosting containers with Azure and AWS both hosting their own platforms for this. But what if you want to test it locally (and you're on Windows), then Docker for Windows has got you covered...
Install Docker for Windows
Whilst Docker isn't the only option for managing containers, it is probably the most common, it can be installed from the official Docker website. I recommend going through the steps and set it up to use the Windows Subsystem for Linux (WSL2), I imagine it will work fine using a Hyper-V image but WSL2 will be quicker and it is the way that I configured my machine.
Once you've got Docker setup and working you'll be able to run some Docker commands.
To check everything is setup correctly type
Docker version
Into a PowerShell window and you should see something like this:
Client:Cloud integration: 1.0.17Version: 20.10.7API version: 1.41Go version: go1.16.4Git commit: f0df350Built: Wed Jun 2 12:00:56 2021OS/Arch: windows/amd64Context: desktop-linuxExperimental: trueServer: Docker Engine - CommunityEngine:Version: 20.10.7API version: 1.41 (minimum version 1.12)Go version: go1.13.15Git commit: b0f5bc3Built: Wed Jun 2 11:54:58 2021OS/Arch: linux/amd64Experimental: falsecontainerd:Version: 1.4.6GitCommit: d71fcd7d8303cbf684402823e425e9dd2e99285drunc:Version: 1.0.0-rc95GitCommit: b9ee9c6314599f1b4a7f497e1f1f856fe433d3b7docker-init:Version: 0.19.0GitCommit: de40ad0
Now that Docker is setup you can setup you will need to enable Kubernetes.
Click on the 'settings cog', then Kubernetes, then finally click 'Enable Kubernetes'
A message will appear stating that an internet connection is required and that it may take some time.
Soon you may notice a new icon in the bottom of the window Docker window:
At this point we've got Docker and Kubernetes installed, to confirm this run the command:
kubectl cluster-info
In the information returned you should see
- context:cluster: docker-desktopuser: docker-desktopname: docker-desktop
This is because when Kubernetes is installed it creates this context for you.
Now to be sure we are using the correct context type:
kubectl config use-context docker-desktop
It should respond with:
Switched to context "docker-desktop".
Now we can list the namespaces and list the pods:
kubectl get namespace
NAME STATUS AGE
default Active 27s
kube-node-lease Active 28s
kube-public Active 28s
kube-system Active 29s
Then to see the pods:
kubectl get pods
No resources found in default namespace.
Okay, so it is empty and there is nothing running. So let's install a dashboard!
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.2.0/aio/deploy/recommended.yaml
You'll see the output of this command as:
serviceaccount/metrics-server createdclusterrole.rbac.authorization.k8s.io/system:aggregated-metrics-reader createdclusterrole.rbac.authorization.k8s.io/system:metrics-server createdrolebinding.rbac.authorization.k8s.io/metrics-server-auth-reader createdclusterrolebinding.rbac.authorization.k8s.io/metrics-server:system:auth-delegator createdclusterrolebinding.rbac.authorization.k8s.io/system:metrics-server createdservice/metrics-server createddeployment.apps/metrics-server createdapiservice.apiregistration.k8s.io/v1beta1.metrics.k8s.io created
Now we need to get a token before we can log into the dashboard (it is possible to enable a skip login option but for security we'll create a token. This is documented in the kubernetes github pages but the process is:
Open up your favourite text editor and create two files:
ClusterRoleBinding.yaml
apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRoleBindingmetadata:name: admin-userroleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: cluster-adminsubjects:- kind: ServiceAccountname: admin-usernamespace: kubernetes-dashboard
ServiceAccount.yaml
apiVersion: v1kind: ServiceAccountmetadata:name: admin-usernamespace: kubernetes-dashboard
With those two files created they need to be merged into the config, to do that run (pointing to where you saved the files):
kubectl apply -f .\ClusterRoleBinding.yaml
kubectl apply -f .\ServiceAccount.yaml
Now to get the token that you need run:
kubectl -n kubernetes-dashboard get secret $(kubectl -n kubernetes-dashboard get sa/admin-user -o jsonpath="{.secrets[0].name}") -o go-template="{{.data.token | base64decode}}"
It will return a long string, this is the token:
eyJhbGciOiJSUzI1NiIsImtpZCI6IllPLTlwRmtaOUJwanhUczNtM0J0a2M5REl2eGlweGI0bzdQRzZJcG5VT3MifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlcm5ldGVzLWRhc2hib2FyZCIsImt1YmV....
Then finally, to login to the dashboard we need to run:
kubectl proxy
Then browse to the dashboard URL:
Paste the token and click Sign In:
Changing the namespace (the dropdown box next to the Kubernetes logo) to kubernetes-dashboard will display the pods that is running the dashboard:
The final step that you may want to do is to add the Metrics Server, this will allow you to see memory and cpu usage for the pods.
To do this we need to install it:
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
It will give the output:
serviceaccount/metrics-server createdclusterrole.rbac.authorization.k8s.io/system:aggregated-metrics-reader createdclusterrole.rbac.authorization.k8s.io/system:metrics-server createdrolebinding.rbac.authorization.k8s.io/metrics-server-auth-reader createdclusterrolebinding.rbac.authorization.k8s.io/metrics-server:system:auth-delegator createdclusterrolebinding.rbac.authorization.k8s.io/system:metrics-server createdservice/metrics-server createddeployment.apps/metrics-server createdapiservice.apiregistration.k8s.io/v1beta1.metrics.k8s.io created
Before we can run this we need to make a slight change. Out of the box the dashboard will only work with HTTPS connections, as we are running this locally we need to add the flag --kubelet-insecure-tls, for more information look at their github page.
kubectl patch deployment metrics-server -n kube-system --type 'json' -p '[{"op": "add", "path": "/spec/template/spec/containers/0/args/-", "value": "--kubelet-insecure-tls"}]'
Now to view the graphs log into the dashboard again:
kubectl proxy
Note: You may need to wait a few minutes for the CPU usage and Memory Usage graphs to appear and populate.
If for any reason you want to revert the system back to the starting state and go from the beginning, click on the Docker icon, the Settings cog and Kubernetes (this is the same place where Kubernetes was enabled), then click the 'Reset Kubernetes Cluster' option. This will remove all the pods and namespaces and put you back to the beginning.
No comments:
Post a Comment