ConfigMap
configmap是一種比較特殊的存儲卷,它的主要作用是用來存儲配置信息的
創建configmap.yaml,內容如下
apiVersion: v1 kind: ConfigMap metadata: name: configmap namespace: dev data: info: username:admin password:123456
接下來,使用此配置文件創建configmap
[root@master ~]# vim configmap.yaml [root@master ~]# kubectl create -f configmap.yaml configmap/configmap created
[root@master ~]# kubectl describe cm configmap -n dev Name: configmap Namespace: dev Labels: <none> Annotations: <none> Data ==== info: ---- username:admin password:123456 Events: <none>
接下來創建一個pod-configmap.yaml,將上面創建的configmap掛載進去
apiVersion: v1 kind: Pod metadata: name: pod-configmap namespace: dev spec: containers: - name: nginx image: nginx:1.17.1 volumeMounts: - name: config mountPath: /configmap/config volumes: - name: config configMap: name: configmap
使用配置文件
[root@master ~]# vim pod-configmap.yaml [root@master ~]# kubectl create -f pod-configmap.yaml pod/pod-configmap created [root@master ~]# kubectl get pod pod-configmap -n dev NAME READY STATUS RESTARTS AGE pod-configmap 1/1 Running 0 32s
進入容器,可以看見映射已經成功,每個configmap都映射成了一個目錄
[root@master ~]# kubectl exec -it pod-configmap -n dev /bin/sh # cd /configmap/config # ls info # more info username:admin password:123456
# exit
編輯configmap,將password改為123456789
[root@master ~]# kubectl edit cm configmap -n dev # Please edit the object below. Lines beginning with a '#' will be ignored, # and an empty file will abort the edit. If an error occurs while saving this file will be # reopened with the relevant failures. # apiVersion: v1 data: info: username:admin password:123456789 kind: ConfigMap metadata: creationTimestamp: "2021-08-18T03:58:59Z" name: configmap namespace: dev resourceVersion: "171455" selfLink: /api/v1/namespaces/dev/configmaps/configmap uid: 46f41475-b95b-4477-9221-50054d6a5ea2
再次查看info文件
[root@master ~]# kubectl exec -it pod-configmap -n dev /bin/sh # more /configmap/config/info username:admin password:123456789
Secret
在k8s中,還存在一種和ConfigMap非常類似的對象,成為Secret對象。它主要用於存儲敏感信息,例如密碼,密鑰,證書等等。
首先使用base64對數據進行編碼
[root@master ~]# echo -n 'admin' | base64 YWRtaW4= [root@master ~]# echo -n '123456' | base64 MTIzNDU2
接下來編寫secret.yaml,並創建secret
apiVersion: v1 kind: Secret metadata: name: secret namespace: dev type: Opaque data: username: YWRtaW4= password: MTIzNDU2
使用配置文件
[root@master ~]# vim secret.yaml [root@master ~]# kubectl create -f secret.yaml secret/secret created [root@master ~]# kubectl describe secret/secret -n dev Name: secret Namespace: dev Labels: <none> Annotations: <none> Type: Opaque Data ==== password: 6 bytes username: 5 bytes
創建pod-secret.yaml,將上面的secret掛載進去
apiVersion: v1 kind: Pod metadata: name: pod-secret namespace: dev spec: containers: - name: nginx image: nginx:1.17.1 volumeMounts: - name: config mountPath: /secret/config volumes: - name: config secret: secretName: secret
使用配置文件
[root@master ~]# vim pod-secret.yaml [root@master ~]# kubectl create -f pod-secret.yaml pod/pod-secret created
#查看secret信息,發現已經自動解碼了 [root@master ~]# kubectl exec -it pod-secret -n dev /bin/sh # cd /secret/config # ls password username # more username admin # more password 123456
至此,已經實現了利用secret實現信息的編碼