Google Cloud Build

ビルド構成ファイルによるビルド

yamlディレクトリの指定に「../」が使えない

だからソースとかはDockerfileがあるディレクトリ配下に設置

OK

steps:
- name: 'gcr.io/cloud-builders/docker'
  args: [ 'build', '-t', 'gcr.io/$PROJECT_ID/test', '.' ]

NG

steps:
- name: 'gcr.io/cloud-builders/docker'
  args: [ 'build', '-t', 'gcr.io/$PROJECT_ID/test', '../' ]

yamlからkubectl applyなど行うには権限設定が必要

https://cloud.google.com/cloud-build/docs/securing-builds/set-service-account-permissions?hl=ja

gitlab-CI連携

IAMでユーザ作成しjson吐く。権限与える必要あり。

試したが「プロジェクト管理者以外」通るものがわからず。いくつかの権限の組み合わせが必要のはず。

qiita.com

tech.enigmo.co.jp

※GitLabのCI/CD の「Variables」は「Protected」を外す

そして以下の変数が必要

  1. GCP_CICD_CREDS:iamのjson
  2. CLOUDSDK_CORE_PROJECT←名前固定:gcpのproject-id
GitLabRunner

↓のUsing binary fileで適当な環境のものを使用。

例)
sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64

docs.gitlab.com

「gitlab-runner register」コマンドで「executor」に設定するものが不明だったので↓の「Executorの種類」参照

www.forcia.com

「gitlab-runner register」したら、以下で有効化
useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
gitlab-runner start

↓参照

docs.gitlab.com

GitHubとかBitbucketとかは↓。これはCloudBuild側でランナー的なもの定義するやり方。

https://cloud.google.com/cloud-build/docs/automating-builds/create-manage-triggers?hl=ja

volumeClaimTemplates

mysqlの場合、「securityContext」にmysqlのUID指定必要。これやらないとmysqlが/var/lib/mysqlに対して権限無い。 「initContainers」で「lost+found」を削除しないと、mysql初期化エラーになる。

[ERROR] --initialize specified but the data directory has files in it. Aborting

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysqld
spec:
  replicas: 1
  serviceName: mysqld
  selector:
    matchLabels:
      app: mysqld
  template:
    metadata:
      labels:
        app: mysqld
    spec:
      containers:
      - name: mysqld
        image: gcr.io/{プロジェクトID}/wall-stickers-mysqld
        ports:
        - containerPort: 3306
        envFrom:
        - configMapRef:
            name: mysqld-conf
        - secretRef:
            name: mysqld-password
        imagePullPolicy: Always
        volumeMounts:
        - name: my-cnf
          mountPath: /etc/mysql/my.cnf
          subPath: my.cnf
        - name: mysqld-data
          mountPath: /var/lib/mysql
      volumes:
      - name: my-cnf
        configMap:
          name: mysqld-my-cnf
  volumeClaimTemplates:
  - metadata:
      name: mysqld-data
    spec:
      accessModes:
      - "ReadWriteOnce"
      storageClassName: standard
      resources:
        requests:
          storage: 1Gi

消すときは↓

kubectl delete pvc <PersistentVolumeClaimの名前>

.gitlab-ci.ymlでkustomizeを使用する

localでpullして、submitする。 やってることはimageをCloud Registoryにpush

git clone https://github.com/GoogleCloudPlatform/cloud-builders-community
cd cloud-builders-community/kustomize
gcloud builds submit --config cloudbuild.yaml .
↓localでのinstall方法

currentディレクトリで動く「./kustomize version」

kustomizeコマンドを手動で試したい時とかに使う。 kubernetes-sigs.github.io

cacheの利用

chacheをpullしてbuild時に利用すると、ビルドの最後になるまで、変更されたレイヤからすべてのレイヤが再構築される。 なので、Dockerfileではなるべく変更がありそうな個所を後に書く。

cloud.google.com

cloudbuild.yaml

# -----------------------------------------
# Pull cache
# -----------------------------------------
# httpd
- name: 'gcr.io/cloud-builders/docker'
  entrypoint: 'bash'
  args:
  - '-c'
  - |
    docker pull gcr.io/$PROJECT_ID/wall-stickers-httpd:$BRANCH_NAME || exit 0
  id: pull-httpd
# mysqld
- name: 'gcr.io/cloud-builders/docker'
  entrypoint: 'bash'
  args:
  - '-c'
  - |
    docker pull gcr.io/$PROJECT_ID/wall-stickers-mysqld:$BRANCH_NAME || exit 0
  id: pull-mysqld
  waitFor:
  - pull-httpd

# -----------------------------------------
# Build
# -----------------------------------------
# httpd
- name: 'gcr.io/cloud-builders/docker'
  args: [
    'build',
    '-t',
    'gcr.io/$PROJECT_ID/wall-stickers-httpd:$BRANCH_NAME',
    '--cache-from',
    'gcr.io/$PROJECT_ID/wall-stickers-httpd:$BRANCH_NAME',
    'docker/docker-httpd'
  ]
  id: build-httpd
  waitFor:
  - pull-mysqld
# mysqld
- name: 'gcr.io/cloud-builders/docker'
  args: [
    'build',
    '-t',
    'gcr.io/$PROJECT_ID/wall-stickers-mysqld:$BRANCH_NAME',
    '--cache-from',
    'gcr.io/$PROJECT_ID/wall-stickers-mysqld:$BRANCH_NAME',
    'docker/docker-mysqld'
  ]
  id: build-mysqld
  waitFor:
  - build-httpd

deployエラー

突然mysqlpodが以下エラーとなった

You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD

configMapRefやめてenvにしたり、namespaceごと削除してもダメ。
何故かlocalで「kubectl apply -k ./」したら正常になった。その後はコンテナ削除してgitlab-ci動かしても正常になった

ingressエラー

All backend services are in UNHEALTHY state 

ヘルスチェックが以下の設定らしい。

/ パスでの GET リクエストに対して、HTTP 200 ステータスのレスポンスを返す。

なのでアプリケーション側でsslリダイレクトとかやるとエラーになる