k8s的ConfigMap使用

1 环境变量模式
结论:不会自动更新,只能重启pod。
cm.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: cm-test
  namespace: default
data:
  allowed: '"true"'
  lives: "3"
  qq: "19122"


dep.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: pod1
  name: pod1
spec:
  replicas: 1
  selector:
    matchLabels:
      app: pod1
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: pod1
    spec:
      containers:
      - image: nginx
        imagePullPolicy: IfNotPresent
        name: nginx
        env:
          - name: SPECIAL_LEVEL_KEY
            valueFrom:
              configMapKeyRef:
                name: cm-test
                key: qq


kubectl get pods 
kubectl  exec -it podID– bin/sh    
echo $SPECIAL_LEVEL_KEY   打印变量。
修改cm之后kubectl apply -f cm.yml 
无论等待多长时间依然不会更新。

2 挂载数据卷模式

cat cm.yml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: cm-test
  namespace: default
data:
  allowed: '"true"'
#  enemies: aliens
  lives: "3"
  qq: "19122"

pod.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: pod1
  name: pod1
spec:
  replicas: 1
  selector:
    matchLabels:
      app: pod1
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: pod1
    spec:
      containers:
      - image: nginx
        imagePullPolicy: IfNotPresent
        name: nginx
        volumeMounts:
        - name: config-volume
          mountPath: /etc/config                
      volumes:
        - name: config-volume
          configMap:        
            name: cm-test
注意:这里的volumes是在spec下面。
进入容器:
kubectl  exec -it pod1-5495c9b4dc-vgpcm -- /bin/sh
cd /etc/config  
cat qq  就可以看到qq的值了。
修改cm之后,kubectl apply -f cm.yml 
等待一分钟,再次执行 cat qq就会看到该值已经改变了。
但是如果放在subpath,依然不会自动更新。

 

posted @ 2022-12-19 17:57  天城1324  阅读(398)  评论(0)    收藏  举报