- focusing on
kubectlcommands 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:80Forward 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:80Use 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/bashGet detailed information about a specific pod, including events and environment variables.
kubectl describe pod <pod-name>
# Example
kubectl describe pod my-podView detailed information about a service, including its endpoint mappings.
kubectl describe service <service-name>
# Example
kubectl describe service my-serviceDisplay all pods in a given namespace.
kubectl get pods -n <namespace>
# Example
kubectl get pods -n defaultShow all services in the current namespace.
kubectl get servicesCheck the nodes in the Kubernetes cluster.
kubectl get nodesAccess logs for a specific container in a pod.
kubectl logs <pod-name> -c <container-name>
# Example
kubectl logs my-pod -c my-containerUse the --all-containers flag to get logs from all containers.
kubectl logs <pod-name> --all-containers=true
# Example
kubectl logs my-pod --all-containers=trueStream logs in real-time.
kubectl logs -f <pod-name> -c <container-name>
# Example
kubectl logs -f my-pod -c my-containerCreate a deployment directly from an image.
kubectl create deployment <deployment-name> --image=<image-name>
# Example
kubectl create deployment my-deployment --image=nginxScale 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=3Change 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.19Get a list of all namespaces in the cluster.
kubectl get namespacesYou 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=productionApply configuration from a YAML file to create or update resources.
kubectl apply -f <file.yaml>
# Example
kubectl apply -f deployment.yamlDelete resources defined in a manifest file.
kubectl delete -f <file.yaml>
# Example
kubectl delete -f deployment.yamlView all events in a namespace to troubleshoot issues.
kubectl get events -n <namespace>
# Example
kubectl get events -n defaultCopy 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.logCreate 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=secretRetrieve 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