- focusing on
kubectl
commands commonly used for development, debugging, and maintenance tasks. - This can be imported as markdown and saved as a Confluence page etc. for easy reference.
This cheat sheet covers some essential kubectl
commands for platform engineers. It includes syntax examples for inspecting resources, managing configurations, debugging, and performing essential tasks.
Forward a local port to a port on a pod, allowing access to internal services without exposing them publicly.
kubectl port-forward pod/<pod-name> <local-port>:<pod-port>
# Example
kubectl port-forward pod/my-pod 8080:80
Forward a local port to a port on a Kubernetes service.
kubectl port-forward service/<service-name> <local-port>:<service-port>
# Example
kubectl port-forward service/my-service 8080:80
Use exec
to run commands in a container within a pod, useful for debugging or checking logs.
kubectl exec -it <pod-name> -- <command>
# Example
kubectl exec -it my-pod -- /bin/bash
Get detailed information about a specific pod, including events and environment variables.
kubectl describe pod <pod-name>
# Example
kubectl describe pod my-pod
View detailed information about a service, including its endpoint mappings.
kubectl describe service <service-name>
# Example
kubectl describe service my-service
Display all pods in a given namespace.
kubectl get pods -n <namespace>
# Example
kubectl get pods -n default
Show all services in the current namespace.
kubectl get services
Check the nodes in the Kubernetes cluster.
kubectl get nodes
Access logs for a specific container in a pod.
kubectl logs <pod-name> -c <container-name>
# Example
kubectl logs my-pod -c my-container
Use the --all-containers
flag to get logs from all containers.
kubectl logs <pod-name> --all-containers=true
# Example
kubectl logs my-pod --all-containers=true
Stream logs in real-time.
kubectl logs -f <pod-name> -c <container-name>
# Example
kubectl logs -f my-pod -c my-container
Create a deployment directly from an image.
kubectl create deployment <deployment-name> --image=<image-name>
# Example
kubectl create deployment my-deployment --image=nginx
Scale a deployment up or down to a specific number of replicas.
kubectl scale deployment <deployment-name> --replicas=<number>
# Example
kubectl scale deployment my-deployment --replicas=3
Change the image of a deployment.
kubectl set image deployment/<deployment-name> <container-name>=<new-image>
# Example
kubectl set image deployment/my-deployment nginx=nginx:1.19
Get a list of all namespaces in the cluster.
kubectl get namespaces
You can set a specific namespace as the default for commands using a context update.
kubectl config set-context --current --namespace=<namespace>
# Example
kubectl config set-context --current --namespace=production
Apply configuration from a YAML file to create or update resources.
kubectl apply -f <file.yaml>
# Example
kubectl apply -f deployment.yaml
Delete resources defined in a manifest file.
kubectl delete -f <file.yaml>
# Example
kubectl delete -f deployment.yaml
View all events in a namespace to troubleshoot issues.
kubectl get events -n <namespace>
# Example
kubectl get events -n default
Copy files to and from a pod for further inspection.
# Copy from local to pod
kubectl cp <local-file-path> <pod-name>:<pod-file-path>
# Copy from pod to local
kubectl cp <pod-name>:<pod-file-path> <local-file-path>
# Example
kubectl cp my-pod:/var/log/app.log ./app.log
Create a new Kubernetes secret from literal values (e.g., storing credentials).
kubectl create secret generic <secret-name> --from-literal=<key>=<value>
# Example
kubectl create secret generic my-secret --from-literal=username=admin --from-literal=password=secret
Retrieve the secret data in a base64-encoded format.
kubectl get secret <secret-name> -o jsonpath='{.data.<key>}' | base64 --decode
# Example
kubectl get secret my-secret -o jsonpath='{.data.password}' | base64 --decode