Open6

[DevOps] 次世代のCICDツール、Flux CDを試す [ArgoCDよりも良い?]

harrythecodeharrythecode

試す

https://fluxcd.io/flux/get-started/ の通り進めます。

  • FluxをBrewでインストール
brew install fluxcd/tap/flux
export GITHUB_TOKEN=<your-token>
  • K8s環境チェック
flux check --pre
► checking prerequisites
✔ Kubernetes 1.27.3 >=1.24.0-0
✔ prerequisites checks passed
flux bootstrap github \
  --owner=harrythecode \
  --repository=flux-demo \
  --path=clusters/my-cluster \
  --personal
► pushing sync manifests to "https://github.com/harrythecode/flux-demo.git"
► applying sync manifests
✔ reconciled sync configuration
◎ waiting for Kustomization "flux-system/flux-system" to be reconciled

一向に動かないので確認したところ CPUリソースが足りない、と怒られてました。podmanのデフォルト設定は 1cpuしか割り当てられてなかったので 4cpuに変更して再度挑戦しました。

flux-system内に新しくリソースができてますね。

$ kubectl get pod -A
NAMESPACE            NAME                                         READY   STATUS    RESTARTS   AGE
flux-system          helm-controller-74b5f87d94-8g54c             1/1     Running   0          4m57s
flux-system          kustomize-controller-59d4cb8bc6-bxk8l        1/1     Running   0          4m57s
flux-system          notification-controller-b7d8566b7-gmwz9      1/1     Running   0          4m57s
flux-system          source-controller-645656595b-qgmhm           1/1     Running   0          4m57s
kube-system          coredns-5d78c9869d-pjrrm                     1/1     Running   0          5m12s
kube-system          coredns-5d78c9869d-sjpzj                     1/1     Running   0          5m12s
kube-system          etcd-kind-control-plane                      1/1     Running   0          5m26s
kube-system          kindnet-hl85l                                1/1     Running   0          5m13s
kube-system          kube-apiserver-kind-control-plane            1/1     Running   0          5m26s
kube-system          kube-controller-manager-kind-control-plane   1/1     Running   0          5m26s
kube-system          kube-proxy-h2r7j                             1/1     Running   0          5m13s
kube-system          kube-scheduler-kind-control-plane            1/1     Running   0          5m26s
local-path-storage   local-path-provisioner-6bc4bddd6b-b8bll      1/1     Running   0          5m12s
harrythecodeharrythecode

確認する

連携したgithub上にはkustomizeで以下のようにファイルがCommitされてました。

https://github.com/harrythecode/flux-demo/tree/main

更にデプロイする

  • 先ほどのflux-demoをローカルにCloneした上で以下のコマンドを実行。
flux create source git podinfo \
  --url=https://github.com/stefanprodan/podinfo \
  --branch=master \
  --interval=1m \
  --export > ./clusters/my-cluster/podinfo-source.yaml
  • 以下のようなファイルができてることを確認
flux-demo $ git status
On branch main
Your branch is up to date with 'origin/main'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	clusters/my-cluster/podinfo-source.yaml

nothing added to commit but untracked files present (use "git add" to track)
flux-demo (main*)$ cat clusters/my-cluster/podinfo-source.yaml
---
apiVersion: source.toolkit.fluxcd.io/v1
kind: GitRepository
metadata:
  name: podinfo
  namespace: flux-system
spec:
  interval: 1m0s
  ref:
    branch: master
  url: https://github.com/stefanprodan/podinfo
  • 変更をgithubにアップロード
git add -A && git commit -m "Add podinfo GitRepository"
git push
harrythecodeharrythecode

podinfoアプリのデプロイ

  • 以下のコマンドを実行
flux create kustomization podinfo \
  --target-namespace=default \
  --source=podinfo \
  --path="./kustomize" \
  --prune=true \
  --wait=true \
  --interval=30m \
  --retry-interval=2m \
  --health-check-timeout=3m \
  --export > ./clusters/my-cluster/podinfo-kustomization.yaml
$ cat clusters/my-cluster/podinfo-kustomization.yaml
---
apiVersion: kustomize.toolkit.fluxcd.io/v1
kind: Kustomization
metadata:
  name: podinfo
  namespace: flux-system
spec:
  interval: 30m0s
  path: ./kustomize
  prune: true
  retryInterval: 2m0s
  sourceRef:
    kind: GitRepository
    name: podinfo
  targetNamespace: default
  timeout: 3m0s
  wait: true
  • 変更をgithubにあげる
git add -A && git commit -m "Add podinfo Kustomization"
git push
  • 中身の構成は以下のようになる。
$ tree
.
├── README.md
└── clusters
    └── my-cluster
        ├── flux-system
        │   ├── gotk-components.yaml
        │   ├── gotk-sync.yaml
        │   └── kustomization.yaml
        ├── podinfo-kustomization.yaml
        └── podinfo-source.yaml

しばらくすると先ほど定義したリソースがK8s上に展開されてました。

$ flux get kustomizations
NAME       	REVISION            	SUSPENDED	READY	MESSAGE
flux-system	main@sha1:d34afc6c  	False    	True 	Applied revision: main@sha1:d34afc6c
podinfo    	master@sha1:4892983f	False    	True 	Applied revision: master@sha1:4892983f

$ kubectl -n default get deployments,services
NAME                      READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/podinfo   2/2     2            2           59s

NAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)             AGE
service/kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP             27m
service/podinfo      ClusterIP   10.96.114.185   <none>        9898/TCP,9999/TCP   59s
harrythecodeharrythecode

はまったこと

リソース展開に失敗した場合の通知がない

以下のようにリソースを展開しましたが何も起きませんでした。

clusters/my-cluster/podinfo-source.yaml
  ref:
    branch: master
  url: https://github.com/stefanprodan/podinfo
+ ---
+ apiVersion: source.toolkit.fluxcd.io/v1beta2
+ kind: HelmRepository
+ metadata:
+   name: podinfo
+   namespace: flux-system
+ spec:
+   interval: 1m0s
+   url: https://stefanprodan.github.io/podinfo
clusters/my-cluster/podinfo-kustomization.yaml
  targetNamespace: default
  timeout: 3m0s
  wait: true
+ ---
+ apiVersion: helm.toolkit.fluxcd.io/v2beta1
+ kind: HelmRelease
+ metadata:
+   name: podinfo-helm
+   namespace: flux-system
+ spec:
+   chart:
+     spec:
+       chart: podinfo
+       sourceRef:
+         kind: HelmRepository
+         name: podinfo
+   values:
+     replicaCount: 2
+     logLevel: debug

ログを見てみると以下の通りエラーが出てました。

kubectl logs -n flux-system kustomize-controller-59d4cb8bc6-zlklm

{"level":"error","ts":"2023-08-23T15:03:11.255Z","msg":"Reconciliation failed after 429.731958ms, next try in 10m0s","controller":"kustomization","controllerGroup":"kustomize.toolkit.fluxcd.io","controllerKind":"Kustomization","Kustomization":{"name":"flux-system","namespace":"flux-system"},"namespace":"flux-system","name":"flux-system","reconcileID":"e49bad63-d775-4614-960c-99753808a9f6","revision":"main@sha1:e83834d29ca097cd7d7c5fd22bd9ea26d413c1f5","error":"HelmRelease/flux-system/podinfo-helm dry-run failed, reason: Invalid: HelmRelease.helm.toolkit.fluxcd.io "podinfo-helm" is invalid: spec.interval: Required value\n"}

spec.interval を 以下の通り追加したところ動きました。commit

@@ -41,4 +41,5 @@ spec:
        name: podinfo
  values:
    replicaCount: 2
-    logLevel: debug
+    logLevel: debug
+  interval: 30m0s
harrythecodeharrythecode

UIを立ち上げる

https://fluxcd.io/ecosystem/#flux-uis--guis ここに詳しく書いてあるので指示通り従います。

  • gitops cliをインストール
brew tap weaveworks/tap
brew install weaveworks/tap/gitops
  • 定義ファイルを生成
PASSWORD="<your-strong-password>"
gitops create dashboard ww-gitops \
  --password=$PASSWORD \
  --export > ./clusters/my-cluster/weave-gitops-dashboard.yaml

以下のようなファイルが生成されてました。

clusters/my-cluster/weave-gitops-dashboard.yaml
---
apiVersion: source.toolkit.fluxcd.io/v1
kind: HelmRepository
metadata:
  annotations:
    metadata.weave.works/description: This is the source location for the Weave GitOps
      Dashboard's helm chart.
  labels:
    app.kubernetes.io/component: ui
    app.kubernetes.io/created-by: weave-gitops-cli
    app.kubernetes.io/name: weave-gitops-dashboard
    app.kubernetes.io/part-of: weave-gitops
  name: ww-gitops
  namespace: flux-system
spec:
  interval: 1h0m0s
  type: oci
  url: oci://ghcr.io/weaveworks/charts
---
apiVersion: helm.toolkit.fluxcd.io/v2beta1
kind: HelmRelease
metadata:
  annotations:
    metadata.weave.works/description: This is the Weave GitOps Dashboard.  It provides
      a simple way to get insights into your GitOps workloads.
  name: ww-gitops
  namespace: flux-system
spec:
  chart:
    spec:
      chart: weave-gitops
      sourceRef:
        kind: HelmRepository
        name: ww-gitops
  interval: 1h0m0s
  values:
    adminUser:
      create: true
      passwordHash: $2a$10$aQRX9ZA9qcmPogR1zSB.N.JWkHtfi4E/yIa9qhrNWG169K7t7qRES
      username: admin
  • 先ほどの生成ファイルでエラーが出てたので修正。

{"level":"error","ts":"2023-08-24T08:29:01.882Z","msg":"Reconciliation failed after 696.829625ms, next try in 10m0s","controller":"kustomization","controllerGroup":"kustomize.toolkit.fluxcd.io","controllerKind":"Kustomization","Kustomization":{"name":"flux-system","namespace":"flux-system"},"namespace":"flux-system","name":"flux-system","reconcileID":"5be0f6c0-d286-4a0b-99b1-cf92cf6aa5fb","revision":"main@sha1:5bb7d775d3908408e3f993f9f5abed40a595fcae","error":"HelmRepository/flux-system/ww-gitops dry-run failed: no matches for kind "HelmRepository" in version "source.toolkit.fluxcd.io/v1"\n"}

clusters/my-cluster/weave-gitops-dashboard.yaml
---
- apiVersion: source.toolkit.fluxcd.io/v1
+ apiVersion: source.toolkit.fluxcd.io/v1beta2
kind: HelmRepository
  • port-forwardでデプロイされたServiceへアクセス
kubectl port-forward svc/ww-gitops-weave-gitops -n flux-system 9001:9001