Concept
A ConfigMap is an API object used to store non-confidential data in key-value pairs. Pods can consume ConfigMaps as environment variables, command-line arguments, or as configuration files in a volume.
A ConfigMap allows you to decouple environment-specific configuration from your container images, so that your applications are easily portable.
Caution: ConfigMap does not provide secrecy or encryption.
If the data you want to store are confidential, use a Secret rather than a ConfigMap,
or use additional (third party) tools to keep your data private.
Motivation
Use a ConfigMap for setting configuration data separately from application code.
For example, imagine that you are developing an application that you can run on your own computer (for development) and in the cloud (to handle real traffic). You write the code to look in an environment variable named DATABASE_HOST. Locally, you set that variable to localhost. In the cloud, you set it to refer to a Kubernetes Service that exposes the database component to your cluster. This lets you fetch a container image running in the cloud and debug the exact same code locally if needed.
A ConfigMap is not designed to hold large chunks of data. The data stored in a ConfigMap cannot exceed 1 MiB. If you need to store settings that are larger than this limit, you may want to consider mounting a volume or use a separate database or file service.
Usage
There are four different ways that you can use a ConfigMap to configure a container inside a Pod:
- Inside a container command and args
- Environment variables for a container
- Add a file in read-only volume, for the application to read
- Write code to run inside the Pod that uses the Kubernetes API to read a ConfigMap
Mounted ConfigMaps are updated automatically
When a ConfigMap currently consumed in a volume is updated, projected keys are eventually updated as well. The kubelet checks whether the mounted ConfigMap is fresh on every periodic sync. However, the kubelet uses its local cache for getting the current value of the ConfigMap. The type of the cache is configurable using the ConfigMapAndSecretChangeDetectionStrategy field in the KubeletConfiguration struct. A ConfigMap can be either propagated by watch (default), ttl-based, or by redirecting all requests directly to the API server. 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 the kubelet sync period + cache propagation delay, where the cache propagation delay depends on the chosen cache type (it equals to watch propagation delay, ttl of cache, or zero correspondingly).
ConfigMaps consumed as environment variables are not updated automatically and require a pod restart.
语法规范
create
# kubectl create -f spark-configMap.yaml
The ConfigMap "spark-configMap" is invalid: metadata.name: Invalid value: "spark-configMap": a DNS-1123 subdomain must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character (e.g. 'example.com', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*')
metadata 里 的value值都调整成小写
describe
# kubectl describe configmaps spark-configmap
Name: spark-configmap
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
spark_driver_host:
----
sparkthrift-headless.default.svc.cluster.local
spark_driver_port:
----
1888
spark_executor_instances:
----
1
spark_kubernetes_container_image:
----
zhixingheyitian/spark:spark2.4.1
spark_kubernetes_driver_pod_name:
----
sparkthrift-pod
Events: <none>
get
# kubectl get configmaps spark-configmap -o yaml
apiVersion: v1
data:
spark_driver_host: sparkthrift-headless.default.svc.cluster.local
spark_driver_port: "1888"
spark_executor_instances: "1"
spark_kubernetes_container_image: zhixingheyitian/spark:spark2.4.1
spark_kubernetes_driver_pod_name: sparkthrift-pod
kind: ConfigMap
metadata:
creationTimestamp: "2019-05-23T08:46:01Z"
name: spark-configmap
namespace: default
resourceVersion: "2914613"
selfLink: /api/v1/namespaces/default/configmaps/spark-configmap
uid: 3191a005-7d37-11e9-ba83-001e67d8acca