gcsfuse 命令失败,gcsfuse 恰好有两个参数

gcsfuse command fail with gcsfuse takes exactly two arguments

我正在使用 GCSFuse 将 GCS 存储桶安装到我在 JupyterHub 中的用户 pod,但它总是失败并显示错误消息 gcsfuse takes exactly two arguments

这是我的 DockerFile:

FROM jupyter/minimal-notebook:177037d09156

ENV GCSFUSE_REPO gcsfuse-stretch
ENV GOOGLE_APPLICATIONS_CREDENTIALS=test-serviceaccount.json
ENV GCS_BUCKET: "my-bucket"
ENV GCS_BUCKET_FOLDER: "shared-data"

USER root

# Add google repositories for gcsfuse and google cloud sdk
RUN apt-get update -y && apt-get install -y --no-install-recommends apt-transport-https ca-certificates curl gnupg
RUN echo "deb http://packages.cloud.google.com/apt $GCSFUSE_REPO main" | tee /etc/apt/sources.list.d/gcsfuse.list
RUN echo "deb https://packages.cloud.google.com/apt cloud-sdk main" | tee -a /etc/apt/sources.list.d/google-cloud-sdk.list
RUN curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -

# Install gcsfuse and google cloud sdk
RUN apt-get update -y  && apt-get install -y gcsfuse google-cloud-sdk \
    && apt-get autoremove -y \
    && apt-get clean -y \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# Switch back to notebook user (defined in the base image)
USER $NB_UID

# make directory for mounting
RUN mkdir -p home/shared-data \
    && mkdir -p etc/scripts

COPY start_mounting.sh etc/scripts

# install extra packages required for model training
RUN pip install --upgrade pip
RUN pip install fasttext
RUN pip install ax-platform

CMD ["bin/bash", "etc/scripts/start_mounting.sh"]

脚本:

#!/bin/bash

# Setup GCSFuse
 gcsfuse --key-file ${GOOGLE_APPLICATIONS_CREDENTIALS} ${GCS_BUCKET} ${GCS_BUCKET_FOLDER}

我的 jupyterhub config.yaml

hub:
  baseUrl: /jupyterhub
  extraConfig: |
    from kubernetes import client
    def modify_pod_hook(spawner, pod):
        pod.spec.containers[0].security_context = client.V1SecurityContext(
            privileged=True,
            capabilities=client.V1Capabilities(
                add=['SYS_ADMIN']
            )
          )
        pod.spec.containers[0].env.append(
              client.V1EnvVar(
                  name='GOOGLE_APPLICATIONS_CREDENTIALS',
                  value_from=client.V1EnvVarSource(
                      secret_key_ref=client.V1SecretKeySelector(
                          name='jhub-secret',
                          key='jhub-serviceaccount',
                      )
                  )
              )
          )
        return pod
    c.KubeSpawner.modify_pod_hook = modify_pod_hook

singleuser:
  storage:
    type: none
  extraEnv:
  GCS_BUCKET: "my-bucket"
  GCS_BUCKET_FOLDER: "shared-data"
  lifecycleHooks:
    postStart:
      exec:
        command: ["/bin/sh", "etc/scripts/start_mounting.sh"]
    preStop:
      exec:
        command: ["fusermount", "-u", "shared-data"]
  image:
    name: gcr.io/project/base-images/jhub-k8s-cust-singleuser
    tag: 1.1.6
    pullPolicy: Always

我正在覆盖 GOOGLE_APPLICATIONS_CREDENTIALS ENV,以便在 gcsfuse 的 --key-file 参数中使用它。

有人能告诉我这里有什么问题吗?我的 pod PostStart Exec 命令有问题吗?还是我的 gcsfuse 错了?

我不是 JupyterHub 的专家(甚至不是用户)。我的回答很笼统

我发现有 2 种方法可以解决您的问题

  • 您可以在运行时将秘密文件(如果文件中有 json 密钥)装载到容器中。但是我不知道实现这个的 jupyterhub 语法
  • 你可以试试这个

在您的 jupyterhub yaml 文件中,更改 json 密钥文件内容的环境变量

          pod.spec.containers[0].env.append(
              client.V1EnvVar(
                  name='GOOGLE_APPLICATIONS_CREDENTIALS_CONTENT',
                  value_from=client.V1EnvVarSource(
                      secret_key_ref=client.V1SecretKeySelector(
                          name='jhub-secret',
                          key='jhub-serviceaccount',
                      )
                  )
              )
          )

像这样更改您的脚本(将内容写入定义的文件):

#!/bin/bash

echo ${GOOGLE_APPLICATIONS_CREDENTIALS_CONTENT} > ${GOOGLE_APPLICATIONS_CREDENTIALS}

# Setup GCSFuse
 gcsfuse --key-file ${GOOGLE_APPLICATIONS_CREDENTIALS} ${GCS_BUCKET} ${GCS_BUCKET_FOLDER}

容器是不可变的。我认为这会起作用,因为更改仅在内存中执行。

注意:GOOGLE_APPLICATIONS_CREDENTIALS 文件路径定义最好使用绝对路径

我通过为 K8s secret(Google 服务帐户)创建卷安装并将其作为 ENV 传递到 gcsfuse 命令的脚本 start_mounting.sh 中来解决它。

下面是我使用的代码:

  storage:
      extraVolumes:
        - name: my-secret-jupyterhub
          secret:
            secretName: my-secret
      extraVolumeMounts:
        - name: my-secret-jupyterhub
          mountPath: /etc/secrets
          readOnly: true
    extraEnv:
      GOOGLE_APPLICATIONS_CREDENTIALS: /etc/secrets/key.json

这似乎比获取服务帐户的文件内容并再次将其放入 gcsfuse 命令的文件中更简洁,就像我之前所做的和上面讨论的那样。