Skip to content

Commit b1490ef

Browse files
committed
Improve ConfigMap concept
This PR adds some missing information to the ConfigMap concept page.
1 parent 0b9a6fd commit b1490ef

File tree

1 file changed

+47
-32
lines changed

1 file changed

+47
-32
lines changed

content/en/docs/concepts/configuration/configmap.md

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,45 @@ or use additional (third party) tools to keep your data private.
1616
{{< /caution >}}
1717

1818

19-
2019
<!-- body -->
2120
## Motivation
2221

2322
Use a ConfigMap for setting configuration data separately from application code.
2423

2524
For example, imagine that you are developing an application that you can run on your
2625
own computer (for development) and in the cloud (to handle real traffic).
27-
You write the code to
28-
look in an environment variable named `DATABASE_HOST`. Locally, you set that variable
29-
to `localhost`. In the cloud, you set it to refer to a Kubernetes
30-
{{< glossary_tooltip text="Service" term_id="service" >}} that exposes the database
31-
component to your cluster.
32-
26+
You write the code to look in an environment variable named `DATABASE_HOST`.
27+
Locally, you set that variable to `localhost`. In the cloud, you set it to
28+
refer to a Kubernetes {{< glossary_tooltip text="Service" term_id="service" >}}
29+
that exposes the database component to your cluster.
3330
This lets you fetch a container image running in the cloud and
3431
debug the exact same code locally if needed.
3532

33+
A ConfigMap is not designed to hold large chunks of data. The data stored in a
34+
ConfigMap cannot exeed 1 MiB. If you need to store settings that are
35+
larger than this limit, you may want to consider mounting a volume or use a
36+
separate database or file service.
37+
3638
## ConfigMap object
3739

3840
A ConfigMap is an API [object](/docs/concepts/overview/working-with-objects/kubernetes-objects/)
3941
that lets you store configuration for other objects to use. Unlike most
40-
Kubernetes objects that have a `spec`, a ConfigMap has a `data` section to
41-
store items (keys) and their values.
42+
Kubernetes objects that have a `spec`, a ConfigMap has `data` and `binaryData`
43+
fields. These fields accepts key-value pairs as their values. Both the `data`
44+
field and the `binaryData` are optional. The `data` field is designed to
45+
contain UTF-8 byte sequences while the `binaryData` field is designed to
46+
contain binary data.
4247

