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/kubectl
NixOS
Add to your configuration.nix
:
{ pkgs, ... }:
{
environment.systemPackages = with pkgs; [ kubectl ];
}
Then run:
sudo nixos-rebuild switch
Connecting 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.25
View deployment status:
kubectl rollout status deployment/webapp
Scale a deployment:
kubectl scale deployment/webapp --replicas=5
Update deployment image:
kubectl set image deployment/webapp nginx=nginx:1.26
Create a service:
kubectl expose deployment webapp --type=LoadBalancer --port=80
View pod logs:
kubectl logs <pod-name>
Create a secret:
kubectl create secret generic mysecret --from-literal=key=value
Create a ConfigMap:
kubectl create configmap myconfig --from-literal=key=value
Real-Life DevOps Scenarios
Use
kubectl
in 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 --context
to 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