Manual Scheduling
在 Kubernetes 中,手動調度框架允許您將 Pod 分配到特定節點,而無需依賴默認調度器。這對于測試、調試或處理特定工作負載非常有用。您可以通過在 Pod 的規范中設置 nodeName
字段來實現手動調度。以下是一個示例:
apiVersion: v1
kind: Pod
metadata:name: manual-scheduled-pod
spec:containers:- name: nginximage: nginxnodeName: your-node-name
將 your-node-name
替換為您希望 Pod 運行的節點名稱。應用此配置后,Kubernetes 會直接將 Pod 放置在指定的節點上。
nodeName
nodeName
?是比親和性或者?nodeSelector
?更為直接的形式。nodeName
?是 Pod 規約中的一個字段。如果?nodeName
?字段不為空,調度器會忽略該 Pod, 而指定節點上的 kubelet 會嘗試將 Pod 放到該節點上。 使用?nodeName
?規則的優先級會高于使用?nodeSelector
?或親和性與非親和性的規則。
使用?nodeName
?來選擇節點的方式有一些局限性:
- 如果所指代的節點不存在,則 Pod 無法運行,而且在某些情況下可能會被自動刪除。
- 如果所指代的節點無法提供用來運行 Pod 所需的資源,Pod 會失敗, 而其失敗原因中會給出是否因為內存或 CPU 不足而造成無法運行。
- 在云環境中的節點名稱并不總是可預測的,也不總是穩定的。
警告:
nodeName
?旨在供自定義調度器或需要繞過任何已配置調度器的高級場景使用。 如果已分配的 Node 負載過重,繞過調度器可能會導致 Pod 失敗。 你可以使用節點親和性或?nodeselector?字段將 Pod 分配給特定 Node,而無需繞過調度器。
下面是一個使用?nodeName
?字段的 Pod 規約示例:
apiVersion: v1
kind: Pod
metadata:name: nginx
spec:containers:- name: nginximage: nginxnodeName: kube-01
上面的 Pod 只能運行在節點?kube-01
?之上。
Practice Detail
Welcome to the KodeKloud Hands-On lab __ ______ ____ ________ __ __ ____ __ ______ / //_/ __ \/ __ \/ ____/ //_// / / __ \/ / / / __ \/ ,< / / / / / / / __/ / ,< / / / / / / / / / / / // /| / /_/ / /_/ / /___/ /| |/ /___/ /_/ / /_/ / /_/ /
/_/ |_\____/_____/_____/_/ |_/_____/\____/\____/_____/ All rights reserved controlplane ~ ? kubectl create -f nginx.yaml
pod/nginx createdcontrolplane ~ ? kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 0/1 Pending 0 8scontrolplane ~ ? kubectl describe pod nginx
Name: nginx
Namespace: default
Priority: 0
Service Account: default
Node: <none>
Labels: <none>
Annotations: <none>
Status: Pending
IP:
IPs: <none>
Containers:nginx:Image: nginxPort: <none>Host Port: <none>Environment: <none>Mounts:/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-bvfbc (ro)
Volumes:kube-api-access-bvfbc:Type: Projected (a volume that contains injected data from multiple sources)TokenExpirationSeconds: 3607ConfigMapName: kube-root-ca.crtConfigMapOptional: <nil>DownwardAPI: true
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300snode.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events: <none>controlplane ~ ? kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-7484cd47db-mhzfg 1/1 Running 0 10m
coredns-7484cd47db-pq6v7 1/1 Running 0 10m
etcd-controlplane 1/1 Running 0 10m
kube-apiserver-controlplane 1/1 Running 0 10m
kube-controller-manager-controlplane 1/1 Running 0 10m
kube-proxy-dd6zp 1/1 Running 0 9m48s
kube-proxy-mtmxb 1/1 Running 0 10mcontrolplane ~ ? echo "no scheduler present"
no scheduler presentcontrolplane ~ ? cat nginx.yaml
---
apiVersion: v1
kind: Pod
metadata:name: nginx
spec:containers:- image: nginxname: nginxcontrolplane ~ ? vi nginx.yamlcontrolplane ~ ? cat nginx.yaml
---
apiVersion: v1
kind: Pod
metadata:name: nginx
spec:nodeName: node01containers:- image: nginxname: nginxcontrolplane ~ ? echo "Manually schedule the pod on node01"
Manually schedule the pod on node01controlplane ~ ? cat nginx.yaml
---
apiVersion: v1
kind: Pod
metadata:name: nginx
spec:nodeName: node01containers:- image: nginxname: nginxcontrolplane ~ ? kubectl replace --force -f nginx.yaml
pod "nginx" deleted
pod/nginx replacedcontrolplane ~ ? kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx 1/1 Running 0 43s 172.17.1.3 node01 <none> <none>controlplane ~ ? vi nginx.yamlcontrolplane ~ ? cat nginx.yaml
---
apiVersion: v1
kind: Pod
metadata:name: nginx
spec:nodeName: controlplanecontainers:- image: nginxname: nginxcontrolplane ~ ? kubectl replace --force -f nginx.yaml
pod "nginx" deleted
pod/nginx replacedcontrolplane ~ ? kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx 1/1 Running 0 8s 172.17.0.4 controlplane <none> <none>controlplane ~ ? echo "Now schedule the same pod on the controlplane node."
Now schedule the same pod on the controlplane node.Powered by Moshow@https://zhengkai.blog.csdn.net/