troubleshoot with kubectl

How to troubleshoot with kubectl

When working with Kubernetes, learning how to troubleshoot with kubectl will help in your discovery of any issues. Kubectl is a command line tool that connects to your control plane using the Kubernetes api. In this article we will go over the most common kubectl commands every Kubernetes admin should know.

To troubleshoot Kubernetes applications we will break down the following commands:

  • kubectl get
  • kubectl describe
  • kubectl logs
  • kubectl exec

What does “kubectl get” do?

“kubectl get” allows you to display one or more resources. Some of the possible resource types are:

  • pods
  • services
  • replicationcontrollers
  • nodes
  • events
  • namespaces
  • endpoints

Starting your troubleshooting with kubectl get allows you to identify the types of resources you are trying to filter. For example you can identify all pods using “kubectl get pods -A”. You can find all your nodes by doing “kubectl get nodes -A”.

There are more resources and examples that you can find by running the command “kubectl get –help”. As you start to get familiar with the basic commands you can start to build a list of examples you can save for your own environment.

Kubectl Describe Command

Since the “kubectl get” commands gets resources you can then use the “kubectl describe” command to get more details of a specific resource or group of resources.

This command shares a similar resemblance to “kubectl get” except that the describe option gives you more specific details about the resource your targeting.

Troubleshooting with kubectl logs

When investigating issues with a particular pod and container the command “kubectl logs [-f] [-p] POD [-c CONTAINER]” will return useful information. For example if you have a pod called webapp and only has one container then your command would be “kubectl logs webapp”

If needing to dig deeper into the logs needed you can use the “kubectl logs –help” to gather more examples on what commands you can use.

How to Execute commands inside a container using kubectl exec

As you start to develop network policies or run into issues connecting from or to containers, the “kubectl exec” command will allow you to send commands from a host directly into a container to execute.

For example to check the disk usage inside of a container called container1 and a pod called pod1 you can run the command “kubectl exec pod1 -c container1 df -h”. This can become very useful to test network connectivity and much more without having to be directly onto a container.

Conclusion

These are 4 commands every admin should get familiar with to help troubleshooting Kubernetes clusters. These commands can further be expanded on by visiting the official kubernetes documentation here.

Comments are closed.