7

I have the pod and it has 2 containers. if i give the command "kubectl logs pod_name" it does not list logs, i need to give container name along with this command.

Is there a way to display both the container logs when we give command "kubectl logs pod_name"?

pampasman
  • 318
  • 2
  • 9

2 Answers2

16

To display the logs of a particular container

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

To display all containers logs use below command

kubectl logs <pod-name> --all-containers=true
freedev
  • 25,946
  • 8
  • 108
  • 125
redInk
  • 699
  • 5
  • 11
1

The rest API to get logs of a pod is

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

You can pass container as a query param to above API to get logs of a particular container

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

When you hit above APIs from code using a service account or a user you need to have below RBAC Role and RoleBinding

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

The API is documented here

Arghya Sadhu
  • 41,002
  • 9
  • 78
  • 107