PodPresets are Admission controller objects in Kubernetes, i.e., if PodPresets are enabled and configured, then, before each Pod creation, the Pod will be validated across the available PodPresets to see if the Pod matches with the label selectors on any of the PodPresets, and if it matches, the information contained in that PodPreset will be injected in to the Pod before its get created. Likewise, the Pod would be validated by all the available PodPresets available in the same namespace. So a single PodPreset could be applied to multiple Pods and vice versa. However, if none of the PodPresets are applied to a Pod if labels are not matched, then, the Pod will be created as usual without any PodPreset

The information that a PodPreset can inject into a Pod could be Pod specific information such as secrets, volumes and Container specific information such as volume mounts, environment variables. Note that environmental variables could be configured directly in a container using ‘env’ section or could be imported from other objects such as ConfigMaps via the ‘envFrom’ section

When a PodPreset is applied to a Pod based on label selection, an annotation will be added to the Pod with the PodPreset’s name

Let’s try checking if there are any PodPresets in our cluster

shakir@k8s-master:~ kubectl get podpresets
error: the server doesn't have a resource type "podpresets"

We need to enable Pod presets by adding --runtime-config=settings.k8s.io/v1alpha1=true after kube-apiserver as shown in the pic.

This is in the file /etc/kubernetes/manifests/kube-apiserver.yaml

We also need to ensure the --enable-admission-plugins has PodPreset

shakir@k8s-master:~ sudo nano /etc/kubernetes/manifests/kube-apiserver.yaml

It should now enabled, however no podpreset objects are created yet

shakir@k8s-master:~ kubectl get podpresets
No resources found.

Let’s define a manifest for a PodPreset to inject Environment variables into the container residing inside selected Pods

To create the PodPreset and to check it’s detail

shakir@k8s-master:~ kubectl create -f ex28-podpreset.yaml
podpreset.settings.k8s.io/podpreset28 created
shakir@k8s-master:~ kubectl get podpresets
NAME CREATED AT
podpreset28 2019-08-09T18:48:54Z

We are going to define a Pod configuration as follows, and give it a matching label that the PodPreset can select. Note that we have not defined any environment variables in the container spec, and we are going to inject it from the PodPreset

Create the Pod

shakir@k8s-master:~ kubectl create -f ex28-po.yaml
pod/po28 created

To check the Pod’s status

Let’s check if the environment variables are present in the container, we see both the variables ‘color’ and ‘fruit’ and their corresponding values. Note that printenv is the command to print all the environment variables, and grep is used to filter a string pattern

We had seen the example of injecting environment variables, however we can use PodPresets to inject other information as mentioned in the beginning of this post.

--end-of-post--