
Mastering Kubernetes: A Beginner’s Guide to Essential Kubectl Commands
Kubernetes is the de facto standard for container orchestration, but for newcomers, its power can feel overwhelming. The key to unlocking this power lies in mastering kubectl
, the command-line interface (CLI) that acts as your control panel for any Kubernetes cluster.
Whether you’re deploying your first application, debugging a mysterious crash, or simply checking the health of your environment, kubectl
is your indispensable tool. This guide will walk you through the most crucial commands you need for daily operations, transforming you from a beginner to a confident practitioner.
Understanding Your Cluster and Context
Before you can manage workloads, you need to know what you’re connected to. These commands provide a high-level overview of your cluster’s state and configuration.
kubectl cluster-info
This is your first handshake with the cluster. It provides the addresses of the Kubernetes control plane and other core services like KubeDNS. It’s the perfect command to confirm you have a working connection.kubectl get nodes
This command lists all the nodes (virtual or physical machines) that form your cluster. It shows their status, roles, age, and version. For more detailed information, like the node’s internal and external IP addresses, use the-o wide
flag. A healthy cluster starts with healthy nodes, and this is how you verify them.kubectl config get-contexts
If you work with multiple Kubernetes clusters (e.g., development, staging, production), this command is essential. It lists all the clusters you have configured in yourkubeconfig
file. The current context, which is the cluster yourkubectl
commands are targeting, will be marked with an asterisk.kubectl config use-context <context-name>
This command allows you to safely switch between different clusters. It’s a critical command to prevent accidentally making changes to your production environment when you meant to work on development.
Deploying and Managing Workloads
Once you understand your environment, it’s time to run applications. These commands are the building blocks of application management in Kubernetes.
kubectl get pods -A
Pods are the smallest deployable units in Kubernetes, and you’ll be interacting with them constantly. Theget pods
command lists the pods in your current namespace. However, using the -A flag shows you pods across all namespaces, giving you a complete picture of everything running on the cluster.kubectl get deployments,services,ingresses
You can askkubectl
to retrieve multiple resource types at once. This command provides a quick summary of your key application components:- Deployments: Manage the state of your application replicas.
- Services: Expose your application to other services within the cluster or externally.
- Ingresses: Manage external access to services, typically for HTTP traffic.
kubectl apply -f <filename.yaml>
This is one of the most powerful commands in Kubernetes. It allows you to create or update resources using a declarative approach. You define the desired state of your application in a YAML file, andkubectl apply
makes it a reality. Usingapply
is the recommended best practice for GitOps and infrastructure-as-code workflows.kubectl delete -f <filename.yaml>
orkubectl delete pod <pod-name>
The counterpart toapply
, this command removes resources from your cluster. You can delete a resource defined in a file or delete it directly by its type and name. Use this command with caution, as deleting a resource like a Deployment will also terminate all the pods it manages.
Inspecting and Troubleshooting Resources
When things don’t go as planned, you need to know how to investigate. These commands are your primary tools for debugging.
kubectl describe pod <pod-name>
If a pod is stuck in aPending
state or is continuously crashing (CrashLoopBackOff
), this is your starting point. Thedescribe
command provides a detailed, human-readable summary of a resource, including its configuration, status, and, most importantly, a log of recent events. The events section will often tell you exactly why a pod isn’t starting (e.g., “Failed scheduling” due to insufficient resources).kubectl logs <pod-name>
This command streams the logs from a container running inside a pod. It’s essential for debugging application-level errors.- Use the
-f
flag (kubectl logs -f <pod-name>
) to follow the log stream in real-time. - If a pod has crashed and restarted, use the
--previous
flag to view the logs from the last container instance.
- Use the
kubectl exec -it <pod-name> -- /bin/sh
This command gives you an interactive shell inside a running container. It is incredibly useful for on-the-fly debugging, checking for configuration files, or testing network connectivity from within the pod.- Security Tip: While powerful,
exec
access should be tightly controlled. Granting broadexec
permissions can be a significant security risk. Use it as a diagnostic tool, not as a standard operational procedure.
- Security Tip: While powerful,
kubectl port-forward service/<service-name> <local-port>:<remote-port>
Need to access a web application running in your cluster from your local machine?port-forward
creates a secure tunnel, allowing you to connect to a service as if it were running onlocalhost
. This is invaluable for development and testing without exposing a service publicly.- Example:
kubectl port-forward svc/my-app-service 8080:80
forwards your local port 8080 to the service’s port 80.
- Example:
Managing Configuration and Secrets
Hardcoding configuration is a bad practice. Kubernetes provides dedicated resources for managing application configuration and sensitive data.
kubectl get configmaps
andkubectl get secrets
These commands list the ConfigMaps (for non-sensitive configuration data) and Secrets (for sensitive data like API keys and passwords) in your cluster.kubectl create secret generic <secret-name> --from-literal=KEY=VALUE
This is an imperative way to quickly create a secret. You can provide credentials directly on the command line.Crucial Security Note: By default, Kubernetes Secrets are only Base64 encoded, which is not the same as encryption. Anyone with API access to read the Secret can easily decode its contents. For true security, you must integrate a secrets management solution like HashiCorp Vault or use built-in encryption-at-rest features provided by your cloud provider.
By mastering these essential kubectl
commands, you build a solid foundation for working effectively with Kubernetes. Practice them, explore their different flags, and you’ll soon be navigating your clusters with confidence and precision.
Source: https://www.linuxtechi.com/kubectl-commands-kubernetes-beginner/