Kubectl
Kubectl is the standard command-line tool for interacting with Kubernetes clusters across all major cloud providers (AKS, EKS, GKE) and on-premises environments. It enables you to deploy, manage, and troubleshoot Kubernetes resources efficiently.
Installation
Linux/WSL
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectlNixOS
Add to your configuration.nix:
{ pkgs, ... }:
{
environment.systemPackages = with pkgs; [ kubectl ];
}Then run:
sudo nixos-rebuild switchConnecting to Managed Clusters
AKS:
az aks get-credentials --resource-group <rg> --name <cluster>EKS:
aws eks update-kubeconfig --region <region> --name <cluster>GKE:
gcloud container clusters get-credentials <cluster> --region <region>
Common Usage Examples
Create a deployment:
kubectl create deployment webapp --image=nginx:1.25View deployment status:
kubectl rollout status deployment/webappScale a deployment:
kubectl scale deployment/webapp --replicas=5Update deployment image:
kubectl set image deployment/webapp nginx=nginx:1.26Create a service:
kubectl expose deployment webapp --type=LoadBalancer --port=80View pod logs:
kubectl logs <pod-name>Create a secret:
kubectl create secret generic mysecret --from-literal=key=valueCreate a ConfigMap:
kubectl create configmap myconfig --from-literal=key=value
Real-Life DevOps Scenarios
Use
kubectlin CI/CD pipelines (GitHub Actions, Azure Pipelines, GitLab CI) for automated deployments and rollbacks.Integrate with GitOps tools (ArgoCD, Flux) for declarative cluster management.
Use LLMs (Copilot, Claude) to generate manifests and troubleshoot errors.
Automate cluster context switching for multi-cloud workflows.
Best Practices (2025)
Always use the latest stable version of kubectl
Use
kubectl --contextto manage multiple clustersValidate YAML with
kubectl apply --dry-run=client -f <file>Use
kubectl explain <resource>for quick documentationPrefer declarative (
apply) over imperative (create,edit) workflowsUse RBAC and namespaces for security and isolation
Common Pitfalls
Not matching kubectl version to cluster version (can cause errors)
Forgetting to set the correct context before running commands
Applying unvalidated YAML (syntax or schema errors)
References
Last updated