Back to list
dev_to 2026年3月21日

FluxCD を使ってコンテナイメージの更新を自動化する(ハンズ・オン・チュートリアル)

Automating Container Image Updates with FluxCD (Hands-On Tutorial)

Translated: 2026/3/21 6:05:04

Japanese Translation

現代の GitOps ワークフローは、Kubernetes クラスターを Git で定義された状態と完全に同期させようとしています。Flux の強力な機能の一つに「Image Automation」があり、新規イメージが利用可能になった際に、Git リポジトリ内のコンテナイメージのタグを自動的に更新します。 このチュートリアルでは、イメージ自動化の仕組みと、Git 認証に関する一般的なトラブルシューティング方法を詳しく解説します。 FluxCD は、以下の構成要素を組み合わせてイメージの更新を自動化します。 ワークフローは以下のようになります: Container Registry Git に手動でイメージタグを更新する代わりに、Flux はコンテナレジストリに一致するイメージが出現した際、新しいタグを自動的にコミットします。 例:GitOps レイアウト ``` flux-minikube-lab ├── apps │ └── web-server │ ├── web-server.yaml │ ├── sealed-db-pass.yaml │ └── kustomization.yaml │ └── clusters └── my-cluster ├── image-reflect.yaml ├── image-policy.yaml ├── web-server-sync.yaml └── kustomization.yaml ``` クラスターの Kustomization は、インフラおよびアプリケーションのシン크リソースを参照しています。 Web サーバーのメンフェストは、以下の場所に定義されています: apps/web-server/ 例:kustomization.yaml ``` resources: - web-server.yaml - sealed-db-pass.yaml ``` Flux Kustomization sync オブジェクトはアプリをデプロイします: ``` kind: Kustomization metadata: name: web-server-sync namespace: flux-system spec: interval: 1m path: ./apps/web-server prune: true sourceRef: kind: GitRepository name: flux-system targetNamespace: engineering ``` これは、Flux に engineering ネームスペースに Web サーバーをデプロイするよう指示します。 以下の 3 つのリソースが自動化された更新を可能にします: 1. ImageRepository - コンテナレジストリのタグをスキャンします。 2. ImagePolicy - どのタグが受け入れ可能なかを定義します(例:semver または latest)。 3. ImageUpdateAutomation - 新しいタグがポリシーに一致するたびに Git リポジトリを更新します。 手動で自動化をトリガーするには: ``` flux reconcile image update flux-system ``` Flux は以下の作業を行います: 1. レジストリのタグを確認する 2. イメージポリシーを適用する 3. Git 内のメンフェストを更新する 4. 変更をコミットしてプッシュする 自動化をテストしながら、以下のエラーが発生しました: ``` failed to update source: failed to push to remote: ERROR: The key you are authenticating with has been marked as read only ``` 自動化の状態を確認: ``` flux get image update ``` 出力: ``` READY: False MESSAGE: failed to push to remote ``` ImageUpdateAutomation は Git リポジトリへの更新をコミットするために書込み権限が必要です。しかし、リポジトリの認証キーは只読みとして設定されていました。 これは、Flux にメンフェストをプルすることを可能にしつつ、更新をプッシュすることを阻止していました。 ``` ssh-keygen ``` リポジトリの設定に移動します: ``` Settings → Deploy Keys ``` 新しい公開キーを追加し、以下の設定を有効にします: ``` Allow write access ``` ``` flux create secret git flux-system \ --url=ssh://git@github.com/pilgrim2go/flux-minikube-lab \ --private-key-file=$PWD/fluxcd-test \ -n flux-system ``` これは、Flux によって使用される認証情報を更新します。 ``` flux reconcile image update flux-system ``` 検証: ``` flux get image update ``` 期待される結果: ``` READY: True MESSAGE: committed and pushed update ``` リポジトリには、更新されたイメージタグ付きの新規コミットが現れるはずです。 Flux リソースを確認: ``` flux get all ``` 自動化の状態を点検: ``` flux get image update ``` コントローラーログを表示: ``` kubectl logs -n flux-system deploy/image-automation-controller ``` Git の同期をトリガー: ``` flux reconcile source git flux-system ``` Flux イメージ自動化を使用する場合: - 専用デプロイ キーまたはボット アカウントを使用し、読取および書込アクセスを許可します。 - 単一の真実のソースを確保します。 Flux イメージ自動化は、手動のイメージ更新を排除し、Kubernetes ワークロードが常に最新の承認されたコンテナイメージを実行していることを保証します。 適切な Git 認証設定があれば、Flux は自動的に以下を行うことができます: - 新規コンテナイメージを検出します。 これにより、完全に自動化され、監査可能な Kubernetes デリバリーパイプラインを実現できます。

