02 - Static PODs

Static Pod

Static Pods 是 Kubernetes 內部機制,用來在特定節點上直接執行 Pod。

Static Pods 由 Kubelet 直接管理,不經 API Server。

適用於:

  • Kubernetes 控制面節點 (Control Plane) 的核心元件(如 kube-apiserver, kube-controller-manager, kube-scheduler, etcd)
  • 在特定節點上執行的系統服務,不受 Deployment 或 DaemonSet 控制
  • 不依賴 Kubernetes API 來管理 Pod


Static Pod 的特點

不經過 API Server,而是由 Kubelet 直接監控和管理
  (修改或刪除 Pod,直接編輯或刪除 YAML 檔案即可
只會在特定節點上運行,不會自動調度到其他節點
沒有 ReplicaSet、Deployment 或 DaemonSet 控制
YAML 檔案存放於節點的 staticPodPath 目錄 = /etc/kubernetes/manifests/
Pod 會自動重啟,但不會自動恢復或遷移到其他節點


查看 Static Pods

$ kubectl get pods --all-namespaces                

$ kubectl get pods -o wide                               

注意:Static Pod 只會在該節點上執行,且不會顯示在 kubectl get deployments 或 kubectl get daemonsets 裡。


刪除 Static Pod

Static Pods 無法透過 `kubectl delete pod` 刪除,因為它們由 Kubelet 自動管理。

$ sudo rm /etc/kubernetes/manifests/static-nginx.yaml                           

Kubelet 會自動偵測到檔案被刪除,並停止該 Pod。


變更 Static Pod

$ sudo vim /etc/kubernetes/manifests/static-nginx.yaml                          

Kubelet 會自動偵測變更並重啟 Pod


摘要:Static Pod vs DaemonSet



何時使用 Static Pod?

✅ Static Pod 適合:

  • Kubernetes 核心元件(API Server、Scheduler、etcd)
  • 單節點 Kubernetes 部署(適合單節點或內部服務,不適合一般應用程式)
  • 獨立於 API Server 的關鍵應用
  • 確保特定節點執行的服務

❌ Static Pod 不適合:

  • 需要自動調度與擴展的應用
  • 需要在多個節點執行的應用



Q: How many static pods exist in this cluster in all namespaces?

Ans: 4

$ kubectl get pods --all-namespaces                   

look for those with `-controlplane` appended in the name

Q: On which nodes are the static pods created currently?

identify the node in which static pods are deployed

$ kubectl get pods -A -o wide                       

By default, static pods are created for the `controlplane` components and hence, they are only created in the `controlplane` node.


Q: What is the path of the directory holding the static pod definition files?

$ ps -aux | grep kubelet                                                

identify the config file `--config=/var/lib/kubelet/config.yaml`

$ cat /var/lib/kubelet/config.yaml | grep staticPodPath     

Then, check in the config file for staticPodPath.

Ans: /etc/kubernetes/manifests


Q: How many pod definition files are present in the manifests directory?

Ans: 4

$ ls /etc/kubernetes/manifests | wc -l                 

Four files should be in this directory for the four static pods we identified earlier.

Q: What is the docker image used to deploy the `kube-api server` as a static pod?

【方法一】

$ kubectl describe pod kube-apiserver-controlplane --namespace kube-system | grep Image         

【方法二】

$ cat /etc/kubernetes/manifests/kube-apiserver.yaml | grep image               

Check the image defined in the manifest file.


Q: Create a static pod named `static-busybox` that uses the `busybox` image and the command `sleep 1000`

  • Name: static-busybox
  • Image: busybox

Create a pod definition file in the manifests folder.

$ kubectl run --restart=Never --image=busybox static-busybox --dry-run=client -o yaml --command -- sleep 1000 > /etc/kubernetes/manifests/static-busybox.yaml            


Q: Edit the image on the static pod to use `busybox:1.28.4`

  • Name: static-busybox
  • Image: busybox:1.28.4

使用相同指令,讓產出的 YAML 檔覆蓋原本的。

$ kubectl run --restart=Never --image=busybox:1.28.4 static-busybox --dry-run=client -o yaml --command -- sleep 1000 > /etc/kubernetes/manifests/static-busybox.yaml      


Q: We just created a new static pod named `static-greenbox`. Find it and delete it.

Identify which node the static pod is created on, ssh to the node and delete the pod definition file. If you don't know the IP of the node, run the `kubectl get nodes -o wide` command and identify the IP.

Then, SSH to the node using that IP. For static pod manifest path look at the file `/var/lib/kubelet/config.yaml` on node01


Step1

First, let's identify the node in which the pod called `static-greenbox` is created.

$ kubectl get pods -A -o wide | grep static-greenbox                  

The pod is running on `node01`.

Step2

Next, SSH to `node01` and identify the path configured for static pods in this node.

$ ssh node01                       

$ ps -aux | grep kubelet        or          $ ps -ef |  grep /usr/bin/kubelet        

$ grep -i staticpod /var/lib/kubelet/config.yaml              

--config=/var/lib/kubelet/config.yaml

staticPodPath is `/etc/just-to-mess-with-you`

Step3

Navigate to this directory and delete the YAML file.

$ /etc/just-to-mess-with-you/            

$ rm greenbox.yaml                            


Step4

Exit out of `node01` using `CTRL + D` or type `exit`. You should return to the controlplane node. Check if the static-greenbox pod has been deleted:

$ exit                                      

$ kubectl get pods -A -o wide | grep static-greenbox           

留言