为 Pod 配置服务质量
此页面显示如何配置 Pod,以便为其分配特定的服务质量 (QoS) 类。 Kubernetes 使用 QoS 类来决定在超过节点资源时驱逐 Pod。
当 Kubernetes 创建 Pod 时,它会为 Pod 分配其中一个 QoS 类
说明
Kubernetes 在创建 Pod 时分配 QoS 类,并在 Pod 的生命周期内保持不变。 如果您尝试调整 Pod 的资源为导致不同 QoS 类的数值,控制平面将拒绝您的请求并显示错误消息。开始之前
您需要一个 Kubernetes 集群,并且 kubectl 命令行工具必须配置为与您的集群通信。 建议在至少有两个节点(不充当控制平面主机)的集群上运行本教程。 如果您还没有集群,可以使用 minikube 创建一个,或者可以使用以下 Kubernetes 游乐场
您还需要能够创建和删除命名空间。
创建命名空间
创建一个命名空间,以便您在此练习中创建的资源与集群的其余部分隔离。
kubectl create namespace qos-example
创建一个被分配 QoS 类为 Guaranteed 的 Pod
为了使 Pod 获得 QoS 类 Guaranteed
- Pod 中的每个容器必须具有内存限制和内存请求。
- 对于 Pod 中的每个容器,内存限制必须等于内存请求。
- Pod 中的每个容器必须具有 CPU 限制和 CPU 请求。
- 对于 Pod 中的每个容器,CPU 限制必须等于 CPU 请求。
这些限制同样适用于 init 容器和 app 容器。Ephemeral 容器无法定义资源,因此这些限制不适用。
这是一个具有一个容器的 Pod 的清单。 该容器具有内存限制和内存请求,两者均为 200 MiB。 该容器具有 CPU 限制和 CPU 请求,两者均为 700 毫 CPU
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"
创建 Pod
kubectl apply -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
...
status:
qosClass: Guaranteed
说明
如果容器指定了自己的内存限制,但没有指定内存请求,Kubernetes 会自动分配与限制匹配的内存请求。 同样,如果容器指定了自己的 CPU 限制,但没有指定 CPU 请求,Kubernetes 会自动分配与限制匹配的 CPU 请求。清理
删除您的 Pod
kubectl delete pod qos-demo --namespace=qos-example
创建一个被分配 QoS 类为 Burstable 的 Pod
如果满足以下条件,Pod 将被分配 QoS 类 Burstable
- Pod 不满足 QoS 类
Guaranteed的标准。 - Pod 中的至少一个容器具有内存或 CPU 请求或限制。
这是一个具有一个容器的 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"
创建 Pod
kubectl apply -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
...
status:
qosClass: Burstable
清理
删除您的 Pod
kubectl delete pod qos-demo-2 --namespace=qos-example
创建一个被分配 QoS 类为 BestEffort 的 Pod
为了使 Pod 获得 QoS 类 BestEffort,Pod 中的容器不得具有任何内存或 CPU 限制或请求。
这是一个具有一个容器的 Pod 的清单。 该容器没有内存或 CPU 限制或请求
apiVersion: v1
kind: Pod
metadata:
name: qos-demo-3
namespace: qos-example
spec:
containers:
- name: qos-demo-3-ctr
image: nginx
创建 Pod
kubectl apply -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: {}
...
status:
qosClass: BestEffort
清理
删除您的 Pod
kubectl delete pod qos-demo-3 --namespace=qos-example
创建一个具有两个容器的 Pod
这是一个具有两个容器的 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 满足 QoS 类 Burstable 的标准。 也就是说,它不满足 QoS 类 Guaranteed 的标准,并且其一个容器具有内存请求。
创建 Pod
kubectl apply -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: {}
...
status:
qosClass: Burstable
检索 Pod 的 QoS 类
与其查看所有字段,您可以只查看您需要的字段
kubectl --namespace=qos-example get pod qos-demo-4 -o jsonpath='{ .status.qosClass}{"\n"}'
Burstable
清理
删除您的命名空间
kubectl delete namespace qos-example