首页 > 解决方案 > 如何在 Kubernetes 部署文件中覆盖 pods 容器中的文件?

问题描述

我想覆盖 pod 容器上的文件。现在我elasticsearch.yml在 location /usr/share/elasticsearch/config

我试图initContainer在 kubernetes 部署文件中实现这一点,所以我添加了类似的内容:

        - name: disabled-the-xpack-security
          image: busybox
          command: 
          - /bin/sh 
          - -c 
          - |
            sleep 20
            rm /usr/share/elasticsearch/config/elasticsearch.yml 
            cp /home/x/IdeaProjects/BD/infra/istio/kube/elasticsearch.yml /usr/share/elasticsearch/config/
          securityContext:
            privileged: true

但这不起作用,错误看起来像:

rm: can't remove '/usr/share/elasticsearch/config/elasticsearch.yml': No such file or directory
cp: can't stat '/home/x/IdeaProjects/BD/infra/istio/kube/elasticsearch.yml': No such file or directory

我试图使用 some echo "some yaml config" >> elasticsearch.yml,但这种解决方法不起作用,因为我能够保持正确的 yaml 格式。

你有什么建议,我该怎么做?

标签: elasticsearchkubernetes

解决方案


正如 Arman 在评论中所述,您可以创建一个包含 ConfigMap 的内容,/home/x/IdeaProjects/BD/infra/istio/kube/elasticsearch.yml并将其作为卷安装在部署中。

要从您的文件创建配置映射,您可以运行:

kubectl create configmap my-es-config --from-file=/home/x/IdeaProjects/BD/infra/istio/kube/elasticsearch.yml

这将使用 yaml 文件在您的 kubernetes 集群中创建一个 ConfigMap。

然后,您可以使用它并将卷安装添加到您的部署中:

  containers:
    - name: elasticsearch
      image: k8s.gcr.io/busybox
      .
      .
      .
      volumeMounts:
      - name: config-volume
        mountPath: /usr/share/elasticsearch/config/
  volumes:
    - name: config-volume
      configMap:
        name: my-es-config

笔记

  • 建议也将 ConfigMap 创建为 yaml。更多信息在这里
  • 直接在 上安装 configmap /usr/share/elasticsearch/config/,将替换该路径内的所有内容并放置 configmap 中的配置文件。如果这导致问题,您可能希望将其安装在另一个位置,然后复制它。

推荐阅读