liveBook · Manning

Last-applied-configuration annotation

As explained in Kubernetes in Action, Second Edition

The last-applied-configuration annotation is a Kubernetes object annotation that stores the configuration last applied by kubectl for comparison during subsequent applies.

The last-applied-configuration annotation stores the configuration most recently applied by kubectl. When kubectl apply runs again, it uses this stored configuration to calculate changes to the Kubernetes API object. section 15.1.2

The annotation is managed internally by kubectl and can make an object’s metadata verbose. It may appear in an object’s YAML or JSON definition but is not shown by kubectl describe; it may eventually be deprecated and removed. section 7.5.3

Use the annotation carefully when fields are omitted from manifests:

  • If a Deployment manifest omits replicas, the API sets replicas to 1; a later kubectl apply can therefore reduce an intentionally scaled Deployment. section 15.1.2
  • Repair the stored configuration by editing the last-applied configuration and removing replicas, so future applies no longer overwrite the desired replica count when that field remains omitted. section 15.1.2
  • Avoid the problem by omitting replicas from the original manifest, then scale the Deployment separately to suit demand. section 15.1.2

Repair the stored applied configuration

Run `kubectl apply edit-last-applied deploy kiada`. An editor opens the Deployment’s stored last-applied configuration; remove the replicas field, save, and close it. Subsequent kubectl apply operations no longer overwrite the Deployment’s separately managed replica count. section 15.1.2

$ kubectl apply edit-last-applied deploy kiada

Use the command listing to open and edit a Deployment’s stored last-applied configuration. section 15.1.2

How Kubernetes in Action, Second Edition covers it

How deep it goes
Explains comparison and omitted-field effects, then demonstrates annotation repair, but does not detail kubectl’s internal diff algorithm.
Examples
Demonstrates omitted replicas, Deployment scaling, annotation inspection, and kubectl apply edit-last-applied.
What it assumes
Assumes familiarity with kubectl commands, Kubernetes objects, annotations, and Deployment replica counts.
What you can do after
Operate/configure Deployments by inspecting and repairing stored apply configuration before reapplying manifests.
How it connects
Connects kubectl apply with Kubernetes annotations, Deployments, and replica scaling.

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.