4348
The name of a ConfigMap must be a valid
4449
[DNS subdomain name](/docs/concepts/overview/working-with-objects/names#dns-subdomain-names).
4550

51+
Each key under the `data` or the `binaryData` field must consist of
52+
alphanumeric characters, `-`, `_` or `.`. The keys stored in `data` must not
53+
overlap with the keys in the `binaryData` field.
54+
55+
Starting from v1.19, you can add an `immutable` field to a ConfigMap
56+
definition to create an [immutable ConfigMap](#configmap-immutable).
57+
4658
## ConfigMaps and Pods
4759

4860
You can write a Pod `spec` that refers to a ConfigMap and configures the container(s)
@@ -62,7 +74,7 @@ data:
6274
# property-like keys; each key maps to a simple value
6375
player_initial_lives: "3"
6476
ui_properties_file_name: "user-interface.properties"
65-
#
77+
6678
# file-like keys
6779
game.properties: |
6880
enemy.types=aliens,monsters
@@ -94,6 +106,7 @@ when that happens. By accessing the Kubernetes API directly, this
94106
technique also lets you access a ConfigMap in a different namespace.
95107
96108
Here's an example Pod that uses values from `game-demo` to configure a Pod:
109+
97110
```yaml
98111
apiVersion: v1
99112
kind: Pod
@@ -134,7 +147,6 @@ spec:
134147
path: "user-interface.properties"
135148
```
136149

137-
138150
A ConfigMap doesn't differentiate between single line property values and
139151
multi-line file-like values.
140152
What matters is how Pods and other objects consume those values.
@@ -153,7 +165,6 @@ ConfigMaps can be mounted as data volumes. ConfigMaps can also be used by other
153165
parts of the system, without being directly exposed to the Pod. For example,
154166
ConfigMaps can hold data that other parts of the system should use for configuration.
155167

156-
{{< note >}}
157168
The most common way to use ConfigMaps is to configure settings for
158169
containers running in a Pod in the same namespace. You can also use a
159170
ConfigMap separately.
@@ -162,16 +173,23 @@ For example, you
162173
might encounter {{< glossary_tooltip text="addons" term_id="addons" >}}
163174
or {{< glossary_tooltip text="operators" term_id="operator-pattern" >}} that
164175
adjust their behavior based on a ConfigMap.
165-
{{< /note >}}
166176

167177
### Using ConfigMaps as files from a Pod
168178

169179
To consume a ConfigMap in a volume in a Pod:
170180

171-
1. Create a config map or use an existing one. Multiple Pods can reference the same config map.
172-
1. Modify your Pod definition to add a volume under `.spec.volumes[]`. Name the volume anything, and have a `.spec.volumes[].configMap.name` field set to reference your ConfigMap object.
173-
1. Add a `.spec.containers[].volumeMounts[]` to each container that needs the config map. Specify `.spec.containers[].volumeMounts[].readOnly = true` and `.spec.containers[].volumeMounts[].mountPath` to an unused directory name where you would like the config map to appear.
174-
1. Modify your image or command line so that the program looks for files in that directory. Each key in the config map `data` map becomes the filename under `mountPath`.
181+
1. Create a ConfigMap or use an existing one. Multiple Pods can reference the
182+
same ConfigMap.
183+
1. Modify your Pod definition to add a volume under `.spec.volumes[]`. Name
184+
the volume anything, and have a `.spec.volumes[].configMap.name` field set
185+
to reference your ConfigMap object.
186+
1. Add a `.spec.containers[].volumeMounts[]` to each container that needs the
187+
ConfigMap. Specify `.spec.containers[].volumeMounts[].readOnly = true` and
188+
`.spec.containers[].volumeMounts[].mountPath` to an unused directory name
189+
where you would like the ConfigMap to appear.
190+
1. Modify your image or command line so that the program looks for files in
191+
that directory. Each key in the ConfigMap `data` map becomes the filename
192+
under `mountPath`.
175193

176194
This is an example of a Pod that mounts a ConfigMap in a volume:
177195

@@ -201,8 +219,8 @@ own `volumeMounts` block, but only one `.spec.volumes` is needed per ConfigMap.
201219

202220
#### Mounted ConfigMaps are updated automatically
203221

204-
When a config map currently consumed in a volume is updated, projected keys are eventually updated as well.
205-
The kubelet checks whether the mounted config map is fresh on every periodic sync.
222+
When a ConfigMap currently consumed in a volume is updated, projected keys are eventually updated as well.
223+
The kubelet checks whether the mounted ConfigMap is fresh on every periodic sync.
206224
However, the kubelet uses its local cache for getting the current value of the ConfigMap.
207225
The type of the cache is configurable using the `ConfigMapAndSecretChangeDetectionStrategy` field in
208226
the [KubeletConfiguration struct](https://github.com/kubernetes/kubernetes/blob/{{< param "docsbranch" >}}/staging/src/k8s.io/kubelet/config/v1beta1/types.go).
@@ -224,12 +242,13 @@ data has the following advantages:
224242

225243
- protects you from accidental (or unwanted) updates that could cause applications outages
226244
- improves performance of your cluster by significantly reducing load on kube-apiserver, by
227-
closing watches for config maps marked as immutable.
245+
closing watches for ConfigMaps marked as immutable.
246+
247+
This feature is controlled by the `ImmutableEphemeralVolumes`
248+
[feature gate](/docs/reference/command-line-tools-reference/feature-gates/).
249+
You can create an immutable ConfigMap by setting the `immutable` field to `true`.
250+
For example:
228251

229-
This feature is controlled by the `ImmutableEphemeralVolumes` [feature
230-
gate](/docs/reference/command-line-tools-reference/feature-gates/),
231-
which is enabled by default since v1.19. You can create an immutable
232-
ConfigMap by setting the `immutable` field to `true`. For example,
233252
```yaml
234253
apiVersion: v1
235254
kind: ConfigMap
@@ -240,17 +259,13 @@ data:
240259
immutable: true
241260
```
242261

243-
{{< note >}}
244-
Once a ConfigMap or Secret is marked as immutable, it is _not_ possible to revert this change
245-
nor to mutate the contents of the `data` field. You can only delete and recreate the ConfigMap.
246-
Existing Pods maintain a mount point to the deleted ConfigMap - it is recommended to recreate
247-
these pods.
248-
{{< /note >}}
249-
262+
Once a ConfigMap is marked as immutable, it is _not_ possible to revert this change
263+
nor to mutate the contents of the `data` or the `binaryData` field. You can
264+
only delete and recreate the ConfigMap. Because existing Pods maintain a mount point
265+
to the deleted ConfigMap, it is recommended to recreate these pods.
250266

251267
## {{% heading "whatsnext" %}}
252268

253-
254269
* Read about [Secrets](/docs/concepts/configuration/secret/).
255270
* Read [Configure a Pod to Use a ConfigMap](/docs/tasks/configure-pod-container/configure-pod-configmap/).
256271
* Read [The Twelve-Factor App](https://12factor.net/) to understand the motivation for

0 commit comments

Comments
 (0)