您现在的位置是:首页 >技术杂谈 >k8s-configmap挂载网站首页技术杂谈
k8s-configmap挂载
简介k8s-configmap挂载
如何使用configmap
1.环境变量方式注入到pod
创建一个包含两个环境变量的configmap:
kubectl create configmap nginx-config --from-literal=nginx_port=80 --from-literal=server_name=myapp.magedu.com
使用configmap
[root@k8s-master configmap]# vim pod-configmap.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-cm-1
namespace: default
labels:
app: myapp
tier: frontend
annotations:
magedu.com/created-by: "cluster admin"
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
ports:
- name: http
containerPort: 80
env:
- name: NGINX_SERVER_PORT
valueFrom:
configMapKeyRef:
name: nginx-config
key: nginx_port
- name: NGINX_SERVER_NAME
valueFrom:
configMapKeyRef:
name: nginx-config
key: server_name
[root@k8s-master configmap]# kubectl apply -f pod-configmap.yaml
pod/pod-cm-1 created
[root@k8s-master configmap]# kubectl exec -it pod-cm-1 -- /bin/sh
/ # echo $NGINX_SERVER_PORT
80
/ # echo $NGINX_SERVER_NAME
myapp.magedu.com
修改端口,可以发现使用环境变化注入pod中的端口不会根据配置的更改而变化
[root@k8s-master volumes]# kubectl edit cm nginx-config
configmap/nginx-config edited
/ # echo $NGINX_SERVER_PORT
80
所以说通过configmap注入的环境变量,容器启动后,变量不会随着configmap的修改而改变,此时需要更新容器里面的, 可能就需要重新运行容器,重新加载configmap了
- 存储卷方式挂载configmap:
Volume 形式的 ConfigMap 也支持动态更新
[root@k8s-master configmap ~]# vim pod-configmap-2.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-cm-2
namespace: default
labels:
app: myapp
tier: frontend
annotations:
magedu.com/created-by: “cluster admin”
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
ports:- name: http
containerPort: 80
volumeMounts: - name: nginxconf
mountPath: /etc/nginx/config.d/
readOnly: true
volumes:
- name: http
- name: nginxconf
configMap:
name: nginx-config
[root@k8s-master configmap ~]# kubectl apply -f pod-configmap-2.yaml
pod/pod-cm-2 created
[root@k8s-master configmap ~]# kubectl get pods
[root@k8s-master configmap ~]# kubectl exec -it pod-cm-2 – /bin/sh
/ # cd /etc/nginx/config.d
/ # cat nginx_port
80
/ # cat server_name
myapp.magedu.com
[root@k8s-master configmap ~]# kubectl edit cm nginx-config #修改端口,再在容器中查看端口是否变化。
apiVersion: v1
data:
nginx_port: “800”
…
/ # cat nginx_port
800
[root@k8s-master configmap ~]# kubectl delete -f pod-configmap2.yaml
风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。