liveBook · Manning

Kubectl patch

As explained in Kubernetes in Action, Second Edition

kubectl patch is a Kubernetes command that updates selected fields of a Kubernetes object without replacing its entire manifest.

kubectl patch updates selected fields of a Kubernetes object without requiring a complete manifest edit or replacement. It is useful when one change must keep related fields consistent, such as changing both a Deployment's container image and its application-version label. section 15.2.1

Update an image and label together

Run `kubectl patch deploy kiada --patch '{"spec":{"template":{"metadata":{"labels":{"ver":"0.6"}},"spec":{"containers":[{"name":"kiada","image":"luksa/kiada:0.6"}]}}}}'`. This changes the image and matching Pod-template label in one operation. section 15.2.1

$ kubectl patch deploy kiada --patch '     #1
spec:                               #2
  template:                         #2
    metadata:                       #2
      labels:                       #2
        ver: "0.6"                  #2
    spec:                           #2
      containers:                   #2
      - name: kiada                 #2
        image: luksa/kiada:0.6'     #2

Use a multiline YAML patch when the JSON form is difficult to parse; the patch contains only the fields being changed. section 15.2.1

After patching a Deployment with the Recreate strategy:

How Kubernetes in Action, Second Edition covers it

How deep it goes
Explains JSON and YAML patch construction, related-field consistency, and resulting Pod and ReplicaSet changes.
Examples
Demonstrates updating the kiada image and version label from 0.5 to 0.6 with JSON and multiline YAML.
What it assumes
Assumes familiarity with Kubernetes manifests, Deployments, containers, labels, and basic kubectl commands.
What you can do after
Apply it via kubectl to change multiple manifest fields without editing or applying a complete manifest file.
How it connects
Connects kubectl patch with Deployments, Pod templates, container images, labels, ReplicaSets, and the Recreate strategy.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kubernetes in Action, Second Edition. The section links point to the parts of the book it draws on.