Kubernetes: From Zero to Hero (Part 13) - Quality of Service 🔥💥
Tags: beginners, kubernetes, tutorial, devops
Published: 2023-01-02 20:18:37

## What is Quality of Service(QoS)

Kubernetes Quality of Service (QoS) is a feature that helps you manage the resources that are allocated to your applications in a cluster. It allows you to specify the relative priority of different applications (called "pods"), which can be useful in a number of different scenarios.

Pods are assigned to one of three QoS classes: "Guaranteed," "Burstable," and "Best Effort." The QoS class of a pod determines the priority of that pod and how it is treated by the scheduler when allocating resources. For example, pods in the Guaranteed class are given the highest priority and are guaranteed a certain amount of resources, while pods in the Best Effort class are given the lowest priority and may be starved of resources if the cluster is under heavy load.

Using QoS can be helpful in a number of different scenarios. For example, if you have a set of applications that are critical to your business and need a consistent level of resources, you might assign them to the Guaranteed class. On the other hand, if you have batch jobs or other non-critical workloads that can tolerate some level of resource contention, you might assign them to the Best Effort class.

## Why is Quality of Service important?

Quality of Service (QoS) is important in Kubernetes because it allows you to specify the relative priority of different applications (pods) and how they should be treated when allocating resources. This can be useful in a number of different scenarios.

For example, if you have a set of applications that are critical to your business and need a consistent level of resources, you might want to give them a higher priority by assigning them to the Guaranteed QoS class. This will ensure that they are allocated the resources they need to run effectively, and will prevent them from being starved of resources.

On the other hand, if you have batch jobs or other non-critical workloads that can tolerate some level of resource contention, you might want to give them a lower priority by assigning them to the Best Effort QoS class. This will allow them to use resources that are available, but they may be starved of resources if the cluster is under heavy load.

## Types Of Quality of Service

In Kubernetes, there are three Quality of Service (QoS) classes that can be assigned to pods: Guaranteed, Burstable, and Best Effort. Each QoS class has a specific set of characteristics and is intended for use in different scenarios. Here's a brief overview of each QoS class:

**Guaranteed**: Pods in the Guaranteed class are given the highest priority and are guaranteed a certain amount of resources. If a pod in this class is not able to be scheduled due to resource constraints, the scheduler will not schedule any other pods until resources become available. Here's an example of how to specify the Guaranteed QoS class in a pod specification:
```yaml
apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: my-container
    image: my-image
    resources:
      limits:
        cpu: "1"
        memory: "500Mi"
      requests:
        cpu: "1"
        memory: "500Mi"
```
NOTE: For a Pod to be given a QoS class of Guaranteed:
- Every Container in the Pod must have a memory limit and a memory request.
- For every Container in the Pod, the memory limit must equal the memory request.
- Every Container in the Pod must have a CPU limit and a CPU request.
- For every Container in the Pod, the CPU limit must equal the CPU request

**Burstable**: Pods in the Burstable class are given a lower priority than pods in the Guaranteed class, but are still allocated a certain amount of resources. If a pod in this class is not able to be scheduled due to resource constraints, the scheduler may choose to schedule other pods instead. Here's an example of how to specify the Burstable QoS class in a pod specification:
```yaml
apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: my-container
    image: my-image
    resources:
      limits:
        cpu: "1"
        memory: "500Mi"
      requests:
        cpu: "0.5"
        memory: "200Mi"

```
NOTE: A Pod is given a QoS class of Burstable if:

- The Pod does not meet the criteria for QoS class Guaranteed.
- At least one Container in the Pod has a memory or CPU request or limit.

**Best Effort**: Pods in the Best Effort class are given the lowest priority and may be starved of resources if the cluster is under heavy load. They are typically used for batch jobs or other non-critical workloads that can tolerate some level of resource contention. Here's an example of how to specify the Best Effort QoS class in a pod specification:

```yaml
apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: my-container
    image: my-image
```
NOTE: For a Pod to be given a QoS class of BestEffort, the Containers in the Pod must not have any memory or CPU limits or requests.

In general, it's a good idea to use the Guaranteed QoS class for critical applications that need a consistent level of resources, the Burstable class for applications that need a certain amount of resources but can tolerate some level of resource contention, and the Best Effort class for batch jobs or other non-critical workloads that can tolerate resource contention.