發表文章

目前顯示的是 3月, 2025的文章

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...

02 - DaemonSets

圖片
DaemonSet DaemonSet 是 Kubernetes 中的一種資源類型,用來 確保每個節點 (Node) 都執行「一個」特定的 Pod 。這通常用於執行節點級別的系統服務,例如: 日誌收集 (如 Fluentd、Filebeat) 監控代理 (如 Prometheus Node Exporter) 網路代理 (如 Cilium, Calico, Istio) DaemonSet 的特性 ✅ 確保所有 符合條件的節點 都會 執行一個 Pod ✅ 當有新節點加入 (或移除) 叢集時,自動新增 (或刪除) Pod ✅ 支援節點選擇器 (Node Selector)、親和性 (Affinity) 及 污點與容忍度 (Taints & Tolerations), 可精細控制 Pod 的調度策略 ✅ 不需要手動擴展副本數 (replicas) ,它會 根據節點數量自動調整 摘要:DaemonSet vs Deployment 何時使用 DaemonSet? ✅ 確保每個節點(Node)都需要運行一個同樣的 Pod (如 監控、日誌收集、網路代理) ✅ 當 Pod 需要存取主機資源 (如 hostPath 掛載 /var/log) ✅ 當 Pod 必須與節點一起運行,不受負載均衡影響 如果你的應用是 水平擴展 (Scaling out) 的服務,應該 選擇 使用 Deployment 。 如果你要在  每個節點上運行一個 Pod ,那就選擇 DaemonSet !

02 - Resource Requirements and Limits

圖片
參考資源 [K8s] Manage Memory, CPU, and API Resources [K8s] Limit Ranges [K8s] Configure Default CPU Requests and Limits for a Namespace [K8s] Configure Default Memory Requests and Limits for a Namespace [Udemy] 第66、67堂課:Resource Requirements and Limits

02 - Node Affinity

圖片
Node Affinity(節點親和性) Node Affinity 是 Kubernetes 調度 Pod 到特定節點 (Node) 的機制。 它比 nodeSelector 更靈活,允許你設定多種匹配條件。 [K8s] Node Affinity Node Affinity 主要有 三種模式: required DuringScheduling Ignored DuringExecution Required 強制規則 , Pod 只會調度到符合條件的節點 ,否則調度失敗。 preferred DuringScheduling Ignored DuringExecution Preferred 軟性規則 ,Kubernetes 會嘗試將 Pod 調度到符合條件的節點,但 如果沒有符合的節點,也會允許調度到其他節點 。 required DuringScheduling Required DuringExecution 這種模式在 調度時和執行時 都強制應用。 常見 operator: In:允許特定值,例如 color=blue NotIn:禁止特定值,例如 color != blue Exists:只要該 key 存在即可,不管值是什麼 DoesNotExist:只允許該 key 不存在的節點 Required(強制規則) Pod 只能調度到 color=blue 的節點。 🚫 如果沒有符合的節點,Pod 會卡住 (Pending),不會被調度! ``` apiVersion: apps/v1 kind: Deployment metadata:   name: blue-app spec:   replicas: 3   selector:     matchLabels:       app: nginx   template:     metadata:       labels:         app: nginx     spec:       affinity:         ...

02 - Taints and Tolerations

圖片
摘要 Taints  與 Tolerations  是 Kubernetes 中用來 控制 Pod 調度到哪些節點 的機制。 [K8s] Taints and Tolerations ✅ Taints 設定在 節點 上,防止 Pod 調度到該節點 ✅ Tolerations 設定在 Pod 上,使其能夠忽略某些 Taints ✅ 透過 NoSchedule、PreferNoSchedule、NoExecute 來 控制 Pod 的行為 ✅ 控制特定 Pod 運行在特定節點, 提升調度靈活性 這個機制在 Kubernetes 多節點部署 、 資源隔離 、 避免影響關鍵應用 時非常有用 🚀 Taints(污點) Taint 是設定在  節點 (Node) 上的屬性,它告訴 Kubernetes 不要在該節點上調度沒有相應 Toleration(容忍度)的 Pod。 $ kubectl taint node <node-name> <key>=<value>:<effect>    <node-name> → 要添加 taint 的節點名稱 <effect> → taint 的影響方式 NoSchedule → 禁止 沒有匹配 Toleration 的 Pod 被調度到該節點 PreferNoSchedule → 盡量避免 調度,但不是強制的 NoExecute → 不僅 禁止 調度,還會 驅逐 已經在該節點上的不符合 Toleration 的 Pod Tolerations(容忍度) Toleration 設定在 Pod 上,允許 Pod 忽略某些 Taint(污點),從而能夠被調度到相應的節點。 在 Pod 規範 (pod.yaml) 中添加 tolerations: ``` apiVersion: v1 kind: Pod metadata:   name: my-pod spec:   tolerations:     - key: "dedicated"       operator: "Equal"       value: "...

02 - Labels and Selectors

圖片
Q: How many PODs exist in the dev environment (env)? ※ Use selectors to filter the output $ kubectl get pods --selector env=dev                                       $ kubectl get pods --selector bu=finance --no-headers | wc -l    $ kubectl get pods --show-labels=true                                       Q: How many objects are in the prod environment including PODs, ReplicaSets and any other objects ? $ kubectl get all --selector env=prod                                         $ kubectl get all --selector env=prod --no-headers | wc -l          Q: Identify the POD which is part of the prod environment, the finance BU and of frontend tier? $ kubec...