Skip to content
ImagineWorld

Application Placement and Debugging with Kubernetes

node_idnode-book_becoming-kcna-certified_chapter-07-application-placement-and-debugging-with-kubernetes
node_typechapter-node
schema_profileworld.chapter_node.v1
statusactive
entropyhigh
manifoldworld
domainimagineworld.net
authorityinforms
layercorpus
chapter_num7
chapter_titleApplication Placement and Debugging with Kubernetes
governed_byCORPUS
constitutional_citationCONSTITUTION
local graph

Chapter 7 — Application Placement and Debugging with Kubernetes

Section titled “Chapter 7 — Application Placement and Debugging with Kubernetes”

In this chapter, we’ll see how we can control the placement of workloads on Kubernetes, how its scheduler works, and how we can debug applications running on K8s when something goes wrong. This chapter covers aspects from the Kubernetes Fundamentals as well as Cloud Native Observability domains of the Kubernetes and Cloud Native Associate (KCNA) exam at the same time.

As before, we will perform a few exercises with our minikube Kubernetes, so keep your setup handy. We’re going to cover the following topics:

  • Scheduling in Kubernetes
  • Resource requests and limits
  • Debugging applications in Kubernetes

Let’s continue our Kubernetes journey!

We’ve already touched the surface of what Kubernetes scheduler (kube-scheduler) does in Chapter 5. The scheduler is the component of the K8s control plane that decides on which node a pod will run.

Scheduling

Scheduling is the process of assigning Pods to Kubernetes nodes for the kubelet to run them. The scheduler watches for newly created Pods that have no node assigned in an infinite loop, and for every Pod it discovers, it will be responsible for finding the optimal node to run it on.

The default kube-scheduler scheduler selects a node for a pod in two stages:

  1. Filtering: The first stage is where the scheduler determines the set of nodes where it is feasible to run the pod. This includes checks for nodes to have sufficient capacity and other requirements for a particular pod. This list might be empty if there are no suitable nodes in the cluster, and in such a case, the pod will hang in an unscheduled state until either the requirements or cluster state is changed.
  2. Scoring: The second stage is where the scheduler ranks the nodes filtered in the first stage to choose the most suitable pod placement. Each node in the list will be ranked and gets a score, and at the end, the node with the highest score is picked.

Let’s have a real-world example. Imagine we have an application that requires nodes with certain hardware. For example, you run machine learning (ML) workloads that can utilize GPUs for faster processing, or an application requires a particular CPU generation that is not available on every node in the cluster. In all such cases, we need to instruct Kubernetes to restrict the list of suitable nodes for our pods. There are multiple ways to do that:

  • Specifying a nodeSelector field in the pod spec and labeling nodes
  • Specifying an exact nodeName field in the pod spec
  • Using affinity and anti-affinity rules
  • Using pod topology spread constraints

Now let’s get back to our minikube setup and extend the cluster by adding one more node with the minikube node add command (this operation might take some time):

$ minikube node add
😄 Adding node m02 to cluster minikube
❗ Cluster was created without any CNI, adding a node to it might cause broken networking.
👍 Starting worker node minikube-m02 in cluster minikube
🚜 Pulling base image ...
🔥 Creating docker container (CPUs=2, Memory=2200MB) ...
🐳 Preparing Kubernetes v1.23.3 on Docker 20.10.12 ...
🔎 Verifying Kubernetes components...
🏄 Successfully added m02 to minikube!

We should have a two-node minikube cluster at this point! Let’s check the list of nodes:

$ minikube kubectl get nodes
NAME STATUS ROLES AGE VERSION
minikube Ready control-plane,master 22d v1.23.3
minikube-m02 Ready <none> 84s v1.23.3

Note

If your system does not have sufficient resources to run another Kubernetes node, you can also continue as before with just one node. In such cases, however, you will need to adjust the example commands you encounter in this chapter to label the first node.

We will now create a modified version of the nginx deployment from before, which will require the node to have purpose: web-server appended to it. The respective Deployment spec could look like this:

$ cat nginx-deployment-with-node-selector.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment-with-node-selector
labels:
app: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
nodeSelector:
purpose: web-server

Note

