DevOps & Cloud71 entries
Kubernetes Commands
kubectl essentials: pods, deployments, services, secrets, scaling, debugging, and RBAC
1Cluster & Context
kubectl cluster-info | Display cluster endpoint and services info |
kubectl config view | Show merged kubeconfig settings |
kubectl config current-context | Show the current context |
kubectl config use-context <ctx> | Switch to a different context |
kubectl config get-contexts | List all available contexts |
kubectl config set-context --current --namespace=<ns> | Set default namespace for current context |
kubectl get nodes | List all nodes in the cluster |
kubectl get nodes -o wide | List nodes with extra details (IP, OS, etc.) |
kubectl api-resources | List all supported resource types |
2Pod Management
kubectl get pods | List pods in current namespace |
kubectl get pods -A | List pods across all namespaces |
kubectl get pods -o wide | List pods with node and IP info |
kubectl get pods -w | Watch pods in real-time |
kubectl describe pod <name> | Show detailed info about a pod |
kubectl run <name> --image=<img> | Create and run a pod from an image |
kubectl delete pod <name> | Delete a specific pod |
kubectl delete pod <name> --force --grace-period=0 | Force delete a stuck pod |
kubectl exec -it <pod> -- bash | Open shell in a running pod |
kubectl cp <pod>:<path> <local> | Copy files from pod to local machine |
3Deployments
kubectl get deployments | List all deployments |
kubectl describe deployment <name> | Show deployment details and events |
kubectl create deployment <name> --image=<img> | Create a deployment |
kubectl set image deployment/<name> <ctr>=<img>:<tag> | Update container image in deployment |
kubectl rollout status deployment/<name> | Watch rollout progress |
kubectl rollout history deployment/<name> | Show rollout revision history |
kubectl rollout undo deployment/<name> | Rollback to previous revision |
kubectl rollout undo deployment/<name> --to-revision=<n> | Rollback to specific revision |
kubectl rollout restart deployment/<name> | Restart all pods in deployment |
kubectl delete deployment <name> | Delete a deployment |
4Services & Networking
kubectl get svc | List all services |
kubectl get svc -A | List services across all namespaces |
kubectl describe svc <name> | Show service details and endpoints |
kubectl expose deployment <name> --port=80 --type=LoadBalancer | Expose deployment as a service |
kubectl port-forward svc/<name> 8080:80 | Forward local port to service |
kubectl port-forward pod/<name> 8080:80 | Forward local port to pod |
kubectl get ingress | List all ingress resources |
kubectl get endpoints <svc> | Show pod IPs behind a service |
5ConfigMaps & Secrets
kubectl get configmaps | List all ConfigMaps |
kubectl create configmap <name> --from-file=<path> | Create ConfigMap from file |
kubectl create configmap <name> --from-literal=key=val | Create ConfigMap from literal values |
kubectl get secrets | List all secrets |
kubectl create secret generic <name> --from-literal=k=v | Create a generic secret |
kubectl create secret tls <name> --cert=tls.crt --key=tls.key | Create a TLS secret |
kubectl get secret <name> -o jsonpath="{.data.key}" | base64 -d | Decode and view a secret value |
kubectl describe configmap <name> | Show ConfigMap contents and metadata |
6Namespaces & RBAC
kubectl get namespaces | List all namespaces |
kubectl create namespace <name> | Create a new namespace |
kubectl delete namespace <name> | Delete a namespace and all its resources |
kubectl get all -n <namespace> | List all resources in a namespace |
kubectl get serviceaccounts | List service accounts |
kubectl auth can-i <verb> <resource> | Check if current user can perform action |
kubectl auth can-i --list | List all permissions for current user |
7Logs & Debugging
kubectl logs <pod> | Show logs for a pod |
kubectl logs <pod> -f | Follow (stream) pod logs |
kubectl logs <pod> -c <container> | Show logs for specific container in pod |
kubectl logs <pod> --previous | Show logs from previous crashed container |
kubectl logs -l app=<label> | Show logs for all pods matching label |
kubectl describe pod <name> | Show events and status (debug crashes) |
kubectl get events --sort-by=.metadata.creationTimestamp | List cluster events sorted by time |
kubectl top pods | Show CPU/memory usage per pod |
kubectl top nodes | Show CPU/memory usage per node |
kubectl debug <pod> -it --image=busybox | Attach debug container to a pod |
8Scaling & Apply
kubectl scale deployment/<name> --replicas=3 | Scale deployment to 3 replicas |
kubectl autoscale deployment/<name> --min=2 --max=10 --cpu-percent=80 | Create HorizontalPodAutoscaler |
kubectl get hpa | List Horizontal Pod Autoscalers |
kubectl apply -f <file.yaml> | Apply config from YAML file |
kubectl apply -f <directory>/ | Apply all YAML files in directory |
kubectl diff -f <file.yaml> | Preview changes before applying |
kubectl delete -f <file.yaml> | Delete resources defined in YAML file |
kubectl get <resource> -o yaml | Export resource definition as YAML |
kubectl explain <resource> | Show documentation for a resource type |