Configure Quality of Service for Pods

When Kubernetes creates a Pod it assigns one of these QoS classes to the Pod:

You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. It is recommended to run this tutorial on a cluster with at least two nodes that are not acting as control plane hosts. If you do not already have a cluster, you can create one by using minikube or you can use one of these Kubernetes playgrounds:

You also need to be able to create and delete namespaces.

Create a namespace

Create a namespace so that the resources you create in this exercise are isolated from the rest of your cluster.

Create a Pod that gets assigned a QoS class of Guaranteed

For a Pod to be given a QoS class of :

  • 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.

These restrictions apply to init containers and app containers equally. cannot define resources so these restrictions do not apply.

Here is a manifest for a Pod that has one Container. The Container has a memory limit and a memory request, both equal to 200 MiB. The Container has a CPU limit and a CPU request, both equal to 700 milliCPU:

pods/qos/qos-pod.yaml

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: qos-demo
  5. namespace: qos-example
  6. spec:
  7. containers:
  8. - name: qos-demo-ctr
  9. image: nginx
  10. resources:
  11. limits:
  12. memory: "200Mi"
  13. cpu: "700m"
  14. requests:
  15. memory: "200Mi"
  16. cpu: "700m"

Create the Pod:

  1. kubectl apply -f https://k8s.io/examples/pods/qos/qos-pod.yaml --namespace=qos-example

View detailed information about the Pod:

  1. kubectl get pod qos-demo --namespace=qos-example --output=yaml

The output shows that Kubernetes gave the Pod a QoS class of Guaranteed. The output also verifies that the Pod Container has a memory request that matches its memory limit, and it has a CPU request that matches its CPU limit.

  1. spec:
  2. containers:
  3. ...
  4. resources:
  5. limits:
  6. memory: 200Mi
  7. requests:
  8. cpu: 700m
  9. memory: 200Mi
  10. ...
  11. status:
  12. qosClass: Guaranteed

Note: If a Container specifies its own memory limit, but does not specify a memory request, Kubernetes automatically assigns a memory request that matches the limit. Similarly, if a Container specifies its own CPU limit, but does not specify a CPU request, Kubernetes automatically assigns a CPU request that matches the limit.

Clean up

Delete your Pod:

  1. kubectl delete pod qos-demo --namespace=qos-example

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.

Configure Quality of Service for Pods - 图2

  1. apiVersion: v1
  2. kind: Pod
  3. name: qos-demo-2
  4. namespace: qos-example
  5. spec:
  6. containers:
  7. - name: qos-demo-2-ctr
  8. image: nginx
  9. resources:
  10. limits:
  11. memory: "200Mi"
  12. requests:
  13. memory: "100Mi"

Create the Pod:

View detailed information about the Pod:

  1. kubectl get pod qos-demo-2 --namespace=qos-example --output=yaml

The output shows that Kubernetes gave the Pod a QoS class of Burstable:

  1. spec:
  2. containers:
  3. - image: nginx
  4. imagePullPolicy: Always
  5. name: qos-demo-2-ctr
  6. resources:
  7. limits:
  8. memory: 200Mi
  9. requests:
  10. memory: 100Mi
  11. ...
  12. status:
  13. qosClass: Burstable

Clean up

Delete your Pod:

  1. kubectl delete pod qos-demo-2 --namespace=qos-example

Create a Pod that gets assigned a QoS class of BestEffort

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.

Here is a manifest for a Pod that has one Container. The Container has no memory or CPU limits or requests:

pods/qos/qos-pod-3.yaml

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: qos-demo-3
  5. namespace: qos-example
  6. spec:
  7. containers:
  8. image: nginx

Create the Pod:

View detailed information about the Pod:

  1. kubectl get pod qos-demo-3 --namespace=qos-example --output=yaml

The output shows that Kubernetes gave the Pod a QoS class of BestEffort:

Clean up

Delete your Pod:

  1. kubectl delete pod qos-demo-3 --namespace=qos-example

Create a Pod that has two Containers

Here is a manifest for a Pod that has two Containers. One container specifies a memory request of 200 MiB. The other Container does not specify any requests or limits.

Configure Quality of Service for Pods - 图4

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: qos-demo-4
  5. namespace: qos-example
  6. spec:
  7. containers:
  8. - name: qos-demo-4-ctr-1
  9. image: nginx
  10. resources:
  11. requests:
  12. memory: "200Mi"
  13. - name: qos-demo-4-ctr-2
  14. image: redis

Create the Pod:

  1. kubectl apply -f https://k8s.io/examples/pods/qos/qos-pod-4.yaml --namespace=qos-example

View detailed information about the Pod:

  1. kubectl get pod qos-demo-4 --namespace=qos-example --output=yaml

The output shows that Kubernetes gave the Pod a QoS class of Burstable:

  1. spec:
  2. containers:
  3. ...
  4. name: qos-demo-4-ctr-1
  5. resources:
  6. requests:
  7. memory: 200Mi
  8. ...
  9. name: qos-demo-4-ctr-2
  10. resources: {}
  11. ...
  12. status:
  13. qosClass: Burstable

Rather than see all the fields, you can view just the field you need:

Clean up

Delete your namespace:

  1. kubectl delete namespace qos-example

What’s next

For cluster administrators