如何创建一个卷来挂载在配置映射中配置路径的文件



我将描述我的目标是什么,然后展示我为实现它所做的一切……我的目标:

  • 创建一个包含属性文件路径的配置映射
  • 创建一个部署,该部署具有从configmap中配置的路径装载文件的卷

我所做的:

ConfigMap:

apiVersion: v1
kind: ConfigMap
metadata:
name: my-configmap
data:
my_properties_file_name: "my.properties"

部署:

apiVersion: apps/v1
kind: Deployment
metadata:
name: my-client-deployment
spec:
selector:
matchLabels:
app: my-client
replicas: 1 # tells deployment to run 2 pods matching the template
template:
metadata:
labels:
app: my-client
spec:
containers:
- name: my-client-container
image: {{ .Values.image.client}}
imagePullPolicy: {{ .Values.pullPolicy.client }}
ports:
- containerPort: 80
env:
- name: MY_PROPERTIES_FILE_NAME
valueFrom:
configMapKeyRef:
name: my-configmap
key: my_properties_file_name
volumeMounts:
- name: config
mountPath: "/etc/config"
readOnly: true
imagePullSecrets:
- name: secret-private-registry  
volumes:
# You set volumes at the Pod level, then mount them into containers inside that Pod
- name: config
configMap:
# Provide the name of the ConfigMap you want to mount.
name: my-configmap
# An array of keys from the ConfigMap to create as files
items:
- key: "my_properties_file_name"
path: "my.properties"

结果是在/etc/config下有一个名为my.properties的文件;我的财产";(正如configmap中的文件名所示(,而不是我在本地磁盘中实际拥有的属性文件的内容。

如何使用配置映射中配置的路径装载该文件?

my.properties文件的内容直接放入ConfigMap:

apiVersion: v1
kind: ConfigMap
metadata:
name: my-configmap
data:
my_properties_file_name: |
This is the content of the file.
It supports multiple lines but do take care of the indentation.

或者您也可以使用kubectl create configmap命令:

kubectl create configmap my-configmap --from-file=my_properties_file_name=./my.properties

在这两种方法中,实际上都是将本地磁盘上文件内容的快照传递给kubernetes进行存储。除非重新创建configmap,否则对本地磁盘上的文件所做的任何更改都不会反映出来。

kubernetes的设计允许对位于地球另一端的kubernete集群运行kubectl命令,这样你就不能简单地在本地磁盘上装载一个文件,以便集群实时访问。如果你想要这样的机制,你不能使用ConfigMap,而是需要设置一个由本地机器和集群装载的共享卷,例如使用NFS服务器。

最新更新