4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Cloud Buildから、デフォルトのkubectlコンテナ1 stepでGKEにデプロイ

Last updated at Posted at 2020-01-28

GCPは、アップデートが早いので、簡単にまとめてみる。
基本的な内容については、記載しない。

要約

  • gcr.io/cloud-builders/kubectlコンテナで、kustomizeは使える
  • gcr.io/cloud-builders/kubectlコンテナにクラスター名、ゾーン or リージョンを設定してデプロイ
  • gcloud container clusters get-credentialsは自動でやってくれる

ディレクトリ構成

.
├── deployments
│   └── kustomize
│       ├── kustomization.yaml
│       └── test-batch.yaml
└──  cloudbuild.yaml

kustomize

非常にシンプルなYAML管理ツール

現状でも、gcr.io/cloud-builders/kubectlは、v1.14なので、kustomizeは組み込まれている。

kubectl -k

kustomization.yaml

resources:
- test-batch.yaml

test-batch.yaml

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: test
spec:
  schedule: "1/* * * * *"
  jobTemplate:
    spec:
      backoffLimit: 3
      completions: 1
      parallelism: 1
      template:
        spec:
          containers:
          - name: busybox
            image: busybox
            command: "/bin/sh -c echo 'hello!!'"
          restartPolicy: OnFailure

cloud build

cloudbuild.yaml

gcr.io/cloud-builders/kubectlイメージを使用して、環境変数を正しく設定すると自動で認証情報を取得してくれる。

step:
  - name: "gcr.io/cloud-builders/kubectl"
    args:
      - "apply"
      - "-k"
      - "./deployments/kustomize"
    env:
      - "CLOUDSDK_COMPUTE_REGION=<cluster region> (regional clusters)"
      - "CLOUDSDK_COMPUTE_ZONE=<cluster zone>"
      - "CLOUDSDK_CONTAINER_CLUSTER=<cluster name>"

環境変数を設定しないと、下記のように怒られる。

Starting Step #1
Step #1: Already have image (with digest): gcr.io/cloud-builders/kubectl
Step #1:   No cluster is set. To set the cluster (and the region/zone where it is found), set the environment variables
Step #1:   CLOUDSDK_COMPUTE_REGION=<cluster region> (regional clusters)
Step #1:   CLOUDSDK_COMPUTE_ZONE=<cluster zone>
Step #1:   CLOUDSDK_CONTAINER_CLUSTER=<cluster name>
Finished Step #1

実行ログ

クラスター名: test-cluster
テストバッチ名: test

Starting Step #1
Step #1: Already have image (with digest): gcr.io/cloud-builders/kubectl
Step #1: Running: gcloud container clusters get-credentials --project="test-ci-gke" --region="asia-northeast1-a" "test-cluster"
Step #1: Fetching cluster endpoint and auth data.
Step #1: kubeconfig entry generated for test-cluster.
Step #1: Running: kubectl apply -k ./deployments/kustomize
Step #1: cronjob.batch/test created
Finished Step #1
4
1
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
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?