ConfifigMap 的创建
Ⅰ、使用目录创建
Ⅱ、使用文件创建
Ⅲ、使用字面值创建
使用目录创建
在/root/test/configmap下创建两个文件,内容如下
[root@xgcloud-ops-k8s-cluster-2 configmap]# cat game.properties enemies=aliens lives=3 enemies.cheat=true enemies.cheat.level=noGoodRotten secret.code.passphrase=UUDDLRLRBABAS secret.code.allowed=true secret.code.lives=30 [root@xgcloud-ops-k8s-cluster-2 configmap]# cat ui.properties color.good=purple color.bad=yellow allow.textmode=true how.nice.to.look=fairlyNice
创建命令:
kubectl create configmap game-config --from-file=/root/test/configmap
使用文件创建
创建命令:
kubectl create configmap game-config-2 --from-file=/root/test/configmap/game.properties
使用字面值创建
kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm
Pod 中使用 ConfifigMap
通过数据卷插件使用ConfifigMap
pod.yaml:
apiVersion: v1 kind: Pod metadata: name: pod-cm-2 namespace: default labels: app: myapp tier: frontend annotations: test.com/created-by: "cluster admin" spec: containers: - name: myapp image: ikubernetes/myapp:v1 ports: - name: http containerPort: 80 volumeMounts: - name: nginxconf mountPath: /etc/nginx/config.d/ #挂载点不存在,Pod会自动创建. readOnly: true #不能让容器修改配置的内容。 volumes: - name: nginxconf #定义存储卷的名字为nginxconf configMap: name: game-config #要挂载nginx-config这个configMap
kubectl apply -f pod.yaml
kubectl exec -it pod/pod-cm-2 sh
相当于已文件的形式引入进去了
使用 ConfifigMap 来替代环境变量
pod1.yaml:
apiVersion: v1 kind: Pod metadata: name: pod-cm-1 namespace: default labels: app: myapp tier: frontend annotations: test.com/created-by: "cluster admin" spec: containers: - name: myapp image: ikubernetes/myapp:v1 ports: - name: http containerPort: 80 env: - name: NGINX_SERVER_PORT valueFrom: configMapKeyRef: name: special-config key: special.type - name: NGINX_SERVER_NAME valueFrom: configMapKeyRef: name: special-config key: special.how
kubectl apply -f pod1.yaml
kubectl exec -it pod/pod-cm-1 sh
ConfifigMap 的热更新
修改configmap
kubectl edit configmap log-config
kubectl patch deployment my-nginx --patch '{"spec": {"template": {"metadata": {"annotations": {"version/config": "20190411" }}}}}'