본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

Flux 시작하기

원문 보기 위키 갱신

Flux 시작하기 (Get Started with Flux)

이 튜토리얼은 Kubernetes 클러스터에 Flux를 bootstrap 하고 GitOps 방식으로 샘플 애플리케이션을 배포하는 방법을 보여줘요.

출처: 문서

본문

이 튜토리얼은 Kubernetes 클러스터에 Flux를 bootstrap 하고 GitOps 방식으로 샘플 애플리케이션을 배포하는 방법을 보여줍니다.

시작하기 전에 (Before you begin)

이 가이드를 따라가려면 다음이 필요해요.

  • Kubernetes 클러스터. 로컬 개발 환경에서 Flux를 시험해 보려면 Kubernetes kind를 추천합니다.
  • repo 권한이 있는 GitHub personal access token. personal access token 생성 방법은 GitHub 문서를 참조하세요.
  • 프로덕션에서는 Flux 전용 GitHub 계정을 별도로 두고 최소 필요 권한만 가진 fine-grained access tokens를 사용하는 것이 좋습니다.

목표 (Objectives)

  • Kubernetes 클러스터에 Flux를 bootstrap 합니다.
  • Flux를 사용해 샘플 애플리케이션을 배포합니다.
  • Kustomize 패치로 애플리케이션 구성을 커스터마이징합니다.

Flux CLI 설치

flux 커맨드라인 인터페이스(CLI)는 Flux를 bootstrap 하고 상호작용하는 데 사용됩니다.

Homebrew로 CLI를 설치하려면 다음을 실행하세요.

brew install fluxcd/tap/flux

다른 설치 방법은 CLI 설치 문서를 참조하세요.

자격 증명 내보내기

GitHub personal access token과 사용자 이름을 내보내세요.

export GITHUB_TOKEN=
export GITHUB_USER=

Kubernetes 클러스터 확인하기

다음 명령을 실행해 Flux를 실행하는 데 필요한 모든 것이 있는지 확인하세요.

flux check --pre

출력은 다음과 비슷해요.

► checking prerequisites
✔ kubernetes 1.28.0 >=1.25.0
✔ prerequisites checks passed

클러스터에 Flux 설치하기

GitHub org, GitLab 및 기타 Git 프로바이더로 bootstrap 하는 방법은 Bootstrapping 문서를 참조하세요.

bootstrap 명령을 실행하세요.

flux bootstrap github \
  --owner=$GITHUB_USER \
  --repository=fleet-infra \
  --branch=main \
  --path=./clusters/my-cluster \
  --personal

출력은 다음과 비슷해요.

► connecting to github.com
✔ repository created
✔ repository cloned
✚ generating manifests
✔ components manifests pushed
► installing components in flux-system namespace
deployment "source-controller" successfully rolled out
deployment "kustomize-controller" successfully rolled out
deployment "helm-controller" successfully rolled out
deployment "notification-controller" successfully rolled out
✔ install completed
► configuring deploy key
✔ deploy key configured
► generating sync manifests
✔ sync manifests pushed
► applying sync manifests
◎ waiting for cluster sync
✔ bootstrap finished

위 bootstrap 명령은 다음을 수행합니다.

  • GitHub 계정에 fleet-infra라는 Git 리포지토리를 만듭니다.
  • Flux 컴포넌트 매니페스트를 리포지토리에 추가합니다.
  • Kubernetes 클러스터에 Flux 컴포넌트를 배포합니다.
  • 리포지토리의 /clusters/my-cluster/ 경로를 추적하도록 Flux 컴포넌트를 구성합니다.

Git 리포지토리 클론하기

fleet-infra 리포지토리를 로컬 머신에 클론하세요.

git clone https://github.com/$GITHUB_USER/fleet-infra
cd fleet-infra

Flux에 podinfo 리포지토리 추가하기

이 예시는 공개 리포지토리 github.com/stefanprodan/podinfo를 사용합니다. podinfo는 Go로 만들어진 아주 작은 웹 애플리케이션이에요.

  • podinfo 리포지토리의 master 브랜치를 가리키는 GitRepository 매니페스트를 만드세요.
flux create source git podinfo \
  --url=https://github.com/stefanprodan/podinfo \
  --branch=master \
  --interval=1m \
  --export > ./clusters/my-cluster/podinfo-source.yaml

출력은 다음과 비슷해요.

