Learn Kubectl: Essential Commands, Log Analysis, Container Debugging.
Table of contents
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
Get Cluster Info
kubectl cluster-info
Get Resources
kubectl get pods kubectl get services kubectl get deployments
Describe Resource
kubectl describe <resource> <name> -n <namespace>
Get Status
kubectl get pods -n <namespace>
Check Events
kubectl get events -n <namespace>
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.
View Pod Logs
kubectl logs <pod-name>
View Logs for a Specific Container in a Pod
kubectl logs <pod-name> -c <container-name>
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>
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
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 fromkube-proxy
, which is responsible for directing traffic to Service endpoints
Debugging
Container issues often stem from misconfigurations or bugs within the image.
- 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.
Debug CrashLoopBackOff Error
kubectl describe pod <pod-name> kubectl logs <pod-name>
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.
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!👇
Subscribe to my newsletter
Read articles from Atul Gawade directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by