Kubectl is the official command-line interface (CLI) for running commands against Kubernetes clusters. Whether you’re a DevOps engineer, system administrator, or developer working with containerized applications, kubectl is your gateway to managing Kubernetes resources efficiently.
Quick Kubectl Installation Guide
Install Kubectl on Ubuntu/Debian Systems
sudo apt-get update && sudo apt-get install -y apt-transport-https
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee -a /etc/apt/sources.list.d/kubernetes.list
sudo apt-get update
sudo apt-get install -y kubectl
Install Kubectl on CentOS/RedHat Systems
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOF
yum install -y kubectl
Kubectl Syntax: Understanding the Command Structure
All kubectl commands follow this universal syntax:
kubectl [command] [TYPE] [NAME] [flags]
- command: The operation (create, get, describe, delete)
- TYPE: Resource type (pods, services, deployments)
- NAME: Resource name (case-sensitive)
- flags: Optional parameters
Essential Kubectl Commands Every Developer Should Know
1. Create Resources – Deploy Applications to Kubernetes
Create Pods and Deployments
# Create a pod from JSON file
kubectl create -f ./pod.json
# Create resources from stdin
cat pod.json | kubectl create -f -
# Create all resources in a directory
kubectl create -f <folder_name>
# Create deployment with specific image
kubectl create deployment nginx --image=nginx
2. Get Resources – List and Display Kubernetes Objects
View Pods and Services
# List all pods in current namespace
kubectl get pods
# List pods with detailed information
kubectl get pods -o wide
# Get pod information in JSON format
kubectl get pod <pod-name> -o json
# List all resources at once
kubectl get all
# List pods across all namespaces
kubectl get pods --all-namespaces
3. Describe Resources – Get Detailed Information
# Describe a specific pod
kubectl describe pod <pod-name>
# Describe all pods
kubectl describe pods
# Describe pods with specific labels
kubectl describe pods -l app=nginx
4. Delete Resources – Clean Up Kubernetes Objects
# Delete pod by name
kubectl delete pod <pod-name>
# Delete multiple resources
kubectl delete pod,service nginx frontend
# Delete resources by label
kubectl delete pods -l name=myapp
# Force delete a pod
kubectl delete pod <pod-name> --grace-period=0 --force
# Delete all pods
kubectl delete pods --all
Advanced Kubectl Commands for Production Environments
Managing Deployments and Scaling
Deployment Management
# Create deployment
kubectl create deployment webapp --image=nginx
# Scale deployment
kubectl scale deployment webapp --replicas=5
# Update deployment image
kubectl set image deployment/webapp nginx=nginx:1.21
# Rollback deployment
kubectl rollout undo deployment/webapp
Autoscaling Configuration
# Create horizontal pod autoscaler
kubectl autoscale deployment webapp --min=2 --max=10 --cpu-percent=80
# Check autoscaler status
kubectl get hpa
Service Exposure and Networking
# Expose deployment as service
kubectl expose deployment nginx --port=80 --target-port=8000
# Create NodePort service
kubectl expose deployment webapp --type=NodePort --port=80
# Create LoadBalancer service
kubectl expose deployment webapp --type=LoadBalancer --port=80
Kubectl Troubleshooting Commands
Debug Pod Issues
# View pod logs
kubectl logs <pod-name>
# Follow logs in real-time
kubectl logs -f <pod-name>
# Get logs from previous container instance
kubectl logs <pod-name> --previous
# Execute commands inside pod
kubectl exec -it <pod-name> -- /bin/bash
# Port forward to local machine
kubectl port-forward <pod-name> 8080:80
Cluster Monitoring and Health Checks
# Check cluster information
kubectl cluster-info
# View cluster nodes
kubectl get nodes
# Check node resource usage
kubectl top nodes
# Check pod resource usage
kubectl top pods
Kubectl Configuration and Context Management
Manage Multiple Clusters
# View current context
kubectl config current-context
# List all contexts
kubectl config get-contexts
# Switch context
kubectl config use-context <context-name>
# View cluster configuration
kubectl config view
Performance Optimization with Kubectl
Resource Management
# Set resource limits
kubectl set resources deployment nginx --limits=cpu=200m,memory=512Mi
# Set resource requests
kubectl set resources deployment nginx --requests=cpu=100m,memory=256Mi
# Update environment variables
kubectl set env deployment/nginx API_URL=https://api.example.com
Batch Operations
# Apply multiple configurations
kubectl apply -f ./configs/
# Delete multiple resources
kubectl delete -f ./configs/
# Update resources with patches
kubectl patch deployment nginx -p '{"spec":{"replicas":3}}'
Kubectl Security Best Practices
RBAC and Permissions
# Check permissions
kubectl auth can-i create pods
# Check permissions for specific user
kubectl auth can-i create pods --as=user@example.com
# View service accounts
kubectl get serviceaccounts
Secrets Management
# Create secret from literal values
kubectl create secret generic mysecret --from-literal=username=admin
# Create secret from files
kubectl create secret generic mysecret --from-file=ssh-key=/path/to/key
# View secrets (without values)
kubectl get secrets
Advanced Kubectl Techniques
Using Labels and Selectors
# Add labels to resources
kubectl label pods webapp version=v1.0
# Update labels
kubectl label pods webapp version=v2.0 --overwrite
# Remove labels
kubectl label pods webapp version-
# Select resources by labels
kubectl get pods -l version=v1.0
Working with Namespaces
# Create namespace
kubectl create namespace development
# List resources in specific namespace
kubectl get pods -n development
# Set default namespace
kubectl config set-context --current --namespace=development
Kubectl Productivity Tips
Shell Completion Setup
# Bash completion
source <(kubectl completion bash)
# Zsh completion
source <(kubectl completion zsh)
# Add to shell profile for permanent setup
echo 'source <(kubectl completion bash)' >>~/.bashrc
Useful Aliases
# Common kubectl aliases
alias k=kubectl
alias kgp='kubectl get pods'
alias kgs='kubectl get services'
alias kgd='kubectl get deployments'
Common Kubectl Error Solutions
Connection Issues
- “Unable to connect to server”: Check cluster connectivity and credentials
- “Forbidden”: Verify RBAC permissions
- “Not Found”: Ensure resource exists and namespace is correct
Resource Issues
- “ImagePullBackOff”: Check image name and registry access
- “CrashLoopBackOff”: Check container logs and resource limits
- “Pending”: Check node resources and scheduling constraints
Kubectl Cheat Sheet Summary
This comprehensive kubectl cheatsheet covers essential commands for managing Kubernetes clusters effectively. From basic resource management to advanced troubleshooting, these commands will help you master Kubernetes administration.
Most Used Commands Quick Reference:
kubectl get pods– List podskubectl describe pod <name>– Get pod detailskubectl logs <pod-name>– View pod logskubectl exec -it <pod-name> -- bash– Access pod shellkubectl apply -f <file>– Apply configurationkubectl delete pod <name>– Delete pod
Remember to always verify your current context with kubectl config current-context before running commands in production environments.
Last updated: June 2025 | Compatible with Kubernetes 1.25+