kubectl logs

Synopsis

列印 Pod 中容器或指定資源的日誌。如果 Pod 只有一個容器,則容器名稱是可選的。

kubectl logs [-f] [-p] (POD | TYPE/NAME) [-c CONTAINER]

示例

  # Return snapshot logs from pod nginx with only one container
  kubectl logs nginx
  
  # Return snapshot logs from pod nginx, prefixing each line with the source pod and container name
  kubectl logs nginx --prefix
  
  # Return snapshot logs from pod nginx, limiting output to 500 bytes
  kubectl logs nginx --limit-bytes=500
  
  # Return snapshot logs from pod nginx, waiting up to 20 seconds for it to start running.
  kubectl logs nginx --pod-running-timeout=20s
  
  # Return snapshot logs from pod nginx with multi containers
  kubectl logs nginx --all-containers=true
  
  # Return snapshot logs from all pods in the deployment nginx
  kubectl logs deployment/nginx --all-pods=true
  
  # Return snapshot logs from all containers in pods defined by label app=nginx
  kubectl logs -l app=nginx --all-containers=true
  
  # Return snapshot logs from all pods defined by label app=nginx, limiting concurrent log requests to 10 pods
  kubectl logs -l app=nginx --max-log-requests=10
  
  # Return snapshot of previous terminated ruby container logs from pod web-1
  kubectl logs -p -c ruby web-1
  
  # Begin streaming the logs from pod nginx, continuing even if errors occur
  kubectl logs nginx -f --ignore-errors=true
  
  # Begin streaming the logs of the ruby container in pod web-1
  kubectl logs -f -c ruby web-1
  
  # Begin streaming the logs from all containers in pods defined by label app=nginx
  kubectl logs -f -l app=nginx --all-containers=true
  
  # Display only the most recent 20 lines of output in pod nginx
  kubectl logs --tail=20 nginx
  
  # Show all logs from pod nginx written in the last hour
  kubectl logs --since=1h nginx
  
  # Show all logs with timestamps from pod nginx starting from August 30, 2024, at 06:00:00 UTC
  kubectl logs nginx --since-time=2024-08-30T06:00:00Z --timestamps=true
  
  # Show logs from a kubelet with an expired serving certificate
  kubectl logs --insecure-skip-tls-verify-backend nginx
  
  # Return snapshot logs from first container of a job named hello
  kubectl logs job/hello
  
  # Return snapshot logs from container nginx-1 of a deployment named nginx
  kubectl logs deployment/nginx -c nginx-1

Options

--all-containers

獲取 Pod 中所有容器的日誌。

--all-pods

獲取所有 Pod 的日誌。將 prefix 設定為 true。

-c, --container string

列印此容器的日誌

-f, --follow

指定是否應流式傳輸日誌。

-h, --help

logs 的幫助

--ignore-errors

如果正在監視/跟蹤 Pod 的日誌,允許發生的任何錯誤不致命

--insecure-skip-tls-verify-backend

跳過驗證正在請求日誌的 kubelet 的身份。理論上,攻擊者可以提供無效的日誌內容。如果你的 kubelet 服務證書已過期,你可能需要使用此選項。

--limit-bytes int

要返回的最大日誌位元組數。預設為無限制。

--max-log-requests int     預設值:5

使用選擇器時,指定要同時跟蹤的最大日誌數。預設為 5。

--pod-running-timeout duration     預設值:20s

等待直到至少有一個 pod 執行的時間長度(例如 5s、2m 或 3h,必須大於零)。

--prefix

在每行日誌前新增日誌源(Pod 名稱和容器名稱)

-p, --previous

如果為 true,則列印 Pod 中容器前一個例項的日誌(如果存在)。

-l, --selector string

用於過濾的選擇器(標籤查詢),支援 '=', '==', '!=', 'in', 'notin'。(例如 -l key1=value1,key2=value2,key3 in (value3))。匹配的物件必須滿足所有指定的標籤約束。