Original Content

Modern GitOps workflows aim to keep your Kubernetes cluster fully synchronized with what is defined in Git. One powerful feature of Flux is Image Automation, which automatically updates container image tags in your Git repository whenever a new image becomes available. In this tutorial, we walk through how image automation works and how to troubleshoot a common issue involving Git authentication. FluxCD provides several components that work together to automate image updates. The workflow looks like this: Container Registry Instead of manually updating image tags in Git, Flux automatically commits the new tag when a matching image appears in your container registry. Example GitOps layout: flux-minikube-lab ├── apps │ └── web-server │ ├── web-server.yaml │ ├── sealed-db-pass.yaml │ └── kustomization.yaml │ └── clusters └── my-cluster ├── image-reflect.yaml ├── image-policy.yaml ├── web-server-sync.yaml └── kustomization.yaml The cluster Kustomization references infrastructure and application sync resources. The web server manifests are defined in: apps/web-server/ Example kustomization.yaml: resources: - web-server.yaml - sealed-db-pass.yaml A Flux Kustomization sync object deploys the app: kind: Kustomization metadata: name: web-server-sync namespace: flux-system spec: interval: 1m path: ./apps/web-server prune: true sourceRef: kind: GitRepository name: flux-system targetNamespace: engineering This instructs Flux to deploy the web server into the engineering namespace. Three resources enable automated updates: This scans container registry tags. kind: ImageRepository Defines which tags are acceptable (e.g., semver or latest). kind: ImagePolicy Updates the Git repo when a new tag matches the policy. kind: ImageUpdateAutomation To manually trigger automation: flux reconcile image update flux-system Flux will: Check registry tags Apply the image policy Update manifests in Git Commit and push the change While testing automation, the following error appeared: failed to update source: failed to push to remote: ERROR: The key you are authenticating with has been marked as read only Checking the automation status: flux get image update Output: READY: False MESSAGE: failed to push to remote ImageUpdateAutomation needs write access to the Git repository to commit updated image tags. However, the repository authentication key was configured as read-only. This allowed Flux to pull manifests but prevented it from pushing updates. ssh-keygen Navigate to the repository settings: Settings → Deploy Keys Add the new public key and enable: Allow write access flux create secret git flux-system \ --url=ssh://git@github.com/pilgrim2go/flux-minikube-lab \ --private-key-file=$PWD/fluxcd-test \ -n flux-system This updates the authentication credentials used by Flux. flux reconcile image update flux-system Verify: flux get image update Expected result: READY: True MESSAGE: committed and pushed update A new commit should appear in the repository with the updated image tag. Check Flux resources: flux get all Inspect automation status: flux get image update View controller logs: kubectl logs -n flux-system deploy/image-automation-controller Trigger Git sync: flux reconcile source git flux-system When using Flux image automation: • Use a dedicated deploy key or bot account read and write access single source of truth Flux image automation eliminates manual image updates and ensures that your Kubernetes workloads always run the latest approved container images. With the proper Git authentication setup, Flux can automatically: • Detect new container images This enables a fully automated and auditable Kubernetes delivery pipeline.