LoginSignup
1
3

More than 3 years have passed since last update.

K8s pod で gsutil を使うコツ

Posted at

盛大にハマりました。

GCP では通常 gcloud という cli tool を使いますが、BQ や GCS ではなぜか別で用意された bq や gsutil を使用します。この場合に gcloud おなじみの認証方法が使えません。普段手元で使っているぶんには自動で連携してくれるのですが、CI などまっさらな環境でいきなり走らせようとするとどうしたもんか? となるわけです。

TL;DR

これで動きます。(例)

GCP IAM で適切な権限 (Storage Admin 等) を付与した service account を作成し、JSON 形式の key file を download しておきます。

kubectl create secret generic sa-key --from-file=/Users/xxxx/Downloads/xxxx.json
apiVersion: v1
kind: Pod
metadata:
  name: test-gsutil
spec:
  containers:
  - name: test-gsutil-ls
    image: gcr.io/cloud-builders/gsutil
    command:
      - gsutil
    args:
      - -DD
#      - -q
      - -o
      - Credentials:gs_service_key_file=/secret/xxxx.json
      - ls
      - gs://<bucket-name>/<path>
    volumeMounts:
    - name: gcp-sa-key
      mountPath: /secret
  volumes:
  - name: gcp-sa-key
    secret:
      secretName: sa-key
  restartPolicy: Never

解説・ハマったポイント

gcloud とおなじ認証が使えない

おなじみの環境変数 GOOGLE_APPLICATION_CREDENTIALS は無視されます。上にあるように、option で渡してやることができます。または、下で説明する BOTO を使うことができるようです。(未検証)

minikube ならではの罠・時刻ズレ

試行錯誤の途中で急に下のエラーが出るようになりました。

ERROR: (gcloud.auth.activate-service-account) There was a problem refreshing your current auth tokens: invalid_grant: Invalid JWT: Token must be a short-lived token (60 minutes) and in a reasonable timeframe. Check your iat and exp values and use a clock with skew to account for clock differences between systems.

Mac minikube を hyperkit で使っている場合、Mac を sleep した時間ぶんの minikube 環境との時刻ズレが自動で sync されないようです。気づけば簡単なことなのですが、ずいぶん時間をとられました。いくつか workaround はあるようですが、とりあえず minikube を再起動してやればなおります。
参考 https://github.com/kubernetes/minikube/issues/1378

その先

BOTO config

gsutil は BOTO という設定ファイルをサポートしています。
https://cloud.google.com/storage/docs/boto-gsutil
何が設定できるかは下の gsutil config のページにありますが、詳細は gsutil config で生成される実際の boto file 内を見てねということのようです。
処理並列数などを設定できるようで、なかなか便利に使えそうです。(未検証)

gsutil の option、parameter

gsutil -DD で debug 情報を表示したり、-q で逆に出力を抑制したりできます。
https://cloud.google.com/storage/docs/gsutil/addlhelp/TopLevelCommandLineOptions
gsutil config でさまざま設定をつくるすることができ、試行錯誤に役立つかもしれません。
https://cloud.google.com/storage/docs/gsutil/commands/config

感想

gcloud に統合していただきたいです。
GCP Cloud Build で動かすならこんな苦労は無いんでしょうね。縛られたくなかったんです。

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3