Kubectl Cheat Sheet
Useful commands list.
General
Overview
https://kubernetes.io/docs/reference/kubectl/overview/
Install
Enable autocomplete
Enable autocomplete for an alias.
References
https://kubernetes.io/docs/tasks/tools/install-kubectl/#enabling-shell-autocompletion
Explain components
Run kubectl from inside a container
TTY connect to your container and make sure kubectl is installed.
Import your Kubernetes config
When you are connected to a container deployed in Kubernetes cluster, it already has access to Kubernetes config and certificates, you only need to import them:
Do not replace any path or URL, you can use exactly the command above.
At this point you should have the file ~/.kube/config
.
WORKAROUND: if, by any change, you are having a hard time, you can get the /root/.kube/config
file from your original installation and restore it inside your container.
Generate kubeconfig from ServiceAccount
Cluster management
Get cluster name
Get cluster endpoints
List all API resources
Logs
Get logs from a previous restart pod:
Namespaces
Force delete namespace (hanging on "Terminating")
If the namespace is not deleted, check its manifest:
Check if it has any finalizers
, for example:
Edit it:
And delete the finalizers
block.
If it does not work, export namespace manifest to a file.
Edit the file, on finalizers
block, remove "kubernetes" (or any other existing finalizer).
Nodes
Get nodes
Permission
can-i
Pods
Connect to pod TTY
The right way
List your pods:
Locate the one you want access, get its name, and run:
Replace --user=root with your container user and hal-66b97c4c88-b675b with your pod name.
If your namespace has only one pod, your use only one command:
Workaround
If by any reason you could not use kubectl exec (for example, if your container does not allow root auth), then SSH to your K8s worker node which is hosting your pod.
Locate the container you want to connect to:
Replace halyard with any keyword you want.
Then connect to it:
Force delete pod
Never force pod deletion unless it is really necessary
If you have a pod which is referenced by a Replica Set that does not exist and you are stuck, force pod deletion.
Replace PUT-YOUR-NAMESPACE-HERE Replace PUT-YOUR-POD-NAME-HERE
References
Get pods, filter by label, print pod name and its namespace
kubectl get pods -Ao jsonpath='{range .items[?(@.metadata.labels.app=="my-ubuntu")]}{@.metadata.name}{" "}{@.metadata.namespace}{"\n"}{end}'
RBAC
(Cluster)RoleBindings and the ServiceAccount(s) they reference with
Resources
List pods resource limits
Last updated