當你的叢集裡面有很多個edge node的時候,又想把服務部到特定的edge node上時,就需要指定一個edge node做派送,那要如何指定呢?這就是今天的重點了!
kubectl get node --show-labels
撰寫服務 yaml 檔nginx-selectnode.yaml
派送服務 yaml 檔
kubectl apply -f nginx-selectnode.yaml
kubectl get pod -o wide
Node 環境必需(完全)滿足 Pod 配置才可進行派送,否則 Pod 將創建失敗
kubectl label node edge-1 disktype=hdd
kubectl get node --show-labels
3.撰寫服務 yaml檔NodeAffinityhardMode-hdd.yaml
&NodeAffinityhardMode-ssd.yaml
NodeAffinityhardMode-hdd.yaml
NodeAffinityhardMode-ssd.yaml
完全符合需求配置方案-選用機制說明
requiredDuringSchedulingRequiredDuringExecution
requiredDuringSchedulingIgnoredDuringExecution
kubectl apply -f NodeAffinityhardMode-hdd.yaml
kubectl apply -f NodeAffinityhardMode-ssd.yaml
kubectl get pod -o wide
到這裡大家可以看到有標籤hdd的pod成功部屬服務到edge node上,反之ssd的會處於pending狀態
Pod 優先找尋相同配置 Node 環境,若所有 Node 皆無法匹配,則將 Pod 啟動於 權重評分較高 與 資源使用率較低 的 Node 之上
kubectl label node edge-2 os.version=16.04
kubectl get node --show-labels
NodeAffinitysoftMode-1.yaml
&NodeAffinitysoftMode-2.yaml
NodeAffinitysoftMode-1.yaml
NodeAffinitysoftMode-2.yaml
kubectl apply -f NodeAffinitysoftMode-1.yaml
kubectl apply -f NodeAffinitysoftMode-2.yaml
kubectl get pod -o wide
在這裡兩個pod都成功派送了,這是因為在SofrMode裡設定的條件只是參考,如果都沒有符合的話就會選一個負載較輕的edge node進行派送
測試方案 : 將node Affinity 的 hardMode和softMode的配置同時執行排送
kubectl get node --show-labels
nodeaffinity-hardandsoftmode.yaml
apiVersion: v1
kind: Pod
metadata:
name: affinity-1
labels:
app: nginx
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: disktype
operator: In
values:
- hdd
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 1
preference:
matchExpressions:
- key: os.version
operator: In
values:
- "16.04"
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
hostPort: 80
---
apiVersion: v1
kind: Pod
metadata:
name: affinity-2
labels:
app: nginx
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: disktype
operator: In
values:
- ssd
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 1
preference:
matchExpressions:
- key: os.version
operator: In
values:
- "16.04"
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
hostPort: 80
kubectl apply -f nodeaffinity-hardandsoftmode.yaml
kubectl get pod -o wide
因無法找到匹配 Pod hardMode需求設置的 Node,Pod 無法進行派送與啟動