上一篇学习了通过args方式还是env方式都是将配置应用,配置参数硬编码在POD配置文件中,实际应用部署的过程中,我们希望配置参数和POD配置能够解藕,今天我们就看一下如何用ConfigMap配置应用。
创建一个ConfigMap
1
| kubectl create configmap fortune-configmap --from-literal=sleep-interval=60
|
创建成功以后可以通过kubectl命令查询configmap的配置。
kubectl get configmap fortune-configmap -o yaml
查询结果如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| apiVersion: v1 data: sleep-interval: "60" kind: ConfigMap metadata: creationTimestamp: "2020-09-29T12:29:40Z" managedFields: - apiVersion: v1 fieldsType: FieldsV1 fieldsV1: f:data: .: {} f:sleep-interval: {} manager: kubectl-create operation: Update time: "2020-09-29T12:29:40Z" name: fortune-configmap namespace: default resourceVersion: "2434" selfLink: /api/v1/namespaces/default/configmaps/fortune-configmap uid: a6562e65-1431-47b1-b153-e4c270561a2c
|
上述查询到信息将metadata中只保留名称就可以创建一个简单的ConfigMap配置文件,使用如下命令即可通过文件创建ConfigMap
kubectl create -f fortune-congfigmap.yaml
在POD文件中使用CongfigMap配置应用
- 使用环境变量配置应用 修改POD配置文件,使用ConfigMap配置env参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| apiVersion: v1 kind: Pod metadata: name: fortune-env labels: app: fortune-env spec: containers: - name: html-gen image: 172.17.0.1:5000/fortune:env env: - name: INTERVAL valueFrom: configMapKeyRef: name: fortune-configmap key: sleep-interval volumeMounts: - name: html mountPath: /var/htdocs - name: log mountPath: /var/log/fortune - name: web-server image: 172.17.0.1:500/nginx:alpine volumeMounts: - name: html mountPath: /usr/share/nginx/html readOnly: true ports: - containerPort: 80 protocol: TCP - name: log-server image: 172.17.0.1:5000/nginx:log volumeMounts: - name: log mountPath: /usr/share/nginx/log readOnly: true ports: - containerPort: 8080 protocol: TCP volumes: - name: html emptyDir: {} - name: log emptyDir: {}
|
重新创建PODkubectl create -f fortune-pod-env.yaml
,
此时再查询fortune每个60秒更新一次。实际应用部署时,也可以修改ConfigMap的配置,当新新建POD时会使用新的ConfigMap值。
和使用环境变量配置POD类似,修改POD配置文件,使用ConfigMap配置args参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| apiVersion: v1 kind: Pod metadata: name: fortune-args labels: app: fortune-args spec: containers: - name: html-gen image: 172.17.0.1:5000/fortune:args env: - name: INTERVAL valueFrom: configMapKeyRef: name: fortune-configmap key: sleep-interval args: [$(INTERVAL)] volumeMounts: - name: html mountPath: /var/htdocs - name: log mountPath: /var/log/fortune - name: web-server image: 172.17.0.1:500/nginx:alpine volumeMounts: - name: html mountPath: /usr/share/nginx/html readOnly: true ports: - containerPort: 80 protocol: TCP - name: log-server image: 172.17.0.1:5000/nginx:log volumeMounts: - name: log mountPath: /usr/share/nginx/log readOnly: true ports: - containerPort: 8080 protocol: TCP volumes: - name: html emptyDir: {} - name: log emptyDir: {}
|
创建PODkubectl create -f fortune-pod-args.yaml
,
此时查询fortune每个60秒更新一次。
此时,如果删除旧的ConfigMap,重新配置一个新的ConfigMap,再同时删除frotune-env
pod和fortune-args pod并重新新建POD时会使用新的ConfigMap值。
ConfigMap可以配置的资源
从文件、文件夹和字符创建的ConfigMap