If you have not yet deleted the resources from the previous chapter’s exercises, do so now by executing kubectl delete deployment, kubectl delete sts, or kubectl delete service in the kcna namespace respectively.

Go ahead and create the aforementioned nginx deployment spec:

$ minikube kubectl -- create -f nginx-deployment-with-node-selector.yaml -n kcna
deployment.apps/nginx-deployment-with-node-selector created

Let’s check what happened; for example, query pods in the kcna namespace:

$ minikube kubectl -- get pods -n kcna
NAME READY STATUS RESTARTS AGE
nginx-deployment-with-node-selector-7668d66698 -48q6b 0/1 Pending 0 1m

There it goes – the created Nginx pod is stuck in a Pending state. Let’s check more details with the kubectl describe command (your pod naming will differ):

$ minikube kubectl -- describe pods nginx-deployment-with-node-selector-7668d66698-48q6b -n kcna
… LONG OUTPUT OMITTED …
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Warning FailedScheduling 1m default-scheduler 0/2 nodes are available: 2 node(s) didn't match Pod's node affinity/selector.

The message is clear – we have requested a node with a certain label for our nginx deployment pod and there are no nodes with such a label available.

We can check which labels our nodes have by adding the -- show-labels parameter:

$ minikube kubectl -- get nodes --show-labels
NAME STATUS ROLES AGE VERSION LABELS
minikube Ready control-plane,master 22d v1.23.3 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=minikube,kubernetes.io/os=linux,minikube.k8s.io/commit=362d5fdc0a3dbee389b3d3f1034e8023e72bd3a7,minikube.k8s.io/name=minikube,minikube.k8s.io/primary=true,minikube.k8s.io/updated_at=2022_06_19T17_20_23_0700,minikube.k8s.io/version=v1.25.2,node-role.kubernetes.io/control-plane=,node-role.kubernetes.io/master=,node.kubernetes.io/exclude-from-external-load-balancers=
minikube-m02 Ready <none> 19m v1.23.3 beta.kubernetes.io/arch=amd64,beta.kubernetes.io /os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=minikube-m02,kubernetes.io/os=linux

Default labels include some useful information about the roles of the nodes, CPU architecture, OS, and more. Let’s now label the newly added node with the same label our nginx deployment is looking for (the node name might be similar in your case, so adjust accordingly):

$ minikube kubectl -- label node minikube-m02 "purpose=web-server"
node/minikube-m02 labeled

And just a moment later, we can see the nginx pod is being created:

$ minikube kubectl -- get pods -n kcna
NAME READY STATUS RESTARTS AGE
nginx-deployment-with-node-selector-7668d66698 -48q6b 0/1 ContainerCreating 0 22m

By adding the -o wide option, we can see which node the pod was assigned to:

$ minikube kubectl -- get pods -n kcna -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deployment-with-node-selector-7668d66698-48q6b 1/1 Running 0 23m 172.17.0.2 minikube-m02 <none> <none>

That was a demonstration of perhaps the most common way to provide placement instructions to a Kubernetes scheduler with nodeSelector.

Let’s move on to discuss the other scheduling controls Kubernetes offers. nodeName should be obvious – it allows us to specify exactly which node we want the workload to be scheduled to. Affinity and anti-affinity rules are more interesting. Conceptually, affinity is similar to nodeSelector but has more customization options.

nodeAffinity and podAffinity

These allow you to schedule Pods either on certain nodes (nodeAffinity) in a cluster or to nodes that are already running specified Pods (podAffinity).

nodeAntiAffinity and podAntiAffinity

The opposite of affinity. These allow you to either schedule Pods to different nodes than the ones specified (nodeAntiAffinity) or to schedule to different nodes where the specified Pods run (podAntiAffinity).

In other words, affinity rules are used to attract Pods to certain nodes or other Pods, and anti-affinity for the opposite – to push back from certain nodes or other Pods. Affinity can also be of two types – hard and soft:

  • requiredDuringSchedulingIgnoredDuringExecution – Hard requirement, meaning the pod won’t be scheduled unless the rule is met
  • preferredDuringSchedulingIgnoredDuringExecution – Soft requirement, meaning the scheduler will try to find the node that satisfies the requirement, but if not available, the pod will still be scheduled on any other node

Note

IgnoredDuringExecution means that if the node labels change after Kubernetes already scheduled the pod, the pod continues to run on the same node.

Last on our list is pod topology spread constraints.

Topology spread constraints

These allow us to control how Pods are spread across the cluster among failure domains such as regions, availability zones (AZs), nodes, or other user-defined topologies.

Essentially, these allow us to control where Pods run, taking into account the physical topology of the cluster. In today’s cloud environments, we typically have multiple AZs in each region where the cloud provider operates.

AZ

This is one or multiple discrete data centers with redundant power, networking, and internet connectivity.

It is a good practice to run both the control plane and worker nodes of Kubernetes across multiple AZs. For example, in the eu-central-1 region, Amazon Web Services (AWS) currently has three AZs, so we can run one control plane node in each AZ and multiple worker nodes per AZ. In this case, to achieve high availability (HA) as well as efficient resource utilization, we can apply topology spread constraints to our workloads to control the spread of Pods by nodes and zones, as shown in Figure 7*.1*:

Figure 7.1 – Example of Pods spread across a cluster

This way, we can protect our workloads against individual node outages as well as wider cloud provider outages that might affect a whole AZ. Besides, it is possible to combine different methods and rules for more precise and granular control of where the Pods will be placed – for example, we can have topology spread constraints together with nodeAffinity and podAntiAffinity rules in one deployment.

Note

It is possible to combine multiple rules per pod - for example, nodeSelector together with hard nodeAffinity rules (requiredDuringSchedulingIgnoredDuringExecution). Both rules must be satisfied for the pod to be scheduled. In cases where at least one rule is not satisfied, the pod will be in a Pending state.

Altogether, scheduling in Kubernetes might seem to be a bit complicated at first, but as you start getting more experience, you’ll see that its extensive features are great and let us handle complex scenarios as well as very large and diverse K8s installations. For the scope of the KCNA exam, you are not required to know in-depth details, but if you have some time, you are encouraged to check the Further reading section at the end of this chapter.

As we were exploring the features of the K8s scheduler previously, have you wondered how Kubernetes knows what is the best node in the cluster for a particular pod? If we create a Deployment with no affinity settings, topology constraints, or node selectors, how can Kubernetes decide what is the best location in the cluster for the application we want to run?

By default, K8s is not aware of how many resources (CPU, memory, and other) each container in a scheduled pod requires to run. Therefore, for Kubernetes to make the best scheduling decisions, we need to make K8s aware of what each container requires for normal operation.

Resource requests

A resource request is an optional specification of how many resources each container in a pod needs. Containers can use more resources than requested if the node where the Pod runs has available resources. The specified request amounts will be reserved on the node where the pod is scheduled.

Kubernetes also allows us to impose hard limits on resources that the container can consume.

Resource limits

A resource limit is an optional specification of the maximum resources a running container can consume that are enforced by the kubelet and container runtime.

For example, we can set that the nginx container requires 250 MiB. If the pod with this container gets scheduled on a node with 8 GiB total memory with few other running Pods, our nginx container could possibly use 1 GiB or even more. However, if we additionally set a limit of 1 GiB, the runtime will prevent nginx from going beyond that limit. If a process tries to allocate more memory, the node kernel will forcefully terminate that process with an Out Of Memory (OOM) error, and the container gets restarted.

In the case of the CPU, limits and requests are measured by absolute units, where 1 CPU unit is an equivalent of 1 virtual CPU (vCPU) core or an actual 1 physical CPU core, depending on whether the worker node is a virtual machine (VM) or a physical server. A CPU unit of 0.5 CPU is the same as 500m units, where m stands for milliCPU, and—as you probably guessed—it allows us to specify a fraction of CPU this way. Unlike with memory, if a process tries to consume more CPU time than allowed by the limit, it won’t be killed; instead, it simply gets throttled.

Note

When a resource limit is specified but not the request, and no default request is set (for example, the default might be inherited from the namespace settings), Kubernetes will copy the specified limit and use it as the request too. For example, a 500 MiB limit will cause the request to be 500 MiB as well.

