kubernetes 創建pod時就給他指定了下列一種 QoS 類:
- Guaranteed
- Burstable
- BestEffort
創建命名空間
創建一個命名空間,以便將本練習所創建的資源與集群的其余資源相隔離。
kubectl create namespace qos-example
創建一個 QoS 類為 Guaranteed 的 Pod
對於 QoS 類為 Guaranteed 的 Pod:
- Pod 中的每個容器都必須指定內存限制和內存請求。
- 對於 Pod 中的每個容器,內存限制必須等於內存請求。
- Pod 中的每個容器都必須指定 CPU 限制和 CPU 請求。
- 對於 Pod 中的每個容器,CPU 限制必須等於 CPU 請求。
這些限制同樣適用於初始化容器和應用程序容器。
下面是包含一個容器的 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"
創建 Pod:
kubectl apply -f -n qos-example
查看 Pod 詳情:
kubectl get pod qos-demo --namespace=qos-example --output=yaml
創建一個 QoS 類為 Burstable 的 Pod
如果滿足下面條件,將會指定 Pod 的 QoS 類為 Burstable:
- Pod 不符合 Guaranteed QoS 類的標准。
- 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 qos-pod-2.yaml -n 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
創建一個 QoS 類為 BestEffort 的 Pod
對於 QoS 類為 BestEffort 的 Pod,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 qos-pod-3.yaml -n 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
下面是包含兩個容器的 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 類標准,因為它的一個容器設有內存請求。
創建 Pod:
kubectl apply -f qos-pod-4.yaml --n 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
