# ArgoCDでKubernetesにGitOpsを実装

はじめに

GitOpsは、Gitをインフラストラクチャとアプリケーション定義の唯一の信頼源とするモダンな継続的デリバリーアプローチです。ArgoCDはKubernetes向けの最も人気のあるGitOpsツールで、Gitリポジトリを監視してクラスター状態を一致させることでデプロイを自動化します。

このチュートリアルでは、ArgoCDのインストールから、自動同期、ヘルスモニタリング、シークレット管理を備えたマルチ環境アプリケーションのデプロイまで、完全なGitOpsパイプラインを構築します。

前提条件

  • Kubernetesクラスター(minikube、kind、またはクラウドマネージド)
  • kubectl 設定済み
  • helm v3 インストール済み
  • Gitリポジトリ(GitHub/GitLab)
  • Kubernetesの基本知識
  • ステップ1: ArgoCDのインストール

    まず、クラスターにArgoCDをインストールします:

    # 名前空間の作成
    

    kubectl create namespace argocd

    # ArgoCDのインストール

    kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml

    # Podの準備完了を待機

    kubectl wait --for=condition=Ready pods --all -n argocd --timeout=300s

    # 初期管理者パスワードの取得

    argocd_password=$(kubectl -n argocd get secret argocd-initial-admin-secret \

    -o jsonpath="{.data.password}" | base64 -d)

    echo "ArgoCD管理者パスワード: $argocd_password"

    ArgoCDサーバーを公開します:

    # ローカルアクセス用のポートフォワード
    

    kubectl port-forward svc/argocd-server -n argocd 8080:443 &

    # ArgoCD CLIのインストール

    curl -sSL -o argocd https://github.com/argoproj/argo-cd/releases/latest/download/argocd-linux-amd64

    chmod +x argocd && sudo mv argocd /usr/local/bin/

    # ログイン

    argocd login localhost:8080 --username admin --password $argocd_password --insecure

    ステップ2: GitOpsリポジトリ構造の設定

    整理されたGitリポジトリを作成します:

    gitops-demo/
    

    ├── apps/ # ArgoCDアプリケーション定義

    │ ├── dev/

    │ │ └── web-app.yaml

    │ ├── staging/

    │ │ └── web-app.yaml

    │ └── production/

    │ └── web-app.yaml

    ├── base/ # 基本Kubernetesマニフェスト

    │ ├── deployment.yaml

    │ ├── service.yaml

    │ ├── ingress.yaml

    │ └── kustomization.yaml

    ├── overlays/ # 環境固有のパッチ

    │ ├── dev/

    │ ├── staging/

    │ └── production/

    └── sealed-secrets/

    └── web-app-secrets.yaml

    ステップ3: 基本Kubernetesマニフェストの作成

    base/deployment.yaml

    apiVersion: apps/v1
    

    kind: Deployment

    metadata:

    name: web-app

    labels:

    app: web-app

    spec:

    replicas: 1

    selector:

    matchLabels:

    app: web-app

    template:

    metadata:

    labels:

    app: web-app

    spec:

    containers:

    - name: web-app

    image: nginx:1.25-alpine

    ports:

    - containerPort: 80

    livenessProbe:

    httpGet:

    path: /healthz

    port: 80

    initialDelaySeconds: 10

    periodSeconds: 10

    readinessProbe:

    httpGet:

    path: /ready

    port: 80

    initialDelaySeconds: 5

    periodSeconds: 5

    resources:

    requests:

    cpu: 100m

    memory: 128Mi

    limits:

    cpu: 250m

    memory: 256Mi

    ステップ4: 環境オーバーレイの作成

    overlays/production/kustomization.yaml

    apiVersion: kustomize.config.k8s.io/v1beta1
    

    kind: Kustomization

    namespace: production

    namePrefix: prod-

    bases:

    - ../../base

    patchesStrategicMerge:

    - patch-replicas.yaml

    - patch-resources.yaml

    labels:

    - pairs:

    environment: production

    ステップ5: ArgoCDアプリケーションの定義

    apiVersion: argoproj.io/v1alpha1
    

    kind: Application

    metadata:

    name: web-app-dev

    namespace: argocd

    spec:

    project: default

    source:

    repoURL: https://github.com/your-org/gitops-demo.git

    targetRevision: main

    path: overlays/dev

    destination:

    server: https://kubernetes.default.svc

    namespace: dev

    syncPolicy:

    automated:

    prune: true

    selfHeal: true

    syncOptions:

    - CreateNamespace=true

    ステップ6: Sealed Secretsによるシークレット管理

    # Sealed Secretsコントローラーのインストール
    

    helm repo add sealed-secrets https://bitnami-labs.github.io/sealed-secrets

    helm install sealed-secrets sealed-secrets/sealed-secrets -n kube-system

    # シークレットの作成とシール

    kubectl create secret generic web-app-secrets \

    --from-literal=DATABASE_URL='postgresql://user:pass@db:5432/app' \

    --dry-run=client -o yaml > /tmp/secret.yaml

    kubeseal --format yaml < /tmp/secret.yaml > sealed-secrets/web-app-secrets.yaml

    rm /tmp/secret.yaml

    ステップ7: App of Appsパターン

    apiVersion: argoproj.io/v1alpha1
    

    kind: Application

    metadata:

    name: root-app

    namespace: argocd

    spec:

    project: default

    source:

    repoURL: https://github.com/your-org/gitops-demo.git

    targetRevision: main

    path: apps

    directory:

    recurse: true

    destination:

    server: https://kubernetes.default.svc

    syncPolicy:

    automated:

    prune: true

    selfHeal: true

    ステップ8: Argo Rolloutsによるカナリアデプロイ

    apiVersion: argoproj.io/v1alpha1
    

    kind: Rollout

    metadata:

    name: web-app

    spec:

    replicas: 3

    strategy:

    canary:

    steps:

    - setWeight: 10

    - pause: { duration: 2m }

    - setWeight: 30

    - pause: { duration: 2m }

    - setWeight: 60

    - pause: { duration: 2m }

    - setWeight: 100

    まとめ

    本番環境対応のGitOpsパイプラインを構築しました:

  • ✅ ArgoCDのインストールと設定
  • ✅ Kustomizeベースのマルチ環境セットアップ
  • ✅ devの自動同期、本番の手動承認
  • ✅ Sealed Secretsによるシークレット管理
  • ✅ App of Appsパターン
  • ✅ Argo Rolloutsによるカナリアデプロイ

GitOpsにより、クラスター状態は常にGitと一致し、監査可能で再現可能かつ復旧可能です。