Kubernetes Pod Presets

Amir Khanof
2 min readMar 4, 2023

--

Introduction

Kubernetes Pod Presets is a feature that allows you to inject additional configuration data into a pod at runtime. This can be used to set environment variables, mount volumes, and add annotations to a pod, among other things. Pod Presets can simplify the management of pod configuration by centralizing it in a single object that can be applied to multiple pods.

Example

Let’s say you have a web application that runs in a pod and requires a database connection. You can create a Pod Preset object that defines the database connection parameters as environment variables, and then apply that Pod Preset to all the pods that run the web application. This way, you don’t have to define the database connection parameters in each pod’s configuration.

To create a Pod Preset, you need to define a Kubernetes object of kind “PodPreset”. Here’s an example:

apiVersion: settings.k8s.io/v1alpha1
kind: PodPreset
metadata:
name: db-connection
spec:
selector:
matchLabels:
app: web
env:
- name: DB_HOST
value: db.example.com
- name: DB_PORT
value: "5432"

In this example, I define a Pod Preset object named db-connection that sets the environment variables DB_HOST and DB_PORT to connect to a database. The selector field is used to specify which pods the Pod Preset should be applied to; in this case, it applies to pods with the label app: web.

To apply the Pod Preset to a pod, simply include the annotation podpreset.admission.kubernetes.io/podpreset-apply: db-connection in the pod's configuration:

apiVersion: v1
kind: Pod
metadata:
name: web-pod
annotations:
podpreset.admission.kubernetes.io/podpreset-apply: db-connection
spec:
containers:
- name: web-container
image: my-web-app

In this example, I apply the db-connection Pod Preset to a pod named web-pod.

Summary

In summary, Kubernetes Pod Presets provide a way to inject additional configuration data into a pod at runtime. They can simplify the management of pod configuration by centralizing it in a single object that can be applied to multiple pods. To use Pod Presets, you need to define a Pod Preset object and include an annotation in the pod’s configuration to apply the Pod Preset.

--

--

Amir Khanof
Amir Khanof

Written by Amir Khanof

Java, Oracle Fusion, javascript (developer, architecture, administrator)

No responses yet