目的
Kanikoはコンテナイメージを作成するためのコンテナです。最近はCI/CDのパイプラインそのものをコンテナで実装するケースが増えてきていますが、その中でイメージを生成してレジストリに登録する際によく使われるツールです。
さて、IBM Cloud Kubernetes Service(以下、IKS)でKanikoを使用してIBM Cloud Container Registry(以下、ICR)にイメージをプッシュする際に、レジストリのトークンをうまくKanikoに渡す必要がありますので、その方法を紹介します。
方法
Secretの確認
最近プロビジョンしたIKSであれば、下記のトークンがSecretとして登録されているはずです。
$ kubectl get secret -n default | grep icr
default-au-icr-io kubernetes.io/dockerconfigjson 1 20d
default-de-icr-io kubernetes.io/dockerconfigjson 1 20d
default-icr-io kubernetes.io/dockerconfigjson 1 20d
default-jp-icr-io kubernetes.io/dockerconfigjson 1 20d
default-uk-icr-io kubernetes.io/dockerconfigjson 1 20d
default-us-icr-io kubernetes.io/dockerconfigjson 1 20d
これを利用します。
その1. ServiceAccountのSecretを追加する
defaultのServiceAccountを使用しているとします。kubectl patchまたはeditでsecretsにイメージをプッシュしたいリージョンのsecretを追加します。
$ kubectl edit sa default
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: v1
imagePullSecrets:
- name: default-icr-io
- name: default-us-icr-io
- name: default-uk-icr-io
- name: default-de-icr-io
- name: default-au-icr-io
- name: default-jp-icr-io
kind: ServiceAccount
metadata:
creationTimestamp: "2019-09-02T02:18:45Z"
name: default
namespace: default
resourceVersion: "2648750"
selfLink: /api/v1/namespaces/default/serviceaccounts/default
uid: fd8c5622-cd27-11e9-aad6-82ec3bdf12d1
secrets:
- name: default-token-2h2lz
- name: default-jp-icr-io # <- 追加(jp.icr.ioの場合)
その2. SecretをVolumeMountして渡す
方法1はServiceAccountの設定変更をすることになるので、嫌な人は、Kaniko実行時にSecretをファイルとしてマウントして見せる方法もあります。
env:
- name: "DOCKER_CONFIG"
value: "/builder/home/.docker/"
volumeMounts:
- name: kaniko-secret
mountPath: /builder/home/.docker/config.json
subpath: .dockerconfigjson
volumes:
- name: kaniko-secret
secret:
secretName: default-jp-icr-io
以上です。