作用:存盤不加密資料到 etcd,讓 Pod 以變數或者 Volume 掛載到容器中
場景:組態檔
創建組態檔
redis.properties
redis.host=127.0.0.1
redis.port=6379
redis.password=123456
創建 ConfigMap
# 根據 redis.properties 創建 redis-config
[root@k8smaster ~]# kubectl create configmap redis-config --from-file=redis.properties
# 查看 configmap
[root@k8smaster ~]# kubectl get cm
# 查看 redis-config 的詳情
[root@k8smaster ~]# kubectl describe cm redis-config
以 Volume 的形式進行掛載到 pod 容器中
cm.yaml
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: busybox
image: busybox
command: [ "/bin/sh","-c","cat /etc/config/redis.properties" ]
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: redis-config
restartPolicy: Never
# 創建 yaml檔案
[root@k8smaster ~]# vi cm.yaml
# 創建pod
[root@k8smaster ~]# kubectl apply -f cm.yaml
# 啟動后可以查看日志
[root@k8smaster ~]# kubectl logs mypod
以變數的形式進行掛載
創建 yaml,宣告變數資訊 configmap 創建
myconfig.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: myconfig
namespace: default
data:
special.level: info
special.type: hello
config-var.yaml
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: busybox
image: busybox
command: [ "/bin/sh", "-c", "echo $(LEVEL) $(TYPE)" ]
env:
- name: LEVEL
valueFrom:
configMapKeyRef:
name: myconfig
key: special.level
- name: TYPE
valueFrom:
configMapKeyRef:
name: myconfig
key: special.type
restartPolicy: Never
[root@k8smaster ~]# vi myconfig.yaml
[root@k8smaster ~]# kubectl apply -f myconfig.yaml
# 以變數形式進行掛載
[root@k8smaster ~]# vi config-var.yaml
[root@k8smaster ~]# kubectl apply -f config-var.yaml
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/539042.html
標籤:其他
上一篇:Linux 基礎-檔案及目錄管理
