configMap描述信息
ConfigMap 功能在 Kubernetes1.2 版本中引入,许多应用程序会从配置文件、命令行参数或环境变量中读取配置信息。
ConfigMap API 给我们提供了向容器中注入配置信息的机制,ConfigMap 可以被用来保存单个属性,也可以用来保存整个配置文件或者 JSON 二进制大对象
k8s中存储分为以下几类:
ConfigMap、Volume、Secret、Perstent Volume(pv)
演示 ConfigMap的创建
Ⅰ 使用目录创建
mkdir -p configmap/dir/
cd dir
创建文件
vim game.properties
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30
创建文件
vim ui.properties
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
vim ui.properties
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
创建game-config
[root@master dir]# kubectl create configmap game-config --from-file=../dir
—from-file 指定在目录下的所有文件都会被用在 ConfigMap 里面创建一个键值对,键的名字就是文件名,值就 是文件的内容
查看configmap
kubectl get cm game-config
#查看详细信息
kubectl get cm game-config -o yaml
apiVersion: v1
data:
game.properties: |
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30
ui.properties: |
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
kind: ConfigMap
metadata:
creationTimestamp: "2022-12-22T04:34:49Z"
name: game-config
namespace: default
resourceVersion: "187406"
selfLink: /api/v1/namespaces/default/configmaps/game-config
uid: e2a7ced2-21fa-4311-b84c-83b2621e0cf5
Ⅱ 使用文件创建
只要指定为一个文件就可以从单个文件中创建 ConfigMap
kubectl create configmap game-config-2 --from-file=../dir/game.properties
与目录创建../dir
的区别是直接指定到文件../dir/game.properties
查看
kubectl get configmaps game-config-2 -o yaml
—from-file
这个参数可以使用多次,你可以使用两次分别指定上个实例中的那两个配置文件,效果就跟指定整个
目录是一样的
Ⅲ 使用字面值创建
使用文字值创建,利用 —from-literal
参数传递配置信息,该参数可以使用多次,格式如下
kubectl create configmap special-config --from-literal=special.how=very --from-
literal=special.type=charm
#special-config:configmap名
#--from-literal=键值名=键值
kubectl get configmaps special-config -o yaml
pod 中使用ConfigMap
Ⅰ 使用ConfigMap来替代环境变量
vim pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-config
image: nginx
command: ["/bin/bash","-c","env"]
env:
- name: SPECIAL_LEVE_KEY
valueFrom:
configMapKeyRef:
name: special-config
key: special.how
- name: SPECIAL_TYPE_KEY
valueFrom:
configMapKeyRef:
name: special-config
key: special.type
envFrom:
- configMapRef:
name: env-config #configmap来源
restartPolicy: Never
#查看pod及环境变量
[root@master dir]# kubectl get pod
NAME READY STATUS RESTARTS AGE
dapi-test-pod 0/1 Completed 0 2m10s
[root@master dir]# kubectl logs dapi-test-pod
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_SERVICE_PORT=443
HOSTNAME=dapi-test-pod
PWD=/
PKG_RELEASE=1~bullseye
HOME=/root
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
log_level=INFO
SPECIAL_LEVE_KEY=very
NJS_VERSION=0.7.9
SHLVL=0
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
KUBERNETES_SERVICE_HOST=10.96.0.1
KUBERNETES_PORT=tcp://10.96.0.1:443
KUBERNETES_PORT_443_TCP_PORT=443
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
SPECIAL_TYPE_KEY=charm
NGINX_VERSION=1.23.3
_=/usr/bin/env
Ⅱ 使用ConfigMap设置命令行参数
vim pod1.yaml
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod1
spec:
containers:
- name: test-config1
image: nginx
command: ["/bin/bash","-c","echo $(SPECIAL_LEVE_KEY) $(SPECIAL_TYPE_KEY)"]
env:
- name: SPECIAL_LEVE_KEY
valueFrom:
configMapKeyRef:
name: special-config
key: special.how
- name: SPECIAL_TYPE_KEY
valueFrom:
configMapKeyRef:
name: special-config
key: special.type
envFrom:
- configMapRef:
name: env-config
restartPolicy: Never
Ⅲ 通过数据卷插入使用ConfigMap
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod2
spec:
containers:
- name: test-config2
image: nginx
command: ["/bin/bash","-c","sleep 600s"]
volumeMounts:
- name: config-volumes
mountPath: /etc/config
volumes:
- name: config-volumes
configMap:
name: special-config
restartPolicy: Never
kubectl create -f pod2.yaml
查看pod2
[root@master dir]# kubectl get pod
NAME READY STATUS RESTARTS AGE
dapi-test-pod 0/1 Completed 0 31m
dapi-test-pod1 0/1 Completed 0 6m59s
dapi-test-pod2 1/1 Running 0 9s
查看挂载目录
[root@master dir]# kubectl exec -it dapi-test-pod2 bash
root@dapi-test-pod2:/# cd /etc/config/
root@dapi-test-pod2:/etc/config# ls
special.how special.type
ConfigMap热更新
[root@apiserver config]# cat 111.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: log-config
data:
log_level: INFO## 输出信息
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: log-config
创建
kubectl apply -f 111.yaml
kubectl get pod
nginx-deployment-58cc897b99-2khhb 1/1 Running 0 24s
nginx-deployment-58cc897b99-h6qj8 1/1 Running 0 24s
nginx-deployment-58cc897b99-xjpdk 1/1 Running 0 24s
查看挂载目录信息
是否正确
[root@master dir]# kubectl exec -it nginx-deployment-58cc897b99-2khhb bash
root@nginx-deployment-58cc897b99-2khhb:/# cd /etc/config/
root@nginx-deployment-58cc897b99-2khhb:/etc/config# ls
log_level
root@nginx-deployment-58cc897b99-2khhb:/etc/config# cat log_level
INFO
编辑configmap实现热更新
kubectl edit cm log-config
修改后
[root@apiserver config]# kubectl exec -it nginx-deployment-58b5f7c9d-bdxrc bash
root@nginx-deployment-58b5f7c9d-bdxrc:/# cd /etc//config/
root@nginx-deployment-58b5f7c9d-bdxrc:/etc/config# ls
log_level
root@nginx-deployment-58b5f7c9d-bdxrc:/etc/config# cat log_level
DEBUG
错误信息
如果configmap配置错误使用
#查看cm信息
kubectl get cm
#选择要重新配置的configmap
kubectl delete configmap cm名称
如果提示有type错误
也要同时删除pod,再重新创建
如果重新查看修改后的configmap
没有该改变的话
删除pod 使nginx重启