configMap
核心中用就是让配置信息和镜像文件解耦
创建configMap
命令行形式
1. 简单 KV 创建简单的KV结构配置数据
# kubectl create cm myapp-config --from-literal=nginx_port=80 --from-literal=server_name=myapp.lotusching.top
获取configMap及详细信息
# kubectl get cm myapp-config -o yaml
apiVersion: v1
data:
nginx_port: "80"
server_name: myapp.lotusching.top
kind: ConfigMap
metadata:
creationTimestamp: 2018-09-12T01:59:11Z
name: myapp-config
namespace: default
resourceVersion: "299594"
selfLink: /api/v1/namespaces/default/configmaps/myapp-config
uid: 718236fb-b62f-11e8-a27b-00163e08c384
# kubectl describe cm myapp-config
Name: myapp-config
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
server_name:
----
myapp.lotusching.top
nginx_port:
----
80
Events: <none>
Pods清单中注入configMap中的配置数据为环境变量
apiVersion: v1
kind: Pod
metadata:
name: myapp-cm-1
namespace: default
labels:
app: test
environment: develop
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
name: http
protocol: TCP
env:
- name: NGINX_SERVER_PORT
valueFrom:
configMapKeyRef:
name: myapp-config
key: nginx_port
- name: NGINX_SERVER_NAME
valueFrom:
configMapKeyRef:
name: myapp-config
key: server_name
创建并检查环境变量
# kubectl apply -f first-mainitest.yaml
pod/myapp-cm-1 created
# kubectl exec -it myapp-cm-1 "/bin/printenv"|grep NGINX
NGINX_SERVER_PORT=80
NGINX_SERVER_NAME=myapp.lotusching.top
NGINX_VERSION=1.12.2
2. 使用配置文件作为配置数据
创建包含nginx配置文件的configMap资源
# kubectl create cm nginx-www --from-file=./www.conf
configmap/nginx-www created
获取configMap信息
# kubectl get cm nginx-www -o yaml
apiVersion: v1
data:
www.conf: |
server {
server_name myapp.lotusching.top;
listen 80;
root /data/web/html/;
}
kind: ConfigMap
metadata:
creationTimestamp: 2018-09-12T02:09:05Z
name: nginx-www
namespace: default
resourceVersion: "300503"
selfLink: /api/v1/namespaces/default/configmaps/nginx-www
uid: d391facf-b630-11e8-a27b-00163e08c384
Pods清单中挂载configMap中的配置数据为配置文件
apiVersion: v1
kind: Pod
metadata:
name: myapp-cm-1
namespace: default
labels:
app: test
environment: develop
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
name: http
protocol: TCP
volumeMounts:
- name: nginxconf
mountPath: /etc/nginx/conf.d/
readOnly: true
volumes:
- name: nginxconf
configMap:
name: nginx-www
创建并检查配置文件
# kubectl apply -f configMap-file-demo.yaml
pod/myapp-cm-1 created
# kubectl exec -it myapp-cm-1 /bin/sh
/ # cat /etc/nginx/conf.d/www.conf
server {
server_name myapp.lotusching.top;
listen 80;
root /data/web/html/;
}
可以创建下对应的解析和目录主页,不出意外然后可以成功测试访问
补充说明
- 通过注入KV配置信息为环境变量不会实时生效