apiVersion: source.toolkit.fluxcd.io/v1
kind: GitRepository
metadata:
  name: podinfo
  namespace: flux-system
spec:
  interval: 1m
  ref:
    branch: master
  url: https://github.com/stefanprodan/podinfo
  • podinfo-source.yaml 파일을 fleet-infra 리포지토리에 커밋하고 push하세요.
git add -A && git commit -m "Add podinfo GitRepository"
git push

podinfo 애플리케이션 배포하기

podinfo 리포지토리에 있는 kustomize 디렉터리를 Flux가 빌드·적용하도록 구성하세요.

  • flux create 명령으로 podinfo 배포를 적용하는 Kustomization을 만듭니다.
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

출력은 다음과 비슷해요.

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
  • Kustomization 매니페스트를 리포지토리에 커밋하고 push하세요.
git add -A && git commit -m "Add podinfo Kustomization"
git push

fleet-infra 리포지토리 구조는 다음과 비슷해야 해요.

fleet-infra
└── clusters/
    └── my-cluster/
        ├── flux-system/                        
        │   ├── gotk-components.yaml
        │   ├── gotk-sync.yaml
        │   └── kustomization.yaml
        ├── podinfo-kustomization.yaml
        └── podinfo-source.yaml

Flux가 애플리케이션을 동기화하는 것 지켜보기

  • flux get 명령으로 podinfo 앱을 실시간으로 확인하세요.
flux get kustomizations --watch

출력은 다음과 비슷해요.

NAME          REVISION             SUSPENDED  READY   MESSAGE
flux-system   main@sha1:4e9c917f   False      True    Applied revision: main@sha1:4e9c917f
podinfo       master@sha1:44157ecd False      True    Applied revision: master@sha1:44157ecd
  • 클러스터에 podinfo가 배포되었는지 확인하세요.
kubectl -n default get deployments,services

출력은 다음과 비슷해요.

NAME                      READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/podinfo   2/2     2            2           108s

NAME                 TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)             AGE
service/podinfo      ClusterIP   10.100.149.126           9898/TCP,9999/TCP   108s

master 브랜치의 podinfo Kubernetes 매니페스트에 대한 변경 사항은 클러스터에 반영됩니다.

podinfo 리포지토리에서 Kubernetes 매니페스트가 제거되면 Flux는 클러스터에서 그것을 제거합니다. fleet-infra 리포지토리에서 Kustomization을 삭제하면 Flux는 그 Kustomization에서 이전에 적용된 모든 Kubernetes 객체를 제거해요.

kubectl edit로 podinfo 배포를 변경하면, Git에 기술된 상태와 일치하도록 변경 사항이 되돌려집니다(reverted).

업데이트 일시 중지 (Suspend updates)

kustomization에 대한 업데이트를 일시 중지하면 Git의 상태에 의해 변경 사항이 되돌려지지 않고, kustomization에서 적용된 객체를 직접 편집할 수 있습니다.

kustomization에 대한 업데이트를 일시 중지하려면 flux suspend kustomization <name> 명령을 실행하세요.

업데이트를 재개하려면 flux resume kustomization <name> 명령을 실행하세요.

podinfo 배포 커스터마이징하기

통제하지 않는 리포지토리의 배포를 커스터마이징하려면 Flux 인라인 패치를 사용할 수 있어요. 다음 예시는 인라인 패치로 podinfo 배포를 변경하는 방법을 보여줍니다.

  • podinfo-kustomization.yaml 파일의 spec 필드에 다음을 추가하세요.
  patches:
    - patch: |-
        apiVersion: autoscaling/v2
        kind: HorizontalPodAutoscaler
        metadata:
          name: podinfo
        spec:
          minReplicas: 3             
      target:
        name: podinfo
        kind: HorizontalPodAutoscaler
  • podinfo-kustomization.yaml 변경 사항을 커밋하고 push하세요.
git add -A && git commit -m "Increase podinfo minimum replicas"
git push

동기화가 끝나면 kubectl get pods를 실행했을 때 3개의 pod가 표시되어야 해요.

멀티 클러스터 설정 (Multi-cluster Setup)

Flux로 둘 이상의 클러스터를 관리하거나 staging에서 production으로 배포를 승격(promote)하려면 아래 나열된 리포지토리의 두 가지 접근 방식을 확인해 보세요.

더 알아보기 (Learn more)