Hướng dẫn này sẽ chỉ cho bạn cách tạo bootstrap Fluxcd vào cụm Kubernetes và triển khai ứng dụng mẫu theo cách thức của GitOps.
Trước khi bắt đầu
Để làm theo hướng dẫn, bạn cần những thứ sau:
- Một Kubernetes cluster. Chúng tôi khuyên bạn nên sử dụng Kubernetes kind để thử Flux trong local development environment.
- Một GitHub personal access token with repo permissions . Xem tài liệu GitHub creating a personal access token.
Lưu ý rằng để sử dụng cho production, bạn nên có một tài khoản GitHub chuyên dụng cho Flux và sử dụng fine-grained access tokens với các quyền tối thiểu bắt buộc.
Mục tiêu
- Bootstrap Flux trên cụm Kubernetes.
- Triển khai ứng dụng mẫu bằng Flux.
- Tùy chỉnh cấu hình ứng dụng thông qua các bản vá Kustomize.
Cài đặt Flux CLI
Giao diện dòng lệnh (CLI) được sử dụng để khởi động và tương tác với Flux.
Để cài đặt CLI bằng Homebrew, hãy chạy:
brew install fluxcd/tap/flux
Để biết các phương pháp cài đặt khác, hãy xem tài liệu cài đặt CLI .
Export thông tin đăng nhập của bạn
Export GitHub personal access token và username:
export GITHUB_TOKEN=<your-token>
export GITHUB_USER=<your-username>
Kiểm tra cụm Kubernetes của bạn
Kiểm tra xem bạn có đủ mọi thứ cần thiết để chạy Flux hay không bằng cách chạy lệnh sau:
flux check --pre
OUTPUT:
► checking prerequisites
✔ kubernetes 1.28.0 >=1.25.0
✔ prerequisites checks passed
Cài đặt Flux vào Cluster
Để biết thông tin về cách khởi động bằng GitHub org, Gitlab và các nhà cung cấp git khác, hãy xem Khởi động .
Chạy lệnh bootstrap:
flux bootstrap github \
--owner=$GITHUB_USER \
--repository=fleet-infra \
--branch=main \
--path=./clusters/my-cluster \
--personal
Output sẽ như sau:
► 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
Lệnh bootstrap ở trên thực hiện những thao tác sau:
- Tạo kho lưu trữ git
fleet-infratrên tài khoản GitHub của bạn. - Thêm manifests Flux vào kho lưu trữ.
- Triển khai các thành phần Flux vào cụm Kubernetes của bạn.
- Cấu hình các thành phần Flux để theo dõi đường dẫn
/clusters/my-cluster/trong kho lưu trữ.
Clone git repository
Clone fleet-infra kho lưu trữ vào máy cục bộ của bạn:
git clone https://github.com/$GITHUB_USER/fleet-infra
cd fleet-infra
Thêm kho lưu trữ podinfo vào Flux
Ví dụ này sử dụng kho lưu trữ công khai github.com/stefanprodan/podinfo , podinfo là một ứng dụng web nhỏ được tạo bằng Go.
- Tạo một bản kê khai GitRepository trỏ tới nhánh chính của kho lưu trữ podinfo:
flux create source git podinfo \
--url=https://github.com/stefanprodan/podinfo \
--branch=master \
--interval=1m \
--export > ./clusters/my-cluster/podinfo-source.yaml
Đầu ra tương tự như sau:
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/podinfo2. Commit và push podinfo-source.yaml tệp vào fleet-infra kho lưu trữ:
git add -A && git commit -m "Add podinfo GitRepository"
git push
Triển khai ứng dụng podinfo
Cấu hình Flux để xây dựng và áp dụng thư mục kustomize nằm trong kho lưu trữ podinfo.
- Sử dụng
flux createlệnh để tạo Kustomization áp dụng triển khai 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Đầu ra tương tự như sau:
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: true2. Commit và push Kustomization manifest vào kho lưu trữ:
git add -A && git commit -m "Add podinfo Kustomization"
git pushCấu trúc của fleet-infra repo phải tương tự như sau:
fleet-infra
└── clusters/
└── my-cluster/
├── flux-system/
│ ├── gotk-components.yaml
│ ├── gotk-sync.yaml
│ └── kustomization.yaml
├── podinfo-kustomization.yaml
└── podinfo-source.yaml
Watch Flux sync the application
- Sử dụng
flux getlệnh để theo dõi ứng dụng podinfo.
flux get kustomizations --watchĐầu ra tương tự như sau:
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@2. Kiểm tra podinfo đã được triển khai trên cụm của bạn chưa:
kubectl -n default get deployments,servicesĐầu ra tương tự như sau:
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 <none> 9898/TCP,9999/TCP Những thay đổi được thực hiện đối với manifests Kubernetes podinfo trong nhánh master sẽ được phản ánh trong cụm của bạn.
Khi một bản kê khai Kubernetes bị xóa khỏi kho lưu trữ podinfo, Flux sẽ xóa nó khỏi cụm của bạn. Khi bạn xóa một Kustomization khỏi fleet-infra kho lưu trữ, Flux sẽ xóa tất cả các đối tượng Kubernetes đã áp dụng trước đó khỏi Kustomization.
Khi bạn thay đổi triển khai podinfo bằng kubectl edit, những thay đổi sẽ được khôi phục để phù hợp với trạng thái được mô tả trong Git.
Tạm dừng cập nhật
Việc tạm dừng cập nhật cho một tùy chỉnh cho phép bạn chỉnh sửa trực tiếp các đối tượng được áp dụng từ một tùy chỉnh mà không làm thay đổi trạng thái trong Git.
Để tạm dừng cập nhật cho mục đích tùy chỉnh, hãy chạy lệnh .
flux suspend kustomization <name>Để tiếp tục cập nhật, hãy chạy lệnh .
flux resume kustomization <name>Tùy chỉnh podinfo deployment
Để tùy chỉnh triển khai từ kho lưu trữ mà bạn không kiểm soát, bạn có thể sử dụng Flux in-line patches . Ví dụ sau đây cho thấy cách sử dụng in-line patches để thay đổi triển khai podinfo.
- Thêm nội dung sau vào trường tệp
speccủa bạnpodinfo-kustomization.yaml:
patches:
- patch: |-
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: podinfo
spec:
minReplicas: 3
target:
name: podinfo
kind: HorizontalPodAutoscaler
2. Commit và push podinfo-kustomization.yaml thay đổi:
git add -A && git commit -m "Increase podinfo minimum replicas"
git push
Sau khi quá trình đồng bộ hóa hoàn tất, chạy kubectl get pods sẽ hiển thị 3 pod.
Thiết lập nhiều cụm
Để sử dụng Flux để quản lý nhiều cụm hoặc thúc đẩy triển khai từ giai đoạn dàn dựng lên giai đoạn sản xuất, hãy xem hai phương pháp trong kho lưu trữ được liệt kê bên dưới.
- https://github.com/fluxcd/flux2-kustomize-helm-example
- https://github.com/fluxcd/flux2-multi-tenancy
FluxCD là gì? Hướng dẫn chi tiết về GitOps với FluxCD










Add Comment