configmap特殊存储卷

ENV  方式挂载configmap,传入参数至容器变量中。

1
.查看当前的configmap: [root@master yaml]# kubectl get configmap NAME DATA AGE nginx-configmap 2 56m nginx-www 1 41m [root@master yaml]# kubectl describe configmap nginx-configmap Name: nginx-configmap Namespace: default Labels: <none> Annotations: kubectl.kubernetes.io/last-applied-configuration: {"apiVersion":"v1","data":{"name_server":"nginx","nginx_port":"99"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"nginx-configma... Data ==== name_server: ##变量名1 ---- nginx ##变量名对应的变量值1 nginx_port: ##变量名2 ---- 99 ##变量名对应的值2 Events: <none> 2.编写nginx-deployment-configmap.yaml [root@master yaml]# cat nginx-deploy-configmap.yaml --- apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment-configmap labels: app: nginx spec: replicas: 2 selector: matchLabels: app: nginx template: metadata: name: nginx labels: app: nginx spec: containers: - name: nginx-configmap image: nginx:latest imagePullPolicy: IfNotPresent env: ##以变量env的方式传入参数到容器 - name: NGINX_SERVER_NAME ##定义容器里面的变量为 NGINX_SERVER_NAME valueFrom: configMapKeyRef: name: nginx-configmap ##指定configmap key: name_server ##configmap中的(变量名1 name_server)的值为被传入容器定义的变量名为 NGINX_SERVER_NAME optional: true ## true 表示如果configmap 里面没有这个name_server变量,容器也能够正常启动。 - name: NGINX_PORT ## 定义容器里的变量名为 NGINX_PORT valueFrom: configMapKeyRef: name: nginx-configmap ##指定configmap key: nginx_port ## configmap中(变量名2 nginx-configmap)的值被传入容器定义的变量名为 NGINX_PORT optional: true


说明:
--- apiVersion: v1 kind: Service metadata: name: nginx-svc labels: app: nginx spec: selector: app: nginx ports: - name: http targetPort: 80 port: 80 [root@master yaml]# kubectl apply -f nginx-deploy-configmap.yaml 3.进入nginx-deploy-configmap: [root@master yaml]# kubectl get pod NAME READY STATUS RESTARTS AGE httpd-856c97d764-6zq2d 1/1 Running 9 8d jenkins-6d7fb8f845-b7rx5 1/1 Running 9 8d jenkins-6d7fb8f845-fwrp2 1/1 Running 3 3d1h myapp-599d47757d-x5955 1/1 Running 9 8d nginx-deployment-configmap-6f99f7dbdf-h5q9j 1/1 Running 0 11m nginx-deployment-configmap-6f99f7dbdf-mmtcz 1/1 Running 0 11m tomcat-deploy-7f994d9dc9-plzjz 1/1 Running 13 9d [root@master yaml]# kubectl exec -it nginx-deployment-configmap-6f99f7dbdf-h5q9j /bin/sh # env |grep ^NGINX .......... NGINX_SERVER_NAME=nginx NGINX_PORT=99 NGINX_SVC_SERVICE_PORT_HTTP=80

说明:表示configmap中,nginx-configmap定义的变量名1,变量名2,的值已经被传入容器中定义的变量了。

[root@master yaml]# kubectl get configmap
NAME DATA AGE
nginx-configmap 2 79m
nginx-www 1 64m


[root@master yaml]# kubectl describe configmap nginx-configmap
Name: nginx-configmap
Namespace: default
Labels: <none>
Annotations: kubectl.kubernetes.io/last-applied-configuration:
{"apiVersion":"v1","data":{"name_server":"nginx","nginx_port":"99"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"nginx-configma...


Data
====
name_server:
----
nginx
nginx_port:
----
99
Events: <none>





存储卷 方式挂载configmap:
1.查看configmap:
[root@master configmap]# kubectl get configmap 
NAME              DATA   AGE
nginx-configmap   2      4h26m
nginx-www         1      4h11m


[root@master configmap]# kubectl describe configmap nginx-configmap 
Name:         nginx-configmap
Namespace:    default
Labels:       <none>
Annotations:  kubectl.kubernetes.io/last-applied-configuration:
                {"apiVersion":"v1","data":{"name_server":"nginx","nginx_port":"99"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"nginx-configma...

Data
====
name_server:       ##key1
----
nginx              ##value1
nginx_port:        ##key2
----
80                 ##value2
Events:  <none>


2.编写deployment,以volumes方式挂载configmap :
[root@master configmap]# cat nginx-deploy-configmap2.yaml 
---
apiVersion: apps/v1
kind: Deployment
metadata:
 name: nginx-deployment-configmap
 labels:
   app: nginx
spec:
 replicas: 2
 selector:
  matchLabels:
   app: nginx
 template:
  metadata:
   name: nginx
   labels:
    app: nginx
  spec:
    containers:
    - name: nginx-configmap
      image: nginx:latest
      imagePullPolicy: IfNotPresent
      volumeMounts:                    
      - name: nginx-conf               ## volume名称
        mountPath: /etc/nginx/conf.d/  ##挂载至容器目录
        readOnly: true                 ##在容器里只读
    volumes:                            ##以volume方式挂载configmap
    - name: nginx-conf                  ##自定义的volume名称
      configMap:                        
       name: nginx-configmap             ##挂载具体的configmap
       
---   
apiVersion: v1
kind: Service
metadata:
 name: nginx-svc
 labels:
  app: nginx
spec:
 selector:
  app: nginx
 ports:
 - name: http
   targetPort: 80
   port: 80


3.进入pod,查看是否挂载成功:
[root@master configmap]# kubectl get pod -o wide 
NAME                                          READY   STATUS    RESTARTS   AGE    IP             NODE    NOMINATED NODE   READINESS GATES
httpd-856c97d764-6zq2d                        1/1     Running   9          8d     10.244.1.148   node1   <none>           <none>
jenkins-6d7fb8f845-b7rx5                      1/1     Running   9          8d     10.244.1.145   node1   <none>           <none>
jenkins-6d7fb8f845-fwrp2                      1/1     Running   3          3d4h   10.244.2.97    node2   <none>           <none>
myapp-599d47757d-x5955                        1/1     Running   9          8d     10.244.1.147   node1   <none>           <none>
nginx-deployment-configmap-68bf56fb6c-9r9r7   1/1     Running   0          11m    10.244.1.152   node1   <none>           <none>
nginx-deployment-configmap-68bf56fb6c-nt968   1/1     Running   0          11m    10.244.2.105   node2   <none>           <none>
tomcat-deploy-7f994d9dc9-plzjz                1/1     Running   13         9d     10.244.1.143   node1   <none>           <none>
[root@master configmap]# kubectl exec -it nginx-deployment-configmap-68bf56fb6c-9r9r7 /bin/sh

# cd /etc/nginx/conf.d/
# ls
name_server  nginx_port           ##此时能看到configmap中的两个key ,以文件的形式挂载至容器里面了
# cat name_server                 ##看到两个文件的值,刚好是configmap对应的值。
nginx# cat nginx_port
80

说明:
此时,configmap已经以volume的当时已经可以成功挂载至容器里。

 


 


















 

posted @ 2019-11-23 17:33  ccbky  阅读(364)  评论(0编辑  收藏  举报