Kubernetes Logs Command

KubernetesKubernetesBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Kubernetes logs command to view logs from pods running in a Kubernetes cluster. You will start with simple examples and gradually progress to more complex scenarios.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL kubernetes(("`Kubernetes`")) -.-> kubernetes/TroubleshootingandDebuggingCommandsGroup(["`Troubleshooting and Debugging Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/BasicCommandsGroup(["`Basic Commands`"]) kubernetes/TroubleshootingandDebuggingCommandsGroup -.-> kubernetes/describe("`Describe`") kubernetes/TroubleshootingandDebuggingCommandsGroup -.-> kubernetes/logs("`Logs`") kubernetes/TroubleshootingandDebuggingCommandsGroup -.-> kubernetes/exec("`Exec`") kubernetes/BasicCommandsGroup -.-> kubernetes/get("`Get`") kubernetes/BasicCommandsGroup -.-> kubernetes/delete("`Delete`") subgraph Lab Skills kubernetes/describe -.-> lab-8099{{"`Kubernetes Logs Command`"}} kubernetes/logs -.-> lab-8099{{"`Kubernetes Logs Command`"}} kubernetes/exec -.-> lab-8099{{"`Kubernetes Logs Command`"}} kubernetes/get -.-> lab-8099{{"`Kubernetes Logs Command`"}} kubernetes/delete -.-> lab-8099{{"`Kubernetes Logs Command`"}} end

Start the Minikube Cluster

Before creating resources, you need a running Kubernetes cluster. Minikube is a lightweight Kubernetes environment that runs on your local machine.

  1. Navigate to your working directory:

    Open the terminal and navigate to the default project folder:

    cd /home/labex/project
  2. Start Minikube:

    Start Minikube to initialize a Kubernetes cluster:

    minikube start
    • This command sets up a single-node Kubernetes cluster on your local machine.
    • Minikube may take a few minutes to start depending on your system's performance.
  3. Verify Minikube is running:

    Check the status of the Minikube cluster:

    minikube status
    • Look for components like kubelet and apiserver listed as Running.
    • If the cluster is not running, rerun minikube start.

If you encounter issues starting Minikube. Use minikube delete to reset the environment if needed.

Explore the kubectl logs Command

The kubectl logs command is used to print logs for a container in a pod or a specified resource. It supports snapshot logs, streaming, filtering, and various other options for efficient log management.

Run the following command to view the available options for kubectl logs:

kubectl logs -h

You will see the following output:

Print the logs for a container in a pod or specified resource. If the pod has only one container, the container name is optional.

Examples:
  ## Return snapshot logs from pod nginx with only one container
  kubectl logs nginx

  ## Return snapshot logs from pod nginx with multi containers
  kubectl logs nginx --all-containers=true

  ## Return snapshot logs from all containers in pods defined by label app=nginx
  kubectl logs -l app=nginx --all-containers=true

  ## Return snapshot of previous terminated ruby container logs from pod web-1
  kubectl logs -p -c ruby web-1

  ## 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 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

Viewing Container Logs

In this step, you will learn how to view the logs of a container running in a pod.

  1. Start by creating a deployment with one replica and an Nginx container:

    kubectl create deployment nginx --image=nginx --replicas=1
  2. Wait for the pod to become ready:

    kubectl wait --for=condition=Ready pod -l app=nginx
  3. Use the kubectl logs command to view the logs of the Nginx container:

    kubectl logs <pod_name>

Replace <pod_name> with the name of the pod created in step 1, and you can get the <pod_name> with the kubectl get pod -l app=nginx command.

Viewing Logs from a Specific Container in a Pod

In this step, you will learn how to view the logs of a specific container running in a pod.

  1. Create a pod with two containers: Nginx and BusyBox:

    cat << EOF | kubectl apply -f -
    apiVersion: v1
    kind: Pod
    metadata:
      name: nginx-busybox
    spec:
      containers:
      - name: nginx
        image: nginx
      - name: busybox
        image: busybox
        command:
          - sleep
          - "3600"
    EOF
  2. Wait for the pod to become ready:

    kubectl wait --for=condition=Ready pod nginx-busybox
  3. Use the kubectl logs command to view the logs of the BusyBox container:

    kubectl logs nginx-busybox -c busybox

Following Logs in Real-Time

In this step, you will learn how to follow logs in real-time as they are generated.

  1. Use the kubectl logs command with the -f option to follow logs in real-time:

    kubectl logs -f nginx-busybox
  2. Open a new terminal and create a shell in the Nginx container:

    kubectl exec -it nginx-busybox -c nginx -- /bin/sh
  3. Generate some logs by running a command inside the container:

    curl 127.0.0.1
  4. Switch back to the first terminal where you are following the logs and observe that the new log entry is displayed.

Viewing Logs from Multiple Containers

In this step, you will learn how to view logs from multiple containers running in a pod.

  1. Create a pod with two containers: Nginx and Fluentd:

    cat << EOF | kubectl apply -f -
    apiVersion: v1
    kind: Pod
    metadata:
      name: nginx-fluentd
    spec:
      containers:
      - name: nginx
        image: nginx
      - name: fluentd
        image: fluentd
    EOF
  2. Wait for the pod to become ready:

    kubectl wait --for=condition=Ready pod nginx-fluentd
  3. Use the kubectl logs command to view logs from both containers:

    kubectl logs nginx-fluentd -c nginx
    kubectl logs nginx-fluentd -c fluentd

Summary

Congratulations, you have successfully completed the Kubernetes logs command lab! In this lab, you learned how to view logs from containers running in a pod using the kubectl logs command. You also learned how to view logs from specific containers and multiple containers, follow logs in real-time, and generate logs from within a container. These skills are essential for troubleshooting issues in a Kubernetes cluster.

Other Kubernetes Tutorials you may like