我如何在 kubernetes 中使用 google 云存储?

How can I use google cloud storage inside kubernetes?

我正在使用节点 js 并且想将文件上传到我的存储桶。我已经设置了秘密:

NAME                         TYPE                                  DATA      AGE
cloudsql-oauth-credentials   Opaque                                1         5d
default-token-dv9kj          kubernetes.io/service-account-token   3         5d

service_account 确实可以访问我的 google 云存储 API,因为我已经在本地(在我自己的计算机上)进行了设置和测试。我不确定如何引用 服务帐户 json 文件的位置?!

这是我的卷挂载:

"volumes": [{
    "name": "cloudsql-oauth-credentials",
     "secret": {
         "secretName": "cloudsql-oauth-credentials"
     }
 }

这是我设置 google-云存储变量的代码:

var gcs = require('@google-cloud/storage')({
  projectId: 'projectID-38838',
  keyFilename: process.env.NODE_ENV == 'production' 
      ? JSON.parse(process.env.CREDENTIALS_JSON) // Parsing js doesn't work
      : '/Users/james/auth/projectID-38838.json' // This works locally
});
var bucket = gcs.bucket('bucket-name');

现在,如果我想在 kubernetes 上的 docker 容器中使用它,我必须参考 json 文件位置...但我不知道它在哪里? !

我试过将凭证文件设置为环境变量,但我无法将 js 对象解析为 keyFilename 对象。我必须解析一个文件位置。我像这样设置环境变量:

{           
 "name": "CREDENTIALS_JSON",
  "valueFrom": {
        "secretKeyRef": {
             "name": "cloudsql-oauth-credentials",
             "key": "credentials.json"
        }
   }
 },

如何在我的 kubernetes pod 中引用 service_account json 文件的位置?!

查看 here 部分中的 使用机密作为 Pod 中的文件

基本上,您需要在安装秘密卷时指定两件事。你拥有的那一点+一些额外的信息。 密钥可能有一些冗余,但这是我所做的并且有效。

创建秘密时,使用密钥创建它:
kubectl create secret generic cloudsql-oauth-credentials --from-file=creds=path/to/json

然后

"volumes": [{
    "name": "cloudsql-oauth-credentials",
    "secret": {
        "secretName": "cloudsql-oauth-credentials"
        "items": [{
            "key": "creds",
            "path": "cloudsql-oauth-credentials.json"
        }]

    }
}

然后还要指定它在容器定义中的位置(在 Pod、Deployment、Replication Controller 中 - 无论您使用什么):

"spec": {
    "containers": [{
        "name": "mypod",
        "image": "myimage",
        "volumeMounts": [{
            "name": "cloudsql-oauth-credentials",
            "mountPath": "/etc/credentials",
            "readOnly": true
        }]
}],

文件将映射到 /etc/credentials/cloudsql-oauth-credentials.json