如何覆盖kubernetes部署文件中pods容器中的文件?

qgelzfjb  于 2021-06-09  发布在  ElasticSearch
关注(0)|答案(1)|浏览(530)

我想覆盖pod容器上的文件。现在我有 elasticsearch.yml 地点 /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

我想用点 echo "some yaml config" >> elasticsearch.yml ,但这种解决方法不起作用,因为我能够保持正确的yaml格式。
你有什么建议,我怎么做?

xytpbqjk

xytpbqjk1#

正如arman在注解中所述,您可以使用 /home/x/IdeaProjects/BD/infra/istio/kube/elasticsearch.yml 并在部署中将其作为卷装载。
要从文件创建配置Map,可以运行:

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放置配置文件。如果这导致了问题,您可能需要将其装载到另一个位置,然后进行复制。

相关问题