繁体   English   中英

如何创建安装文件的卷,该文件的路径在 ConfigMap 中配置

[英]How to create a volume that mounts a file which it's path configured in a 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的文件,但该文件的内容是“my.properties”(因为它在 configmap 中被指示为文件名),而不是属性文件的内容作为我实际上在我的本地磁盘中。

如何使用在 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 进行存储。 除非您重新创建配置映射,否则您对本地磁盘上的文件所做的任何更改都不会反映出来。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM