kubectl logs

概要

打印 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

选项

--all-containers

获取 Pod(s) 中所有容器的日志。

--all-pods

从所有 Pod(s) 获取日志。将 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 运行的时间长度(例如 5 秒、2 分钟或 3 小时,大于零)

--prefix

在每行日志前添加日志源(Pod 名称和容器名称)前缀

-p, --previous

如果为 true,则打印 Pod 中容器上一个实例的日志(如果存在)。

-l, --selector string

用于过滤的选择器(标签查询),支持 '=', '==', '!=', 'in', 'notin'。(例如 -l key1=value1,key2=value2,key3 in (value3))。匹配的对象必须满足所有指定的标签约束。

--since duration

只返回早于相对持续时间(例如 5 秒、2 分钟或 3 小时)的日志。默认为所有日志。只能使用 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 上下文名称

--default-not-ready-toleration-seconds int     默认值: 300

指示对 notReady:NoExecute 的容忍度 tolerationSeconds,此容忍度默认添加到每个尚未拥有此类容忍度的 Pod 中。

--default-unreachable-toleration-seconds int     默认值: 300

指示对 unreachable:NoExecute 的容忍度 tolerationSeconds,此容忍度默认添加到每个尚未拥有此类容忍度的 Pod 中。

--disable-compression

如果为 true,则对所有服务器请求禁用响应压缩

--insecure-skip-tls-verify

如果为 true,则不会检查服务器证书的有效性。这会使你的 HTTPS 连接不安全

--kubeconfig string

CLI 请求使用的 kubeconfig 文件路径。

--match-server-version

要求服务器版本与客户端版本匹配

-n, --namespace string

如果存在,则指定此 CLI 请求的命名空间范围

--password string

用于对 API 服务器进行基本认证的密码

--profile string     默认值: "none"

要捕获的 Profile 名称。可选值包括 (none|cpu|heap|goroutine|threadcreate|block|mutex)

--profile-output string     默认值: "profile.pprof"

写入 Profile 的文件名

--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 年 4 月 24 日太平洋标准时间上午 8:03:v1.33 的 kubectl 参考 (80481646c3)