Search code examples
kubernetesresourcesimmutability

Why are some Kubernetes' resources immutable after creation?


From the Kubernetes' validation source code, at least those resources are immutable after creation:

  • Persistent Volumes
  • Storage Classes

Why is that ?


Solution

  • This is a core concept on Kubernetes. A few specs are immutable because their change has impact in the basic structure of the resource it's connected.

    For example, changing the Persistent Volumes may impact pods that are using this PV. Let's suppose you have a mysql pod running on a PV and you change it in a way that all the data is gone.

    On Kubernetes 1.18 Secrets and ConfigMaps also became immutable as an Alpha feature, meaning that this will be the new default soon. Check the GitHub Issue here.

    What is it good for?

    The most popular and the most convenient way of consuming Secrets and ConfigMaps by Pods is consuming it as a file. However, any update to a Secret or ConfigMap object is quickly (roughly within a minute) reflected in updates of the file mounted for all Pods consuming them. That means that a bad update (push) of Secret and/or ConfigMap can very quickly break the entire application.

    Here you can read more about the motivation behind this decision.

    In this KEP, we are proposing to introduce an ability to specify that contents of a particular Secret/ConfigMap should be immutable for its whole lifetime. For those Secrets/ConfigMap, Kubelets will not be trying to watch/poll for changes to updated mounts for their Pods. Given there are a lot of users not really taking advantage of automatic updates of Secrets/ConfigMaps due to consequences described above, this will allow them to:

    • protect themselves better for accidental bad updates that could cause outages of their applications
    • achieve better performance of their cluster thanks to significant reduction of load on apiserver