Configure Quality of Service for Pods

This page shows how to configure Pods so that they will be assigned particular Quality of Service (QoS) classes. Kubernetes uses QoS classes to make decisions about scheduling and evicting Pods.

Before you begin

You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. If you do not already have a cluster, you can create one by using minikube or you can use one of these Kubernetes playgrounds:

To check the version, enter kubectl version.

QoS classes

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

  • Guaranteed
  • Burstable
  • BestEffort

Create a namespace

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

  1. kubectl create namespace qos-example

Create a Pod that gets assigned a QoS class of Guaranteed

For a Pod to be given a QoS class of Guaranteed:

  • Every Container, including init containers, in the Pod must have a memory limit and a memory request, and they must be the same.
  • Every Container, including init containers, in the Pod must have a CPU limit and a CPU request, and they must be the same.

Here is the configuration file 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 Configure Quality of Service for Pods - 图1

  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. cpu: 700m
  7. memory: 200Mi
  8. requests:
  9. cpu: 700m
  10. memory: 200Mi
  11. ...
  12. status:
  13. 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.

Delete your Pod:

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

Create a Pod that gets assigned a QoS class of Burstable

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.

Here is the configuration file for a Pod that has one Container. The Container has a memory limit of 200 MiB and a memory request of 100 MiB.

pods/qos/qos-pod-2.yaml Configure Quality of Service for Pods - 图2

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

Create the Pod:

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

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

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 the configuration file for a Pod that has one Container. The Container has no memory or CPU limits or requests:

pods/qos/qos-pod-3.yaml Configure Quality of Service for Pods - 图3

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

Create the Pod:

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

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.

  1. spec:
  2. containers:
  3. ...
  4. resources: {}
  5. ...
  6. status:
  7. qosClass: BestEffort

Delete your Pod:

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

Create a Pod that has two Containers

Here is the configuration file 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.

pods/qos/qos-pod-4.yaml 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

Notice that this Pod meets the criteria for QoS class Burstable. That is, it does not meet the criteria for QoS class Guaranteed, and one of its Containers has a memory request.

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

Delete your Pod:

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

Clean up

Delete your namespace:

  1. kubectl delete namespace qos-example

What’s next

For app developers

For cluster administrators