简体   繁体   English

如何将配置文件挂载到 Kube.netes Pod 中?

[英]How to mount configuration files into a Kubernetes Pod?

I have a postgres instance that I generally spin up using a docker-compose.yml file.我有一个 postgres 实例,我通常使用docker-compose.yml文件启动它。 It mounts some configuration files like this:它会像这样挂载一些配置文件:

    volumes:
      - postgres-volume:/var/lib/postgresql/data
      - ./postgresql.conf:/etc/postgresql.conf
      - ./logs:/logs
      - ./pg_hba.conf:/etc/pg_hba.conf

These files are located in the same folder as the docker-compose.yml file is.这些文件与docker-compose.yml文件位于同一文件夹中。

My question is: how do I make a Pod that contains this container also mount these configuration files?我的问题是:如何使包含此容器的 Pod 也挂载这些配置文件? I read something about PVC in Kube.netes but I'm having a hard time connecting the dots and going from the docker-compose way of thinking to the Kube.netes way of thinking.我在 Kube.netes 上读到了一些关于 PVC 的内容,但我很难把这些点联系起来,也很难从 docker-compose 的思维方式转向 Kube.netes 的思维方式。

Any help is greatly appreciated.任何帮助是极大的赞赏。

Easiest approach would be to store the files in a ConfigMap and mount those as volumes in the container.最简单的方法是将文件存储在ConfigMap中,并将它们作为卷挂载到容器中。

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-config
data:
  my.properties: |
    url=example.com
    username=username
    password=password
  index.html: |
    Hello world
[...]
  containers:
    - name: my-container
      image: some-registry/some-image
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config/my.properties
        subPath: my.properties
      - name: config-volume
        mountPath: /some/different/path/index.html
        subPath: index.html
  volumes:
    - name: config-volume
      configMap:
        name: my-config
[...]

By that the my.properties file is mounted into /etc/config and index.html into /some/different/path of the my-container file system.通过这种方式, my.properties文件被挂载到/etc/configindex.htmlmy-container文件系统的/some/different/path中。

You can also generate the configmap from the file by kubectl create configmap my-config --from-file /path/to/my.properties --from-file /path/to/index.html您还可以通过kubectl create configmap my-config --from-file /path/to/my.properties --from-file /path/to/index.html从文件生成 configmap

See docs for more info on that.有关更多信息,请参阅文档

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

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