This page shows how to use PodPreset objects to inject information like SecretsStores sensitive information, such as passwords, OAuth tokens, and ssh keys. , volume mounts, and environment variablesContainer environment variables are name=value pairs that provide useful information into containers running in a Pod. into Pods at creation time.
You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. If you do not already have a cluster, you can create one by using Minikube, or you can use one of these Kubernetes playgrounds:
Your Kubernetes server must be at or later than version v1.10.
To check the version, enter kubectl version
.
In this step, you create a preset that has a volume mount and one environment variable. Here is the manifest for the PodPreset:
podpreset/preset.yaml
|
---|
|
The name of a PodPreset object must be a valid DNS subdomain name.
In the manifest, you can see that the preset has an environment variable definition called DB_PORT
and a volume mount definition called cache-volume
which is mounted under /cache
. The selectorAllows users to filter a list of resources based on labels.
specifies that
the preset will act upon any Pod that is labeled role:frontend
.
Create the PodPreset:
kubectl apply -f https://k8s.io/examples/podpreset/preset.yaml
Verify that the PodPreset has been created:
kubectl get podpreset
NAME CREATED AT
allow-database 2020-01-24T08:54:29Z
This manifest defines a Pod labelled role: frontend
(matching the PodPreset’s selector):
podpreset/pod.yaml
|
---|
|
Create the Pod:
kubectl create -f https://k8s.io/examples/podpreset/pod.yaml
Verify that the Pod is running:
kubectl get pods
The output shows that the Pod is running:
NAME READY STATUS RESTARTS AGE
website 1/1 Running 0 4m
View the Pod spec altered by the admission controller in order to see the effects of the preset having been applied:
kubectl get pod website -o yaml
podpreset/merged.yaml
|
---|
|
The DB_PORT
environment variable, the volumeMount
and the podpreset.admission.kubernetes.io
annotation
of the Pod verify that the preset has been applied.
This is an example to show how a Pod spec is modified by a Pod preset that references a ConfigMap containing environment variables.
Here is the manifest containing the definition of the ConfigMap:
podpreset/configmap.yaml
|
---|
|
Create the ConfigMap:
kubectl create -f https://k8s.io/examples/podpreset/configmap.yaml
Here is a PodPreset manifest referencing that ConfigMap:
podpreset/allow-db.yaml
|
---|
|
Create the preset that references the ConfigMap:
kubectl create -f https://k8s.io/examples/podpreset/allow-db.yaml
The following manifest defines a Pod matching the PodPreset for this example:
podpreset/pod.yaml
|
---|
|
Create the Pod:
kubectl create -f https://k8s.io/examples/podpreset/pod.yaml
View the Pod spec altered by the admission controller in order to see the effects of the preset having been applied:
kubectl get pod website -o yaml
podpreset/allow-db-merged.yaml
|
---|
|
The DB_PORT
environment variable and the podpreset.admission.kubernetes.io
annotation of the Pod
verify that the preset has been applied.
This is an example to show that only Pod specs are modified by Pod presets. Other workload types like ReplicaSets or Deployments are unaffected.
Here is the manifest for the PodPreset for this example:
podpreset/preset.yaml
|
---|
|
Create the preset:
kubectl apply -f https://k8s.io/examples/podpreset/preset.yaml
This manifest defines a ReplicaSet that manages three application Pods:
podpreset/replicaset.yaml
|
---|
|
Create the ReplicaSet:
kubectl create -f https://k8s.io/examples/podpreset/replicaset.yaml
Verify that the Pods created by the ReplicaSet are running:
kubectl get pods
The output shows that the Pods are running:
NAME READY STATUS RESTARTS AGE
frontend-2l94q 1/1 Running 0 2m18s
frontend-6vdgn 1/1 Running 0 2m18s
frontend-jzt4p 1/1 Running 0 2m18s
View the spec
of the ReplicaSet:
kubectl get replicasets frontend -o yaml
Note:The ReplicaSet object’s
spec
was not changed, nor does the ReplicaSet contain apodpreset.admission.kubernetes.io
annotation. This is because a PodPreset only applies to Pod objects.To see the effects of the preset having been applied, you need to look at individual Pods.
The command to view the specs of the affected Pods is:
kubectl get pod --selector=role=frontend -o yaml
podpreset/replicaset-merged.yaml
|
---|
|
Again the podpreset.admission.kubernetes.io
annotation of the Pods
verifies that the preset has been applied.
This is an example to show how a Pod spec is modified by multiple Pod presets.
Here is the manifest for the first PodPreset:
podpreset/preset.yaml
|
---|
|
Create the first PodPreset for this example:
kubectl apply -f https://k8s.io/examples/podpreset/preset.yaml
Here is the manifest for the second PodPreset:
podpreset/proxy.yaml
|
---|
|
Create the second preset:
kubectl apply -f https://k8s.io/examples/podpreset/proxy.yaml
Here’s a manifest containing the definition of an applicable Pod (matched by two PodPresets):
podpreset/pod.yaml
|
---|
|
Create the Pod:
kubectl create -f https://k8s.io/examples/podpreset/pod.yaml
View the Pod spec altered by the admission controller in order to see the effects of both presets having been applied:
kubectl get pod website -o yaml
podpreset/multi-merged.yaml
|
---|
|
The DB_PORT
environment variable, the proxy-volume
VolumeMount and the two podpreset.admission.kubernetes.io
annotations of the Pod verify that both presets have been applied.
This is an example to show how a Pod spec is not modified by a Pod preset when there is a conflict.
The conflict in this example consists of a VolumeMount
in the PodPreset conflicting with a Pod that defines the same mountPath
.
Here is the manifest for the PodPreset:
podpreset/conflict-preset.yaml
|
---|
|
Note the mountPath
value of /cache
.
Create the preset:
kubectl apply -f https://k8s.io/examples/podpreset/conflict-preset.yaml
Here is the manifest for the Pod:
podpreset/conflict-pod.yaml
|
---|
|
Note the volumeMount element with the same path as in the PodPreset.
Create the Pod:
kubectl create -f https://k8s.io/examples/podpreset/conflict-pod.yaml
View the Pod spec:
kubectl get pod website -o yaml
podpreset/conflict-pod.yaml
|
---|
|
You can see there is no preset annotation (podpreset.admission.kubernetes.io
). Seeing no annotation tells you that no preset has not been applied to the Pod.
However, the
PodPreset admission controller
logs a warning containing details of the conflict.
You can view the warning using kubectl
:
kubectl -n kube-system logs -l=component=kube-apiserver
The output should look similar to:
W1214 13:00:12.987884 1 admission.go:147] conflict occurred while applying podpresets: allow-database on pod: err: merging volume mounts for allow-database has a conflict on mount path /cache:
v1.VolumeMount{Name:"other-volume", ReadOnly:false, MountPath:"/cache", SubPath:"", MountPropagation:(*v1.MountPropagationMode)(nil), SubPathExpr:""}
does not match
core.VolumeMount{Name:"cache-volume", ReadOnly:false, MountPath:"/cache", SubPath:"", MountPropagation:(*core.MountPropagationMode)(nil), SubPathExpr:""}
in container
Note the conflict message on the path for the VolumeMount.
Once you don’t need a PodPreset anymore, you can delete it with kubectl
:
kubectl delete podpreset allow-database
The output shows that the PodPreset was deleted:
podpreset "allow-database" deleted
Was this page helpful?
Thanks for the feedback. If you have a specific, answerable question about how to use Kubernetes, ask it on Stack Overflow. Open an issue in the GitHub repo if you want to report a problem or suggest an improvement.