ConfigMap 是一种 API 对象,用来将非机密性的数据保存到健值对中。使用时可以用作环境变量、命令行参数或者存储卷中的配置文件。
ConfigMap 允许您将配置文件与镜像文件分离,以使容器化的应用程序具有可移植性。
格式:
kubectl create configmap <map-name> <data-source>
map-name
是要分配给 ConfigMap 的名称
data-source
是要从中提取数据的目录,文件或者文字值
数据源对应于 ConfigMap 中的 key-value (键值对)
从同一目录中的多个文件创建 ConfigMap
[root@harbor configmap]# ll
总用量 16
-rw-r-----. 1 root root 2737 6月 29 14:51 base-ms.yml
-rw-r-----. 1 root root 2452 6月 29 16:10 business-ms.yml
-rw-r-----. 1 root root 540 6月 29 14:50 eureka.yml
-rw-r-----. 1 root root 1119 6月 29 14:50 oauth2.yml
[root@harbor configmap]# kubectl create configmap test-configmap --from-file=/opt/configmap/ -n xzzyy-test
configmap/test-configmap created
查看configmap内容
kubectl describe configmaps -n xzzyy-test test-configmap
输出类似以下内容:
Name: test-configmap
Namespace: xzzyy-test
Labels: <none>
Annotations: <none>
Data
====
base-ms.yml:
----
cn:
enableCodeGenerator: true
enableDruidDBConfig: true
enableGenerateCode: true
enableGlobalException: true
enableTxAdviceInterceptor: true
......
business-ms.yml:
----
cn:
enableCodeGenerator: true
enableDruidDBConfig: true
enableGenerateCode: true
enableGlobalException: true
enableRequestLogHandler: true
enableTxAdviceInterceptor: true
enableAopLog: true
......
eureka.yml:
----
server:
port: 40000
......
oauth2.yml:
----
eureka:
client:
serviceUrl:
defaultZone: http://root:root@test-eureka-svc:40000/eureka
.....
Events: <none>
修改
kubectl edit configmaps -n xzzyy-test test-configmap
删除
kubectl delete configmaps -n xzzyy-test test-configmap
从单个文件或多个文件创建 ConfigMap
kubectl create configmap test-configmap2 \
--from-file=/opt/configmap/base-ms.yml \
--from-file=/opt/configmap/business-ms.yml \
--from-file=/opt/configmap/eureka.yml \
--from-file=/opt/configmap/oauth2.yml \
-n xzzyy-test
内容与从目录创建类似,此处不展示了。
kubectl create configmap special-config \
--from-literal=special.how=very \
--from-literal=special.type=charm \
查看内容
[root@harbor opt]# kubectl describe configmaps special-config
Name: special-config
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
special.how:
----
very
special.type:
----
charm
Events: <none>
自 1.14 开始, kubectl 开始支持 kustomization.yaml。
个人认为这种创建方式不如kubectl简单,此处就不研究了,想继续了解可以参考官网:https://kubernetes.io/zh/docs/tasks/configure-pod-container/configure-pod-configmap/#%E6%A0%B9%E6%8D%AE%E7%94%9F%E6%88%90%E5%99%A8%E5%88%9B%E5%BB%BA-configmap
1、在 ConfigMap 中将环境变量定义为键值对:
kubectl create configmap special-config --from-literal=special.how=very
2、将 ConfigMap 中定义的 special.how 值分配给 Pod 的 SPECIAL_LEVEL_KEY 环境变量。
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: busybox:latest
command: [ "/bin/sh", "-c", "env" ]
env:
- name: SPECIAL_LEVEL_KEY # 容器环境变量名称
valueFrom:
configMapKeyRef:
name: special-config # configmap的名字
key: special.how # configmap的key
restartPolicy: Never
1、在 ConfigMap 中将环境变量定义为键值对:
kubectl create configmap special-config --from-literal=special.how=very
kubectl create configmap env-config --from-literal=log_level=INFO
2、将 ConfigMap 中定义的 special.how
、log_level
值分配给 Pod 中的 SPECIAL_LEVEL_KEY
、LOG_LEVEL
环境变量。
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: busybox:latest
command: [ "/bin/sh", "-c", "env" ]
env:
- name: SPECIAL_LEVEL_KEY
valueFrom:
configMapKeyRef:
name: special-config
key: special.how
- name: LOG_LEVEL
valueFrom:
configMapKeyRef:
name: env-config
key: log_level
restartPolicy: Never
1、创建一个包含多个键值对的 ConfigMap。
kubectl create configmap special-config-1 \
--from-literal=SPECIAL_LEVEL=very \
--from-literal=SPECIAL_TYPE=INFO \
--from-literal=AUTH=liuli
2、使用 envFrom
将所有 ConfigMap
的数据定义为容器环境变量,ConfigMap
中的键成为 Pod 中的环境变量名称。
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: busybox:latest
command: [ "/bin/sh", "-c", "env" ]
envFrom:
- configMapRef:
name: special-config-1
restartPolicy: Never
kubectl create configmap special-config-2 \
--from-literal=SPECIAL_LEVEL=very \
--from-literal=SPECIAL_TYPE=INFO \
--from-literal=AUTH=liuli
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: busybox:latest
command: [ "/bin/sh", "-c", "echo $(SPECIAL_LEVEL_KEY) $(SPECIAL_TYPE_KEY) $(AUTH_KEY)" ]
args: ["$(SPECIAL_LEVEL_KEY) $(SPECIAL_TYPE_KEY) $(AUTH_KEY)"]
env:
- name: SPECIAL_LEVEL_KEY
valueFrom:
configMapKeyRef:
name: special-config-2
key: SPECIAL_LEVEL
- name: SPECIAL_TYPE_KEY
valueFrom:
configMapKeyRef:
name: special-config-2
key: SPECIAL_TYPE
- name: AUTH_KEY
valueFrom:
configMapKeyRef:
name: special-config-2
key: AUTH
restartPolicy: Never
注意:该操作会清空mountPath
即/etc/config
目录下的内容
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: busybox:latest
command: [ "/bin/sh", "-c", "ls -l /etc/config/" ]
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: special-config-1
restartPolicy: Never
注意:该操作会清空mountPath
即/etc/config
目录下的内容
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: busybox:latest
command: [ "/bin/sh","-c","cat /etc/config/keys" ]
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: special-config-1
items:
- key: SPECIAL_LEVEL
path: keys
restartPolicy: Never
结合公司项目,我选择了将ConfigMap
中的键值通过volumeMounts
的方式生成配置文件,供Jar包读取启动。
配置文件目录
[root@harbor configmap]# ll
总用量 72
-rw-r-----. 1 root root 2731 6月 30 09:50 dev-base-ms.yml
-rw-r-----. 1 root root 2448 6月 30 09:51 dev-business-ms.yml
-rw-r-----. 1 root root 1466 6月 30 09:51 dev-common-ws.yml
-rw-r-----. 1 root root 2972 6月 30 09:52 dev-data-rep-ws.yml
-rw-r-----. 1 root root 2926 6月 30 09:53 dev-doctor-ws.yml
-rw-r-----. 1 root root 2534 6月 30 09:54 dev-esb-ws.yml
-rw-r-----. 1 root root 401 6月 30 09:54 dev-eureka.yml
-rw-r-----. 1 root root 4474 6月 30 09:55 dev-job-web.yml
-rw-r-----. 1 root root 3126 6月 30 09:55 dev-message-ms.yml
-rw-r-----. 1 root root 2276 6月 30 09:56 dev-netty.yml
-rw-r-----. 1 root root 1116 6月 30 09:56 dev-oauth2.yml
-rw-r-----. 1 root root 1421 6月 30 09:57 dev-offline-registration-ms.yml
-rw-r-----. 1 root root 3529 6月 30 09:58 dev-online-registration-ms.yml
-rw-r-----. 1 root root 3250 6月 30 10:00 dev-recommender-ms.yml
-rw-r-----. 1 root root 3965 6月 30 10:00 dev-saas-ws.yml
-rw-r-----. 1 root root 2718 6月 30 10:01 dev-vasc-ms.yml
-rw-r-----. 1 root root 2192 6月 30 10:01 dev-zuul.yml
通过目录生成ConfigMap
[root@harbor configmap]# kubectl create configmap dev-config -n xzzyy-dev --from-file=/opt/config/java/dev/xzzyy/configmap/
configmap/dev-config created
查看
[root@harbor configmap]# kubectl get configmaps -n xzzyy-dev
NAME DATA AGE
dev-config 17 56s
以eureka为例
FROM openjdk:8u252-slim-buster
EXPOSE 30000
WORKDIR /my-java
ADD eureka-sever-0.0.1.jar /my-java
CMD ["java","-Xms2048m","-Xmx4096m","-XX:PermSize=256m","-XX:MaxPermSize=512m","-XX:MaxNewSize=512m","-Dfile.encoding=UTF8","-Duser.timezone=GMT+08","-jar","eureka-sever-0.0.1.jar","--spring.config.location=/config/bootstrap.yml"]
apiVersion: apps/v1
kind: Deployment
metadata:
name: dev-eureka
namespace: xzzyy-dev
spec:
replicas: 1
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
selector:
matchLabels:
app: dev-eureka-pod
template:
metadata:
labels:
app: dev-eureka-pod
spec:
restartPolicy: Always
imagePullSecrets:
- name: xzzyy-dev-secret
containers:
- name: dev-eureka-con
image: dev-eureka:v1
imagePullPolicy: Always
ports:
- name:
containerPort: 30000
protocol: TCP
volumeMounts:
- name: config-volume
mountPath: /config
volumes:
- name: config-volume
configMap:
name: dev-config
items:
- key: dev-eureka.yml
path: bootstrap.yml
---
apiVersion: v1
kind: Service
metadata:
name: dev-eureka-svc
namespace: xzzyy-dev
labels:
name: dev-eureka-svc
spec:
type: NodePort
selector:
app: dev-eureka-pod
ports:
- protocol: TCP
port: 30000
targetPort: 30000
nodePort: 30000