GMO NIKKOのT.Nです。
Google Cloud の Container Registry が非推奨になったことで、
既に Artifact Registry に移行したプロジェクトや、これから移行するプロジェクトも多いと思います。
2024年3月から自動移行ツールが使用できるようになったため、
今回は自動移行ツールでの移行について書きました。
移行方法
移行方法は以下の Google Cloud のサイトに記載されています。
https://cloud.google.com/artifact-registry/docs/transition/auto-migrate-gcr-ar?hl=ja
移行用のコマンドを実行すると、移行に必要な手順が自動的に実行されます。
移行先は以下の二通りがあります。
- Artifact Registry でホストされている gcr.
io リポジトリに移行する - 標準の Artifact Registry リポジトリに移行する
今回は標準の Artifact Registry リポジトリに移行する方を行いました。
実行するコマンド
標準の Artifact Registry リポジトリへの移行は、
以下のコマンドで行うことができます。
1 2 3 |
gcloud artifacts docker upgrade migrate \ --from-gcr=GCR_HOSTNAME/GCR_PROJECT \ --to-pkg-dev=AR_PROJECT/AR_REPOSITORY |
- GCR_HOSTNAME
- Container Registry のホスト名(gcr.io、us.gcr.io、asia.gcr.ioなど。)
- GCR_PROJECT
- 移行元の Google Cloud プロジェクト ID
- AR_PROJECT
- 移行先の Google Cloud プロジェクト ID
- AR_REPOSITORY
- 移行先のリポジトリ名
今回はasia.gcr.ioからの移行だったので、
asiaのマルチリージョンのリポジトリを、事前に Artifact Registry に作成しておきました。
移行コマンドを実行するためには、
Artifact Registry サービスアカウントに以下のロールを設定する必要があります。
- roles/storage.objectViewer (Storage オブジェクト閲覧者)
ロールを設定していないと以下のようなエラーが出ます。
1 2 3 4 5 6 |
Copying images to asia-docker.pkg.dev/******************/*****... failed. ERROR: The Artifact Registry service account doesn't have access to ****************** for copying images The following command will grant the necessary access (may take a few minutes): gcloud projects add-iam-policy-binding ****************** --member='serviceAccount:service-************@gcp-sa-artifactregistry.iam.gserviceaccount.com' --role='roles/storage.objectViewer' You can re-run this script after granting access. |
移行コマンドを実行すると、
移行が始まり、IAMの設定に関する選択肢が表示されます。
回答するとimageのコピーが始まり、待っていると移行が完了します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
$ gcloud artifacts docker upgrade migrate \ --from-gcr=asia.gcr.io/****************** \ --to-pkg-dev=******************/***** Potential IAM change for ***** repository in project ******************: bindings: - members: - serviceAccount:service-************@gcp-sa-gkenode.iam.gserviceaccount.com role: roles/artifactregistry.reader - members: - serviceAccount:********************@******************.iam.gserviceaccount.com - serviceAccount:********************@******************.iam.gserviceaccount.com - serviceAccount:********************@******************.iam.gserviceaccount.com - serviceAccount:service-************@dataflow-service-producer-prod.iam.gserviceaccount.com - serviceAccount:service-************@firebase-rules.iam.gserviceaccount.com - serviceAccount:service-************@gcp-sa-firestore.iam.gserviceaccount.com role: roles/artifactregistry.repoAdmin - members: - serviceAccount:service-************@compute-system.iam.gserviceaccount.com role: roles/artifactregistry.writer This IAM policy will grant users the ability to perform all actions in Artifact Registry that they can currently perform in Container Registry. This policy may allow access that was previously prevented by deny policies or IAM conditions. Warning: Generated bindings may be insufficient because you do not have access to analyze IAM for the following resources: ['organizations/************', 'folders/************', 'folders/************'] See https://cloud.google.com/policy-intelligence/docs/analyze-iam-policies#required-permissions [1] Apply above policy to the ******************/***** Artifact Registry repository [2] Edit policy [3] Do not change permissions for this repo [4] Exit Please enter your numeric choice (2): 1 Applying policy to repository ******************/***** Copying images to asia-docker.pkg.dev/******************/*****... done. ******************/*****: Successfully copied 264 additional tags and 392 additional manifests. There were 0 failures. Any reference to asia.gcr.io/****************** will still need to be updated to reference asia-docker.pkg.dev/******************/***** |
完了後には、
設定ファイルなどで Container Registry を参照している箇所を、
必要に応じて Artifact Registry を参照するように修正します。
今回の弊社での移行では、
移行後に Artifact Registry にDockerイメージをpushした時に、
以下のようなメッセージが表示されたので、
denied: Unauthenticated request. Unauthenticated requests do not have permission “artifactregistry.repositories.uploadArtifacts” on resource “projects/******************/locations/asia/repositories/******” (or it may not exist)
以下のコマンドで、~/.docker/config.jsonの内容を変更しました。
1 |
gcloud auth configure-docker asia-docker.pkg.dev |
まとめ
今回は gcloudコマンドで Container Registry から Artifact Registry に移行する方法についての記事でした。
今後移行するプロジェクトの参考になると良いです。