2017-04-04 00:40:48 +00:00
|
|
|
---
|
2018-02-18 19:29:37 +00:00
|
|
|
reviewers:
|
2017-04-04 00:40:48 +00:00
|
|
|
- jessfraz
|
2017-06-08 19:12:52 +00:00
|
|
|
title: Inject Information into Pods Using a PodPreset
|
2018-05-17 23:46:49 +00:00
|
|
|
weight: 60
|
2017-04-04 00:40:48 +00:00
|
|
|
---
|
|
|
|
|
2017-10-25 21:41:51 +00:00
|
|
|
You can use a `podpreset` object to inject information like secrets, volume
|
|
|
|
mounts, and environment variables etc into pods at creation time.
|
|
|
|
This task shows some examples on using the `PodPreset` resource.
|
|
|
|
You can get an overview of PodPresets at
|
|
|
|
[Understanding Pod Presets](/docs/concepts/workloads/pods/podpreset/).
|
2017-04-04 00:40:48 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{< toc >}}
|
2017-04-04 00:40:48 +00:00
|
|
|
|
2017-06-08 19:12:52 +00:00
|
|
|
## Create a Pod Preset
|
2017-04-04 00:40:48 +00:00
|
|
|
|
|
|
|
### Simple Pod Spec Example
|
|
|
|
|
|
|
|
This is a simple example to show how a Pod spec is modified by the Pod
|
2017-06-22 21:21:14 +00:00
|
|
|
Preset.
|
2017-04-04 00:40:48 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{< code file="podpreset-preset.yaml" >}}
|
2017-11-23 06:38:27 +00:00
|
|
|
|
2017-11-23 12:53:58 +00:00
|
|
|
Create the PodPreset:
|
2017-11-23 06:38:27 +00:00
|
|
|
|
2017-11-23 12:51:55 +00:00
|
|
|
```shell
|
|
|
|
kubectl create -f https://k8s.io/docs/tasks/inject-data-application/podpreset-preset.yaml
|
|
|
|
```
|
2017-11-23 06:38:27 +00:00
|
|
|
|
2017-11-23 12:53:58 +00:00
|
|
|
Examine the created PodPreset:
|
2017-11-23 06:38:27 +00:00
|
|
|
|
2017-11-23 12:51:55 +00:00
|
|
|
```shell
|
2017-11-24 11:11:37 +00:00
|
|
|
$ kubectl get podpreset
|
2017-11-23 12:51:55 +00:00
|
|
|
NAME AGE
|
|
|
|
allow-database 1m
|
|
|
|
```
|
2017-11-23 06:38:27 +00:00
|
|
|
|
2017-11-23 12:53:58 +00:00
|
|
|
The new PodPreset will act upon any pod that has label `role: frontend`.
|
2017-04-04 00:40:48 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{< code file="podpreset-pod.yaml" >}}
|
2017-04-04 00:40:48 +00:00
|
|
|
|
2017-11-23 12:53:58 +00:00
|
|
|
Create a pod:
|
2017-11-23 06:38:27 +00:00
|
|
|
|
2017-11-23 12:51:55 +00:00
|
|
|
```shell
|
2017-11-24 11:11:37 +00:00
|
|
|
$ kubectl create -f https://k8s.io/docs/tasks/inject-data-application/podpreset-pod.yaml
|
2017-11-23 12:51:55 +00:00
|
|
|
```
|
2017-11-23 06:38:27 +00:00
|
|
|
|
2017-11-23 12:53:58 +00:00
|
|
|
List the running Pods:
|
2017-11-23 06:38:27 +00:00
|
|
|
|
2017-11-23 12:51:55 +00:00
|
|
|
```shell
|
2017-11-24 11:11:37 +00:00
|
|
|
$ kubectl get pods
|
2017-11-23 12:51:55 +00:00
|
|
|
NAME READY STATUS RESTARTS AGE
|
|
|
|
website 1/1 Running 0 4m
|
|
|
|
```
|
2017-04-04 00:40:48 +00:00
|
|
|
|
|
|
|
**Pod spec after admission controller:**
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{< code file="podpreset-merged.yaml" >}}
|
2017-04-04 00:40:48 +00:00
|
|
|
|
2017-11-23 12:51:55 +00:00
|
|
|
To see above output, run the following command:
|
2017-11-23 06:38:27 +00:00
|
|
|
|
2017-11-23 12:51:55 +00:00
|
|
|
```shell
|
2017-11-24 11:11:37 +00:00
|
|
|
$ kubectl get pod website -o yaml
|
2017-11-23 12:51:55 +00:00
|
|
|
```
|
2017-11-23 06:38:27 +00:00
|
|
|
|
2017-04-04 00:40:48 +00:00
|
|
|
### Pod Spec with `ConfigMap` Example
|
|
|
|
|
2017-07-28 15:23:11 +00:00
|
|
|
This is an example to show how a Pod spec is modified by the Pod Preset
|
2017-06-22 21:21:14 +00:00
|
|
|
that defines a `ConfigMap` for Environment Variables.
|
2017-04-04 00:40:48 +00:00
|
|
|
|
|
|
|
**User submitted pod spec:**
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{< code file="podpreset-pod.yaml" >}}
|
2017-04-04 00:40:48 +00:00
|
|
|
|
|
|
|
**User submitted `ConfigMap`:**
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{< code file="podpreset-configmap.yaml" >}}
|
2017-04-04 00:40:48 +00:00
|
|
|
|
|
|
|
**Example Pod Preset:**
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{< code file="podpreset-allow-db.yaml" >}}
|
2017-04-04 00:40:48 +00:00
|
|
|
|
|
|
|
**Pod spec after admission controller:**
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{< code file="podpreset-allow-db-merged.yaml" >}}
|
2017-04-04 00:40:48 +00:00
|
|
|
|
|
|
|
### ReplicaSet with Pod Spec Example
|
|
|
|
|
|
|
|
The following example shows that only the pod spec is modified by the Pod
|
2017-06-22 21:21:14 +00:00
|
|
|
Preset.
|
2017-04-04 00:40:48 +00:00
|
|
|
|
|
|
|
**User submitted ReplicaSet:**
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{< code file="podpreset-replicaset.yaml" >}}
|
2017-04-04 00:40:48 +00:00
|
|
|
|
|
|
|
**Example Pod Preset:**
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{< code file="podpreset-preset.yaml" >}}
|
2017-04-04 00:40:48 +00:00
|
|
|
|
|
|
|
**Pod spec after admission controller:**
|
|
|
|
|
2017-10-25 20:50:27 +00:00
|
|
|
Note that the ReplicaSet spec was not changed, users have to check individual pods
|
|
|
|
to validate that the PodPreset has been applied.
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{< code file="podpreset-replicaset-merged.yaml" >}}
|
2017-04-04 00:40:48 +00:00
|
|
|
|
|
|
|
### Multiple PodPreset Example
|
|
|
|
|
|
|
|
This is an example to show how a Pod spec is modified by multiple Pod
|
|
|
|
Injection Policies.
|
|
|
|
|
|
|
|
**User submitted pod spec:**
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{< code file="podpreset-pod.yaml" >}}
|
2017-04-04 00:40:48 +00:00
|
|
|
|
|
|
|
**Example Pod Preset:**
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{< code file="podpreset-preset.yaml" >}}
|
2017-04-04 00:40:48 +00:00
|
|
|
|
|
|
|
**Another Pod Preset:**
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{< code file="podpreset-proxy.yaml" >}}
|
2017-04-04 00:40:48 +00:00
|
|
|
|
|
|
|
**Pod spec after admission controller:**
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{< code file="podpreset-multi-merged.yaml" >}}
|
2017-04-04 00:40:48 +00:00
|
|
|
|
|
|
|
### Conflict Example
|
|
|
|
|
2017-07-28 15:23:11 +00:00
|
|
|
This is an example to show how a Pod spec is not modified by the Pod Preset
|
2017-06-22 21:21:14 +00:00
|
|
|
when there is a conflict.
|
2017-04-04 00:40:48 +00:00
|
|
|
|
|
|
|
**User submitted pod spec:**
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{< code file="podpreset-conflict-pod.yaml" >}}
|
2017-04-04 00:40:48 +00:00
|
|
|
|
|
|
|
**Example Pod Preset:**
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{< code file="podpreset-conflict-preset.yaml" >}}
|
2017-04-04 00:40:48 +00:00
|
|
|
|
|
|
|
**Pod spec after admission controller will not change because of the conflict:**
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{< code file="podpreset-conflict-pod.yaml" >}}
|
2017-04-04 00:40:48 +00:00
|
|
|
|
|
|
|
**If we run `kubectl describe...` we can see the event:**
|
|
|
|
|
2017-11-24 11:11:37 +00:00
|
|
|
```shell
|
2017-04-04 00:40:48 +00:00
|
|
|
$ kubectl describe ...
|
|
|
|
....
|
|
|
|
Events:
|
|
|
|
FirstSeen LastSeen Count From SubobjectPath Reason Message
|
2017-09-14 11:18:03 +00:00
|
|
|
Tue, 07 Feb 2017 16:56:12 -0700 Tue, 07 Feb 2017 16:56:12 -0700 1 {podpreset.admission.kubernetes.io/podpreset-allow-database } conflict Conflict on pod preset. Duplicate mountPath /cache.
|
2017-04-04 00:40:48 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
## Deleting a Pod Preset
|
|
|
|
|
|
|
|
Once you don't need a pod preset anymore, you can delete it with `kubectl`:
|
|
|
|
|
|
|
|
```shell
|
|
|
|
$ kubectl delete podpreset allow-database
|
|
|
|
podpreset "allow-database" deleted
|
|
|
|
```
|
|
|
|
|