# Kubernetes Ephemeral Containers 本頁面概述了 ephemeral Containers :**一種特殊類型的 Container ,可在現有 Pod 中臨時執行,以完成使用者啟動的動作**,例如疑難排解。您可以使用 ephemeral containers 來檢查服務,而不是建立應用程式。 ## Understanding ephemeral containers Pods 是 Kubernetes 應用程式的基本構建單元。由於 Pods 是一次性且可替換的,因此在 Pod 建立後無法新增 Container 。相反地,通常使用部署(deployments)以受控的方式刪除和替換 Pods。 有時候,可能需要檢查現有 Pod 的狀態,例如排查難以重現的錯誤。在這些情況下,可以在現有的 Pod 中運行一個 ephemeral container,來檢查其狀態並執行任意命令。 ## What is an ephemeral container? 短暫的貨櫃 (ephemeral containers)與其他 containers 不同,因為它們沒有資源或執行的保證,且永遠不會自動重新啟動,因此不適合用於構建應用程式。 ephemeral containers 使用與常規 containers 相同的 `ContainerSpec` 進行描述,但許多欄位對 ephemeral containers 來說是不相容的且被禁止使用。 - Ephemeral containers 可能無法擁有 ports,因此像是 `ports`、`livenessProbe`、`readinessProbe` 等欄位被禁止使用。 - Pod 的資源配置是 immutable ( 不可變的,因此設定資源也是被禁止的。 - 有關允許欄位的完整列表,請參閱 [EphemeralContainer 參考文件](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.31/#ephemeralcontainer-v1-core)。 Ephemeral containers 是通過 API 中的特殊 `ephemeralcontainers` 處理程序創建的,而不是直接將它們添加到 `pod.spec`,因此無法使用 `kubectl edit` 添加 ephemeral containers 。 與常規 containers 一樣,在將 ephemeral containers 添加到 Pod 之後,無法更改或移除它。 :::info Note: Ephemeral containers are not supported by [**static pods**](https://kubernetes.io/docs/tasks/configure-pod-container/static-pod/). ::: ## Uses for ephemeral containers ephemeral containers 在 `kubectl exec` 不足以解決問題時非常有用,例如當 container 崩潰或 container image 不包含除錯工具時,可以用於交互式排查問題。 特別是,[distroless images](https://github.com/GoogleContainerTools/distroless) 允許您部署最小化的 container images,以減少攻擊面和漏洞暴露。由於 distroless images 不包含 shell 或任何除錯工具,僅使用 `kubectl exec` 很難對這些映像進行排查。 使用 ephemeral containers 時,啟用 [process namespace sharing](https://kubernetes.io/docs/tasks/configure-pod-container/share-process-namespace/) 會很有幫助,這樣您就可以查看其他 containers 中的 processes 。 ## What's next - Learn how to [debug pods using ephemeral containers](https://hackmd.io/@QI-AN/Debugging-with-an-ephemeral-debug-container). ## Ref - [Ephemeral Containers - K8s Docs](https://kubernetes.io/docs/concepts/workloads/pods/ephemeral-containers/)