kubectl: Your Gateway to the Kubernetes Cluster

Kubernetes, the container orchestration platform, empowers developers to manage containerised applications at scale. But interacting directly with the Kubernetes API wouldn't be very user-friendly. That's where kubectl, the Kubernetes command-line tool, comes in.

What is "kubectl" ?

kubectl is the command-line tool that acts as your key to interacting with a Kubernetes cluster. It allows you to manage and maintain your containerised applications deployed on Kubernetes, providing a powerful interface for tasks like deployment, inspection, and troubleshooting. From a technical point of view, kubectl is a client written on top of Kubernetes API for managing the Kubernetes cluster.

kubectl acts as a bridge between you and your Kubernetes cluster. It allows you to:

  • Deploy applications: Easily deploy containerised applications as Kubernetes resources like deployments and pods.

  • Manage resources: View, edit, and delete various Kubernetes resources like deployments, services, and pods.

  • Inspect cluster health: Get insights into the health of your cluster by viewing logs, events, and resource usage.

  • Debug applications: Troubleshoot issues by viewing logs, running commands within containers, and port-forwarding.

How to install?

Before using kubectl, ensure you have a running Kubernetes cluster and kubectl is installed and configured on your local machine. You can find installation instructions for various platforms on the Kubernetes website: https://kubernetes.io/docs/tasks/tools/

Common kubectl Commands:

  1. version: This command retrieves the version of kubectl installed on the system where you running kubectl. (e.g., kubectl version)

  2. cluster-info**:**This command display addresses of the control plane and services with label which help in debugging and diagnosing the cluster problems,. (e.g., kubectl cluster-info dump)

  3. get: This fundamental command retrieves information about various resources within your cluster, such as deployments, pods, services, and more. You can specify specific resources or utilise wildcards for broader searches. (e.g., kubectl get pods)

  4. apply: This command is crucial for deploying applications to your cluster. It takes YAML or JSON manifest files containing resource definitions and applies them to the cluster, creating or updating resources as needed. (e.g., kubectl apply -f deployment.yaml)

  5. delete: As the name suggests, this command removes resources from your cluster. Similar to get, you can specify the resource type and name for targeted deletion. (e.g., kubectl delete pod my-pod)

  6. describe: This command delves deeper into specific resources, providing detailed information about their configuration, status, and events. It's helpful for understanding the current state of your deployments, pods, and other cluster components. (e.g., kubectl describe pod my-pod)

  7. logs: Accessing container logs is essential for debugging and monitoring. The kubectl logs command allows you to view logs from running pods, helping you diagnose issues and track application behavior. (e.g., kubectl logs my-pod)

  8. exec: This command grants you a shell session directly within a running container. It's valuable for troubleshooting purposes, allowing you to execute commands and interact with the container's environment. (e.g., kubectl exec my-pod -- date)

  9. port-forward: Exposing container ports to your local machine for testing or development can be simplified with kubectl port-forward. This command creates a tunnel, forwarding traffic from your local port to a specific container port, enabling direct access from your development environment. (e.g., kubectl port-forward my-pod 8080:80)

  10. scale: Managing the number of replicas in a deployment is crucial for scaling your application. The kubectl scale command allows you to adjust the desired number of replicas, effectively scaling your application up or down based on your needs. (e.g., kubectl scale deployment my-deployment --replicas=3)

  11. explain: This command can be used to learn more about the fields that a particular resource requires and their meanings. It is mostly useful for two purposes: obtaining information about a resource's fields (e.g., kubectl explain pods) or obtaining information about a particular field inside a resource. (e.g., kubectl explain pods.spec.containers)

  12. top: For a quick overview of resource utilisation in your cluster, the kubectl top command provides insights into CPU, memory, and storage usage across pods, namespaces, or nodes.

  13. namespace: Kubernetes utilises namespaces to organise and isolate resources within a cluster. The kubectl commands can be scoped to specific namespaces using the -n flag, ensuring you work with the intended resources.

These are just a few of the commonly used kubectl commands. As you delve deeper into the world of Kubernetes, exploring the full range of kubectl functionalities will empower you to effectively manage and maintain your containerised applications. Remember to refer to the official kubectl documentation for detailed information and a comprehensive list of commands and options.