# lastTimestamp is added by k8s in resource definition yaml k get events --sort-by=".lastTimestamp"
Watching from events.
1 2 3 4 5 6 7 8
k get events -w --field-selector=type=Warning -A
# watch events for specific container # The "involvedObject.fieldPath" is JSON return of events, see # https://stackoverflow.com/questions/51931113/kubectl-get-events-only-for-a-pod k get events \ --field-selector involvedObject.fieldPath="spec.containers{<container name>}" \ --sort-by=".lastTimestamp"
Get raw json for APIs.
1 2 3
k get --raw /apis/apps/v1 # Get metrics k get --raw /metrics
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 setenv <resource>/<resource-name> --list
List node and its pods mapping.
1 2 3
# column:value mapping: # NODE:.spec.nodeName k get po -o=custom-columns=NODE:.spec.nodeName,NAME:.metadata.name
To examine logs of previous restarted pod/container:
1 2 3 4 5 6 7 8 9
# Other useful options: # -f: streaming logs # --tail: number of line to display # --since: return logs newer then a relative duration 5s, 2m, 3h kubectl logs <pod name> -c <container name> --previous # if no -c specified, return first container log by default
# --tail 5: only show last 5 lines kubectl logs <pod name> -c <container name> -f --tail=5
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 2 3 4 5 6
# Tail all pods from a deployment/sts. kubetail <deployment name> # Tail specific container from deploy/sts. kubetail <deploy name> -c container1 -c container2 # Tail with regex matching. kubetail "^app1|.*my-demo.*" --regex
Rolling out/back resources
Better than editing resource manually with kubectl edit cmd.
The cheat sheet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# rolling update with new image # foo1 is the container name k set image deploy/foo foo1=image:v2
# Check the history of deployments including the revision k rollout history deploy/foo
# Rollback to the previous deployment or specific version # Undo twice turns it back to the original: 1->2->1 k rollout undo deploy/foo
# Watch rolling update status until completion k rollout status -w deploy/foo # Rolling restart of the "foo" deployment k rollout restart deploy/foo
How to roll out back to a specific version:
1 2 3 4 5 6 7 8 9
# first check current version number k get deploy/foo -o yaml | grep revision
# check rollout history and the revision detail k rollout history deploy/foo k rollout history deploy/foo --revision=13
# rollout to the target version k rollout undo deploy/foo --to-revision=13