【云原生 | Kubernetes 系列】K8s 实战 配置 Pod 的服务质量(QoS)类

作者:半身风雪
上一节:容器和 Pods资源分配
内容简介:上一节主要学习为容器设置 CPU request(请求) 和 CPU limit(限制)。 本篇文章,我们将学习怎样配置 Pod 让其获得特定的服务质量(QoS)类。

在这里插入图片描述



学习目标

Kubernetes 使用 QoS 类来决定 Pod 的调度和驱逐策略。
本篇文章将学习如何创建QoS 类的三种方式,以及如何创建包含两个容器的 Pod的相关知识。


一、创建命名空间

在文章开始之前,我们需要先创建一个命名空间,关于命名空间的内容,在之前的文章中也有讲过,使用命名空间是为了将创建的资源与集群的其余资源相隔离。

$ kubectl create namespace qos-example

二、创建 QoS 类

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

  • Guaranteed
  • Burstable
  • BestEffort

2.1、 QoS 类为 Guaranteed 的 Pod

对于 QoS 类为 Guaranteed 的 Pod:

  • Pod 中的每个容器都必须指定内存限制和内存请求。
  • 对于 Pod 中的每个容器,内存限制必须等于内存请求。
  • Pod 中的每个容器都必须指定 CPU 限制和 CPU 请求。
  • 对于 Pod 中的每个容器,CPU 限制必须等于 CPU 请求。

这些限制同样适用于初始化容器和应用程序容器。

下面是目录 pods/qos/qos-pod.yaml 容器的 Pod 配置文件。 容器设置了内存请求和内存限制,值都是 200 MiB。 容器设置了 CPU 请求和 CPU 限制,值都是 700 milliCPU:

apiVersion: v1
kind: Pod
metadata:
  name: qos-demo
  namespace: qos-example
spec:
  containers:
  - name: qos-demo-ctr
    image: nginx
    resources:
      limits:
        memory: "200Mi"
        cpu: "700m"
      requests:
        memory: "200Mi"
        cpu: "700m"
  1. 创建 Pod
$ kubectl create -f https://k8s.io/examples/pods/qos/qos-pod.yaml --namespace=qos-example
  1. 查看 Pod 详情
$ kubectl get pod qos-demo --namespace=qos-example --output=yaml
  1. 输出结果表明 Kubernetes 为 Pod 配置的 QoS 类为 Guaranteed。 结果也确认了 Pod 容器设置了与内存限制匹配的内存请求,设置了与 CPU 限制匹配的 CPU 请求。
spec:
  containers:
    ...
    resources:
      limits:
        cpu: 700m
        memory: 200Mi
      requests:
        cpu: 700m
        memory: 200Mi
status:
  qosClass: Guaranteed
  1. 我们也可以执行下面的命令,删除Pod
$ kubectl delete pod qos-demo --namespace=qos-example

2.2、 QoS 类为 Burstable 的 Pod

指定 Pod 的 QoS 类为 Burstable需要满足下面的两个条件:

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

下面是目录 pods/qos/qos-pod-2.yaml 容器的 Pod 配置文件。 容器设置了内存限制 200 MiB 和内存请求 100 MiB。

apiVersion: v1
kind: Pod
metadata:
  name: qos-demo-2
  namespace: qos-example
spec:
  containers:
  - name: qos-demo-2-ctr
    image: nginx
    resources:
      limits:
        memory: "200Mi"
      requests:
        memory: "100Mi"
  1. 创建 Pod
$ kubectl create -f https://k8s.io/examples/pods/qos/qos-pod-2.yaml --namespace=qos-example
  1. 查看 Pod 详情
$ kubectl get pod qos-demo-2 --namespace=qos-example --output=yaml
  1. 输出结果表明 Kubernetes 为 Pod 配置的 QoS 类为 Burstable。
spec:
  containers:
  - image: nginx
    imagePullPolicy: Always
    name: qos-demo-2-ctr
    resources:
      limits:
        memory: 200Mi
      requests:
        memory: 100Mi
  ...
status:
  qosClass: Burstable
  1. 删除 Pod
$ kubectl delete pod qos-demo-2 --namespace=qos-example

2.3、 QoS 类为 BestEffort 的 Pod

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

下面是目录 pods/qos/qos-pod-3.yaml 容器的 Pod 配置文件。 容器没有设置内存和 CPU 限制或请求。

apiVersion: v1
kind: Pod
metadata:
  name: qos-demo-3
  namespace: qos-example
spec:
  containers:
  - name: qos-demo-3-ctr
    image: nginx
  1. 创建 Pod
$ kubectl create -f https://k8s.io/examples/pods/qos/qos-pod-3.yaml --namespace=qos-example
  1. 查看 Pod 详情
$ kubectl get pod qos-demo-3 --namespace=qos-example --output=yaml
  1. 输出结果表明 Kubernetes 为 Pod 配置的 QoS 类为 BestEffort。
spec:
  containers:
    ...
    resources: {
    
    }
  ...
status:
  qosClass: BestEffort
  1. 删除Pod
$ kubectl delete pod qos-demo-3 --namespace=qos-example

三、创建包含两个容器的 Pod

先看一下目录 pods/qos/qos-pod-4.yaml 包含两个容器的 Pod 配置文件。 一个容器指定了内存请求 200 MiB。 另外一个容器没有指定任何请求和限制。

apiVersion: v1
kind: Pod
metadata:
  name: qos-demo-4
  namespace: qos-example
spec:
  containers:

  - name: qos-demo-4-ctr-1
    image: nginx
    resources:
      requests:
        memory: "200Mi"

  - name: qos-demo-4-ctr-2
    image: redis

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

  1. 创建 Pod
$ kubectl create -f https://k8s.io/examples/pods/qos/qos-pod-4.yaml --namespace=qos-example
  1. 查看 Pod 详情
$ kubectl get pod qos-demo-4 --namespace=qos-example --output=yaml
  1. 结果表明 Kubernetes 为 Pod 配置的 QoS 类为 Burstable:
spec:
  containers:
    ...
    name: qos-demo-4-ctr-1
    resources:
      requests:
        memory: 200Mi
    ...
    name: qos-demo-4-ctr-2
    resources: {
    
    }
    ...
status:
  qosClass: Burstable
  1. 删除 Pod
$ kubectl delete pod qos-demo-4 --namespace=qos-example

五、环境清理

在文章的开头,我们创建了一个命令空间,并这个空间中,完成了(QoS)类 的学习,那么在文章最后,我们再来删除个这个命令空间:

$ kubectl delete namespace qos-example

总结

本篇文章类容比较短,主要介绍了怎样配置 Pod 让其获得特定的服务质量(QoS)类,并且运用了之前文章中讲解的命名空间的相关知识。

猜你喜欢

转载自blog.csdn.net/u010755471/article/details/126007603