|
1 | 1 | # GitOps for Apache Kafka Example |
2 | 2 |
|
3 | | -For this example we assume a single clusters simulated a production environment. The end goal is to leverage Flux and Kustomize to manage [Confluent Operator for Kubernetes](https://github.com/confluentinc/operator-earlyaccess). You can extend the with another cluster while minimizing duplicated declarations. |
| 3 | +For this example we assume a single cluster simulating a production confluent environment. The end goal is to leverage Flux and Kustomize to manage [Confluent Operator for Kubernetes](https://github.com/confluentinc/operator-earlyaccess). You can extend with another cluster while minimizing duplicated declarations. |
4 | 4 |
|
5 | | -We will configure [Flux](https://fluxcd.io/) to install, deploy and config the [Confluent Platform](https://www.confluent.io/product/confluent-platform) using their private `HelmRepository` and `HelmRelease` custom resources. |
6 | | -Flux will monitor the Helm repository, and it will automatically upgrade the Helm releases to their latest chart version based on semver ranges. |
| 5 | +We will configure [Flux](https://fluxcd.io/) to install, deploy and config the [Confluent Platform](https://www.confluent.io/product/confluent-platform) using their `HelmRepository` and `HelmRelease` custom resources. |
| 6 | +Flux will monitor the Helm repository, and can be configured to automatically upgrade the Helm releases to their latest chart version based on semver ranges. |
7 | 7 |
|
8 | 8 | You may find this project helpful by simply referencing the documentation, code, and strategies for managing Kafka resources on Kubernetes. Additionally, if you just wish to operate a working example of the new Confluent operator, the following usage instructions will guide you. |
9 | 9 |
|
10 | | -## Prerequisites |
11 | | -You will need a Kubernetes cluster version 1.16 or newer and kubectl version 1.18. |
12 | 10 |
|
13 | | -In order to follow the guide you'll need a GitHub account and a |
14 | | -[personal access token](https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line) |
15 | | -that can create repositories (check all permissions under `repo`). |
| 11 | +## Repository structure |
16 | 12 |
|
17 | | -Install the Flux CLI on MacOS and Linux using Homebrew: |
| 13 | +The Git repository contains the following top directories: |
18 | 14 |
|
19 | | -```sh |
20 | | -brew install fluxcd/tap/flux |
21 | | -``` |
| 15 | +- **flux-system** dir contains the required kubernetes resources for flux to operate |
| 16 | +- **kustomize/base** dir contains the base definition of the confluent stack. |
| 17 | +- **kustomize/environments** dir containing an example environment, folders could be copied to create additional environments. Files within are 'patches' which are layered on top of the definitions found in kustomize/base |
| 18 | +- **kustomize/operator** dir the helm chart definition for confluent-for-kubernetes (CFK). |
22 | 19 |
|
23 | | -Install the Confluent CLI |
24 | | -```she |
25 | | -curl -sL --http1.1 https://cnfl.io/cli | sh -s -- latest |
| 20 | + |
| 21 | +``` |
| 22 | +├── flux-system |
| 23 | +├── kustomize |
| 24 | +│ ├── base |
| 25 | +│ │ ├── confluent |
| 26 | +│ ├── environments |
| 27 | +│ │ └── sandbox |
| 28 | +│ └── operator |
26 | 29 | ``` |
27 | 30 |
|
28 | | -Get early access by registering interest here: [Confluent Operator Early Access Registration](https://events.confluent.io/confluentoperatorearlyaccess) For this Early Access program, you will have received an API key (associated with your email address) to the Confluent JFrog Artifactory. This is required to pull down the Helm charts and Confluent Docker images. |
| 31 | +## Forking this repository. |
| 32 | +In order to showcase the GitOps behaviour of the FluxCD toolkit you will require the ability to write to a repository. Fork this repository, and update line 11 of the file `./flux-system/gotk-sync.yaml` to the new https git address of your forked repository. Also make note of line 10 'branch'; this is the branch of the repository which Flux will monitor |
29 | 33 |
|
30 | | -## Repository structure |
| 34 | +## Deploy base Flux components |
| 35 | +### Overview |
| 36 | +This step will install the base Flux kubernetes components onto your kubernetes cluster. To inspect what is being applied, simply look through the contents of `./flux-system/gotk-components.yaml`. You will see a mix of Custom Resource Definitions, Service Accounts, Deployments, and other various components. After the application of these resource definitions is completed, you should see the following pods running: |
31 | 37 |
|
32 | | -The Git repository contains the following top directories: |
| 38 | +* Helm-Controller |
| 39 | +* Kustomize Controller |
| 40 | +* Notification Controller |
| 41 | +* Source Controller |
33 | 42 |
|
34 | | -- **apps** dir contains Helm releases with a custom configuration per cluster |
35 | | -- **infrastructure** dir contains common infra tools such as Confluent Operator, example LDAP controller and Helm repository definitions |
36 | | -- **clusters** dir contains the Flux configuration per cluster |
| 43 | +For more information on what these controllers do, please review [the documentation here](https://fluxcd.io/docs/components/). |
37 | 44 |
|
38 | | -``` |
39 | | -├── apps |
40 | | -│ ├── base |
41 | | -│ │ ├── kafka |
42 | | -│ │ └── rolebindings |
43 | | -│ ├── production |
44 | | -├── infrastructure |
45 | | -│ ├── confluent |
46 | | -│ ├── sources |
47 | | -│ └── tools |
48 | | -└── clusters |
49 | | - └── production |
50 | | -``` |
51 | | -### /apps |
52 | | -The apps configuration contains all the Confluent Platform configuration and is structured into: |
53 | | - |
54 | | -- **apps/base/kakfa/** dir common values for all clusters: namespaces, certificates, secrets, Confluent components via Helm release definitions and Deployments |
55 | | -- **apps/base/rolebings/** dir contains the common RBAC bindings for all deployments |
56 | | -- **apps/production/** dir contains the production values |
57 | | - |
58 | | -### /infrastructure |
59 | | -The infrastructure `sources` folder contains the [Flux Source Controller](https://fluxcd.io/docs/components/source/) configuration and some common tooling which is required for this Confluent LDAP / RBAC example. |
60 | | -```yaml |
61 | | -apiVersion: source.toolkit.fluxcd.io/v1beta1 |
62 | | -kind: HelmRepository |
63 | | -metadata: |
64 | | - name: confluent-private |
65 | | - namespace: flux-system |
66 | | -spec: |
67 | | - url: https://confluent.jfrog.io/confluent/helm-early-access-operator-2 |
68 | | - secretRef: |
69 | | - name: https-credentials |
70 | | - interval: 5m |
71 | | -``` |
72 | | -Note secretRef: The Confluent helm repository is private and requires a username and password which we must create. |
73 | | -Note that with interval: 5m we configure Flux to pull the Helm repository index every five minutes. If the index contains a new chart version that matches a HelmRelease semver range, Flux will upgrade the release. |
74 | | -
|
75 | | -The `confluent` folder contains the Helm release which is performed by the [Helm Controller](https://fluxcd.io/docs/components/helm/helmreleases/) and also requires access to the private Docker registry to pull down the Confluent images. |
76 | | -```yaml |
77 | | -apiVersion: helm.toolkit.fluxcd.io/v2beta1 |
78 | | -kind: HelmRelease |
79 | | -metadata: |
80 | | - name: confluent |
81 | | - namespace: confluent |
82 | | -spec: |
83 | | - interval: 1m |
84 | | - chart: |
85 | | - spec: |
86 | | - chart: confluent-for-kubernetes |
87 | | - sourceRef: |
88 | | - kind: HelmRepository |
89 | | - name: confluent-private |
90 | | - namespace: flux-system |
91 | | - values: |
92 | | - image: |
93 | | - registry: confluent-docker-internal-early-access-operator-2.jfrog.io |
94 | | -``` |
95 | | -Note: The Helm automatically looks for a secret called `confluent-registry` which we must create in the confluent namespace. |
96 | 45 |
|
97 | | -## Setup |
98 | | -Following this example, you'll set up secure Confluent Platform clusters with SASL PLAIN authentication, role-based access control (RBAC) authorization, and inter-component TLS. The clusters dir contains the Kustomization definitions:: |
99 | | -``` |
100 | | -./clusters/ |
101 | | -└── production |
102 | | - ├── apps.yaml |
103 | | - └── infrastructure.yaml |
104 | | -``` |
105 | | -1. Using GitOps will require the FluxCD toolkit to have read and write access to the repository. For your own local version, you must create a fork of this repository and clone it locally; otherwise, the GitOps automation will not be authorized to read and write from the repository. Fork this repository on your personal GitHub account and export your GitHub access token, username and repo name: |
106 | | -```sh |
107 | | -export GITHUB_TOKEN=<your-token> |
108 | | -export GITHUB_USER=<your-username> |
109 | | -export GITHUB_REPO=<repository-name> |
110 | | -``` |
111 | | - |
112 | | -2. After forking and cloning the repository, navigate to the project root and verify that your production cluster folder satisfies the prerequisites with: |
113 | | -```sh |
114 | | -flux check --pre |
115 | | -``` |
| 46 | +### Deployment Process |
| 47 | +* Navigate to `./flux-system` |
| 48 | +* Run `kubectl apply -f gotk-components.yaml` |
116 | 49 |
|
117 | | -3. Flux will now need connectivity do your cluster, ensure the correct kubectl context to your cluster and bootstrap Flux: |
118 | | -```sh |
119 | | -flux bootstrap github \ |
120 | | - --owner=${GITHUB_USER} \ |
121 | | - --repository=${GITHUB_REPO} \ |
122 | | - --branch=main \ |
123 | | - --personal \ |
124 | | - --path=clusters/production |
125 | | -``` |
126 | | -4. Deploy the secrets required by the application. The secrets referenced in `./resources/populate_secrets.sh` will match up to the LDAP/LDIFs located at `./infrastructure/tools/ldap.yaml` |
127 | | -```sh |
128 | | -./resources/populate_secrets.sh |
129 | | -``` |
130 | 50 |
|
131 | | -5. The source controller will be unable to pull the Helm chart or connect to the Docker registry. You now should create the following secrets using Confluent early access credentials: |
132 | | -```sh |
133 | | -export USER=<user id here (often same as email)> |
134 | | -export APIKEY=<API KEY sent via email> |
135 | | -export EMAIL=<user email here> |
136 | | -
|
137 | | -kubectl create secret docker-registry confluent-registry -n confluent \ |
138 | | - --docker-server=confluent-docker-internal-early-access-operator-2.jfrog.io \ |
139 | | - --docker-username=$USER \ |
140 | | - --docker-password=$APIKEY \ |
141 | | - --docker-email=$EMAIL && \ |
142 | | -kubectl create secret -n flux-system generic https-credentials \ |
143 | | ---from-literal=username=$USER \ |
144 | | ---from-literal=password=$APIKEY |
| 51 | +## Deploy Flux Sync |
| 52 | +### Overview |
| 53 | +This next step will tell Flux what repository to monitor, and, within that repository, what kustomization files to start with. The first Kustomize resource that Flux will look for to is located at `./kustomize/operator`. This will install the confluent-for-kubernetes Helm chart. After a successful health check of the operator (which will run as a pod), Flux will then proceed to deploy our first environment located at `./kustomize/environments/sandbox`. |
145 | 54 |
|
146 | | -``` |
147 | | -Watch for the Helm releases being installed in production cluster: |
| 55 | +### Deployment Process |
| 56 | +* Navigate to `./flux-system` |
| 57 | +* run `kubectl apply -f gotk-sync.yaml` |
148 | 58 |
|
| 59 | +## Watch Flux in action! |
| 60 | +Now that we have flux monitoring the forked Git repository, let's demonstrate the GitOps behaviour! If everything has deployed successfully, you should see a healthy confluent stack looking like this: |
149 | 61 | ```console |
150 | | -$ watch flux get helmreleases --all-namespaces |
| 62 | +│ NAME PF READY RESTARTS STATUS IP NODE AGE │ |
| 63 | +│ confluent-operator-global-7ffc5b469d-knmfj ● 1/1 0 Running 172.17.0.7 minikube 21m │ |
| 64 | +│ connect-0 ● 1/1 0 Running 172.17.0.17 minikube 9m31s │ |
| 65 | +│ controlcenter-0 ● 1/1 1 Running 172.17.0.11 minikube 21m │ |
| 66 | +│ kafka-0 ● 1/1 3 Running 172.17.0.8 minikube 21m │ |
| 67 | +│ kafka-1 ● 1/1 3 Running 172.17.0.10 minikube 21m │ |
| 68 | +│ kafka-2 ● 1/1 3 Running 172.17.0.9 minikube 21m │ |
| 69 | +│ ksqldb-0 ● 1/1 1 Running 172.17.0.12 minikube 21m │ |
| 70 | +│ schemaregistry-0 ● 1/1 1 Running 172.17.0.14 minikube 21m │ |
| 71 | +│ zookeeper-0 ● 1/1 0 Running 172.17.0.15 minikube 21m │ |
| 72 | +│ zookeeper-1 ● 1/1 0 Running 172.17.0.16 minikube 21m │ |
| 73 | +│ zookeeper-2 ● 1/1 0 Running 172.17.0.13 minikube 21m │ |
| 74 | +│ |
151 | 75 | ``` |
| 76 | +To exhibit Flux, let's change our kafka replicas from the default of 3, to 4: |
| 77 | +* In the file `./kustomize/environments/sandbox/kafka.yaml` uncomment the line `# replicas: 4`, commit that change to your repository (git), and push upstream. The next time flux performs a 'sync' (observable in the 'source controller' logs), it will the change to the kafka spec, and in turn increase our kafka cluster from size '3' to '4'. |
| 78 | + |
| 79 | +## Develop Locally |
| 80 | +If you want to test configuration out locally without the need to push up to git (i.e. testing locally with Minikube), the deployment can be replicated very simply: |
152 | 81 |
|
| 82 | +* Navigate to `./flux-system` |
| 83 | +* Run `kubectl apply -f gotk-components.yaml` |
153 | 84 |
|
154 | | -## Appendix |
155 | | -### Useful commands |
| 85 | +**instead of deploying the gotk-sync.yaml, we'll perform the kubectl kustomize applies ourselves.** |
156 | 86 |
|
157 | | -* Force Flux Reconciliation |
158 | | - `flux reconcile source git flux-system` |
| 87 | +* Navigate to `./kustomize/operator` |
| 88 | +* Run `kubectl apply -k .` |
159 | 89 |
|
160 | | -* Decode secrets |
161 | | - `kubectl get secrets -n flux-system https-credentials -o json | jq '.data | map_values(@base64d)'` |
| 90 | +**monitor the running pods, wait until the 'confluent-operator' pod is in a running state** |
162 | 91 |
|
163 | | -* Access Control Centre |
164 | | - `kubectl port-forward -n confluent controlcenter-0 9021:9021`. The web UI credentials will be c3/c3-secret (as defined by the populated secrets) |
| 92 | +* Navigate to `./kustomize/environments/` |
| 93 | +* Run `kubectl apply -k .` |
165 | 94 |
|
166 | | -* LDAP Testing. Exec onto the ldap container by running: `kubectl exec -it -n tools ldap -- bash`. Running |
167 | | - `ldapsearch -LLL -x -H ldap://ldap.tools.svc.cluster.local:389 -b 'dc=test,dc=com' -D "cn=mds,dc=test,dc=com" -w 'Developer!'` will return a list of LDAP users presently configured |
168 | 95 |
|
169 | | -* For testing a repeatable deployment process, for example on a local minikube, a `tldr.sh` script which captures the above steps has been included at the root of this project |
0 commit comments