Kubernetes配置管理configMap
ConfigMap介绍
为什么要用configMap?
将配置文件和POD解耦
congiMap里的配置文件是如何存储的?
键值对
key:value
文件名:配置文件的内容
configMap支持的配置类型
直接定义的键值对
基于文件创建的键值对
configMap创建方式
命令行
资源配置清单
configMap的配置文件如何传递到POD里
变量传递
数据卷挂载
使用configMap的限制条件
1.ConfigMap必须在Pod之前创建,Pod才能引用他
2.ConfigMap受限于命名空间限制,只有处于同一个命名空间中的Pod才可以被引用
创建configMap
命令行创建configMap
[root@db01 tmp]# kubectl create configmap nginx-config --from-literal=nginx_port=80 --from-literal=server_name=blog.lw.com
--from-literal= 定义configMap中的变量
## 查看configMap
[root@db01 tmp]# kubectl get cm
NAME DATA AGE
nginx-config 2 43s
## 详细信息
[root@db01 tmp]# kubectl describe cm nginx-config
## 清单引用configMap
apiVersion: v1
kind: Pod
metadata:
name: nginx-cm
spec:
containers:
- name: nginx-pod
image: nginx:1.14.0
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
env:
- name: NGINX_PORT
valueFrom:
configMapKeyRef:
name: nginx-config
key: nginx_port
- name: SERVER_NAME
valueFrom:
configMapKeyRef:
name: nginx-config
key: server_name
查看pod是否引入了变量
[root@node1~lconfimap]# kubectL exec-it nginx-cm/bin/bash
root@nginx-cm:~# echo${NGINX_PORT}
80
Root@nginx-cm:~# echo${SERVER_NAME}
blog.lw.com
注意:
变量传递的形式,修改conf Map的配置,POD内并不会生效因为变量只有在创建POD的时候才会引用生效,POD一旦创建好,环境变量就不变了
使用文件创建configmap
[root@db01 config-map]# cat www.conf
server {
listen 80;
server_name www.lw.com;
location / {
root /usr/share/nginx/html/www;
index index.html index.htm;
}
}
# 命令行创建configmap资源
[root@db01 config-map]# kubectl create configmap nginx-www --from-file=www.conf=./www.conf
configmap/nginx-www created
[root@db01 config-map]# kubectl get cm
NAME DATA AGE
nginx-config 2 47m
nginx-www 1 49s
[root@db01 config-map]# kubectl describe cm nginx-www
# 清单引用configmap
vim nginx-cm-volume.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx-cm-01
spec:
volumes:
- name: configmap-data
configMap:
name: nginx-www
items:
- key: www.conf
path: www.conf
containers:
- name: nginx-pod
image: nginx:alpine
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
volumeMounts:
- name: configmap-data
mountPath: /etc/nginx/conf.d/
测试效果
1.进到容器内查看文件
kubectl exec -it nginx-cm /bin/bash
cat /etc/nginx/conf.d/vww . conf
2.动态修改
configMap kubectl edit cm nginx-cm-01
3.再次进入容器内观察配置会不会自动更新
cat /etc/nginx/conf.d/wwnw . conf
nginx -T
资源清单创建configMap
[root@db01 config-map]# cat nginx-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: ngx-config
data:
www.conf: |
server {
listen 80;
server_name www.lw.com;
location / {
root /usr/share/nginx/html/www;
index index.html index.htm;
}
}
blog.conf: |
server {
listen 80;
server_name blog.lw.com;
location / {
root /usr/share/nginx/html/blog;
index index.html index.htm;
}
}
[root@db01 config-map]# kubectl apply -f nginx-configmap.yaml
[root@db01 config-map]# kubectl get cm
NAME DATA AGE
nginx-config 2 70m
nginx-www 1 24m
ngx-config 2 15s
[root@db01 config-map]# kubectl describe cm ngx-config
# 创建pod资源清单并引用configmap
[root@db01 config-map]# cat nginx-dp.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-dp
spec:
replicas: 2
selector:
matchLabels:
app: nginx-cm
template:
metadata:
name: nginx-pod
labels:
app: nginx-cm
spec:
volumes:
- name: cm-data
configMap:
name: ngx-config
items:
- key: www.conf
path: www.conf
- key: blog.conf
path: blog.conf
containers:
- name: nginx-pod
image: nginx:alpine
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
volumeMounts:
- name: cm-data
mountPath: /etc/nginx/conf.d/
[root@db01 config-map]# vim clusterip.yaml
apiVersion: v1
kind: Service
metadata:
name: ngx-svc
spec:
selector:
app: ngx-cm
ports:
- name: http
port: 80
protocol: TCP
targetPort: 80
type: ClusterIP
[root@db01 config-map]# cat nginx-ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ngx-ingress
spec:
rules:
- host: www.lw.com
http:
paths:
- path: /
pathType: ImplementationSpecific
backend:
service:
name: ngx-svc
port:
number: 80
- host: blog.lw.com
http:
paths:
- path: /
pathType: ImplementationSpecific
backend:
service:
name: ngx-svc
port:
number: 80
Comments | NOTHING