已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>