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/logs("`Logs`") kubernetes/BasicCommandsGroup -.-> kubernetes/create("`Create`") subgraph Lab Skills kubernetes/logs -.-> lab-8099{{"`Kubernetes Logs Command`"}} kubernetes/create -.-> lab-8099{{"`Kubernetes Logs Command`"}} end

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