Time to see these in action! Let’s get back to our minikube setup and try creating the following example pod with a single container in the kcna namespace:

apiVersion: v1
kind: Pod
metadata:
name: memory-demo
spec:
containers:
- name: memory-demo-ctr
image: polinux/stress
resources:
requests:
memory: "100Mi"
limits:
memory: "200Mi"
command: ["stress"]
args: ["--vm", "1", "--vm-bytes", "150M", "--vm-hang", "1"]

Execute the following command:

$ minikube kubectl -- create -f memory-request-limit.yaml -n kcna
pod/memory-demo created

The application inside is a simple stress test tool that generates configurable load and memory consumption. With the arguments specified in the preceding spec, it consumes exactly 150 Mi of memory. Because 150 Mi is less than the limit set (200 Mi), everything is working fine.

Now, let’s modify the stress arguments in the spec to use 250M instead of 150M. The respective changes are highlighted in the following code snippet:

memory: "200Mi"
command: ["stress"]
args: ["--vm", "1", "--vm-bytes", "250M", "--vm-hang", "1"]

Delete the old pod and apply the updated spec, assuming that the file is now called memory-request-over-limit.yaml:

$ minikube kubectl -- delete pods memory-demo -n kcna
pod "memory-demo" deleted
$ minikube kubectl -- apply -f memory-request-over-limit.yaml -n kcna
pod/memory-demo created
$ minikube kubectl -- get pods -n kcna
NAME READY STATUS RESTARTS AGE
memory-demo 0/1 ContainerCreating 0 3s

If you’re typing quickly enough, you should be able to see the OOMKilled status and, eventually, CrashLoopBackOff:

$ minikube kubectl -- get pods -n kcna
NAME READY STATUS RESTARTS AGE
memory-demo 0/1 OOMKilled 0 5s
$ minikube kubectl -- get pods -n kcna
NAME READY STATUS RESTARTS AGE
memory-demo 0/1 CrashLoopBackOff 2 (31s ago) 54s

Also, you can invoke minikube kubectl -- describe po memory-demo -n kcna to see more details:

… LONG OUTPUT OMITTED …
State: Waiting
Reason: CrashLoopBackOff
Last State: Terminated
Reason: OOMKilled
Exit Code: 1

Because the process allocates 250 MiB with a limit of 150 MiB set on the container, it gets killed. Remember that if you run multiple containers in a pod, the whole pod will stop accepting requests if at least one container of that pod is not running.

To sum all this up, requests and limits are very important, and the best practice is to configure both for all workloads running in Kubernetes because Kubernetes does not know how many resources your applications need, and you might end up with overloaded or underutilized worker nodes in your cluster, impacting stability when resource requests are undefined. Resource limits, on the other hand, help protect from rogue pods and applications with bugs that might be leaking memory or trying to use all available CPU time, affecting the neighbor workloads.

After you’re done, feel free to delete the pod and other resources in the kcna namespace, if any. Next, we will continue exploring Kubernetes and learn about the ways to debug applications running on Kubernetes.

As you start using Kubernetes to run various applications, you’ll eventually face the need to debug at least some of them. Sometimes, an application might have a bug that causes it to crash – maybe it was misconfigured or misbehaving under certain scenarios. Kubernetes provides multiple mechanisms that help us figure out what is wrong with the containerized payload and individual pod containers, including the following:

  • Fetching logs from all containers in a pod, and also logs from the previous pod run
  • Querying events that happened in a cluster recently
  • Port forwarding from a pod to a local environment
  • Running arbitrary commands inside containers of a pod

Logs play a crucial role and are very helpful to understand what is not working as intended. Applications often support multiple log levels categorized by the severity and verbosity of information that gets recorded, such as received requests and their payload; interrupted connections; failures to connect to a database or other services; and so on.

Common log levels are INFO, WARNING, ERROR, DEBUG, and CRITICAL, where the ERROR and CRITICAL settings will only record events that are considered errors and major problems as such. INFO and WARNING levels might provide generic information about what is happening or what might indicate an application problem, and DEBUG usually gives the most details by recording everything that happened with the application. As the name suggests, it might be wise to enable maximum verbosity by enabling the DEBUG log level to help debug problems. While this level of categorization is pretty standard across the industry, some software might have its own ways and definitions of log verbosity, so refer to the respective documentation and configuration samples. A standard log representation format today is JSON, and it is widely supported by development libraries in any language and all sorts of applications.