--since duration

僅返回比相對時長(例如 5s、2m 或 3h)更新的日誌。預設為所有日誌。只能使用 since-time / since 中的一個。

--since-time string

僅返回特定日期(RFC3339)之後的日誌。預設為所有日誌。只能使用 since-time / since 中的一個。

--tail int     預設值:-1

顯示最近日誌檔案的行數。預設值為 -1,顯示所有日誌行;如果提供了選擇器,則顯示 10 行。

--timestamps

在日誌輸出的每行中包含時間戳

繼承的父選項

--as string

為操作假定的使用者名稱。使用者可以是常規使用者或名稱空間中的服務帳戶。

--as-group strings

為操作假定的組,此標誌可以重複以指定多個組。

--as-uid string

為操作假定的 UID。

--cache-dir string     預設值: "$HOME/.kube/cache"

預設快取目錄

--certificate-authority string

證書頒發機構的證書檔案路徑

--client-certificate string

TLS 的客戶端證書檔案路徑

--client-key string

TLS 的客戶端金鑰檔案路徑

--cluster string

要使用的 kubeconfig 叢集名稱

--context string

要使用的 kubeconfig 上下文名稱

--disable-compression

如果為 true,則選擇退出所有到伺服器的請求的響應壓縮

--insecure-skip-tls-verify

如果為 true,則不會檢查伺服器證書的有效性。這將使您的 HTTPS 連線不安全。

--kubeconfig string

用於 CLI 請求的 kubeconfig 檔案路徑。

--kuberc string

用於首選項的 kuberc 檔案路徑。可以透過匯出 KUBECTL_KUBERC=false 功能標誌或關閉 KUBERC=off 功能來停用此功能。

--match-server-version

要求伺服器版本與客戶端版本匹配

-n, --namespace string

如果存在,則為本次 CLI 請求的名稱空間範圍

--password string

API 伺服器基本身份驗證的密碼

--profile string     預設值: "none"

要捕獲的配置檔名稱。選擇之一(none|cpu|heap|goroutine|threadcreate|block|mutex)

--profile-output string     預設值: "profile.pprof"

要將配置檔案寫入的檔案的名稱

--request-timeout string     預設值: "0"

在放棄單個伺服器請求之前等待的時間。非零值應包含相應的時間單位(例如 1s、2m、3h)。零值表示不超時請求。

-s, --server string

Kubernetes API 伺服器的地址和埠

--storage-driver-buffer-duration duration     預設值: 1m0s

儲存驅動程式中的寫入將緩衝此持續時間,並作為單個事務提交到非記憶體後端。

--storage-driver-db string     預設值: "cadvisor"

資料庫名稱

--storage-driver-host string     預設值: "localhost:8086"

資料庫主機:埠

--storage-driver-password string     預設值: "root"

資料庫密碼

--storage-driver-secure

使用安全的資料庫連線

--storage-driver-table string     預設值: "stats"

表名

--storage-driver-user string     預設值: "root"

資料庫使用者名稱

--tls-server-name string

用於伺服器證書驗證的伺服器名稱。如果未提供,則使用用於聯絡伺服器的主機名。

--token string

API 伺服器身份驗證的 Bearer token

--user string

要使用的 kubeconfig 使用者名稱

--username string

API 伺服器基本身份驗證的使用者名稱

--version version[=true]

--version, --version=raw 列印版本資訊並退出;--version=vX.Y.Z... 設定報告的版本

--warnings-as-errors

將從伺服器收到的警告視為錯誤,並以非零退出程式碼退出。

另請參閱

  • kubectl - kubectl 控制 Kubernetes 叢集管理器

本頁面是自動生成的。

如果你打算報告此頁面存在的問題,請在問題描述中提及此頁面是自動生成的。修復可能需要在 Kubernetes 專案的其他地方進行。

最後修改於 2025 年 9 月 4 日下午 3:30 PST: Update kubectl reference for v1.34 (bdc4bba2a5)