【问题标题】:How to create a volume that mounts a file which it's path configured in a ConfigMap如何创建一个卷以挂载其路径在 ConfigMap 中配置的文件
【发布时间】:2021-09-08 23:35:36
【问题描述】:

我将描述我的目标是什么,然后展示我为实现它所做的工作......我的目标是:

  • 创建一个包含属性文件路径的配置映射
  • 创建一个部署,其中有一个卷从 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 中配置的路径挂载该文件?

【问题讨论】:

    标签: kubernetes docker-volume configmap


    【解决方案1】:

    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 服务器。

    【讨论】:

      猜你喜欢
      • 2023-03-22
      • 1970-01-01
      • 2018-09-19
      • 2022-08-15
      • 2020-05-30
      • 2017-07-21
      • 1970-01-01
      • 2015-08-26
      • 1970-01-01
      相关资源
      最近更新 更多