How to use the command 'kubectl logs' (with examples)

How to use the command 'kubectl logs' (with examples)

The kubectl logs command is used to show the logs for containers running inside a pod in a Kubernetes cluster. It is a useful command for debugging and troubleshooting purposes, as it allows users to inspect and analyze the logs generated by their applications.

Use case 1: Show logs for a single-container pod

Code:

kubectl logs pod_name

Motivation: This use case is useful when you have a pod running a single container and you want to view its logs. This can help you understand the behavior of your application and check for any error messages or warnings.

Explanation:

  • kubectl logs is the command to show pod logs.
  • pod_name is the name of the pod for which you want to view the logs.

Example output:

2021-12-01 10:00:00 INFO: Application started
...
2021-12-01 10:05:00 ERROR: Database connection failed
...

Use case 2: Show logs for a specified container in a pod

Code:

kubectl logs --container container_name pod_name

Motivation: When a pod contains multiple containers, this use case allows you to specify which container’s logs you want to view. This is helpful when you need to troubleshoot a specific component of your application.

Explanation:

  • --container container_name specifies the name of the container for which you want to view the logs.
  • pod_name is the name of the pod containing the specified container.

Example output:

2021-12-01 10:00:00 INFO: Application started
...
2021-12-01 10:05:00 INFO: API server started
...

Use case 3: Show logs for all containers in a pod

Code:

kubectl logs --all-containers=true pod_name

Motivation: When a pod has multiple containers, this use case allows you to view the logs of all the containers simultaneously. This can help you correlate the logs of different components and identify any inter-component issues.

Explanation:

  • --all-containers=true indicates that logs for all containers in the pod should be shown.
  • pod_name is the name of the pod for which you want to view the logs.

Example output:

Container 1:
2021-12-01 10:00:00 INFO: Application started

Container 2:
2021-12-01 10:00:00 INFO: API server started
...

Use case 4: Stream pod logs

Code:

kubectl logs --follow pod_name

Motivation: In some cases, you may want to continuously monitor the logs of a pod in real-time. This use case allows you to stream the logs, providing you with live updates.

Explanation:

  • --follow enables streaming of the logs.
  • pod_name is the name of the pod for which you want to stream the logs.

Example output:

2021-12-01 10:00:00 INFO: Application started
...
2021-12-01 10:05:00 WARNING: High CPU usage detected
...

Use case 5: Stream logs for a specified container in a pod

Code:

kubectl logs --follow --container container_name pod_name

Motivation: Similar to use case 4, this use case allows you to stream the logs of a specific container in a pod. This is particularly useful when you need to monitor the logs of a specific component in real-time.

Explanation:

  • --follow enables streaming of the logs.
  • --container container_name specifies the name of the container for which you want to stream the logs.
  • pod_name is th…

Related Posts

How to use the command 'q' (with examples)

How to use the command 'q' (with examples)

The ‘q’ command allows users to execute SQL-like queries on .

Read More
Symfony Console Command Examples (with examples)

Symfony Console Command Examples (with examples)

Creating a new Symfony project To create a new Symfony project, you can use the symfony new command.

Read More
How to use the command pwlauncher (with examples)

How to use the command pwlauncher (with examples)

The pwlauncher command is used to manage the Windows To Go startup options.

Read More