kubectl - Kubernetes Command Line
kubectl is the command-line interface for interacting with Kubernetes clusters.
Installation
Ubuntu/Debian
# Install kubectl
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
# Verify
kubectl version --clientCentOS/RHEL
# Install kubectl
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/kubectlConfiguration
# View kubeconfig
kubectl config view
# Set cluster context
kubectl config use-context <context-name>
# Get current context
kubectl config current-context
# Create context
kubectl config set-context mycontext --cluster=mycluster --user=myuser
# Delete context
kubectl config delete-context mycontextPod Management
# List pods
kubectl get pods
kubectl get pods -n kube-system
kubectl get pods -A # All namespaces
# Describe pod
kubectl describe pod <pod-name>
# View pod logs
kubectl logs <pod-name>
kubectl logs <pod-name> -c <container-name>
kubectl logs <pod-name> -f # Follow logs
# Execute command in pod
kubectl exec -it <pod-name> -- /bin/bash
# Port forward
kubectl port-forward <pod-name> 8080:8080
# Copy files
kubectl cp <pod-name>:/path/to/file ./local-fileDeployment Management
# Create deployment
kubectl create deployment myapp --image=myimage:latest
# Apply manifest
kubectl apply -f deployment.yaml
# List deployments
kubectl get deployments
# Describe deployment
kubectl describe deployment <deployment-name>
# Scale deployment
kubectl scale deployment <deployment-name> --replicas=3
# Update image
kubectl set image deployment/<deployment-name> <container-name>=<new-image>
# Rollout history
kubectl rollout history deployment/<deployment-name>
# Rollback
kubectl rollout undo deployment/<deployment-name>
# Delete deployment
kubectl delete deployment <deployment-name>Service and Network
# List services
kubectl get svc
# Describe service
kubectl describe svc <service-name>
# Create service
kubectl expose deployment <deployment-name> --port=80 --target-port=8080
# Get ingress
kubectl get ingress
# Describe ingress
kubectl describe ingress <ingress-name>Useful Flags
# Output formats
kubectl get pods -o yaml
kubectl get pods -o json
kubectl get pods -o wide
kubectl get pods -o custom-columns=NAME:.metadata.name,STATUS:.status.phase
# Filter
kubectl get pods -l app=myapp
kubectl get pods --field-selector status.phase=Running
# All namespaces
kubectl get pods -A
# Watch changes
kubectl get pods -w
# Verbose output
kubectl get pods -v=9Troubleshooting
# Check node status
kubectl get nodes
kubectl describe node <node-name>
# Check cluster info
kubectl cluster-info
kubectl cluster-info dump
# View events
kubectl get events
kubectl describe pod <pod-name> # Shows recent events
# Check resources
kubectl top nodes
kubectl top pods
# Debug pod
kubectl debug <pod-name> -it --image=busybox