When it comes to logging architecture, the best way is to use a separate backend to store, analyze, and query logs that will persist the log records independent from the lifecycle of Kubernetes nodes, Pods, and containers. This approach is known as cluster-level logging. Kubernetes does not provide a native log storage solution and only keeps the most recent logs on each node. However, there are plenty of logging solutions that offer seamless integration with Kubernetes, such as Grafana Loki or Elastic Stack (ELK), to name a few. The opposite of cluster-level logging is when logging is configured individually for each pod (application) running in the cluster. This is not a recommended way to go with Kubernetes.

In order to collect logs from each Kubernetes node for aggregation and longer storage, it is common to use node logging agents. Those are small, containerized agents that run on every node and push all collected logs to a logging backend server. Because they need to run on each node in the cluster, it is common to define them as a DaemonSet. A schematic of such a setup is shown in Figure 7*.2*:

Figure 7.2 – Using a node logging agent for log collection and aggregation

For a moment, let’s get back to our minikube setup and see in action how to fetch application logs. Let’s start with a basic pod that simply writes the current date and time into the standard output (stdout):

$ minikube kubectl -- apply -f
https://k8s.io/examples/debug/counter-pod.yaml -n kcna
pod/counter created
$ minikube kubectl -- get pods -n kcna
NAME READY STATUS RESTARTS AGE
counter 1/1 Running 0 22s

Execute kubectl logs to get the container logs. If a pod has multiple containers, you’ll have to additionally specify which particular container you want to get the logs from with the -- container argument:

$ minikube kubectl -- logs counter -n kcna
0: Sun Aug 7 13:31:21 UTC 2022
1: Sun Aug 7 13:31:22 UTC 2022
2: Sun Aug 7 13:31:23 UTC 2022
3: Sun Aug 7 13:31:24 UTC 2022
4: Sun Aug 7 13:31:25 UTC 2022
5: Sun Aug 7 13:31:26 UTC 2022
6: Sun Aug 7 13:31:27 UTC 2022
7: Sun Aug 7 13:31:28 UTC 2022
8: Sun Aug 7 13:31:29 UTC 2022
9: Sun Aug 7 13:31:30 UTC 2022
10: Sun Aug 7 13:31:31 UTC 2022
11: Sun Aug 7 13:31:32 UTC 2022
12: Sun Aug 7 13:31:33 UTC 2022

Note

It is also possible to fetch logs from the previous container execution (before it was restarted) by adding the --previous argument to the kubectl logs command.

What we can see here is actually what the application inside the container writes to stdout and stderr (standard error) Linux output streams. If the logs are written to any file in the local container filesystem, those won’t be visible with kubectl logs.

However, if an application does not have a configuration to log to stdout and stderr, it is possible to add a logging sidecar – a separate container running in the same pod that captures the logs from the main container and forwards those to either a logging server or its own stdout and stderr output streams. Such a setup is depicted in Figure 7*.3*:

Figure 7.3 – Log steaming with a sidecar container

Next on our list are events that can provide valuable insights into what is happening in your Kubernetes cluster.

Kubernetes event

This is a record documenting a change in the state of Kubernetes resources—for example, changes to nodes, pods, and any other resources when a node becomes NotReady or when a PersistentVolume (PV) fails to be mounted.

Events are namespaced, and the kubectl get events command can be used to query recent events. For example, if we recently scaled an nginx deployment, we could see something similar to this:

$ minikube kubectl -- get events -n kcna
LAST SEEN TYPE REASON OBJECT MESSAGE
5s Normal Scheduled pod/nginx-deployment-with-node-selector-7668d66698-bx7kl Successfully assigned kcna/nginx-deployment-with-node-selector-7668d66698-bx7kl to minikube-m02
3s Normal Pulled pod/nginx-deployment-with-node-selector-7668d66698-bx7kl Container image "nginx:1.14.2" already present on machine
3s Normal Created pod/nginx-deployment-with-node-selector-7668d66698-bx7kl Created container nginx
3s Normal Started pod/nginx-deployment-with-node-selector-7668d66698-bx7kl Started container nginx
5s Normal SuccessfulCreate replicaset/nginx-deployment-with-node-selector-7668d66698 Created pod: nginx-deployment-with-node-selector-7668d66698-bx7kl
5s Normal ScalingReplicaSet deployment/nginx-deployment-with-node-selector Scaled up replica set nginx-deployment-with-node-selector-7668d66698 to 2

Note

By default, only events that happened in the last hour are kept. The duration can be increased in the kube-apiserver configuration.

Coming next is another useful feature that can help during debugging or development with Kubernetes— port forwarding. Using a simple kubectl command, you can configure to forward connections made on the local port on your system to the specified remote port of any pod running in your K8s cluster. The syntax of the command is shown here:

kubectl port-forward POD_NAME LOCAL_PORT:REMOTE_POD_PORT

This is very helpful if you need to access a Kubernetes payload as though it is running locally on your workstation. For example, you have a web application listening on port 80 in the pod, and you forward your local port 8080 toward remote port 80 of the respective pod. While port forwarding is running, you can reach the application locally under localhost:8080.

Port forwarding is not a part of the KCNA exam; however, if you have time, feel free to check the Further reading section at the end of the chapter for examples of how to use it.

The last point on our list is about starting processes inside already running containers of a pod. We’ve already done that previously, in Chapter 6, Deploying and Scaling Applications with Kubernetes.

The respective kubectl exec command allows us to start transient, arbitrary processes in containers, and most of the time, you’d probably use it to start a shell (bash or sh) in interactive mode. This feels much like logging inside a container with a Secure Shell (SSH) protocol. Here’s an example:

kubectl exec -it POD_NAME -c CONTAINER_NAME bash

Kubernetes also allows us to copy files between your local system and remote containers. The respective command is kubectl cp, which works very similarly to the Linux scp tool, but in the Kubernetes context.

Both exec and cp are very practical for understanding what is going on inside the container with an application we’re debugging. It allows us to quickly verify configuration settings, perform an HTTP request, or fetch logs that are not written to stdout or stderr.

All in all, in this chapter, we’ve learned a few important aspects of running and operating workloads with K8s. We’ve seen how pod scheduling works with Kubernetes, its stages (filtering and scoring), and how we can control placement decisions with nodeSelector, nodeName, and affinity and anti-affinity settings, as well as topology spread constraints. Extensive features of the Kubernetes scheduler allow us to cover all imaginable scenarios of controlling how a certain workload will be placed within the nodes of a cluster. With those controls, we can spread Pods of one application between nodes in multiple AZs for HA; schedule Pods that require specialized hardware (for example, a GPU) only to nodes that have it available; magnet multiple applications together to run on the same nodes with affinity; and much more.

Next, we’ve seen that resource requests help Kubernetes make better scheduling decisions, and resource limits are there to protect the cluster and other Pods from misbehaving apps or simply restrict resource utilization. With the container reaching the specified memory limit, its process gets killed (and the container restarted), and when reaching allocated CPU units, the process gets throttled.

After, we explored ways to debug applications on K8s. Logs are one of the basic and most important tools in any problem analysis. Kubernetes follows a cluster-level approach to logging and does not provide long-term log storage. In case of Kubernetes node failure, the logs of the node’s Pods are all gone. Therefore, it is important to set up a logging solution for aggregation and log storage. To collect and ship logs from all nodes, we use node logging agents, which can be deployed with a help of DaemonSet. If an application running in K8s cannot send its log output to stdout and stderr, the solution is to run a logging sidecar container in the same pod as the application that will stream the logs.

Other practical ways to debug applications and get insights about what is happening in a cluster include Kubernetes events, port forwarding, and execution of arbitrary processes such as a shell inside containers with the kubectl exec command.

Coming next is the final chapter from the Kubernetes part, where we will learn some of the best practices and explore other operational aspects of K8s. If you would like to go deeper into the content described in this section, check out the Further reading section.

