pod 的 kubectl 日志(如果它包含 2 个容器)

问题描述 投票:0回答:3

我有 pod,它有 2 个容器。 如果我发出命令“

kubectl logs pod_name
” 它不列出日志,我需要随此命令一起提供容器名称。

当我们发出命令“

kubectl logs pod_name
”时,有没有办法同时显示容器日志?

kubernetes kubectl kubernetes-pod
3个回答
16
投票

显示特定容器的日志

kubectl logs <pod-name> -c <container_name>

要显示所有容器日志,请使用以下命令

kubectl logs <pod-name> --all-containers=true

1
投票

获取 pod 日志的其余 API 是

GET /api/v1/namespaces/{namespace}/pods/{name}/log

您可以将容器作为查询参数传递给上述 API 以获取特定容器的日志

GET /api/v1/namespaces/{namespace}/pods/{name}/log?container=containername

当您使用服务帐户或用户从代码中点击上述 API 时,您需要拥有以下 RBAC

Role
RoleBinding

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  namespace: default
  name: pod-logs-reader
rules:
- apiGroups: [""]
  resources: ["pods/log"]
  verbs: ["get", "list"]

API 已记录在此处


0
投票

如果您想实时关注日志,请添加

-f
后缀。

kubectl logs -f <your-pod> --all-containers=true
© www.soinside.com 2019 - 2024. All rights reserved.