一、ConfigMap创建 1、目录创建ConfigMap 功能在Kubernetes1.2版本中引入,许多应用程序会从配置文件、命令行或环境变量中读取配置信息。ConfigMap API 给我们提供了向容器中注入配置信息的机制,ConfigMap 可以用来保存单个属性,也可以用来保存整个配置文件或者JSON 二进制大对象。
--from-file
指定在目录下的所有文件都会被用在 ConfigMap
里面创建一个键值对,建的名字就是文件名,值就是文件的内容
# ls /data/configmap/kubectl/
game.properties log4j.properties
# cat /data/configmap/kubectl/game.properties
XXCrontabSwitchOn=1
JFMatch3SwitchOn=1
ActiveTaskNewSwitchOn=1
GuildInfoSwitch5=1
# cat /data/configmap/kubectl/log4j.properties
log4j.rootLogger=INFO,A1
log4j.appender.A1.DatePattern='.'yyyy-MM-dd
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
# kubectl create configmap game-config --from-file=/data/configmap/kubectl/
2、文件创建
只要指定为一个文件就可以从单个文件中创建 ConfigMap
# kubectl create configmap game-config-2 --from-file=/data/configmap/kubectl/game.properties
# kubectl get configmaps game-config-2 -o yaml
3、字面值创建
使用文字值创建,利用 --from-literal
参数传递配置信息,该参数可以使用多次,格式如下
# kubectl create configmap special-config --from-literal=special.cpu=2 --from-literal=special.mem=1024
# kubectl get configmaps special-config -o yaml
4、YML文件
apiVersion: v1
kind: ConfigMap
metadata:
name: test-config
namespace: default
data:
cache_host: "10.10.1.1"
redis_port: "6379"
my.cnf: |
[mysqld]
log-bin = mysql-bin
log-error = error.log
# kubectl create -f configmap.yml
二、ConfigMap应用
Pod
中使用 ConfigMap
kind: ConfigMap
apiVersion: v1
metadata:
name: special-config
namespace: default
data:
special.cpu: "2"
special.mem: "1024"
---
kind: ConfigMap
apiVersion: v1
metadata:
name: env-config
namespace: default
data:
log_level: INFO
- 使用
valueFrom、configMapKeyRef、name、key
指定要用的key
- 通过
envFrom、configMapRef、name
使得configmap
中的所有key/value
对都自动变成环境变量
# cat test-nginx-pod.yml
apiVersion: v1
kind: Pod
metadata:
name: "test-nginx-pod"
namespace: default
labels:
app: "test-nginx-pod"
spec:
containers:
- name: test-nginx-pod
image: "nginx:1.7"
env:
- name: SPECIAL_LEVEL_KEY
valueFrom:
configMapKeyRef:
name: env-config
key: log_level
envFrom:
- configMapRef:
name: special-config
restartPolicy: Always
# kubectl exec -it test-nginx-pod /bin/bash
echo $SPECIAL_LEVEL_KEY
INFO
# env
2、ConfigMap 设置命令行参数
apiVersion: v1
kind: Pod
metadata:
name: test-pod-busybox
spec:
containers:
- name: test-pod-busybox
image: busybox
command: [ "/bin/sh", "-c", "echo (SPECIAL_CPU_KEY) (SPECIAL_MEM_KEY)" ]
env:
- name: SPECIAL_CPU_KEY
valueFrom:
configMapKeyRef:
name: special-config
key: special.cpu
- name: SPECIAL_MEM_KEY
valueFrom:
configMapKeyRef:
name: special-config
key: special.mem
restartPolicy: Always
3、ConfigMap 用在volume挂载
我们可以以 Nginx
配置文件导入进 ConfigMap
中被 Pod
引用
# kubectl create cm nginx-config --from-file=nginx.conf
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-configmap
namespace: default
spec:
selector:
matchLabels:
app: nginx-configmap
template:
metadata:
labels:
app: nginx-configmap
spec:
volumes:
- configMap:
name: nginx-config # 指定使用ConfigMap的名称
name: config # volumes的名称
containers:
- name: nginx
image: nginx:1.7.9
imagePullPolicy: IfNotPresent
volumeMounts:
- mountPath: "/etc/application" # 容器挂载的目录(空的)
name: config # 指定上面的volumes名称
# kubectl exec -it nginx-configmap-xf5zw ls /etc/application/
nginx.conf
三、ConfigMap更新
apiVersion: v1
kind: ConfigMap
metadata:
name: log-config
namespace: default
data:
log_level: INFO
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-configmap
namespace: default
spec:
selector:
matchLabels:
app: nginx-configmap
template:
metadata:
labels:
app: nginx-configmap
spec:
volumes:
- configMap:
name: log-config
name: config
containers:
- name: nginx
image: nginx:1.7.9
imagePullPolicy: IfNotPresent
volumeMounts:
- mountPath: "/etc/config"
name: config
# kubectl exec `kubectl get pods -l app=nginx-configmap -o=name|cut -d "/" -f2` cat /etc/config/log_level
INFO
修改 ConfigMap
,将 log_level
改成 DEBUG
等待10秒钟左右,再次查看环境变量的值
# kubectl edit configmap log-config
# kubectl exec `kubectl get pods -l app=nginx-configmap -o=name|cut -d "/" -f2` cat /etc/config/log_level
DEBUG
- 使用该
ConfigMap
挂载的ENV
不会同步更新 - 使用该
ConfigMap
挂载的Volume
中的数据需要一段时间(实测大概10秒)才能同步更新
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)