As we conclude, here is a list of questions for you to test your knowledge regarding this chapter’s material. You will find the answers in the Assessments section of the Appendix:

  1. Which of the following stages are part of scheduling in Kubernetes (pick multiple)?
    1. Spreading 2. Launching 3. Filtering 4. Scoring
  2. What happens if the Kubernetes scheduler cannot assign a pod to a node?
    1. It will be stuck in a CrashLoopBackOff state 2. It will be stuck in a Pending state 3. It will be stuck in a NotScheduled state 4. It will be forcefully run on one of the control plane nodes
  3. Which of the following scheduler instructions will not prevent a pod from being scheduled if a condition cannot be satisfied (soft affinity or soft anti-affinity)?
    1. requiredDuringSchedulingIgnoredDuringExecution 2. preferredDuringSchedulingIgnoredDuringExecution 3. neededDuringSchedulingIgnoredDuringExecution 4. softAffinity
  4. Which Kubernetes scheduler feature should be used to control how Pods are spread across different failure domains (such as AZs, nodes, and so on)?
    1. Pod failure domain constraints 2. Pod topology spread constraints 3. podAntiAffinity 4. nodeName
  5. What is the purpose of podAffinity in Kubernetes?
    1. To schedule Pods to certain nodes in the cluster 2. To group two or more Pods together for better performance 3. To schedule Pods to nodes where other Pods already running 4. To schedule Pods to different nodes where other Pods already running
  6. What is the purpose of resource requests in Kubernetes?
    1. They help to plan ahead the cluster extension 2. They define more important workloads in the cluster 3. They are needed to pick the right hardware in the cluster for the workload 4. They are needed for optimal pod placement in the cluster
  7. What happens if a container has a memory limit of 500Mi set but tries to allocate 550Mi?
    1. 550Mi is within a 10% margin, so the container will allocate memory normally 2. Pods have higher limits than containers, so memory allocation will work 3. The container process will be killed with an OOM error 4. The container process will be stuck when it gets over 500Mi
  8. What will be the value of the container CPU request if the limit is set to 1.5 and there are no defaults for that namespace?
    1. 0.0 2. 0.75 3. 1.5 4. 1.0
  9. What happens with a pod if its containers request a total of 10.0 CPU units, but the largest node in the cluster only has 8.0 CPUs?
    1. Requests are not hard requirements; the pod gets scheduled 2. Requests are hard requirements; the pod will be stuck in a Pending state 3. Because of the preferredDuringScheduling option, the pod gets scheduled anyway 4. The pod will be in a CrashLoopBackOff state due to a lack of resources in the cluster
  10. Which logging level typically provides maximum verbosity for debugging purposes?
    1. INFO 2. ERROR 3. MAXIMUM 4. DEBUG
  11. What does cluster-level logging mean for log storage in Kubernetes?
    1. K8s aggregates all cluster logs on control plane nodes 2. K8s needs separate log collection and aggregation systems 3. K8s provides a complete log storage and aggregation solution 4. K8s provides storage only for the most important cluster health logs
  12. What do node logging agents in Kubernetes do (pick multiple)?
    1. Collect logs only from worker nodes 2. Collect logs from all nodes 3. Send logs from worker nodes to control plane nodes 4. Send logs for aggregation and storage to a logging backend
  13. What is the purpose of logging sidecar agents in Kubernetes?
    1. To collect and stream logs from applications that cannot log to stdout and stderr 2. To provide a backup copy of logs in case of a node failure 3. To allow logging on verbosity levels such as ERROR and DEBUG 4. To enable the kubectl logs command to work
  14. Which of the following kubectl commands allows us to run an arbitrary process in a container?
    1. kubectl run 2. kubectl start 3. kubectl exec 4. kubectl proc
  15. Which of the following commands will return logs from a pod container that has failed and restarted?
    1. kubectl logs POD_NAME --previous 2. kubectl logs POD_NAME 3. kubectl previous logs POD_NAME 4. kubectl logs
  16. Which Kubernetes scheduler feature provides a simple way to constrain Pods to nodes with specific labels?
    1. kubectl local 2. nodeConstrain 3. nodeSelector 4. nodeName

To learn more about the topics that were covered in this chapter, take a look at the following resources: