Skip to content

Kubernetes: Patch, label, annotate & scale

9 Kubernetes commands: Patch, label, annotate & scale. Click a command to see what each part does.

Index
9 items
Click a command to see what each part does.

Patch, label, annotate & scale9

  • kubectl patch deploy <deployment> -n <namespace> -p '{"spec":{"replicas":3}}'Patch a field (strategic merge)Caution

    What each part does

    1. patchchange part of a live object without a full manifest
    2. -p '{...}'JSON or YAML snippet merged into the object
  • kubectl patch svc <service> --type=json -p '[{"op":"replace","path":"/spec/type","value":"NodePort"}]'Patch with a JSON patchCaution

    What each part does

    1. --type=jsonRFC 6902 operations: add, remove, replace at a path
    2. -p '[...]'the operations
  • kubectl label po <pod> <key>=<value>; kubectl label po <pod> <key>-Add / remove a label

    What each part does

    1. <key>=<value>add (add --overwrite to change)
    2. <key>-remove the label; also a way to take a pod out of a service for debugging
  • kubectl annotate deploy <deployment> <key>='<value>' --overwriteAdd an annotation

    What each part does

    1. annotatenon-identifying metadata read by tools
    2. --overwritereplace an existing value
  • kubectl autoscale deploy <deployment> --min=2 --max=10 --cpu-percent=80Autoscale a deployment

    What each part does

    1. autoscalecreate an HPA
    2. --min / --maxreplica bounds
    3. --cpu-percent=80target average CPU vs requests (pods need CPU requests)
  • kubectl set resources deploy <deployment> -c <container> --requests=cpu=100m,memory=128Mi --limits=memory=256MiSet CPU / memory requests and limitsCaution

    What each part does

    1. set resourceschange resources in the pod template (rollout follows)
    2. --requests / --limitsvalues per container
  • kubectl rollout pause deploy/<deployment>; kubectl rollout resume deploy/<deployment>Pause and resume a rollout

    What each part does

    1. rollout pausestop rolling out; batch several changes
    2. rollout resumeroll them out together
  • kubectl rollout history deploy/<deployment> --revision=<n>Details of one rollout revision

    What each part does

    1. --revision=<n>show the pod template (images, env) of that revision before undoing to it
  • kubectl scale sts <statefulset> --replicas=<n> -n <namespace>Scale a StatefulSet

    What each part does

    1. scale stspods are added/removed in order (highest ordinal first on scale-down)

My DevOps Playbook by Angelo John Calleja

New runbook

GitHub not connected

Saving is handled by the site: the GitHub token is stored in Cloudflare, not in your browser. Enter the editor password (the EDITOR_PASSWORD secret) to unlock saving in this browser.

Saving commits to gelogrammer/My_DevOps_Playbook. Create a fine-grained token with access to only this repo andContents: Read and write, then paste it here. It is stored only in this browser.

src/content/docs/…