Kubernetes 之 ConfigMap

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值