You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/en/docs/tasks/configure-pod-container/configure-pod-configmap.md
+70-20Lines changed: 70 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,7 @@ card:
10
10
<!-- overview -->
11
11
Many applications rely on configuration which is used during either application initialization or runtime.
12
12
Most of the times there is a requirement to adjust values assigned to configuration parameters.
13
-
ConfigMaps is the kubernetes way to inject application pods with configuration data.
13
+
ConfigMaps are the Kubernetes way to inject application pods with configuration data.
14
14
ConfigMaps allow you to decouple configuration artifacts from image content to keep containerized applications portable. This page provides a series of usage examples demonstrating how to create ConfigMaps and configure Pods using data stored in ConfigMaps.
15
15
16
16
@@ -623,24 +623,6 @@ Like before, all previous files in the `/etc/config/` directory will be deleted.
623
623
You can project keys to specific paths and specific permissions on a per-file
624
624
basis. The [Secrets](/docs/concepts/configuration/secret/#using-secrets-as-files-from-a-pod) user guide explains the syntax.
625
625
626
-
### Optional References
627
-
628
-
A ConfigMap reference may be marked "optional". If the ConfigMap is non-existent, the mounted volume will be empty. If the ConfigMap exists, but the referenced
629
-
key is non-existent the path will be absent beneath the mount point.
630
-
631
-
### Mounted ConfigMaps are updated automatically
632
-
633
-
When a mounted ConfigMap is updated, the projected content is eventually updated too. This applies in the case where an optionally referenced ConfigMap comes into
634
-
existence after a pod has started.
635
-
636
-
Kubelet checks whether the mounted ConfigMap is fresh on every periodic sync. However, it uses its local TTL-based cache for getting the current value of the
637
-
ConfigMap. As a result, the total delay from the moment when the ConfigMap is updated to the moment when new keys are projected to the pod can be as long as
638
-
kubelet sync period (1 minute by default) + TTL of ConfigMaps cache (1 minute by default) in kubelet.
639
-
640
-
{{< note >}}
641
-
A container using a ConfigMap as a [subPath](/docs/concepts/storage/volumes/#using-subpath) volume will not receive ConfigMap updates.
642
-
{{< /note >}}
643
-
644
626
645
627
646
628
<!-- discussion -->
@@ -675,7 +657,7 @@ data:
675
657
676
658
### Restrictions
677
659
678
-
- You must create a ConfigMapbefore referencing it in a Pod specification (unless you mark the ConfigMap as "optional"). If you reference a ConfigMap that doesn't exist, the Pod won't start. Likewise, references to keys that don't exist in the ConfigMap will prevent the pod from starting.
660
+
- You must create the `ConfigMap` object before you reference it in a Pod specification. Alternatively, mark the ConfigMap reference as `optional` in the Pod spec (see [Optional ConfigMaps](#optional-configmaps)). If you reference a ConfigMap that doesn't exist and you don't mark the reference as `optional`, the Pod won't start. Similarly, references to keys that don't exist in the ConfigMap will also prevent the Pod from starting, unless you mark the key references as `optional`.
679
661
680
662
- If you use `envFrom` to define environment variables from ConfigMaps, keys that are considered invalid will be skipped. The pod will be allowed to start, but the invalid names will be recorded in the event log (`InvalidVariableNames`). The log message lists each skipped key. For example:
681
663
@@ -693,7 +675,75 @@ data:
693
675
694
676
- You can't use ConfigMaps for {{< glossary_tooltip text="static pods" term_id="static-pod" >}}, because the Kubelet does not support this.
695
677
678
+
### Optional ConfigMaps
679
+
680
+
You can mark a reference to a ConfigMap as _optional_ in a Pod specification.
681
+
If the ConfigMap doesn't exist, the configuration for which it provides data in the Pod (e.g. environment variable, mounted volume) will be empty.
682
+
If the ConfigMap exists, but the referenced key is non-existent the data is also empty.
683
+
684
+
For example, the following Pod specification marks an environment variable from a ConfigMap as optional:
685
+
686
+
```yaml
687
+
apiVersion: v1
688
+
kind: Pod
689
+
metadata:
690
+
name: dapi-test-pod
691
+
spec:
692
+
containers:
693
+
- name: test-container
694
+
image: gcr.io/google_containers/busybox
695
+
command: [ "/bin/sh", "-c", "env" ]
696
+
env:
697
+
- name: SPECIAL_LEVEL_KEY
698
+
valueFrom:
699
+
configMapKeyRef:
700
+
name: a-config
701
+
key: akey
702
+
optional: true # mark the variable as optional
703
+
restartPolicy: Never
704
+
```
705
+
706
+
If you run this pod, and there is no ConfigMap named `a-config`, the output is empty.
707
+
If you run this pod, and there is a ConfigMap named `a-config` but that ConfigMap doesn't have
708
+
a key named `akey`, the output is also empty. If you do set a value for `akey` in the `a-config`
709
+
ConfigMap, this pod prints that value and then terminates.
710
+
711
+
You can also mark the volumes and files provided by a ConfigMap as optional. Kubernetes always creates the mount paths for the volume, even if the referenced ConfigMap or key doesn't exist. For example, the following
712
+
Pod specification marks a volume that references a ConfigMap as optional:
713
+
714
+
```yaml
715
+
apiVersion: v1
716
+
kind: Pod
717
+
metadata:
718
+
name: dapi-test-pod
719
+
spec:
720
+
containers:
721
+
- name: test-container
722
+
image: gcr.io/google_containers/busybox
723
+
command: [ "/bin/sh", "-c", "ls /etc/config" ]
724
+
volumeMounts:
725
+
- name: config-volume
726
+
mountPath: /etc/config
727
+
volumes:
728
+
- name: config-volume
729
+
configMap:
730
+
name: no-config
731
+
optional: true # mark the source ConfigMap as optional
732
+
restartPolicy: Never
733
+
```
734
+
735
+
### Mounted ConfigMaps are updated automatically
736
+
737
+
When a mounted ConfigMap is updated, the projected content is eventually updated too. This applies in the case where an optionally referenced ConfigMap comes into
738
+
existence after a pod has started.
696
739
740
+
The kubelet checks whether the mounted ConfigMap is fresh on every periodic sync. However, it uses its local TTL-based cache for getting the current value of the
741
+
ConfigMap. As a result, the total delay from the moment when the ConfigMap is updated to the moment when new keys are projected to the pod can be as long as
742
+
kubelet sync period (1 minute by default) + TTL of ConfigMaps cache (1 minute by default) in kubelet.
743
+
744
+
{{< note >}}
745
+
A container using a ConfigMap as a [subPath](/docs/concepts/storage/volumes/#using-subpath) volume will not receive ConfigMap updates.
0 commit comments