ConfigMap
ConfigMap是一种比较特殊的存储卷,它的主要作用是用来存储配置信息的。
Secret
在kubernetes中,还存在一种和ConfigMap非常类似的对象,称为Secret对象。它主要用于存储敏感信
息,例如密码、秘钥、证书等等。
创建configmap
[root@k8s-master1 ConfigMap_secret]# kubectl create configmap configmap -n my-ns-xmcc -o yaml >> configmap.yaml
[root@k8s-master1 ConfigMap_secret]# cat configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: configmap
namespace: my-ns-xmcc
data:
info: |
username: xm #配置了用户名和密码信息
password: 123456
#使用此配置文件创建configmap
[root@k8s-master1 ConfigMap_secret]# kubectl apply -f configmap.yaml
Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply
configmap/configmap configured
[root@k8s-master1 ConfigMap_secret]# kubectl get cm -n my-ns-xmcc
NAME DATA AGE
configmap 1 3m3s
# 查看configmap详情
[root@k8s-master1 ConfigMap_secret]# kubectl describe cm configmap -n my-ns-xmcc
Name: configmap
Namespace: my-ns-xmcc
Labels: <none>
Annotations: <none>
Data
====
info:
----
username: xm
password: 123456
Events: <none>
接下来创建一个deployment,将上面创建的configmap挂载进pod中
先用命令导出yaml文件
[root@k8s-master1 ConfigMap_secret]# kubectl create deploy deploy-xmcc -n my-ns-xmcc --image=centos-nginx:1.23.1 -oyaml >> deploy.yaml
修改yaml文件,加入configmap配置信息
[root@k8s-master1 ConfigMap_secret]# cat deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: deploy-xmcc
name: deploy-xmcc
namespace: my-ns-xmcc
spec:
progressDeadlineSeconds: 600
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
app: deploy-xmcc
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
labels:
app: deploy-xmcc
spec:
containers:
- image: centos-nginx:1.23.1
imagePullPolicy: IfNotPresent
name: centos-nginx
resources: {}
volumeMounts: #configmap挂载上去
- name: config
mountPath: /configmap/config
volumes:
- name: config
configMap:
name: configmap
dnsPolicy: ClusterFirst
restartPolicy: Always
#使用配置文件创建deployment
[root@k8s-master1 ConfigMap_secret]# kubectl apply -f deploy.yaml
Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply
#查询资源
[root@k8s-master1 ConfigMap_secret]# kubectl get all -n my-ns-xmcc
NAME READY STATUS RESTARTS AGE
pod/deploy-xmcc-7b49c87f8-zg6sp 1/1 Running 0 57s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/deploy-xmcc 1/1 1 1 7m44s
NAME DESIRED CURRENT READY AGE
replicaset.apps/deploy-xmcc-5d64d5c64d 0 0 0 7m44s
replicaset.apps/deploy-xmcc-7b49c87f8 1 1 1 57s
#进入容器
[root@k8s-master1 ConfigMap_secret]# kubectl exec -it -n my-ns-xmcc deploy-xmcc-7b49c87f8-zg6sp /bin/bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
[root@deploy-xmcc-7b49c87f8-zg6sp /]# cd /configmap/config/
[root@deploy-xmcc-7b49c87f8-zg6sp config]# ll
total 0
lrwxrwxrwx 1 root root 11 Sep 20 10:17 info -> ..data/info
[root@deploy-xmcc-7b49c87f8-zg6sp config]# cat info
username: xm
password: 123456
# 可以看到映射已经成功,每个configmap都映射成了一个目录
# key--->文件 value---->文件中的内容
# 此时如果更新configmap的内容, 容器中的值也会动态更新
标签:ConfigMap,xmcc,deploy,root,configmap,Secret,k8s,kubectl From: https://www.cnblogs.com/xmwan/p/16710715.html