命令行指定方式创建
[root@master vml]# kubectl create configmap nginx --from-literal=nginx_port=80 --from-literal=server_name=www.chenxi.com configmap/nginx created
查看
[root@master vml]# kubectl get configmap NAME DATA AGE nginx 2 15s ui-config 1 37d [root@master vml]# kubectl describe configmap nginx Name: nginx Namespace: default Labels:Annotations: Data ==== nginx_port: ---- 80 server_name: ---- www.chenxi.com Events:
命令行已指定文件方式创建
[root@master vml]# kubectl create configmap www.nginx.conf --from-file=./www.conf configmap/www.nginx.conf created
查看
[root@master vml]# kubectl get configmap NAME DATA AGE nginx 2 35m ui-config 1 37d www.nginx.conf 1 2m14s
编写pod文件:这个方式不能实时更新
[root@master vml]# cat pod.yaml apiVersion: v1 kind: Pod metadata: name: pod-demo namespace: default labels: app: myapp tier: frontend spec: containers: - name: myapp image: ikubernetes/myapp:v1 ports: - name: http containerPort: 80 - name: https containerPort: 443 env: - name: NGINX valueFrom: configMapKeyRef: name: nginx key: nginx_port optional:
启动
[root@master vml]# kubectl apply -f pod.yaml pod/pod-demo created [root@master vml]# kubectl exec -it pod-demo -- /bin/sh / # echo $NGINX 80 / # exit
与存储卷的方式挂载
[root@master vml]# cat pod.yaml apiVersion: v1 kind: Pod metadata: name: pod-demo namespace: default labels: app: myapp tier: frontend spec: containers: - name: myapp image: ikubernetes/myapp:v1 ports: - name: http containerPort: 80 - name: https containerPort: 443 volumeMounts: - name: nginx mountPath: /chenxi readOnly: true volumes: - name: nginx configMap: name: www.nginx.conf [root@master vml]# kubectl exec -it pod-demo -- /bin/sh / # ls /chenxi www.conf / # ls /chenxi/www.conf /chenxi/www.conf / # cat /chenxi/www.conf server { server_name www.chenxi.com; listen 80; root /data/www/html; }
使用edit在线修改configMap内容
[root@master vml]# kubectl edit configMap www.nginx.conf # 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: www.conf: |+ server { server_name www.chenxi.com; listen 9090; root /data/www/html; } kind: ConfigMap metadata: creationTimestamp: "2020-04-27T02:58:24Z" name: www.nginx.conf namespace: default resourceVersion: "8747971" selfLink: /api/v1/namespaces/default/configmaps/www.nginx.conf uid: 5a958a9e-31f6-453c-9379-42bde7a21200 configmap/www.nginx.conf edited ---表示修改成功 [root@master vml]# kubectl exec -it pod-demo -- /bin/sh / # cat /chenxi/www.conf 会有一定延迟 server { server_name www.chenxi.com; listen 80; root /data/www/html; } / # cat /chenxi/www.conf 再次查看 server { server_name www.chenxi.com; listen 9090; 修改成功 root /data/www/html; }
如果存在多个文件如何只挂在一个文件
[root@master vml]# kubectl explain pods.spec.volumes.configMap. KIND: Pod VERSION: v1 RESOURCE: configMap
secret 投射数据卷的介绍
[root@master vml]# kubectl create secret --help Create a secret using specified subcommand. Available Commands: docker-registry 创建一个给 Docker registry 使用的 secret generic 从本地 file, directory 或者 literal value 创建一个 secret tls 创建一个 TLS secret Usage: kubectl create secret [flags] [options] Use "kubectl--help" for more information about a given command. Use "kubectl options" for a list of global command-line options (applies to all commands)