CKA Simulator Kubernetes 1.24
Pre Setup
Once you’ve gained access to your terminal it might be wise to spend ~1 minute to setup your environment. You could set these:
1 | alias k=kubectl # will already be pre-configured |
Vim
The following settings will already be configured in your real exam environment in ~/.vimrc
. But it can never hurt to be able to type these down:
1 | set tabstop=2 |
More setup suggestions are in the tips section.
Question 1 | Contexts
Task weight: 1%
You have access to multiple clusters from your main terminal through kubectl
contexts. Write all those context names into /opt/course/1/contexts
.
Next write a command to display the current context into /opt/course/1/context_default_kubectl.sh
, the command should use kubectl
.
Finally write a second command doing the same thing into /opt/course/1/context_default_no_kubectl.sh
, but without the use of kubectl
.
Answer:
Maybe the fastest way is just to run:
1 | k config get-contexts # copy manually |
Or using jsonpath:
1 | k config view -o yaml # overview |
The content should then look like:
1 | # /opt/course/1/contexts |
Next create the first command:
1 | # /opt/course/1/context_default_kubectl.sh |
And the second one:
1 | # /opt/course/1/context_default_no_kubectl.sh |
In the real exam you might need to filter and find information from bigger lists of resources, hence knowing a little jsonpath and simple bash filtering will be helpful.
The second command could also be improved to:
1 | # /opt/course/1/context_default_no_kubectl.sh |
Question 2 | Schedule Pod on Master Node
Task weight: 3%
Use context: kubectl config use-context k8s-c1-H
Create a single Pod of image httpd:2.4.41-alpine
in Namespace default
. The Pod should be named pod1
and the container should be named pod1-container
. This Pod should only be scheduled on a master node, do not add new labels any nodes.
Answer:
First we find the master node(s) and their taints:
1 | k get node # find master node |
NOTE: In K8s 1.24 master/controlplane nodes have two Taints which means we have to add Tolerations for both. This is done during transitioning from the wording “master” to “controlplane”.
Next we create the Pod template:
1 | # check the export on the very top of this document so we can use $do |
Perform the necessary changes manually. Use the Kubernetes docs and search for example for tolerations and nodeSelector to find examples:
1 | # 2.yaml |
Important here to add the toleration for running on master nodes, but also the nodeSelector to make sure it only runs on master nodes. If we only specify a toleration the Pod can be scheduled on master or worker nodes.
Now we create it:
1 | k -f 2.yaml create |
Let’s check if the pod is scheduled:
1 | ➜ k get pod pod1 -o wide |
Question 3 | Scale down StatefulSet
Task weight: 1%
Use context: kubectl config use-context k8s-c1-H
There are two Pods named o3db-*
in Namespace project-c13
. C13 management asked you to scale the Pods down to one replica to save resources.
Answer:
If we check the Pods we see two replicas:
1 | ➜ k -n project-c13 get pod | grep o3db |
From their name it looks like these are managed by a StatefulSet. But if we’re not sure we could also check for the most common resources which manage Pods:
1 | ➜ k -n project-c13 get deploy,ds,sts | grep o3db |
Confirmed, we have to work with a StatefulSet. To find this out we could also look at the Pod labels:
1 | ➜ k -n project-c13 get pod --show-labels | grep o3db |
To fulfil the task we simply run:
1 | ➜ k -n project-c13 scale sts o3db --replicas 1 |
C13 Mangement is happy again.
Question 4 | Pod Ready if Service is reachable
Task weight: 4%
Use context: kubectl config use-context k8s-c1-H
Do the following in Namespace default
. Create a single Pod named ready-if-service-ready
of image nginx:1.16.1-alpine
. Configure a LivenessProbe which simply runs true
. Also configure a ReadinessProbe which does check if the url http://service-am-i-ready:80
is reachable, you can use wget -T2 -O- http://service-am-i-ready:80
for this. Start the Pod and confirm it isn’t ready because of the ReadinessProbe.
Create a second Pod named am-i-ready
of image nginx:1.16.1-alpine
with label id: cross-server-ready
. The already existing Service service-am-i-ready
should now have that second Pod as endpoint.
Now the first Pod should be in ready state, confirm that.
Answer:
It’s a bit of an anti-pattern for one Pod to check another Pod for being ready using probes, hence the normally available readinessProbe.httpGet
doesn’t work for absolute remote urls. Still the workaround requested in this task should show how probes and Pod<->Service communication works.->
First we create the first Pod:
1 | k run ready-if-service-ready --image=nginx:1.16.1-alpine $do > 4_pod1.yaml |
Next perform the necessary additions manually:
1 | # 4_pod1.yaml |
Then create the Pod:
1 | k -f 4_pod1.yaml create |
And confirm its in a non-ready state:
1 | ➜ k get pod ready-if-service-ready |
We can also check the reason for this using describe:
1 | ➜ k describe pod ready-if-service-ready |
Now we create the second Pod:
1 | k run am-i-ready --image=nginx:1.16.1-alpine --labels="id=cross-server-ready" |
The already existing Service service-am-i-ready
should now have an Endpoint:
1 | k describe svc service-am-i-ready |
Which will result in our first Pod being ready, just give it a minute for the Readiness probe to check again:
1 | ➜ k get pod ready-if-service-ready |
Look at these Pods coworking together!
Question 5 | Kubectl sorting
Task weight: 1%
Use context: kubectl config use-context k8s-c1-H
There are various Pods in all namespaces. Write a command into /opt/course/5/find_pods.sh
which lists all Pods sorted by their AGE (metadata.creationTimestamp
).
Write a second command into /opt/course/5/find_pods_uid.sh
which lists all Pods sorted by field metadata.uid
. Use kubectl
sorting for both commands.
Answer:
A good resources here (and for many other things) is the kubectl-cheat-sheet. You can reach it fast when searching for “cheat sheet” in the Kubernetes docs.
1 | # /opt/course/5/find_pods.sh |
And to execute:
1 | ➜ sh /opt/course/5/find_pods.sh |
For the second command:
1 | # /opt/course/5/find_pods_uid.sh |
And to execute:
1 | ➜ sh /opt/course/5/find_pods_uid.sh |
Question 6 | Storage, PV, PVC, Pod volume
Task weight: 8%
Use context: kubectl config use-context k8s-c1-H
Create a new PersistentVolume named safari-pv
. It should have a capacity of 2Gi, accessMode ReadWriteOnce, hostPath /Volumes/Data
and no storageClassName defined.
Next create a new PersistentVolumeClaim in Namespace project-tiger
named safari-pvc
. It should request 2Gi storage, accessMode ReadWriteOnce and should not define a storageClassName. The PVC should bound to the PV correctly.
Finally create a new Deployment safari
in Namespace project-tiger
which mounts that volume at /tmp/safari-data
. The Pods of that Deployment should be of image httpd:2.4.41-alpine
.
Answer
1 | vim 6_pv.yaml |
Find an example from https://kubernetes.io/docs and alter it:
1 | # 6_pv.yaml |
Then create it:
1 | k -f 6_pv.yaml create |
Next the PersistentVolumeClaim:
1 | vim 6_pvc.yaml |
Find an example from https://kubernetes.io/docs and alter it:
1 | # 6_pvc.yaml |
Then create:
1 | k -f 6_pvc.yaml create |
And check that both have the status Bound:
1 | ➜ k -n project-tiger get pv,pvc |
Next we create a Deployment and mount that volume:
1 | k -n project-tiger create deploy safari \ |
Alter the yaml to mount the volume:
1 | # 6_dep.yaml |
We can confirm its mounting correctly:
1 | ➜ k -n project-tiger describe pod safari-5cbf46d6d-mjhsb | grep -A2 Mounts: |
Question 7 | Node and Pod Resource Usage
Task weight: 1%
Use context: kubectl config use-context k8s-c1-H
The metrics-server has been installed in the cluster. Your college would like to know the kubectl commands to:
- show Nodes resource usage
- show Pods and their containers resource usage
Please write the commands into /opt/course/7/node.sh
and /opt/course/7/pod.sh
.
Answer:
The command we need to use here is top:
1 | ➜ k top -h |
We see that the metrics server provides information about resource usage:
1 | ➜ k top node |
We create the first file:
1 | # /opt/course/7/node.sh |
For the second file we might need to check the docs again:
1 | ➜ k top pod -h |
With this we can finish this task:
1 | # /opt/course/7/pod.sh |
Question 8 | Get Master Information
Task weight: 2%
Use context: kubectl config use-context k8s-c1-H
Ssh into the master node with ssh cluster1-master1
. Check how the master components kubelet, kube-apiserver, kube-scheduler, kube-controller-manager and etcd are started/installed on the master node. Also find out the name of the DNS application and how it’s started/installed on the master node.
Write your findings into file /opt/course/8/master-components.txt
. The file should be structured like:
1 | # /opt/course/8/master-components.txt |
Choices of [TYPE]
are: not-installed
, process
, static-pod
, pod
Answer:
We could start by finding processes of the requested components, especially the kubelet at first:
1 | ➜ ssh cluster1-master1 |
We can see which components are controlled via systemd looking at /etc/systemd/system
directory:
1 | ➜ root@cluster1-master1:~# find /etc/systemd/system/ | grep kube |
This shows kubelet is controlled via systemd, but no other service named kube nor etcd. It seems that this cluster has been setup using kubeadm, so we check in the default manifests directory:
1 | ➜ root@cluster1-master1:~# find /etc/kubernetes/manifests/ |
(The kubelet could also have a different manifests directory specified via parameter --pod-manifest-path
in it’s systemd startup config)
This means the main 4 master services are setup as static Pods. Actually, let’s check all Pods running on in the kube-system
Namespace on the master node:
1 | ➜ root@cluster1-master1:~# kubectl -n kube-system get pod -o wide | grep master1 |
There we see the 5 static pods, with -cluster1-master1
as suffix.
We also see that the dns application seems to be coredns, but how is it controlled?
1 | ➜ root@cluster1-master1$ kubectl -n kube-system get ds |
Seems like coredns is controlled via a Deployment. We combine our findings in the requested file:
1 | # /opt/course/8/master-components.txt |
You should be comfortable investigating a running cluster, know different methods on how a cluster and its services can be setup and be able to troubleshoot and find error sources.
Question 9 | Kill Scheduler, Manual Scheduling
Task weight: 5%
Use context: kubectl config use-context k8s-c2-AC
Ssh into the master node with ssh cluster2-master1
. Temporarily stop the kube-scheduler, this means in a way that you can start it again afterwards.
Create a single Pod named manual-schedule
of image httpd:2.4-alpine
, confirm its created but not scheduled on any node.
Now you’re the scheduler and have all its power, manually schedule that Pod on node cluster2-master1. Make sure it’s running.
Start the kube-scheduler again and confirm its running correctly by creating a second Pod named manual-schedule2
of image httpd:2.4-alpine
and check if it’s running on cluster2-worker1.
Answer:
Stop the Scheduler
First we find the master node:
1 | ➜ k get node |
Then we connect and check if the scheduler is running:
1 | ➜ ssh cluster2-master1 |
Kill the Scheduler (temporarily):
1 | ➜ root@cluster2-master1:~# cd /etc/kubernetes/manifests/ |
And it should be stopped:
1 | ➜ root@cluster2-master1:~# kubectl -n kube-system get pod | grep schedule |
Create a Pod
Now we create the Pod:
1 | k run manual-schedule --image=httpd:2.4-alpine |
And confirm it has no node assigned:
1 | ➜ k get pod manual-schedule -o wide |
Manually schedule the Pod
Let’s play the scheduler now:
1 | k get pod manual-schedule -o yaml > 9.yaml |
The only thing a scheduler does, is that it sets the nodeName for a Pod declaration. How it finds the correct node to schedule on, that’s a very much complicated matter and takes many variables into account.
As we cannot kubectl apply
or kubectl edit
, in this case we need to delete and create or replace:
1 | k -f 9.yaml replace --force |
How does it look?
1 | ➜ k get pod manual-schedule -o wide |
It looks like our Pod is running on the master now as requested, although no tolerations were specified. Only the scheduler takes tains/tolerations/affinity into account when finding the correct node name. That’s why its still possible to assign Pods manually directly to a master node and skip the scheduler.
Start the scheduler again
1 | ➜ ssh cluster2-master1 |
Checks its running:
1 | ➜ root@cluster2-master1:~# kubectl -n kube-system get pod | grep schedule |
Schedule a second test Pod:
1 | k run manual-schedule2 --image=httpd:2.4-alpine |
Back to normal.
Question 10 | RBAC ServiceAccount Role RoleBinding
Task weight: 6%
Use context: kubectl config use-context k8s-c1-H
Create a new ServiceAccount processor
in Namespace project-hamster
. Create a Role and RoleBinding, both named processor
as well. These should allow the new SA to only create Secrets and ConfigMaps in that Namespace.
Answer:
Let’s talk a little about RBAC resources
A ClusterRole|Role defines a set of permissions and where it is available, in the whole cluster or just a single Namespace.
A ClusterRoleBinding|RoleBinding connects a set of permissions with an account and defines where it is applied, in the whole cluster or just a single Namespace.
Because of this there are 4 different RBAC combinations and 3 valid ones:
- Role + RoleBinding (available in single Namespace, applied in single Namespace)
- ClusterRole + ClusterRoleBinding (available cluster-wide, applied cluster-wide)
- ClusterRole + RoleBinding (available cluster-wide, applied in single Namespace)
- Role + ClusterRoleBinding (NOT POSSIBLE: available in single Namespace, applied cluster-wide)
To the solution
We first create the ServiceAccount:
1 | ➜ k -n project-hamster create sa processor |
Then for the Role:
1 | k -n project-hamster create role -h # examples |
So we execute:
1 | k -n project-hamster create role processor \ |
Which will create a Role like:
1 | # kubectl -n project-hamster create role accessor --verb=create --resource=secret --resource=configmap |
Now we bind the Role to the ServiceAccount:
1 | k -n project-hamster create rolebinding -h # examples |
So we create it:
1 | k -n project-hamster create rolebinding processor \ |
This will create a RoleBinding like:
1 | # kubectl -n project-hamster create rolebinding processor --role processor --serviceaccount project-hamster:processor |
To test our RBAC setup we can use kubectl auth can-i
:
1 | k auth can-i -h # examples |
Like this:
1 | ➜ k -n project-hamster auth can-i create secret \ |
Done.
Question 11 | DaemonSet on all Nodes
Task weight: 4%
Use context: kubectl config use-context k8s-c1-H
Use Namespace project-tiger
for the following. Create a DaemonSet named ds-important
with image httpd:2.4-alpine
and labels id=ds-important
and uuid=18426a0b-5f59-4e10-923f-c0e078e82462
. The Pods it creates should request 10 millicore cpu and 10 mebibyte memory. The Pods of that DaemonSet should run on all nodes, master and worker.
Answer:
As of now we aren’t able to create a DaemonSet directly using kubectl
, so we create a Deployment and just change it up:
1 | k -n project-tiger create deployment --image=httpd:2.4-alpine ds-important $do > 11.yaml |
(Sure you could also search for a DaemonSet example yaml in the Kubernetes docs and alter it.)
NOTE: In K8s 1.24 master/controlplane nodes have two Taints which means we have to add Tolerations for both. This is done during transitioning from the wording “master” to “controlplane”.
Then we adjust the yaml to:
1 | # 11.yaml |
It was requested that the DaemonSet runs on all nodes, so we need to specify the toleration for this.
Let’s confirm:
1 | k -f 11.yaml create |
Question 12 | Deployment on all Nodes
Task weight: 6%
Use context: kubectl config use-context k8s-c1-H
Use Namespace project-tiger
for the following. Create a Deployment named deploy-important
with label id=very-important
(the Pods
should also have this label) and 3 replicas. It should contain two containers, the first named container1
with image nginx:1.17.6-alpine
and the second one named container2 with image kubernetes/pause
.
There should be only ever one Pod of that Deployment running on one worker node. We have two worker nodes: cluster1-worker1
and cluster1-worker2
. Because the Deployment has three replicas the result should be that on both nodes one Pod is running. The third Pod won’t be scheduled, unless a new worker node will be added.
In a way we kind of simulate the behaviour of a DaemonSet here, but using a Deployment and a fixed number of replicas.
Answer:
There are two possible ways, one using podAntiAffinity
and one using topologySpreadConstraint
.
PodAntiAffinity
The idea here is that we create a “Inter-pod anti-affinity” which allows us to say a Pod should only be scheduled on a node where another Pod of a specific label (here the same label) is not already running.
Let’s begin by creating the Deployment template:
1 | k -n project-tiger create deployment \ |
Then change the yaml to:
1 | # 12.yaml |
Specify a topologyKey, which is a pre-populated Kubernetes label, you can find this by describing a node.
TopologySpreadConstraints
We can achieve the same with topologySpreadConstraints
. Best to try out and play with both.
1 | # 12.yaml |
Apply and Run
Let’s run it:
1 | k -f 12.yaml create |
Then we check the Deployment status where it shows 2/3 ready count:
1 | ➜ k -n project-tiger get deploy -l id=very-important |
And running the following we see one Pod on each worker node and one not scheduled.
1 | ➜ k -n project-tiger get pod -o wide -l id=very-important |
If we kubectl describe the Pod deploy-important-58db9db6fc-lnxdb
it will show us the reason for not scheduling is our implemented podAntiAffinity ruling:
1 | Warning FailedScheduling 63s (x3 over 65s) default-scheduler 0/3 nodes are available: 1 node(s) had taint {node-role.kubernetes.io/master: }, that the pod didn't tolerate, 2 node(s) didn't match pod affinity/anti-affinity, 2 node(s) didn't satisfy existing pods anti-affinity rules. |
Or our topologySpreadConstraints:
1 | Warning FailedScheduling 16s default-scheduler 0/3 nodes are available: 1 node(s) had taint {node-role.kubernetes.io/master: }, that the pod didn't tolerate, 2 node(s) didn't match pod topology spread constraints. |
Question 13 | Multi Containers and Pod shared Volume
Task weight: 4%
Use context: kubectl config use-context k8s-c1-H
Create a Pod named multi-container-playground
in Namespace default
with three containers, named c1
, c2
and c3
. There should be a volume attached to that Pod and mounted into every container, but the volume shouldn’t be persisted or shared with other Pods.
Container c1
should be of image nginx:1.17.6-alpine
and have the name of the node where its Pod is running available as environment variable MY_NODE_NAME.
Container c2
should be of image busybox:1.31.1
and write the output of the date
command every second in the shared volume into file date.log
. You can use while true; do date >> /your/vol/path/date.log; sleep 1; done
for this.
Container c3
should be of image busybox:1.31.1
and constantly send the content of file date.log
from the shared volume to stdout. You can use tail -f /your/vol/path/date.log
for this.
Check the logs of container c3
to confirm correct setup.
Answer:
First we create the Pod template:
1 | k run multi-container-playground --image=nginx:1.17.6-alpine $do > 13.yaml |
And add the other containers and the commands they should execute:
1 | # 13.yaml |
Oh boy, lot’s of requested things. We check if everything is good with the Pod:
1 | ➜ k get pod multi-container-playground |
Good, then we check if container c1 has the requested node name as env variable:
1 | ➜ k exec multi-container-playground -c c1 -- env | grep MY |
And finally we check the logging:
1 | ➜ k logs multi-container-playground -c c3 |
Question 14 | Find out Cluster Information
Task weight: 2%
Use context: kubectl config use-context k8s-c1-H
You’re ask to find out following information about the cluster k8s-c1-H
:
- How many master nodes are available?
- How many worker nodes are available?
- What is the Service CIDR?
- Which Networking (or CNI Plugin) is configured and where is its config file?
- Which suffix will static pods have that run on cluster1-worker1?
Write your answers into file /opt/course/14/cluster-info
, structured like this:
1 | # /opt/course/14/cluster-info |
Answer:
How many master and worker nodes are available?
1 | ➜ k get node |
We see one master and two workers.
What is the Service CIDR?
1 | ➜ ssh cluster1-master1 |
Which Networking (or CNI Plugin) is configured and where is its config file?
1 | ➜ root@cluster1-master1:~# find /etc/cni/net.d/ |
By default the kubelet looks into /etc/cni/net.d
to discover the CNI plugins. This will be the same on every master and worker nodes.
Which suffix will static pods have that run on cluster1-worker1?
The suffix is the node hostname with a leading hyphen. It used to be -static
in earlier Kubernetes versions.
Result
The resulting /opt/course/14/cluster-info
could look like:
1 | # /opt/course/14/cluster-info |
Question 15 | Cluster Event Logging
Task weight: 3%
Use context: kubectl config use-context k8s-c2-AC
Write a command into /opt/course/15/cluster_events.sh
which shows the latest events in the whole cluster, ordered by time. Use kubectl
for it.
Now kill the kube-proxy Pod running on node cluster2-worker1 and write the events this caused into /opt/course/15/pod_kill.log
.
Finally kill the containerd container of the kube-proxy Pod on node cluster2-worker1 and write the events into /opt/course/15/container_kill.log
.
Do you notice differences in the events both actions caused?
Answer:
1 | # /opt/course/15/cluster_events.sh |
Now we kill the kube-proxy Pod:
1 | k -n kube-system get pod -o wide | grep proxy # find pod running on cluster2-worker1 |
Now check the events:
1 | sh /opt/course/15/cluster_events.sh |
Write the events the killing caused into /opt/course/15/pod_kill.log
:
1 | # /opt/course/15/pod_kill.log |
Finally we will try to provoke events by killing the container belonging to the container of the kube-proxy Pod:
1 | ➜ ssh cluster2-worker1 |
We killed the main container (1e020b43c4423), but also noticed that a new container (0ae4245707910) was directly created. Thanks Kubernetes!
Now we see if this caused events again and we write those into the second file:
1 | sh /opt/course/15/cluster_events.sh |
Comparing the events we see that when we deleted the whole Pod there were more things to be done, hence more events. For example was the DaemonSet in the game to re-create the missing Pod. Where when we manually killed the main container of the Pod, the Pod would still exist but only its container needed to be re-created, hence less events.
Question 16 | Namespaces and Api Resources
Task weight: 2%
Use context: kubectl config use-context k8s-c1-H
Create a new Namespace called cka-master
.
Write the names of all namespaced Kubernetes resources (like Pod, Secret, ConfigMap…) into /opt/course/16/resources.txt
.
Find the project-*
Namespace with the highest number of Roles
defined in it and write its name and amount of Roles into /opt/course/16/crowded-namespace.txt
.
Answer:
Namespace and Namespaces Resources
We create a new Namespace:
1 | k create ns cka-master |
Now we can get a list of all resources like:
1 | k api-resources # shows all |
Which results in the file:
1 | # /opt/course/16/resources.txt |
Namespace with most Roles
1 | ➜ k -n project-c13 get role --no-headers | wc -l |
Finally we write the name and amount into the file:
1 | # /opt/course/16/crowded-namespace.txt |
Question 17 | Find Container of Pod and check info
Task weight: 3%
Use context: kubectl config use-context k8s-c1-H
In Namespace project-tiger
create a Pod named tigers-reunite
of image httpd:2.4.41-alpine
with labels pod=container
and container=pod
. Find out on which node the Pod is scheduled. Ssh into that node and find the containerd container belonging to that Pod.
Using command crictl
:
- Write the ID of the container and the
info.runtimeType
into/opt/course/17/pod-container.txt
- Write the logs of the container into
/opt/course/17/pod-container.log
Answer:
First we create the Pod:
1 | k -n project-tiger run tigers-reunite \ |
Next we find out the node it’s scheduled on:
1 | k -n project-tiger get pod -o wide |
Then we ssh into that node and and check the container info:
1 | ➜ ssh cluster1-worker2 |
Then we fill the requested file (on the main terminal):
1 | # /opt/course/17/pod-container.txt |
Finally we write the container logs in the second file:
1 | ssh cluster1-worker2 'crictl logs b01edbe6f89ed' &> /opt/course/17/pod-container.log |
The &>
in above’s command redirects both the standard output and standard error.
You could also simply run crictl logs
on the node and copy the content manually, if its not a lot. The file should look like:
1 | # /opt/course/17/pod-container.log |
Question 18 | Fix Kubelet
Task weight: 8%
Use context: kubectl config use-context k8s-c3-CCC
There seems to be an issue with the kubelet not running on cluster3-worker1
. Fix it and confirm that cluster has node cluster3-worker1
available in Ready state afterwards. You should be able to schedule a Pod on cluster3-worker1
afterwards.
Write the reason of the issue into /opt/course/18/reason.txt
.
Answer:
The procedure on tasks like these should be to check if the kubelet is running, if not start it, then check its logs and correct errors if there are some.
Always helpful to check if other clusters already have some of the components defined and running, so you can copy and use existing config files. Though in this case it might not need to be necessary.
Check node status:
1 | ➜ k get node |
First we check if the kubelet is running:
1 | ➜ ssh cluster3-worker1 |
Nope, so we check if its configured using systemd as service:
1 | ➜ root@cluster3-worker1:~# service kubelet status |
Yes, its configured as a service with config at /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
, but we see its inactive. Let’s try to start it:
1 | ➜ root@cluster3-worker1:~# service kubelet start |
We see its trying to execute /usr/local/bin/kubelet
with some parameters defined in its service config file. A good way to find errors and get more logs is to run the command manually (usually also with its parameters).
1 | ➜ root@cluster3-worker1:~# /usr/local/bin/kubelet |
Another way would be to see the extended logging of a service like using journalctl -u kubelet
.
Well, there we have it, wrong path specified. Correct the path in file /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
and run:
1 | vim /etc/systemd/system/kubelet.service.d/10-kubeadm.conf # fix |
Also the node should be available for the api server, give it a bit of time though:
1 | ➜ k get node |
Finally we write the reason into the file:
1 | # /opt/course/18/reason.txt |
Question 19 | Create Secret and mount into Pod
Task weight: 3%
NOTE: This task can only be solved if questions 18 or 20 have been successfully implemented and the k8s-c3-CCC cluster has a functioning worker node
Use context: kubectl config use-context k8s-c3-CCC
Do the following in a new Namespace secret
. Create a Pod named secret-pod
of image busybox:1.31.1
which should keep running for some time.
There is an existing Secret located at /opt/course/19/secret1.yaml
, create it in the Namespace secret
and mount it readonly into the Pod at /tmp/secret1
.
Create a new Secret in Namespace secret
called secret2
which should contain user=user1
and pass=1234
. These entries should be available inside the Pod’s container as environment variables APP_USER and APP_PASS.
Confirm everything is working.
Answer
First we create the Namespace and the requested Secrets in it:
1 | k create ns secret |
We need to adjust the Namespace for that Secret:
1 | # 19_secret1.yaml |
1 | k -f 19_secret1.yaml create |
Next we create the second Secret:
1 | k -n secret create secret generic secret2 --from-literal=user=user1 --from-literal=pass=1234 |
Now we create the Pod template:
1 | k -n secret run secret-pod --image=busybox:1.31.1 $do -- sh -c "sleep 5d" > 19.yaml |
Then make the necessary changes:
1 | # 19.yaml |
It might not be necessary in current K8s versions to specify the readOnly: true
because it’s the default setting anyways.
And execute:
1 | k -f 19.yaml create |
Finally we check if all is correct:
1 | ➜ k -n secret exec secret-pod -- env | grep APP |
All is good.
Question 20 | Update Kubernetes Version and join cluster
Task weight: 10%
Use context: kubectl config use-context k8s-c3-CCC
Your coworker said node cluster3-worker2
is running an older Kubernetes version and is not even part of the cluster. Update Kubernetes on that node to the exact version that’s running on cluster3-master1
. Then add this node to the cluster. Use kubeadm for this.
Answer:
Upgrade Kubernetes to cluster3-master1 version
Search in the docs for kubeadm upgrade: https://kubernetes.io/docs/tasks/administer-cluster/kubeadm/kubeadm-upgrade
1 | ➜ k get node |
Master node seems to be running Kubernetes 1.23.1 and cluster3-worker2
is not yet part of the cluster.
1 | ➜ ssh cluster3-worker2 |
Here kubeadm is already installed in the wanted version, so we can run:
1 | ➜ root@cluster3-worker2:~# kubeadm upgrade node |
This is usually the proper command to upgrade a node. But this error means that this node was never even initialised, so nothing to update here. This will be done later using kubeadm join
. For now we can continue with kubelet and kubectl:
1 | ➜ root@cluster3-worker2:~# apt update |
Now we’re up to date with kubeadm, kubectl and kubelet. Restart the kubelet:
1 | ➜ root@cluster3-worker2:~# systemctl restart kubelet |
We can ignore the errors and move into next step to generate the join command.
Add cluster3-master2 to cluster
First we log into the master1 and generate a new TLS bootstrap token, also printing out the join command:
1 | ➜ ssh cluster3-master1 |
We see the expiration of 23h for our token, we could adjust this by passing the ttl argument.
Next we connect again to worker2 and simply execute the join command:
1 | ➜ ssh cluster3-worker2 |
If you have troubles with kubeadm join
you might need to run kubeadm reset
.
This looks great though for us. Finally we head back to the main terminal and check the node status:
1 | ➜ k get node |
Give it a bit of time till the node is ready.
1 | ➜ k get node |
We see cluster3-worker2
is now available and up to date.
Question 21 | Create a Static Pod and Service
Task weight: 2%
Use context: kubectl config use-context k8s-c3-CCC
Create a Static Pod
named my-static-pod
in Namespace default
on cluster3-master1. It should be of image nginx:1.16-alpine
and have resource requests for 10m
CPU and 20Mi
memory.
Then create a NodePort Service named static-pod-service
which exposes that static Pod on port 80 and check if it has Endpoints and if its reachable through the cluster3-master1
internal IP address. You can connect to the internal node IPs from your main terminal.
Answer:
1 | ➜ ssh cluster3-master1 |
Then edit the my-static-pod.yaml
to add the requested resource requests:
1 | # /etc/kubernetes/manifests/my-static-pod.yaml |
And make sure its running:
1 | ➜ k get pod -A | grep my-static |
Now we expose that static Pod:
1 | k expose pod my-static-pod-cluster3-master1 \ |
This would generate a Service like:
1 | # kubectl expose pod my-static-pod-cluster3-master1 --name static-pod-service --type=NodePort --port 80 |
Then run and test:
1 | ➜ k get svc,ep -l run=my-static-pod |
Looking good.
Question 22 | Check how long certificates are valid
Task weight: 2%
Use context: kubectl config use-context k8s-c2-AC
Check how long the kube-apiserver server certificate is valid on cluster2-master1
. Do this with openssl or cfssl. Write the exipiration date into /opt/course/22/expiration
.
Also run the correct kubeadm
command to list the expiration dates and confirm both methods show the same date.
Write the correct kubeadm
command that would renew the apiserver server certificate into /opt/course/22/kubeadm-renew-certs.sh
.
Answer:
First let’s find that certificate:
1 | ➜ ssh cluster2-master1 |
Next we use openssl to find out the expiration date:
1 | ➜ root@cluster2-master1:~# openssl x509 -noout -text -in /etc/kubernetes/pki/apiserver.crt | grep Validity -A2 |
There we have it, so we write it in the required location on our main terminal:
1 | # /opt/course/22/expiration |
And we use the feature from kubeadm to get the expiration too:
1 | ➜ root@cluster2-master1:~# kubeadm certs check-expiration | grep apiserver |
Looking good. And finally we write the command that would renew all certificates into the requested location:
1 | # /opt/course/22/kubeadm-renew-certs.sh |
Question 23 | Kubelet client/server cert info
Task weight: 2%
Use context: kubectl config use-context k8s-c2-AC
Node cluster2-worker1 has been added to the cluster using kubeadm
and TLS bootstrapping.
Find the “Issuer” and “Extended Key Usage” values of the cluster2-worker1:
- kubelet client certificate, the one used for outgoing connections to the kube-apiserver.
- kubelet server certificate, the one used for incoming connections from the kube-apiserver.
Write the information into file /opt/course/23/certificate-info.txt
.
Compare the “Issuer” and “Extended Key Usage” fields of both certificates and make sense of these.
Answer:
To find the correct kubelet certificate directory, we can look for the default value of the --cert-dir
parameter for the kubelet. For this search for “kubelet” in the Kubernetes docs which will lead to: https://kubernetes.io/docs/reference/command-line-tools-reference/kubelet. We can check if another certificate directory has been configured using ps aux
or in /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
.
First we check the kubelet client certificate:
1 | ➜ ssh cluster2-worker1 |
Next we check the kubelet server certificate:
1 | ➜ root@cluster2-worker1:~# openssl x509 -noout -text -in /var/lib/kubelet/pki/kubelet.crt | grep Issuer |
We see that the server certificate was generated on the worker node itself and the client certificate was issued by the Kubernetes api. The “Extended Key Usage” also shows if its for client or server authentication.
More about this: https://kubernetes.io/docs/reference/command-line-tools-reference/kubelet-tls-bootstrapping
Question 24 | NetworkPolicy
Task weight: 9%
Use context: kubectl config use-context k8s-c1-H
There was a security incident where an intruder was able to access the whole cluster from a single hacked backend Pod.
To prevent this create a NetworkPolicy called np-backend
in Namespace project-snake
. It should allow the backend-*
Pods only to:
- connect to
db1-*
Pods on port 1111 - connect to
db2-*
Pods on port 2222
Use the app
label of Pods in your policy.
After implementation, connections from backend-*
Pods to vault-*
Pods on port 3333 should for example no longer work.
Answer:
First we look at the existing Pods and their labels:
1 | ➜ k -n project-snake get pod |
We test the current connection situation and see nothing is restricted:
1 | ➜ k -n project-snake get pod -o wide |
Now we create the NP by copying and chaning an example from the k8s docs:
1 | vim 24_np.yaml |
The NP above has two rules with two conditions each, it can be read as:
1 | allow outgoing traffic if: |
Wrong example
Now let’s shortly look at a wrong example:
1 | # WRONG |
The NP above has one rule with two conditions and two condition-entries each, it can be read as:
1 | allow outgoing traffic if: |
Using this NP it would still be possible for backend-*
Pods to connect to db2-*
Pods on port 1111 for example which should be forbidden.
Create NetworkPolicy
We create the correct NP:
1 | k -f 24_np.yaml create |
And test again:
1 | ➜ k -n project-snake exec backend-0 -- curl -s 10.44.0.25:1111 |
Also helpful to use kubectl describe
on the NP to see how k8s has interpreted the policy.
Great, looking more secure. Task done.
Question 25 | Etcd Snapshot Save and Restore
Task weight: 8%
Use context: kubectl config use-context k8s-c3-CCC
Make a backup of etcd running on cluster3-master1 and save it on the master node at /tmp/etcd-backup.db
.
Then create a Pod of your kind in the cluster.
Finally restore the backup, confirm the cluster is still working and that the created Pod is no longer with us.
Answer:
Etcd Backup
First we log into the master and try to create a snapshop of etcd:
1 | ➜ ssh cluster3-master1 |
But it fails because we need to authenticate ourselves. For the necessary information we can check the etc manifest:
1 | ➜ root@cluster3-master1:~# vim /etc/kubernetes/manifests/etcd.yaml |
We only check the etcd.yaml
for necessary information we don’t change it.
1 | # /etc/kubernetes/manifests/etcd.yaml |
But we also know that the api-server is connecting to etcd, so we can check how its manifest is configured:
1 | ➜ root@cluster3-master1:~# cat /etc/kubernetes/manifests/kube-apiserver.yaml | grep etcd |
We use the authentication information and pass it to etcdctl:
1 | ➜ root@cluster3-master1:~# ETCDCTL_API=3 etcdctl snapshot save /tmp/etcd-backup.db \ |
NOTE: Dont use
snapshot status
because it can alter the snapshot file and render it invalid
Etcd restore
Now create a Pod in the cluster and wait for it to be running:
1 | ➜ root@cluster3-master1:~# kubectl run test --image=nginx |
NOTE: If you didn’t solve questions 18 or 20 and cluster3 doesn’t have a ready worker node then the created pod might stay in a Pending state. This is still ok for this task.
Next we stop all controlplane components:
1 | root@cluster3-master1:~# cd /etc/kubernetes/manifests/ |
Now we restore the snapshot into a specific directory:
1 | ➜ root@cluster3-master1:~# ETCDCTL_API=3 etcdctl snapshot restore /tmp/etcd-backup.db \ |
We could specify another host to make the backup from by using etcdctl --endpoints http://IP
, but here we just use the default value which is: http://127.0.0.1:2379,http://127.0.0.1:4001
.
The restored files are located at the new folder /var/lib/etcd-backup
, now we have to tell etcd to use that directory:
1 | ➜ root@cluster3-master1:~# vim /etc/kubernetes/etcd.yaml |
Now we move all controlplane yaml again into the manifest directory. Give it some time (up to several minutes) for etcd to restart and for the api-server to be reachable again:
1 | root@cluster3-master1:/etc/kubernetes/manifests# mv ../*.yaml . |
Then we check again for the Pod:
1 | ➜ root@cluster3-master1:~# kubectl get pod -l run=test |
Awesome, backup and restore worked as our pod is gone.
Extra Question 1 | Find Pods first to be terminated
Use context: kubectl config use-context k8s-c1-H
Check all available Pods in the Namespace project-c13
and find the names of those that would probably be terminated first if the nodes run out of resources (cpu or memory) to schedule all Pods. Write the Pod names into /opt/course/e1/pods-not-stable.txt
.
Answer:
When available cpu or memory resources on the nodes reach their limit, Kubernetes will look for Pods that are using more resources than they requested. These will be the first candidates for termination. If some Pods containers have no resource requests/limits set, then by default those are considered to use more than requested.
Kubernetes assigns Quality of Service classes to Pods based on the defined resources and limits, read more here: https://kubernetes.io/docs/tasks/configure-pod-container/quality-service-pod
Hence we should look for Pods without resource requests defined, we can do this with a manual approach:
1 | k -n project-c13 describe pod | less -p Requests # describe all pods and highlight Requests |
Or we do:
1 | k -n project-c13 describe pod | egrep "^(Name:| Requests:)" -A1 |
We see that the Pods of Deployment c13-3cc-runner-heavy
don’t have any resources requests specified. Hence our answer would be:
1 | # /opt/course/e1/pods-not-stable.txt |
To automate this process you could use jsonpath like this:
1 | ➜ k -n project-c13 get pod \ |
This lists all Pod names and their requests/limits, hence we see the three Pods without those defined.
Or we look for the Quality of Service classes:
1 | ➜ k get pods -n project-c13 \ |
Here we see three with BestEffort, which Pods get that don’t have any memory or cpu limits or requests defined.
A good practice is to always set resource requests and limits. If you don’t know the values your containers should have you can find this out using metric tools like Prometheus. You can also use kubectl top pod
or even kubectl exec
into the container and use top
and similar tools.
Extra Question 2 | Curl Manually Contact API
Use context: kubectl config use-context k8s-c1-H
There is an existing ServiceAccount secret-reader
in Namespace project-hamster
. Create a Pod of image curlimages/curl:7.65.3
named tmp-api-contact
which uses this ServiceAccount. Make sure the container keeps running.
Exec into the Pod and use curl
to access the Kubernetes Api of that cluster manually, listing all available secrets. You can ignore insecure https connection. Write the command(s) for this into file /opt/course/e4/list-secrets.sh
.
Answer:
https://kubernetes.io/docs/tasks/run-application/access-api-from-pod
It’s important to understand how the Kubernetes API works. For this it helps connecting to the api manually, for example using curl. You can find information fast by search in the Kubernetes docs for “curl api” for example.
First we create our Pod:
1 | k run tmp-api-contact \ |
Add the service account name and Namespace:
1 | # e2.yaml |
Then run and exec into:
1 | k -f 6.yaml create |
Once on the container we can try to connect to the api using curl
, the api is usually available via the Service named kubernetes
in Namespace default
(You should know how dns resolution works across Namespaces.). Else we can find the endpoint IP via environment variables running env
.
So now we can do:
1 | curl https://kubernetes.default |
The last command shows 403 forbidden, this is because we are not passing any authorisation information with us. The Kubernetes Api Server thinks we are connecting as system:anonymous
. We want to change this and connect using the Pods ServiceAccount named secret-reader
.
We find the the token in the mounted folder at /var/run/secrets/kubernetes.io/serviceaccount
, so we do:
1 | ➜ TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token) |
Now we’re able to list all Secrets, registering as the ServiceAccount secret-reader
under which our Pod is running.
To use encrypted https connection we can run:
1 | CACERT=/var/run/secrets/kubernetes.io/serviceaccount/ca.crt |
For troubleshooting we could also check if the ServiceAccount is actually able to list Secrets using:
1 | ➜ k auth can-i get secret --as system:serviceaccount:project-hamster:secret-reader |
Finally write the commands into the requested location:
1 | # /opt/course/e4/list-secrets.sh |
CKA Simulator Preview Kubernetes 1.24
This is a preview of the full CKA Simulator course content.
The full course contains 25 scenarios from all the CKA areas. The course also provides a browser terminal which is a very close replica of the original one. This is great to get used and comfortable before the real exam. After the test session (120 minutes), or if you stop it early, you’ll get access to all questions and their detailed solutions. You’ll have 36 hours cluster access in total which means even after the session, once you have the solutions, you can still play around.
The following preview will give you an idea of what the full course will provide. These preview questions are in addition to the 25 of the full course. But the preview questions are part of the same CKA simulation environment which we setup for you, so with access to the full course you can solve these too.
The answers provided here assume that you did run the initial terminal setup suggestions as provided in the tips section, but especially:
1 | alias k=kubectl |
These questions can be solved in the test environment provided through the CKA Simulator
Preview Question 1
Use context: kubectl config use-context k8s-c2-AC
The cluster admin asked you to find out the following information about etcd running on cluster2-master1:
- Server private key location
- Server certificate expiration date
- Is client certificate authentication enabled
Write these information into /opt/course/p1/etcd-info.txt
Finally you’re asked to save an etcd snapshot at /etc/etcd-snapshot.db
on cluster2-master1 and display its status.
Answer:
Find out etcd information
Let’s check the nodes:
1 | ➜ k get node |
First we check how etcd is setup in this cluster:
1 | ➜ root@cluster2-master1:~# kubectl -n kube-system get pod |
We see its running as a Pod, more specific a static Pod. So we check for the default kubelet directory for static manifests:
1 | ➜ root@cluster2-master1:~# find /etc/kubernetes/manifests/ |
So we look at the yaml and the parameters with which etcd is started:
1 | # /etc/kubernetes/manifests/etcd.yaml |
We see that client authentication is enabled and also the requested path to the server private key, now let’s find out the expiration of the server certificate:
1 | ➜ root@cluster2-master1:~# openssl x509 -noout -text -in /etc/kubernetes/pki/etcd/server.crt | grep Validity -A2 |
There we have it. Let’s write the information into the requested file:
1 | # /opt/course/p1/etcd-info.txt |
Create etcd snapshot
First we try:
1 | ETCDCTL_API=3 etcdctl snapshot save /etc/etcd-snapshot.db |
We get the endpoint also from the yaml. But we need to specify more parameters, all of which we can find the yaml declaration above:
1 | ETCDCTL_API=3 etcdctl snapshot save /etc/etcd-snapshot.db \ |
This worked. Now we can output the status of the backup file:
1 | ➜ root@cluster2-master1:~# ETCDCTL_API=3 etcdctl snapshot status /etc/etcd-snapshot.db |
The status shows:
- Hash: 4d4e953
- Revision: 7213
- Total Keys: 1291
- Total Size: 2.7 MB
Preview Question 2
Use context: kubectl config use-context k8s-c1-H
You’re asked to confirm that kube-proxy is running correctly on all nodes. For this perform the following in Namespace project-hamster
:
Create a new Pod named p2-pod
with two containers, one of image nginx:1.21.3-alpine
and one of image busybox:1.31
. Make sure the busybox container keeps running for some time.
Create a new Service named p2-service
which exposes that Pod internally in the cluster on port 3000->80.
Find the kube-proxy container on all nodes cluster1-master1
, cluster1-worker1
and cluster1-worker2
and make sure that it’s using iptables. Use command crictl
for this.
Write the iptables rules of all nodes belonging the created Service p2-service
into file /opt/course/p2/iptables.txt
.
Finally delete the Service and confirm that the iptables rules are gone from all nodes.
Answer:
Create the Pod
First we create the Pod:
1 | # check out export statement on top which allows us to use $do |
Next we add the requested second container:
1 | # p2.yaml |
And we create the Pod:
1 | k -f p2.yaml create |
Create the Service
Next we create the Service:
1 | k -n project-hamster expose pod p2-pod --name p2-service --port 3000 --target-port 80 |
This will create a yaml like:
1 | apiVersion: v1 |
We should confirm Pods and Services are connected, hence the Service should have Endpoints.
1 | k -n project-hamster get pod,svc,ep |
Confirm kube-proxy is running and is using iptables
First we get nodes in the cluster:
1 | ➜ k get node |
The idea here is to log into every node, find the kube-proxy container and check its logs:
1 | ➜ ssh cluster1-master1 |
This should be repeated on every node and result in the same output Using iptables Proxier
.
Check kube-proxy is creating iptables rules
Now we check the iptables rules on every node first manually:
1 | ➜ ssh cluster1-master1 iptables-save | grep p2-service |
Great. Now let’s write these logs into the requested file:
1 | ➜ ssh cluster1-master1 iptables-save | grep p2-service >> /opt/course/p2/iptables.txt |
Delete the Service and confirm iptables rules are gone
Delete the Service:
1 | k -n project-hamster delete svc p2-service |
And confirm the iptables rules are gone:
1 | ➜ ssh cluster1-master1 iptables-save | grep p2-service |
Done.
Kubernetes Services are implemented using iptables rules (with default config) on all nodes. Every time a Service has been altered, created, deleted or Endpoints of a Service have changed, the kube-apiserver contacts every node’s kube-proxy to update the iptables rules according to the current state.
Preview Question 3
Use context: kubectl config use-context k8s-c2-AC
Create a Pod named check-ip
in Namespace default
using image httpd:2.4.41-alpine
. Expose it on port 80 as a ClusterIP Service named check-ip-service
. Remember/output the IP of that Service.
Change the Service CIDR to 11.96.0.0/12
for the cluster.
Then create a second Service named check-ip-service2
pointing to the same Pod to check if your settings did take effect. Finally check if the IP of the first Service has changed.
Answer:
Let’s create the Pod and expose it:
1 | k run check-ip --image=httpd:2.4.41-alpine |
And check the Pod and Service ips:
1 | ➜ k get svc,ep -l run=check-ip |
Now we change the Service CIDR on the kube-apiserver:
1 | ➜ ssh cluster2-master1 |
Give it a bit for the kube-apiserver and controller-manager to restart
Wait for the api to be up again:
1 | ➜ root@cluster2-master1:~# kubectl -n kube-system get pod | grep api |
Now we do the same for the controller manager:
1 | ➜ root@cluster2-master1:~# vim /etc/kubernetes/manifests/kube-controller-manager.yaml |
Give it a bit for the controller-manager to restart.
We can check if it was restarted using crictl
:
1 | ➜ root@cluster2-master1:~# crictl ps | grep scheduler |
Checking our existing Pod and Service again:
1 | ➜ k get pod,svc -l run=check-ip |
Nothing changed so far. Now we create another Service like before:
1 | k expose pod check-ip --name check-ip-service2 --port 80 |
And check again:
1 | ➜ k get svc,ep -l run=check-ip |
There we go, the new Service got an ip of the new specified range assigned. We also see that both Services have our Pod as endpoint.