Some useful CLI tips in daily uses, references:
Explain resources with specific fields.
This can help you explore the complete resource definition to find all available fields.
1 | k explain ep |
Create manual job from cronjob, you can also output as json template, edit
and use it.
1 | k create job --from=cronjob/<name of cronjob> <manual job name> |
Check service account/regular user permission.
1 | # get sa name |
To check what you can do:
1 | k auth can-i --list |
Force delete pods with no grace period
This only works on pod resource:
1 | k delete pod xxx --grace-period=0 --force |
List endpoints(pod IP:port).
1 | k get ep -n <namespace> |
List events sorted by lastTimestamp.
1 | # lastTimestamp is added by k8s in resource definition yaml |
Watching from events.
1 | k get events -w --field-selector=type=Warning -A |
Get raw json for APIs.
1 | k get --raw /apis/apps/v1 |
Wait for pods to be ready.
1 | k wait --for=condition=ready pod -l foo=bar |
List customer env vars from a resource.
1 | k set env <resource>/<resource-name> --list |
List node and its pods mapping.
1 | # column:value mapping: |
Create starter YAML for resources.
1 | kubectl create deploy anyname --image=nginx --dry-run=client -o yaml |
Pods sorted by memory usage.
1 | kubectl top pods -A --sort-by='memory' |
To examine logs of previous restarted pod/container:
1 | # Other useful options: |
Aggregate(tail/follow) logs from multiple pods into one stream.
kubetail is written by bash so can be
used without installing other dependencies, git clone and put the executable to
$PATH
.
1 | # Tail all pods from a deployment/sts. |
Rolling out/back resources
Better than editing resource manually with kubectl edit
cmd.
The cheat sheet
1 | # rolling update with new image |
How to roll out back to a specific version:
1 | # first check current version number |