首页 > 解决方案 > 如何将本地文件复制到 helm 部署中

问题描述

我正在尝试使用带有初始化脚本的 mongo 映像在 Kubernetes 中部署几个 pod。我正在使用 helm 进行部署。由于我从官方的 Mongo docker 映像开始,我试图在 /docker-entrypoint-initdb.d 中添加一个脚本,因此它将在开始时执行以初始化我的 Mongo 的一些参数。

我不知道如何使用 helm 插入我的脚本,也就是说,在我的本地机器中,在 /docker-entrypoint-initdb.d 中。

我正在尝试做类似 docker run -v hostfile:mongofile 但我需要 helm 中的等效项,因此这将在部署的所有 pod 中完成

标签: mongodbdockerkuberneteskubernetes-helm

解决方案


您可以使用配置映射。让我们通过 configmap 将 nginx 配置文件放到容器中。我们有名为 nginx 的目录名称,具有相同级别的 values.yml。在里面我们有实际的配置文件。

apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-config-file
  labels:
    app: ...    
data:
  nginx.conf: |-
{{ .Files.Get "nginx/nginx.conf" | indent 4 }}

---

apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: SomeDeployment
  ...  
spec:
  replicas: 
  selector:
    matchLabels:
      app: ...
      release: ...
  template:
    metadata:
      labels:
        app: ...
        release: ...
    spec:
      volumes:
        - name: nginx-conf
          configMap:
            name: nginx-config-file
            items:
            - key: nginx.conf
              path: nginx.conf
      containers:        
        - name: ...
          image: ...          
          volumeMounts:
            - name: nginx-conf
              mountPath: /etc/nginx/nginx.conf
              subPath: nginx.conf

您还可以从此链接检查 initContainers 概念: https ://kubernetes.io/docs/concepts/workloads/pods/init-containers/


推荐阅读