|
1 | 1 | --- |
2 | | -id: flux-cd |
3 | | -sidebar_label: Flux-KCL-Controller |
| 2 | +id: gitops-with-fluxcd |
| 3 | +sidebar_label: Implement GitOps with KCL and FluxCD |
4 | 4 | --- |
5 | 5 |
|
6 | | -# Flux KCL Controller |
| 6 | +# Quick Start |
7 | 7 |
|
8 | 8 | ## Introduction |
9 | 9 |
|
10 | | -The kcl-controller is a component developed for the integration of [KCL](https://github.com/kcl-lang/kcl) and [Flux](https://github.com/fluxcd/flux2), designed to orchestrate continuous delivery pipelines for infrastructure and workloads defined with KCL based on the [source-controller](https://github.com/fluxcd/source-controller) to acquire the KCL program from repositories. |
| 10 | +### What is GitOps |
11 | 11 |
|
12 | | - |
| 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 | 13 |
|
14 | | -## Features |
| 14 | +By changing the files in repository, developers can apply the applications automatically. The benefits of applying GitOps include: |
15 | 15 |
|
16 | | -- Periodically monitor git repositories that store KCL programs and reconcile k8s cluster status according to changes in git repositories. |
| 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. |
17 | 20 |
|
18 | | -## Quick Start |
| 21 | +### GitOps with KCL and FluxCD |
19 | 22 |
|
20 | | -### Prerequisites |
| 23 | +Benefits of Using KCL and FluxCD Together: |
21 | 24 |
|
22 | | -- k3d: used to create a k8s cluster for testing, if you already have a k8s cluster, you can skip ignore this. |
23 | | -- Kustomize |
24 | | -- Kubectl |
| 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. |
25 | 29 |
|
26 | | -### Create a test k8s cluster |
| 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. |
27 | 31 |
|
28 | | -Create a cluster using the following command: |
| 32 | +### Flux-KCL-Controller |
29 | 33 |
|
30 | | -```shell |
31 | | -k3d cluster create |
32 | | -``` |
| 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]( |
33 | 35 |
|
34 | | -### Download kcl-controller and install it into the cluster |
| 36 | + |
35 | 37 |
|
36 | | -Clone this repository to local: |
| 38 | +## Prerequisite |
37 | 39 |
|
38 | | -```shell |
39 | | -git clone https://github.com/kcl-lang/flux-kcl-controller.git |
40 | | -``` |
| 40 | +- Install [KCL](https://kcl-lang.io/docs/user_docs/getting-started/install) |
41 | 41 |
|
42 | | -Enter the root directory of this repository: |
| 42 | +## Quick Start |
| 43 | + |
| 44 | +### 1. Install Kubernetes and GitOps Tools |
| 45 | + |
| 46 | +#### Configure Kubernetes Cluster and FluxCD Controller |
43 | 47 |
|
44 | | -```shell |
45 | | -cd flux-kcl-controller |
| 48 | +- Install [K3d](https://github.com/k3d-io/k3d) to create a default cluster. |
| 49 | + |
| 50 | +```bash |
| 51 | +k3d cluster create mycluster |
46 | 52 | ``` |
47 | 53 |
|
48 | | -Install kcl-controller into the cluster: |
| 54 | +- Install Flux KCL Controller |
49 | 55 |
|
50 | | -```shell |
51 | | -make deploy |
| 56 | +```bash |
| 57 | +git clone https://github.com/kcl-lang/flux-kcl-controller.git && cd flux-kcl-controller && make deploy |
52 | 58 | ``` |
53 | 59 |
|
54 | | -### Monitor a git repository |
| 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 | +``` |
55 | 65 |
|
56 | | -Take the github repository https://github.com/awesome-kusion/kcl-deployment as an example. This repository stores a KCL program that defines a `Deployment`. We will use kcl-controller to deploy this program. |
| 66 | +### 2. Write Flux-KCL-Controller Configuration File |
57 | 67 |
|
58 | | -Define a `GitRepository` object through the `gitrepo.yaml` file to monitor the repository: |
| 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`. |
59 | 69 |
|
60 | 70 | ```yaml |
61 | 71 | apiVersion: source.toolkit.fluxcd.io/v1 |
62 | 72 | kind: GitRepository |
63 | 73 | metadata: |
64 | 74 | name: kcl-deployment |
65 | | - namespace: source-system |
| 75 | + namespace: default |
66 | 76 | spec: |
67 | | - interval: 30s |
68 | | - url: https://github.com/awesome-kusion/kcl-deployment.git |
| 77 | + interval: 10s # Check every 10 seconds |
| 78 | + url: https://github.com/kcl-lang/flask-demo-kcl-manifests.git |
69 | 79 | ref: |
70 | | - branch: main |
| 80 | + branch: main # Monitor the main branch |
71 | 81 | --- |
72 | 82 | apiVersion: krm.kcl.dev.fluxcd/v1alpha1 |
73 | 83 | kind: KCLRun |
74 | 84 | metadata: |
75 | | - name: kcl-deployment |
76 | | - namespace: source-system |
| 85 | + name: kcl-git-controller |
| 86 | + namespace: default |
77 | 87 | spec: |
78 | 88 | sourceRef: |
79 | 89 | kind: GitRepository |
80 | 90 | name: kcl-deployment |
81 | 91 | ``` |
82 | 92 |
|
83 | | -Use the command `kubectl apply -f gitrepo.yaml` to deploy the object to the cluster. |
| 93 | +Apply the `GitRepository` object to the cluster by running the `kubectl apply -f gitrepo.yaml` command. |
84 | 94 |
|
85 | | -### View the deployment result |
| 95 | +### 3. Check the Deployment Result |
86 | 96 |
|
87 | | -Use the command `kubectl get deployment` to view the deployment result: |
| 97 | +Check the deployment result by running the `kubectl get deployments` command. |
88 | 98 |
|
89 | | -```shell |
90 | | -NAME READY UP-TO-DATE AVAILABLE AGE |
91 | | -nginx-deployment 1/1 1 0 28s |
| 99 | +``` |
| 100 | +kubectl get deployments |
92 | 101 | ``` |
93 | 102 |
|
94 | | -The `nginx-deployment` is deployed successfully. |
95 | | - |
96 | | -### Update the KCL program in the repository |
97 | | - |
98 | | -We can update the `Deployment` in the cluster by modifying the KCL program in the repository. |
99 | | - |
100 | | -Change the version of nginx from `1.7.7` to `1.7.8` and the name of `deployment` to `nginx-deployment-1`, and commit to the main branch. |
101 | | - |
102 | | -The changes can be referred to: [nginx:1.7.7 deployment](https://github.com/awesome-kusion/kcl-deployment/commit/dc8b2aa70b1b47bef0637212ea184792b8c43449) -> [nginx:1.7.8 deployment](https://github.com/awesome-kusion/kcl-deployment/commit/f257a71fdff6cb9190f49c1dbf5fa4496d7b3cb2) |
103 | | - |
104 | | -Use the command `kubectl get deployment` to view the deployment result: |
| 103 | +You can see the result, and the deployment is successful. |
105 | 104 |
|
106 | | -```shell |
107 | | -NAME READY UP-TO-DATE AVAILABLE AGE |
108 | | -nginx-deployment 1/1 1 1 20m |
109 | | -nginx-deployment-1 1/1 1 0 4s |
| 105 | +``` |
| 106 | +NAME READY UP-TO-DATE AVAILABLE AGE |
| 107 | +flask-demo 1/1 1 1 17d |
110 | 108 | ``` |
111 | 109 |
|
112 | | -kcl-controller creates a `nginx-deployment-1` according to the KCL program in the repository. |
113 | | - |
114 | | -## Roadmap |
| 110 | +### 4. More |
115 | 111 |
|
116 | | -- Add KCL OCI Registry Controller to support KCL programs stored in OCI registry. |
| 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