k8s-ConfigMap

本文详细介绍如何在Kubernetes中使用ConfigMap进行配置管理,包括创建、读取及热更新ConfigMap,以及如何将ConfigMap注入Pod环境中。通过具体实例展示了ConfigMap在实际部署中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

[root@wyl01  cm]# cat env-config.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: env-config
  namespace: default
data:
  log_level: INFO

[root@wyl01  cm]# kubectl get cm env-config -o yaml
apiVersion: v1
data:
  log_level: DEBUG
kind: ConfigMap
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","data":{"log_level":"INFO"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"env-config","namespace":"default"}}
  creationTimestamp: "2019-12-27T02:35:19Z"
  name: env-config
  namespace: default
  resourceVersion: "2028897"
  selfLink: /api/v1/namespaces/default/configmaps/env-config
  uid: 0208b7ce-2811-4bb4-b12c-faafff39c857

 
[root@wyl01  cm]# kubectl describe cm env-config 
Name:         env-config
Namespace:    default
Labels:       <none>
Annotations:  kubectl.kubernetes.io/last-applied-configuration:
                {"apiVersion":"v1","data":{"log_level":"INFO"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"env-config","namespace":"default"}}

Data
====
log_level:
----
INFO
Events:  <none>
[root@wyl01 cm]# cat special-config.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config
  namespace: default
data:
  special.how: very
  special.type: charm

#将两个ConfigMap文件注入到pod环境中

[root@wyl01 cm]# cat test-pod.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: ikubernetes/myapp:v1
      command: ["/bin/sh","-c","env"]
      env:                       
        - name: SPECIAL_LEVEL_KEY
          valueFrom:
            configMapKeyRef:
              name: special-config   
              key: special.how      
        - name: SPECIAL_TYPE_KEY
          valueFrom:
            configMapKeyRef:
              name: special-config
              key: special.type
      envFrom:               
        - configMapRef: 
            name: env-config
  restartPolicy: Never

[root@wyl01 cm]# vim test2-pod.yaml 

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod2
spec:
  containers:
    - name: test-container2
      image: ikubernetes/myapp:v1
      command: ["/bin/sh","-c","echo $(SPECIAL_LEVEL_KEY) $(SPECIAL_TYPE_KEY)"]
      env:
        - name: SPECIAL_LEVEL_KEY
          valueFrom:
            configMapKeyRef:
              name: special-config
              key: special.how
        - name: SPECIAL_TYPE_KEY
          valueFrom:
            configMapKeyRef:
              name: special-config
              key: special.type
  restartPolicy: Never

ConfigMap 的热更新


[root@wyl01 cm]# cat test4-deploy.yml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-nginx
  template:
    metadata:
      labels:
        app: my-nginx
    spec:
      containers:
        - name: my-nginx
          image: ikubernetes/myapp:v1
          ports:
            - containerPort: 80
          volumeMounts:
            - name: config-volume
              mountPath: /etc/config
      volumes:
        - name: config-volume
          configMap:
            name: env-config

等待15s,值会变成INFO

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值