在 node.js 中检查存储桶是否准备就绪的最佳方法

Best way to check if a bucket is ready in node.js

我正在使用 gcsfuse 在容器中安装一个卷,我需要它来启动我的 node.js 应用程序。

为了挂载卷,我使用了 kubernetes 的 生命周期挂钩 ,但它不能确保它会在我的容器的入口点之前执行。

我一直在想我应该如何检查卷何时安装,以及它是否关闭。

为了检查它何时安装和卸载,我阅读并搜索了 /proc/mounts 中卷的存在,并向其添加了一个观察程序以进行更改。

是否有更简单的方法来确保卷安装在 node.js、docker 或 kubernetes 中?

你可以在特权模式下运行这个dockerfile:

FROM ubuntu 
RUN echo "deb http://packages.cloud.google.com/apt gcsfuse-stretch main" | tee /etc/apt/sources.list.d/gcsfuse.list 
RUN apt-get update && apt install curl -y 
RUN curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add - 
RUN apt-get update 
RUN apt-get install gcsfuse fuse -y 
RUN mkdir -p /mnt/tmp 
CMD gcsfuse [BUCKET NAME] /mnt/tmp && /bin/bash

这样您就可以确保在 pod 初始化时安装了存储桶。

另一方面,我不推荐这种方法,因为 Google 云存储 [1] 有一个 Node.sj 库。

这里是桶列表的例子:

// Imports the Google Cloud client library
const Storage = require('@google-cloud/storage');

// Creates a client
const storage = new Storage();

// Lists all buckets in the current project
storage
  .getBuckets()
  .then(results => {
    const buckets = results[0];

    console.log('Buckets:');
    buckets.forEach(bucket => {
      console.log(bucket.name);
    });
  })
  .catch(err => {
    console.error('ERROR:', err);
  });

[1] https://github.com/googleapis/nodejs-storage/tree/master/samples