Sub topic 7
Stored objects and storage versions in etcd
This chapter assumes you know basic kubectl usage, what etcd is (the key-value store behind the API server), and the idea of API versions from earlier chapters. Migrating manifests fixes the files in your repository. It does not change what the cluster has already stored.
What the API server stores
Every object you create is saved in etcd. Each resource has one storage version, and the API server converts every object to that version before writing it. When a client asks for a different served version, the server converts the stored object on the way out.
An object you created years ago can therefore still be stored in an old version. The API server only rewrites it when something updates it.
Why this matters for upgrades
When a release removes an API version, the API server can no longer decode objects stored in that version. Removal rules are meant to give you time to rewrite them first. If you skip that step, you can end up with objects that exist in etcd but that the API server cannot read or return correctly.
The storage version of a built-in resource changes between releases, so objects can stay in an older storage version across an upgrade. Rewriting them lets later upgrades succeed.
Inspecting storage versions
For custom resources, the CustomResourceDefinition (CRD) records the storage version and every version that has ever been used to store objects.
kubectl get crd <crd-name> -o jsonpath='{.spec.versions[*].name}{"\n"}{.status.storedVersions}{"\n"}'The first value lists the versions the CRD serves. status.storedVersions lists the versions that may still exist in etcd. If it contains a version you plan to remove from the CRD, objects in that version may still be stored.
Rewriting stored objects
Reading an object and writing it back without changes makes the API server save it in the current storage version. For built-in resources, this works:
kubectl get <resource> --all-namespaces -o json | kubectl replace -f -Use this only for namespaced resources that you have checked, and run it on a staging cluster first. Objects can be large, and a conflicting update from a controller can make replace fail for some items. Run it again and check the errors.
For custom resources, the Kubernetes project provides the kube-storage-version-migrator for this task. After every object has been rewritten, remove the old version from status.storedVersions and only then from the CRD's spec.versions.
Common errors
- Migrating only the manifests in Git and assuming the cluster is clean.
- Upgrading past a release that removes a version before objects have been rewritten.
- Editing
storedVersionsbefore all objects are migrated. - Running a bulk
replaceon resources that a controller keeps updating, without checking for failures.
Exercise
A CRD serves v1beta1 and v1, with v1 as the storage version, but status.storedVersions shows ["v1beta1","v1"]. Can you remove v1beta1 from the CRD now?
Show answer
No. v1beta1 is still listed, so some objects may be stored in that version. Rewrite every object of that resource (for example with the storage version migrator), then remove v1beta1 from status.storedVersions, and only then from the CRD's versions.