【问题标题】:Best way to check if a bucket is ready in node.js在 node.js 中检查存储桶是否准备就绪的最佳方法
【发布时间】:2018-07-26 12:05:35
【问题描述】:

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

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

我一直在考虑如何检查卷何时挂载,以及它是否下降。

为了检查它何时被挂载和卸载,我在/proc/mounts 中读取并搜索该卷的存在,并为其添加一个观察者以进行更改。

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

【问题讨论】:

    标签: node.js docker kubernetes google-cloud-storage gcsfuse


    【解决方案1】:

    您可以在特权模式下运行此 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 Cloud Storage [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

    【讨论】:

    • 你是对的。我虽然将 gcs 作为标准文件系统处理会更容易,但事实并非如此。
    猜你喜欢
    • 2016-04-07
    • 2011-07-08
    • 1970-01-01
    • 2015-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多