website/content/en/docs/tasks/inject-data-application/podpreset.md

165 lines
3.7 KiB
Markdown
Raw Normal View History

2017-04-04 00:40:48 +00:00
---
reviewers:
2017-04-04 00:40:48 +00:00
- jessfraz
title: Inject Information into Pods Using a PodPreset
weight: 60
2017-04-04 00:40:48 +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
{{< toc >}}
2017-04-04 00:40:48 +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
Preset.
2017-04-04 00:40:48 +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
{{< 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:**
{{< 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
This is an example to show how a Pod spec is modified by the Pod Preset
that defines a `ConfigMap` for Environment Variables.
2017-04-04 00:40:48 +00:00
**User submitted pod spec:**
{{< code file="podpreset-pod.yaml" >}}
2017-04-04 00:40:48 +00:00
**User submitted `ConfigMap`:**
{{< code file="podpreset-configmap.yaml" >}}
2017-04-04 00:40:48 +00:00
**Example Pod Preset:**
{{< code file="podpreset-allow-db.yaml" >}}
2017-04-04 00:40:48 +00:00
**Pod spec after admission controller:**
{{< 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
Preset.
2017-04-04 00:40:48 +00:00
**User submitted ReplicaSet:**
{{< code file="podpreset-replicaset.yaml" >}}
2017-04-04 00:40:48 +00:00
**Example Pod Preset:**
{{< 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.
{{< 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:**
{{< code file="podpreset-pod.yaml" >}}
2017-04-04 00:40:48 +00:00
**Example Pod Preset:**
{{< code file="podpreset-preset.yaml" >}}
2017-04-04 00:40:48 +00:00
**Another Pod Preset:**
{{< code file="podpreset-proxy.yaml" >}}
2017-04-04 00:40:48 +00:00
**Pod spec after admission controller:**
{{< code file="podpreset-multi-merged.yaml" >}}
2017-04-04 00:40:48 +00:00
### Conflict Example
This is an example to show how a Pod spec is not modified by the Pod Preset
when there is a conflict.
2017-04-04 00:40:48 +00:00
**User submitted pod spec:**
{{< code file="podpreset-conflict-pod.yaml" >}}
2017-04-04 00:40:48 +00:00
**Example Pod Preset:**
{{< 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:**
{{< 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
```