website/content/zh/docs/tasks/configure-pod-container/quality-service-pod.md

11 KiB
Raw Blame History

title content_template weight
配置 Pod 的服务质量 templates/task 30

{{% capture overview %}}

本文介绍怎样配置 Pod 让其获得特定的服务质量QoS类。Kubernetes 使用 QoS 类来决定 Pod 的调度和驱逐策略。

{{% /capture %}}

{{% capture prerequisites %}}

{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}

{{% /capture %}}

{{% capture steps %}}

QoS 类

Kubernetes 创建 Pod 时就给它指定了下列一种 QoS 类:

  • Guaranteed
  • Burstable
  • BestEffort

创建命名空间

创建一个命名空间,以便将本练习所创建的资源与集群的其余资源相隔离。

kubectl create namespace qos-example

创建一个 QoS 类为 Guaranteed 的 Pod

对于 QoS 类为 Guaranteed 的 Pod

  • Pod 中的每个容器必须指定内存请求和内存限制,并且两者要相等。
  • Pod 中的每个容器必须指定 CPU 请求和 CPU 限制,并且两者要相等。

下面是包含一个容器的 Pod 配置文件。 容器设置了内存请求和内存限制,值都是 200 MiB。 容器设置了 CPU 请求和 CPU 限制,值都是 700 milliCPU

{{< codenew file="pods/qos/qos-pod.yaml" >}}

创建 Pod

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

查看 Pod 详情:

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

结果表明 Kubernetes 为 Pod 配置的 QoS 类为 Guaranteed。 结果也确认了 Pod 容器设置了与内存限制匹配的内存请求,设置了与 CPU 限制匹配的 CPU 请求。

spec:
  containers:
    ...
    resources:
      limits:
        cpu: 700m
        memory: 200Mi
      requests:
        cpu: 700m
        memory: 200Mi
...
  qosClass: Guaranteed

{{< note >}}

如果容器指定了自己的内存限制但没有指定内存请求Kubernetes 会自动为它指定与内存限制匹配的内存请求。 同样,如果容器指定了自己的 CPU 限制,但没有指定 CPU 请求Kubernetes 会自动为它指定与 CPU 限制匹配的 CPU 请求。 {{< /note >}}

删除 Pod

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

创建一个 QoS 类为 Burstable 的 Pod

如果满足下面条件,将会指定 Pod 的 QoS 类为 Burstable

  • Pod 不符合 Guaranteed QoS 类的标准。
  • Pod 中至少一个容器具有内存或 CPU 请求。

下面是包含一个容器的 Pod 配置文件。 容器设置了内存限制 200 MiB 和内存请求 100 MiB。

{{< codenew file="pods/qos/qos-pod-2.yaml" >}}

创建 Pod

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

查看 Pod 详情:

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

结果表明 Kubernetes 为 Pod 配置的 QoS 类为 Burstable。

spec:
  containers:
  - image: nginx
    imagePullPolicy: Always
    name: qos-demo-2-ctr
    resources:
      limits:
        memory: 200Mi
      requests:
        memory: 100Mi
...
  qosClass: Burstable

删除 Pod

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

创建一个 QoS 类为 BestEffort 的 Pod

对于 QoS 类为 BestEffort 的 PodPod 中的容器必须没有设置内存和 CPU 限制或请求。

下面是包含一个容器的 Pod 配置文件。 容器没有设置内存和 CPU 限制或请求。

{{< codenew file="pods/qos/qos-pod-3.yaml" >}}

创建 Pod

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

查看 Pod 详情:

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

结果表明 Kubernetes 为 Pod 配置的 QoS 类为 BestEffort。

spec:
  containers:
    ...
    resources: {}
  ...
  qosClass: BestEffort

删除 Pod

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

创建包含两个容器的 Pod

下面是包含两个容器的 Pod 配置文件。 一个容器指定了内存请求 200 MiB。 另外一个容器没有指定任何请求和限制。

{{< codenew file="pods/qos/qos-pod-4.yaml" >}}

注意此 Pod 满足 Burstable QoS 类的标准。 也就是说它不满足 Guaranteed QoS 类标准,因为它的一个容器设有内存请求。

创建 Pod

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

查看 Pod 详情:

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

结果表明 Kubernetes 为 Pod 配置的 QoS 类为 Burstable

spec:
  containers:
    ...
    name: qos-demo-4-ctr-1
    resources:
      requests:
        memory: 200Mi
    ...
    name: qos-demo-4-ctr-2
    resources: {}
    ...
  qosClass: Burstable

删除 Pod

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

环境清理

删除命名空间:

kubectl delete namespace qos-example

{{% /capture %}}

{{% capture whatsnext %}}

应用开发者参考

集群管理员参考

{{% /capture %}}