|
| 1 | +--- |
| 2 | +id: gitops-with-fluxcd |
| 3 | +sidebar_label: Implement GitOps with KCL and FluxCD |
| 4 | +--- |
| 5 | + |
| 6 | +# Quick Start |
| 7 | + |
| 8 | +## Introduction |
| 9 | + |
| 10 | +### What is GitOps |
| 11 | + |
| 12 | +GitOps is a modern way to do continuous delivery. Its core idea is to have a Git repository which contains environmental and application configurations. An automated process is also needed for sync the config to cluster. |
| 13 | + |
| 14 | +By changing the files in repository, developers can apply the applications automatically. The benefits of applying GitOps include: |
| 15 | + |
| 16 | +- Increased productivity. Continuous delivery can speed up the time of deployment. |
| 17 | +- Lower the barrier for developer to deploy. By pushing code instead of container configuration, developers can easily deploy Kubernetes without knowing its internal implementation. |
| 18 | +- Trace the change records. Managing the cluster with Git makes every change traceable, enhancing the audit trail. |
| 19 | +- Recover the cluster with Git's rollback and branch. |
| 20 | + |
| 21 | +### GitOps with KCL and FluxCD |
| 22 | + |
| 23 | +Benefits of Using KCL and FluxCD Together: |
| 24 | + |
| 25 | +- KCL can help us **simplify complex Kubernetes deployment configuration files**, reduce the error rate of manually writing YAML files, and improve code readability and maintainability. |
| 26 | +- FluxCD can **automate** the deployment of Kubernetes applications, achieve continuous deployment, and provide comprehensive monitoring and control functions. |
| 27 | +- By combining KCL and FluxCD, deployment efficiency can be improved, errors reduced, and management and monitoring of Kubernetes applications strengthened. |
| 28 | +- The combination of KCL and FluxCD can also help us achieve **Infrastructure as Code (IaC)**, simplify application deployment and management, and better implement DevOps principles. |
| 29 | + |
| 30 | +With GitOps, developer and operation teams can manage application deployment and configuration by modifying KCL code and generating YAML files. The GitOps toolchain will automatically synchronize the changes to the Kubernetes cluster, enabling continuous deployment and ensuring consistency. If there are issues, the GitOps toolchain can be used to quickly rollback. |
| 31 | + |
| 32 | +### Flux-KCL-Controller |
| 33 | + |
| 34 | +flux-kcl-controller is a component that integrates [KCL](https://github.com/kcl-lang/kcl) and [Flux](https://github.com/fluxcd/flux2), which is mainly used to define infrastructure and workloads based on KCL programs stored in git/oci repositories, and to achieve continuous delivery of infrastructure and workloads through [source-controller]( |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | +## Prerequisite |
| 39 | + |
| 40 | +- Install [KCL](https://kcl-lang.io/docs/user_docs/getting-started/install) |
| 41 | + |
| 42 | +## Quick Start |
| 43 | + |
| 44 | +### 1. Install Kubernetes and GitOps Tools |
| 45 | + |
| 46 | +#### Configure Kubernetes Cluster and FluxCD Controller |
| 47 | + |
| 48 | +- Install [K3d](https://github.com/k3d-io/k3d) to create a default cluster. |
| 49 | + |
| 50 | +```bash |
| 51 | +k3d cluster create mycluster |
| 52 | +``` |
| 53 | + |
| 54 | +- Install Flux KCL Controller |
| 55 | + |
| 56 | +```bash |
| 57 | +git clone https://github.com/kcl-lang/flux-kcl-controller.git && cd flux-kcl-controller && make deploy |
| 58 | +``` |
| 59 | + |
| 60 | +- Check if the fluxcd controller container is initialized and running by using the `kubectl get` command. |
| 61 | + |
| 62 | +```bash |
| 63 | +kubectl get pod -n source-system -l app=kcl-controller |
| 64 | +``` |
| 65 | + |
| 66 | +### 2. Write Flux-KCL-Controller Configuration File |
| 67 | + |
| 68 | +Create a `GitRepository` object for `flux-kcl-controller` to monitor the KCL program stored in the git repository. For example, we use the flask demo in [“Implementing GitOps using Github, Argo CD, and KCL to Simplify DevOps”](https://kcl-lang.io/blog/2023-07-31-kcl-github-argocd-gitops/#3-get-the-application-code) as an example. We create a `GitRepository` object in the `flux-kcl-controller` repository to monitor the KCL program stored in the git repository. Save the following content in the file `gitrepo.yaml`. |
| 69 | + |
| 70 | +```yaml |
| 71 | +apiVersion: source.toolkit.fluxcd.io/v1 |
| 72 | +kind: GitRepository |
| 73 | +metadata: |
| 74 | + name: kcl-deployment |
| 75 | + namespace: default |
| 76 | +spec: |
| 77 | + interval: 10s # Check every 10 seconds |
| 78 | + url: https://github.com/kcl-lang/flask-demo-kcl-manifests.git |
| 79 | + ref: |
| 80 | + branch: main # Monitor the main branch |
| 81 | +--- |
| 82 | +apiVersion: krm.kcl.dev.fluxcd/v1alpha1 |
| 83 | +kind: KCLRun |
| 84 | +metadata: |
| 85 | + name: kcl-git-controller |
| 86 | + namespace: default |
| 87 | +spec: |
| 88 | + sourceRef: |
| 89 | + kind: GitRepository |
| 90 | + name: kcl-deployment |
| 91 | +``` |
| 92 | +
|
| 93 | +Apply the `GitRepository` object to the cluster by running the `kubectl apply -f gitrepo.yaml` command. |
| 94 | + |
| 95 | +### 3. Check the Deployment Result |
| 96 | + |
| 97 | +Check the deployment result by running the `kubectl get deployments` command. |
| 98 | + |
| 99 | +``` |
| 100 | +kubectl get deployments |
| 101 | +``` |
| 102 | +
|
| 103 | +You can see the result, and the deployment is successful. |
| 104 | +
|
| 105 | +``` |
| 106 | +NAME READY UP-TO-DATE AVAILABLE AGE |
| 107 | +flask-demo 1/1 1 1 17d |
| 108 | +``` |
| 109 | +
|
| 110 | +### 4. More |
| 111 | +
|
| 112 | +- [FluxCD](https://toolkit.fluxcd.io/) |
| 113 | +- [Flux Source Controller](https://fluxcd.io/flux/components/source/) |
| 114 | +- [GitRepositrory](https://fluxcd.io/flux/components/source/gitrepositories/) |
0 commit comments