将元数据传递给gce中的containers命令

时间:2015-01-27 13:59:50

标签: metadata google-compute-engine

我想将api密钥(例如:twitter密钥)存储到元数据中。 然后从子容器的命令行中使用它。

#api-keys.yaml:
TWITTER-CONSUMER-KEY: blahah
TWITTER-CONSUMER-SECRET: blihih

#google-container-manifest.yaml:
version: v1beta2
containers:
  - name: tweet-thing-one
    image: gcr.io/my-project/tweetfeed
    command:
     - --consumer-key=$TWITTER-CONSUMER-KEY
     - --consumer-secret=$TWITTER-CONSUMER-SECRET
     - --params-for=one
  - name: tweet-thing-two
    image: gcr.io/my-project/tweetfeed
    command:
     - --consumer-key=$TWITTER-CONSUMER-KEY
     - --consumer-secret=$TWITTER-CONSUMER-SECRET
     - --params-for=two
...

所以我可以运行

$ gcloud compute instances create containervm-test-1 \
    --image container-vm \
    --metadata-from-file api-keys=api-keys.yaml \
                         google-container-manifest=google-container-manifest.yaml \
    --zone us-central1-a \
    --machine-type f1-micro

谢谢!

1 个答案:

答案 0 :(得分:2)

看起来像两个不同的问题: (1)api-keys.yaml文件未被转换为VM内的环境变量;相反,那些需要直接从元数据服务器查询。参见例如Instance environment variableshttps://developers.google.com/compute/docs/metadata#querying

(2)如果指定"命令:"在清单中,您需要指定要运行的整个命令,而不仅仅是标志。来自https://cloud.google.com/compute/docs/containers/container_vms

  

containers []。command [] 字符串列表要运行的命令行。如果省略,则假定容器中嵌入了一个命令。"