Installing and Configuring kubectl
Bash
# Install kubectl (Linux)
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
chmod +x kubectl && mv kubectl /usr/local/bin/
# Check cluster contexts
kubectl config get-contexts
kubectl config use-context my-cluster
# Current context
kubectl config current-contextBasic Pod Commands
Bash
# List Pods
kubectl get pods
kubectl get pods -n kube-system # Specify a namespace
kubectl get pods -A # All namespaces
kubectl get pods -o wide # Include node information
# Pod details
kubectl describe pod my-pod
# Pod logs
kubectl logs my-pod
kubectl logs my-pod -c my-container # Multi-container
kubectl logs -f my-pod # Follow (live)
# Exec into a Pod
kubectl exec -it my-pod -- bash
kubectl exec -it my-pod -c my-container -- sh
# Delete a Pod
kubectl delete pod my-podManaging Deployments
Bash
# Create a Deployment
kubectl create deployment nginx --image=nginx:latest --replicas=3
# List Deployments and check status
kubectl get deployments
kubectl rollout status deployment/nginx
# Update the image
kubectl set image deployment/nginx nginx=nginx:1.25
# Rollback
kubectl rollout undo deployment/nginx
kubectl rollout undo deployment/nginx --to-revision=2
# Scale
kubectl scale deployment nginx --replicas=5
# Rolling update history
kubectl rollout history deployment/nginxCreating and Exposing Services
Bash
# ClusterIP (internal traffic)
kubectl expose deployment nginx --port=80 --target-port=80
# NodePort (external access)
kubectl expose deployment nginx \
--type=NodePort --port=80 --target-port=80
# LoadBalancer (cloud)
kubectl expose deployment nginx \
--type=LoadBalancer --port=80
# List Services
kubectl get services
kubectl get svc
# Port-forward (local testing)
kubectl port-forward service/nginx 8080:80Managing Resources with YAML
YAML
# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
labels:
app: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: my-app:1.0
ports:
- containerPort: 3000
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"Bash
kubectl apply -f deployment.yaml
kubectl delete -f deployment.yamlHandy Command Cheatsheet
Bash
# List all resources
kubectl get all -n my-namespace
# Filter by label
kubectl get pods -l app=nginx
# Check events (troubleshooting)
kubectl get events --sort-by='.lastTimestamp'
# Node status
kubectl get nodes -o wide
kubectl top nodes
# Pod resource usage
kubectl top pods
# ConfigMap / Secret
kubectl get configmap
kubectl get secret
kubectl describe secret my-secretEditorial note
AI tools may assist drafting. Check the evidence in each guide for whether commands were run and which environment was used. Confirm the current docs for your version before applying instructions, and email us about errors. email us
Topic-related official docs (auto-linked · not per-claim sources)Kubernetes documentation ↗Docker documentation ↗
Questions & answers
Ask a question about this guide. We'll review and reply.