k3s/docs/design/configmap.md

324 lines
9.9 KiB
Markdown
Raw Normal View History

2015-04-04 18:25:14 +00:00
<!-- BEGIN MUNGE: UNVERSIONED_WARNING -->
<!-- BEGIN STRIP_FOR_RELEASE -->
<img src="http://kubernetes.io/img/warning.png" alt="WARNING"
width="25" height="25">
<img src="http://kubernetes.io/img/warning.png" alt="WARNING"
width="25" height="25">
<img src="http://kubernetes.io/img/warning.png" alt="WARNING"
width="25" height="25">
<img src="http://kubernetes.io/img/warning.png" alt="WARNING"
width="25" height="25">
<img src="http://kubernetes.io/img/warning.png" alt="WARNING"
width="25" height="25">
<h2>PLEASE NOTE: This document applies to the HEAD of the source tree</h2>
If you are using a released version of Kubernetes, you should
refer to the docs that go with that version.
Documentation for other releases can be found at
[releases.k8s.io](http://releases.k8s.io).
</strong>
--
<!-- END STRIP_FOR_RELEASE -->
<!-- END MUNGE: UNVERSIONED_WARNING -->
# Generic Configuration Object
## Abstract
2016-01-21 15:47:44 +00:00
The `ConfigMap` API resource stores data used for the configuration of applications deployed on
Kubernetes.
2015-04-04 18:25:14 +00:00
2016-01-21 15:47:44 +00:00
The main focus of this resource is to:
2015-04-04 18:25:14 +00:00
2016-01-21 15:47:44 +00:00
* Provide dynamic distribution of configuration data to deployed applications.
2015-04-04 18:25:14 +00:00
* Encapsulate configuration information and simplify `Kubernetes` deployments.
* Create a flexible configuration model for `Kubernetes`.
## Motivation
A `Secret`-like API resource is needed to store configuration data that pods can consume.
Goals of this design:
1. Describe a `ConfigMap` API resource
2. Describe the semantics of consuming `ConfigMap` as environment variables
3. Describe the semantics of consuming `ConfigMap` as files in a volume
2015-04-04 18:25:14 +00:00
## Use Cases
1. As a user, I want to be able to consume configuration data as environment variables
2. As a user, I want to be able to consume configuration data as files in a volume
3. As a user, I want my view of configuration data in files to be eventually consistent with changes
to the data
### Consuming `ConfigMap` as Environment Variables
2015-04-04 18:25:14 +00:00
Many programs read their configuration from environment variables. `ConfigMap` should be possible
to consume in environment variables. The rough series of events for consuming `ConfigMap` this way
2015-04-04 18:25:14 +00:00
is:
1. A `ConfigMap` object is created
2015-04-04 18:25:14 +00:00
2. A pod that consumes the configuration data via environment variables is created
3. The pod is scheduled onto a node
4. The kubelet retrieves the `ConfigMap` resource(s) referenced by the pod and starts the container
2015-04-04 18:25:14 +00:00
processes with the appropriate data in environment variables
### Consuming `ConfigMap` in Volumes
2015-04-04 18:25:14 +00:00
Many programs read their configuration from configuration files. `ConfigMap` should be possible
to consume in a volume. The rough series of events for consuming `ConfigMap` this way
2015-04-04 18:25:14 +00:00
is:
1. A `ConfigMap` object is created
2. A new pod using the `ConfigMap` via the volume plugin is created
2015-04-04 18:25:14 +00:00
3. The pod is scheduled onto a node
4. The Kubelet creates an instance of the volume plugin and calls its `Setup()` method
5. The volume plugin retrieves the `ConfigMap` resource(s) referenced by the pod and projects
2015-04-04 18:25:14 +00:00
the appropriate data into the volume
### Consuming `ConfigMap` Updates
2015-04-04 18:25:14 +00:00
Any long-running system has configuration that is mutated over time. Changes made to configuration
data must be made visible to pods consuming data in volumes so that they can respond to those
changes.
The `resourceVersion` of the `ConfigMap` object will be updated by the API server every time the
2015-04-04 18:25:14 +00:00
object is modified. After an update, modifications will be made visible to the consumer container:
1. A `ConfigMap` object is created
2. A new pod using the `ConfigMap` via the volume plugin is created
2015-04-04 18:25:14 +00:00
3. The pod is scheduled onto a node
4. During the sync loop, the Kubelet creates an instance of the volume plugin and calls its
`Setup()` method
5. The volume plugin retrieves the `ConfigMap` resource(s) referenced by the pod and projects
2015-04-04 18:25:14 +00:00
the appropriate data into the volume
6. The `ConfigMap` referenced by the pod is updated
2015-04-04 18:25:14 +00:00
7. During the next iteration of the `syncLoop`, the Kubelet creates an instance of the volume plugin
and calls its `Setup()` method
8. The volume plugin projects the updated data into the volume atomically
It is the consuming pod's responsibility to make use of the updated data once it is made visible.
Because environment variables cannot be updated without restarting a container, configuration data
consumed in environment variables will not be updated.
### Advantages
* Easy to consume in pods; consumer-agnostic
* Configuration data is persistent and versioned
* Consumers of configuration data in volumes can respond to changes in the data
## Proposed Design
### API Resource
2016-01-21 15:47:44 +00:00
The `ConfigMap` resource will be added to the main API:
2015-04-04 18:25:14 +00:00
```go
package api
// ConfigMap holds configuration data for pods to consume.
type ConfigMap struct {
2015-04-04 18:25:14 +00:00
TypeMeta `json:",inline"`
ObjectMeta `json:"metadata,omitempty"`
// Data contains the configuration data. Each key must be a valid DNS_SUBDOMAIN or leading
// dot followed by valid DNS_SUBDOMAIN.
Data map[string]string `json:"data,omitempty"`
}
type ConfigMapList struct {
2015-04-04 18:25:14 +00:00
TypeMeta `json:",inline"`
ListMeta `json:"metadata,omitempty"`
Items []ConfigMap `json:"items"`
2015-04-04 18:25:14 +00:00
}
```
A `Registry` implementation for `ConfigMap` will be added to `pkg/registry/configmap`.
2015-04-04 18:25:14 +00:00
### Environment Variables
The `EnvVarSource` will be extended with a new selector for `ConfigMap`:
2015-04-04 18:25:14 +00:00
```go
package api
// EnvVarSource represents a source for the value of an EnvVar.
type EnvVarSource struct {
// other fields omitted
// Specifies a ConfigMap key
ConfigMap *ConfigMapSelector `json:"configMap,omitempty"`
2015-04-04 18:25:14 +00:00
}
// ConfigMapSelector selects a key of a ConfigMap.
type ConfigMapSelector struct {
// The name of the ConfigMap to select a key from.
ConfigMapName string `json:"configMapName"`
// The key of the ConfigMap to select.
2015-04-04 18:25:14 +00:00
Key string `json:"key"`
}
```
### Volume Source
A new `ConfigMapVolumeSource` type of volume source containing the `ConfigMap` object will be
2015-11-20 04:19:46 +00:00
added to the `VolumeSource` struct in the API:
```go
package api
type VolumeSource struct {
// other fields omitted
ConfigMap *ConfigMapVolumeSource `json:"configMap,omitempty"`
2015-11-20 04:19:46 +00:00
}
2016-01-21 15:47:44 +00:00
// Represents a volume that holds configuration data.
type ConfigMapVolumeSource struct {
2016-01-21 15:47:44 +00:00
LocalObjectReference `json:",inline"`
// A list of keys to project into the volume.
// If unspecified, each key-value pair in the Data field of the
// referenced ConfigMap will be projected into the volume as a file whose name
// is the key and content is the value.
// If specified, the listed keys will be project into the specified paths, and
// unlisted keys will not be present.
Items []KeyToPath `json:"items,omitempty"`
2015-11-20 04:19:46 +00:00
}
2016-01-21 15:47:44 +00:00
// Represents a mapping of a key to a relative path.
type KeyToPath struct {
// The name of the key to select
Key string `json:"key"`
2015-11-20 04:19:46 +00:00
// The relative path name of the file to be created.
// Must not be absolute or contain the '..' path. Must be utf-8 encoded.
// The first item of the relative path must not start with '..'
Path string `json:"path"`
}
```
**Note:** The update logic used in the downward API volume plug-in will be extracted and re-used in
the volume plug-in for `ConfigMap`.
2015-04-04 18:25:14 +00:00
2016-01-21 15:47:44 +00:00
### Changes to Secret
We will update the Secret volume plugin to have a similar API to the new ConfigMap volume plugin.
The secret volume plugin will also begin updating secret content in the volume when secrets change.
2015-04-04 18:25:14 +00:00
## Examples
#### Consuming `ConfigMap` as Environment Variables
2015-04-04 18:25:14 +00:00
```yaml
2016-01-21 15:47:44 +00:00
apiVersion: v1
kind: ConfigMap
2015-04-04 18:25:14 +00:00
metadata:
name: etcd-env-config
data:
2016-01-21 15:47:44 +00:00
number-of-members: 1
initial-cluster-state: new
initial-cluster-token: DUMMY_ETCD_INITIAL_CLUSTER_TOKEN
discovery-token: DUMMY_ETCD_DISCOVERY_TOKEN
discovery-url: http://etcd-discovery:2379
etcdctl-peers: http://etcd:2379
2015-04-04 18:25:14 +00:00
```
This pod consumes the `ConfigMap` as environment variables:
2015-04-04 18:25:14 +00:00
```yaml
apiVersion: v1
kind: Pod
metadata:
name: config-env-example
spec:
containers:
- name: etcd
image: openshift/etcd-20-centos7
ports:
- containerPort: 2379
protocol: TCP
- containerPort: 2380
protocol: TCP
env:
- name: ETCD_NUM_MEMBERS
valueFrom:
configMap:
configMapName: etcd-env-config
2016-01-21 15:47:44 +00:00
key: number-of-members
2015-04-04 18:25:14 +00:00
- name: ETCD_INITIAL_CLUSTER_STATE
valueFrom:
configMap:
configMapName: etcd-env-config
2016-01-21 15:47:44 +00:00
key: initial-cluster-state
2015-04-04 18:25:14 +00:00
- name: ETCD_DISCOVERY_TOKEN
valueFrom:
configMap:
configMapName: etcd-env-config
2016-01-21 15:47:44 +00:00
key: discovery-token
2015-04-04 18:25:14 +00:00
- name: ETCD_DISCOVERY_URL
valueFrom:
configMap:
configMapName: etcd-env-config
2016-01-21 15:47:44 +00:00
key: discovery-url
2015-04-04 18:25:14 +00:00
- name: ETCDCTL_PEERS
valueFrom:
configMap:
configMapName: etcd-env-config
2016-01-21 15:47:44 +00:00
key: etcdctl-peers
2015-04-04 18:25:14 +00:00
```
2016-01-21 15:47:44 +00:00
#### Consuming `ConfigMap` as Volumes
2015-11-20 04:19:46 +00:00
`redis-volume-config` is intended to be used as a volume containing a config file:
```yaml
apiVersion: extensions/v1beta1
kind: ConfigMap
2015-11-20 04:19:46 +00:00
metadata:
name: redis-volume-config
data:
redis.conf: "pidfile /var/run/redis.pid\nport6379\ntcp-backlog 511\n databases 1\ntimeout 0\n"
```
The following pod consumes the `redis-volume-config` in a volume:
```yaml
apiVersion: v1
kind: Pod
metadata:
name: config-volume-example
spec:
containers:
- name: redis
image: kubernetes/redis
command: "redis-server /mnt/config-map/etc/redis.conf"
2015-11-20 04:19:46 +00:00
ports:
- containerPort: 6379
volumeMounts:
- name: config-map-volume
mountPath: /mnt/config-map
2015-11-20 04:19:46 +00:00
volumes:
2016-01-21 15:47:44 +00:00
- name: config-map-volume
configMap:
name: redis-volume-config
items:
- path: "etc/redis.conf"
key: redis.conf
2015-11-20 04:19:46 +00:00
```
2016-01-21 15:47:44 +00:00
## Future Improvements
2015-04-04 18:25:14 +00:00
In the future, we may add the ability to specify an init-container that can watch the volume
contents for updates and respond to changes when they occur.
<!-- BEGIN MUNGE: GENERATED_ANALYTICS -->
2016-01-21 15:47:44 +00:00
[![Analytics](https://kubernetes-site.appspot.com/UA-36037335-10/GitHub/docs/design/configmap.md?pixel)]()
2015-04-04 18:25:14 +00:00
<!-- END MUNGE: GENERATED_ANALYTICS -->