Watch — get real-time notifications of resource changes. Bookmark — optimization for long watches.
1# Watch resources2kubectl get pods -w # Watch for changes3kubectl get events -w --sort-by=.lastTimestamp4kubectl get deployments -w -n production56# Watch specific fields7kubectl get pods -w -o json | jq -c '.' | while read line; do8 echo "$line" | jq -r 'select(.object.status.phase != "Running") | .metadata.name'9done1011# Watch with label selector12kubectl get pods -l app=myapp -w1314# Watch for specific events15kubectl get events -w --field-selector reason=FailedScheduling16kubectl get events -w --field-selector reason=Killing1718# Watch endpoint changes19kubectl get endpoints myapp -w2021# API server watch (raw)22kubectl get --raw "/api/v1/pods?watch=true" | jq -c '.'23kubectl get --raw "/apis/apps/v1/deployments?watch=true&resourceVersion=0" | jq -c '.'2425# Bookmark optimization26kubectl get pods --watch-bookmark -w # Get bookmark events
1// Kubernetes JS client watch example2import { CoreV1Api, Watch } from '@kubernetes/client-node';34const kc = new KubeConfig();5kc.loadFromDefault();6const k8sApi = kc.makeApiClient(CoreV1Api);7const watch = new Watch(kc);89watch.watch('/api/v1/pods', {}, (phase, apiObj) => {10 console.log(`Pod ${phase}: ${apiObj.metadata.name}`);11}, (err) => {12 console.error(`Watch error: ${err}`);13});
Event Types:
Use cases: