【问题标题】:Kubernetes copying jars into a pod and restartKubernetes 将 jar 复制到 pod 中并重新启动
【发布时间】:2021-07-23 12:57:57
【问题描述】:

我有一个 Kubernetes 问题,我需要在部署 pod 后将 2 个 jar(每个 jar > 1Mb)复制到一个 pod 中。所以理想的解决方案是我们不能使用 configMap (> 1Mb) 但我们需要在“initcontainer”中使用“wget”并下载 jars。 所以下面是我修改过的 kubernetes-template 配置。原版可在https://github.com/dremio/dremio-cloud-tools/blob/master/charts/dremio/templates/dremio-executor.yaml获取。

{{ if not .Values.DremioAdmin }}
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: dremio-executor
spec:
  serviceName: "dremio-cluster-pod"
  replicas: {{.Values.executor.count}}
  podManagementPolicy: "Parallel"
  revisionHistoryLimit: 1
  selector:
    matchLabels:
      app: dremio-executor
  template:
    metadata:
      labels:
        app: dremio-executor
        role: dremio-cluster-pod
      annotations:
        dremio-configmap/checksum: {{ (.Files.Glob "config/*").AsConfig | sha256sum }}
    spec:
      terminationGracePeriodSeconds: 5
      {{- if .Values.nodeSelector }}
      nodeSelector:
        {{- range $key, $value := .Values.nodeSelector }}
        {{ $key }}: {{ $value }}
        {{- end }}
      {{- end }}
      containers:
      - name: dremio-executor
        image: {{.Values.image}}:{{.Values.imageTag}}
        imagePullPolicy: IfNotPresent
        securityContext:
          runAsUser: 0
        resources:
          requests:
            memory: {{.Values.executor.memory}}M
            cpu: {{.Values.executor.cpu}}
        volumeMounts:
        - name: dremio-executor-volume
          mountPath: /opt/dremio/data
       ##################### START added this section #####################
        - name: dremio-connector
          mountPath: /opt/dremio/jars
       #################### END added this section ##########################
        - name: dremio-config
          mountPath: /opt/dremio/conf
        env:
        - name: DREMIO_MAX_HEAP_MEMORY_SIZE_MB
          value: "{{ template "HeapMemory" .Values.executor.memory }}"
        - name: DREMIO_MAX_DIRECT_MEMORY_SIZE_MB
          value: "{{ template "DirectMemory" .Values.executor.memory }}"
        - name: DREMIO_JAVA_EXTRA_OPTS
          value: >-
            -Dzookeeper=zk-hs:2181
            -Dservices.coordinator.enabled=false
            {{- if .Values.extraStartParams }}
            {{ .Values.extraStartParams }}
            {{- end }}
        command: ["/opt/dremio/bin/dremio"]
        args:
        - "start-fg"
        ports:
        - containerPort: 45678
          name: server
      initContainers:
      ################ START added this section ######################
      - name: installjars
        image: {{.Values.image}}:{{.Values.imageTag}}
        imagePullPolicy: IfNotPresent 
        securityContext:
          runAsUser: 0
        volumeMounts:
        - name: dremio-connector
          mountPath: /opt/dremio/jars
        command: ["/bin/sh","-c"]
        args: ["wget --no-check-certificate -O /dir/connector.jar https://<some nexus repo URL>/connector.jar; sleep 10;"]
      ################ END added this section ###############
      - name: wait-for-zk
        image: busybox
        command:  ["sh", "-c", "until ping -c 1 -W 1 zk-hs > /dev/null; do echo waiting for zookeeper host; sleep 2; done;"]
      # since we're mounting a separate volume, reset permission to
      # dremio uid/gid
      - name: chown-data-directory
        image: {{.Values.image}}:{{.Values.imageTag}}
        imagePullPolicy: IfNotPresent
        securityContext:
          runAsUser: 0
        volumeMounts:
        - name: dremio-executor-volume
          mountPath: /opt/dremio/data
        command: ["chown"]
        args:
        - "dremio:dremio"
        - "/opt/dremio/data"
      volumes:
      - name: dremio-config
        configMap:
          name: dremio-config
      {{- if .Values.imagePullSecrets }}
      imagePullSecrets:
        - name: {{ .Values.imagePullSecrets }}
      {{- end}}
     #################### START added this section ########################
      - name: dremio-connector
        emptyDir: {}
     #################### END added this section ########################
  volumeClaimTemplates:
  - metadata:
      name: dremio-executor-volume
    spec:
      accessModes: [ "ReadWriteOnce" ]
      {{- if .Values.storageClass }}
      storageClassName: {{ .Values.storageClass }}
      {{- end }}
      resources:
        requests:
          storage: {{.Values.executor.volumeSize}}
{{ end }}

所以上面的方法不起作用,一旦我“执行”到 pod 中,我看不到任何 jar 被下载。我不明白上面有什么问题。但是请注意,如果我在 pod 内运行相同的 wget 命令,它会下载让我感到困惑的 jar。所以 URL 是工作的,目录的读写没有问题,但仍然没有下载 jar ???

【问题讨论】:

  • 我认为从长远来看最好修改(或构建)另一个包含 jar 的镜像,这样您的 kubernets 配置更易于管理
  • args: 看起来不对。您将一个大字符串包装到一个数组中,但它应该是一个字符串数组。你试过改变它吗?您收到的错误信息是什么?

标签: kubernetes kubernetes-helm kubernetes-pod dremio


【解决方案1】:

如果您可以完全消除对 Wget 的需求,那会让生活更轻松...

选项 1

如果可以的话,使用你自己的 docker 镜像会减轻一些痛苦

Dockerfile

# docker build -f Dockerfile -t ghcr.io/yourOrg/projectId/dockerImageName:0.0.1 .
# docker push ghcr.io/yourOrg/projectId/dockerImageName:0.0.1

FROM nginx:1.19.10-alpine

# Use local copies of config
COPY files/some1.jar /dir/
COPY files/some2.jar /dir/

文件将在容器中准备好,不需要在您的 pod 定义中使用毫无意义的神秘命令。或者,如果您需要下载文件,您可以将执行该工作的脚本复制到 Docker 映像中,并在启动时通过 docker 指令 CMD 运行。

选项 2

或者,您可以进行两阶段部署...

  1. 创建持久卷
  2. 将卷安装到 pod(使用 busybox 作为基础?),该 pod 将运行足够的时间以便从本地计算机复制文件(或者如果您继续使用 Wget,则可以下载它们)
  3. kubectl cp(保留的)PersistentVolume 需要的文件
  4. 现在将 PV 挂载到 pod 的容器中,以便在 pod 启动时随时可以使用文件。

【讨论】:

  • 我尝试了选项 1,但它失败了,但我认为它失败了,因为 docker 映像不是由我构建的,它在构建时要求依赖关系,所以没有太多选择。选项2你能详细说明一下吗?它与我们下载文件并将其复制到主容器中的 InitContainer 选项相同,然后再启动它。能否请您详细说明选项 2。谢谢。
  • 而不是将文件复制到容器中,我建议您通过 pod/容器将文件复制到安装在容器中的持久卷中。然后您可以删除 pod,留下 PV完好无损,因为它可以“保留”并重新安装在您真正想要访问文件的容器/pod上。
  • "@robevans" 好主意。会试试看。感谢您的建议。
  • 谢谢@robevans。尝试了选项 1 并以某种方式使其工作。由于一些 PVC 问题,选项 2 奇怪地不起作用。
  • 也许您将 PVC 访问模式设置为 ReadWriteOnce 而不是 ReadWriteMany?
【解决方案2】:

您的方法似乎是正确的。 另一种解决方案可能是将 jar 包含在 Docker 映像中,但我认为这是不可能的吗?

您可以只使用emptyDir 而不是VolumeClaim

最后一个,我会在等待 ZooKeeper 获得一些时间之前下载 jar。

【讨论】:

  • 我编辑了问题。即使 wget 有效,我的解决方案也不起作用,一旦我在 pod 中执行 kubect exec 并手动运行 wget 命令会下载 jar。只是 initcontainer 在下载它时遇到了问题。
  • 您可以尝试使用上面提到的 emptyDir 吗?
  • 我添加了 emptyDir。它抛出错误。错误:无法找到或加载主类 com.dremio.dac.daemon.DremioDaemon。不知道出了什么问题。请检查文件,如果您发现缺少某些内容,请告诉我。如果我注释掉 container:/volumeMounts:/ dremio-connector 部分,那么它会启动,但文件不会复制到 /opt/dremio/jars 文件夹中。如果我将目录更改为 /opt/dremio/data 一切工作文件也会被下载。奇怪的问题。
  • 检索日志:kubectl logs --follow dremio-executor-0 -p -c installjars
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-15
  • 2016-08-22
  • 2022-01-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多