Learn Kubectl: Essential Commands, Log Analysis, Container Debugging.

Atul GawadeAtul Gawade
3 min read

Welcome to the final day of the #KubeWeek challenge, where we will deep dive into some of the essential kubectl commands, how to do log analysis in K8s, Debugging the containers and how to monitor K8 clusters.

Essential kubectl Commands

  1. Get Cluster Info

      kubectl cluster-info
    
  2. Get Resources

      kubectl get pods
      kubectl get services
      kubectl get deployments
    
  3. Describe Resource

     kubectl describe <resource> <name> -n <namespace>
    
  4. Get Status

     kubectl get pods -n <namespace>
    
  5. Check Events

     kubectl get events -n <namespace>
    
  6. Get Resource Usage

     kubectl top pod
     kubectl top node
    

Analyzing Logs.

Logs are crucial for debugging and understanding what's happening inside your applications and pods. Reviewing logs is an essential part of this process, as they provide a record of events and actions performed by our application or system.

  1. View Pod Logs

      kubectl logs <pod-name>
    
  2. View Logs for a Specific Container in a Pod

      kubectl logs <pod-name> -c <container-name>
    
  3. Using the tail Option

    By default, the kubectl logs command retrieves all logs from the start of the container’s output. However, we can use the –tail option to fetch only the latest logs

     kubectl logs <pod-name> -c <container-name> --tail=<number-of-lines>
    
  4. Using the follow Option

    Using the —follow option allows us to see logs in real-time as they are generated, making it especially useful when troubleshooting an issue and needing to view logs as soon as they are created.

     kubectl logs <pod-name> -c <container-name> --follow
    
  5. View previous container's crash log

     kubectl logs --previous ${POD_NAME} ${CONTAINER_NAME}
    

    Troubleshoot Clusters:

    The first thing to do is to list your cluster and check if everything is registered correctly :

     kubectl get nodes
    

    To get detailed information about the overall health of your cluster, you can run:

     kubectl cluster-info dump
    

    For now, digging deeper into the cluster requires logging into the relevant machines. Here are the locations of the relevant log files. On systemd-based systems, you may need to use journalctl instead of examining log files.

    Control Plane nodes

    • /var/log/kube-apiserver.log - API Server, responsible for serving the API

    • /var/log/kube-scheduler.log - Scheduler, responsible for making scheduling decisions

    • /var/log/kube-controller-manager.log - Controller that manages replication controllers.

Worker Nodes

  • /var/log/kubelet.log - Kubelet is responsible for running containers on the node.

  • /var/log/kube-proxy.log - logs from kube-proxy, which is responsible for directing traffic to Service endpoints

Debugging

Container issues often stem from misconfigurations or bugs within the image.

  1. Check the current status of POD
kubectl describe pods ${POD_NAME}

Continue debugging depending on the state of the pods.
Here are the most common Pod's status :

  • Init:N/M : The Pod has M Init Containers, and N have completed so far.

  • Init:Error : An Init Container has failed to execute.

  • Init:CrashLoopBackOff : An Init Container has failed repeatedly.

  • Pending : The Pod has not yet begun executing

  • Init Containers.PodInitializingorRunning : The Pod has already finished executing Init Containers.

  1. Debug CrashLoopBackOff Error

       kubectl describe pod <pod-name>
       kubectl logs <pod-name>
    
  2. Debugging with an ephemeral debug container

     kubectl debug -it <your-pod-name> --image=busybox --target=<your-container-name>
    

Workflow

This workflow can be very helpful if you are facing issues on your K8S cluster.

No alt text provided for this image

source: learnk8s

Conclusion

We’ve wrapped up some of the most critical skills for managing and troubleshooting Kubernetes clusters.

Here’s what we covered:

  • Mastering kubectl commands to control your clusters like a pro.

  • Analyzing logs to keep your systems running smoothly.

  • Debugging container images to resolve issues before they escalate.

These tools and techniques are your allies in maintaining a healthy, efficient Kubernetes environment.

P.S. Which skill are you most excited to apply? Let’s discuss in the comments below!👇

0
Subscribe to my newsletter

Read articles from Atul Gawade directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Atul Gawade
Atul Gawade