-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: create use-case for integration with Flux
Signed-off-by: odubajDT <[email protected]>
- Loading branch information
Showing
6 changed files
with
278 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
apiVersion: v1 | ||
kind: Namespace | ||
metadata: | ||
name: podtato-kubectl | ||
annotations: | ||
keptn.sh/lifecycle-toolkit: "enabled" | ||
--- | ||
apiVersion: lifecycle.keptn.sh/v1beta1 | ||
kind: KeptnAppContext | ||
metadata: | ||
name: podtato-head | ||
spec: | ||
preDeploymentTasks: | ||
- hello-task | ||
--- | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: podtato-head-entry | ||
labels: | ||
app: podtato-head | ||
spec: | ||
selector: | ||
matchLabels: | ||
component: podtato-head-entry | ||
template: | ||
metadata: | ||
labels: | ||
component: podtato-head-entry | ||
annotations: | ||
keptn.sh/app: podtato-head | ||
keptn.sh/workload: podtato-head-entry | ||
keptn.sh/version: 0.1.0 | ||
keptn.sh/pre-deployment-tasks: hello-task | ||
keptn.sh/post-deployment-tasks: hello-task | ||
spec: | ||
terminationGracePeriodSeconds: 5 | ||
containers: | ||
- name: server | ||
image: ghcr.io/podtato-head/entry:latest | ||
imagePullPolicy: Always | ||
ports: | ||
- containerPort: 9000 | ||
env: | ||
- name: PODTATO_PORT | ||
value: "9000" | ||
--- | ||
apiVersion: lifecycle.keptn.sh/v1beta1 | ||
kind: KeptnTaskDefinition | ||
metadata: | ||
name: hello-task | ||
spec: | ||
deno: | ||
inline: | ||
code: | | ||
console.log('hello'); | ||
retries: 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
apiVersion: kustomize.toolkit.fluxcd.io/v1 | ||
kind: Kustomization | ||
metadata: | ||
name: app | ||
namespace: flux-system | ||
spec: | ||
interval: 30m0s | ||
path: ./apps/dev | ||
prune: true | ||
retryInterval: 2m0s | ||
sourceRef: | ||
kind: GitRepository | ||
name: app | ||
namespace: flux-system | ||
targetNamespace: podtato-kubectl | ||
timeout: 3m0s | ||
wait: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
apiVersion: source.toolkit.fluxcd.io/v1 | ||
kind: GitRepository | ||
metadata: | ||
name: app | ||
namespace: flux-system | ||
spec: | ||
interval: 1m0s | ||
ref: | ||
branch: main | ||
url: <git-repo-url> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
apiVersion: kustomize.config.k8s.io/v1beta1 | ||
kind: Kustomization | ||
resources: | ||
- app.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
--- | ||
comments: true | ||
--- | ||
|
||
# Deploying Applications with Flux | ||
|
||
Flux is a tool for keeping Kubernetes clusters in sync with sources | ||
of configuration (like Git repositories), and automating updates to | ||
configuration when there is new code to deploy. | ||
|
||
This section shows an already existing use case of running | ||
[pre and post-deployment jobs with Flux](https://fluxcd.io/flux/use-cases/running-jobs/) | ||
and how Keptn makes it simpler and and more straight-forward. | ||
|
||
## High-level strucutre of the git repository | ||
|
||
Since Flux uses a GitOps approach to continuous delivery, the git | ||
repository structure needs to look like the following: | ||
|
||
``` | ||
├── apps | ||
│ └── dev | ||
│ ├── podtato-head.yaml | ||
│ └── kustomize.yaml | ||
└── clusters | ||
└── dev | ||
├── flux-system | ||
│ ├── gotk-components.yaml | ||
│ ├── gotk-sync.yaml | ||
│ └── kustomize.yaml | ||
├── podtato-head-source.yaml | ||
└── podtato-head-kustomization.yaml | ||
``` | ||
|
||
The `apps` directory contains application manifests, that will be deployed. | ||
The `clusters` directory contains Flux configuration manifests and custom | ||
resources, that are needed for the delivery. | ||
`apps` and `clusters` directories can live in two separate repositories, | ||
but for simplicity of this excercise, we will keed them in a single one. | ||
|
||
## Set up environment | ||
|
||
Before starting, you need to install Flux CLI and Keptn. | ||
You can find the installation instructions of Keptn [here](./../installation/index.md) | ||
and for Flux [here](https://fluxcd.io/flux/installation/). | ||
|
||
After succesffully installing Flux CLI and Keptn, you need to | ||
retrieve your git repository credentials. | ||
You can use any available git providers, but be sure to store your `token` | ||
for later usage. | ||
For simplicity, we will use `GitHub`. | ||
|
||
In the end, you need to install Flux to your cluster. | ||
This step will require | ||
[bootstrapping the git repository](https://fluxcd.io/flux/installation/bootstrap/) | ||
in order to set up all Flux structures. | ||
For that, you can use the following command: | ||
|
||
```bash | ||
GITHUB_USER=<user-name> GITHUB_TOKEN=<token> \ | ||
flux bootstrap github \ | ||
--owner=$GITHUB_USER \ | ||
--repository=podtato-head \ | ||
--branch=main \ | ||
--path=./clusters/dev \ | ||
--personal | ||
``` | ||
|
||
The bootstrap command above does the following: | ||
|
||
* Creates a git repository `podtato-head` on your `GitHub` account. | ||
* Adds Flux component manifests to the repository - | ||
creates `./clusters/dev/flux-system/*` structure. | ||
* Deploys Flux Components to your Kubernetes Cluster. | ||
* Configures Flux components to track the path `./clusters/dev/` in the repository. | ||
|
||
## Creating application | ||
|
||
Now it's time to add the application together with pre- and | ||
post-deployments checks to the repository. | ||
|
||
Firstly, clone the `podtato-head` repository to your local machine. | ||
|
||
Add the following manifests into `podtato-head.yaml` representing the application | ||
and store it into `./apps/dev/` directory of your repository: | ||
|
||
```yaml | ||
{% include "./assets/flux/app.yaml" %} | ||
``` | ||
|
||
Additionally, create a `kustomize.yaml` file right next to it: | ||
|
||
```yaml | ||
{% include "./assets/flux/kustomize.yaml" %} | ||
``` | ||
|
||
You can commit and push these manifests to your git repository. | ||
|
||
> **Note** | ||
Notice, that the application has pre- and post-deployment tasks defined | ||
in the manifests. | ||
This enhances the | ||
[Flux pre and post-deployment jobs](https://fluxcd.io/flux/use-cases/running-jobs/) | ||
setup with added observability out of the box. | ||
|
||
## Set up continuous delivery for the application | ||
|
||
Firstly, we need to create a | ||
[GitRepository](https://fluxcd.io/flux/components/source/gitrepositories/) | ||
Flux custom resource | ||
pointing to a repository where the application manifests are present. | ||
In this case, it will be our `podtato-head` repository. | ||
To create it, we will use Flux CLI: | ||
|
||
```bash | ||
flux create source git podtato-head \ | ||
--url=<git-repo-url> \ | ||
--branch=main \ | ||
--interval=1m \ | ||
--export > ./clusters/dev/podtato-head-source.yaml | ||
``` | ||
|
||
which will result output similar to: | ||
|
||
```yaml | ||
{% include "./assets/flux/gitrepository.yaml" %} | ||
``` | ||
|
||
In the last step, create a | ||
[Kustomization](https://fluxcd.io/flux/components/kustomize/kustomizations/) | ||
Flux custom resource to deploy the `podtato-head` application to the cluster. | ||
You can create it using the Flux CLI: | ||
|
||
```bash | ||
flux create kustomization podinfo \ | ||
--target-namespace=podtato-kubectl \ | ||
--source=podtato-head \ | ||
--path="./apps/dev" \ | ||
--prune=true \ | ||
--wait=true \ | ||
--interval=30m \ | ||
--retry-interval=2m \ | ||
--health-check-timeout=3m \ | ||
--export > ./clusters/dev/podtato-head-kustomization.yaml | ||
``` | ||
|
||
which will result output similar to: | ||
|
||
```yaml | ||
{% include "./assets/flux/flux-kustomization.yaml" %} | ||
``` | ||
|
||
Now commit and push the resources you created in the recent steps. | ||
After pushing them, Flux should pick up the configuration and | ||
deploy your application into the cluster. | ||
|
||
## Watch Flux sync of the application | ||
|
||
You can watch the synchronization of the application | ||
with Flux CLI | ||
|
||
```bash | ||
$ flux get kustomizations --watch | ||
|
||
NAME REVISION SUSPENDED READY MESSAGE | ||
flux-system main@sha1:4e9c917f False True Applied revision: main@sha1:4e9c917f | ||
podtato-head main@sha1:44157ecd False True Applied revision: main@sha1:44157ecd | ||
|
||
``` | ||
|
||
or using `kubectl`: | ||
|
||
```shell | ||
$ kubectl get keptnappversion -n podtato-head | ||
|
||
NAME APPNAME VERSION PHASE | ||
podtato-head-v0.1.0-6b86b273 podtato-head v0.1.0 Completed | ||
``` | ||
|
||
Every time you update the application, the changes will be | ||
synced to the cluster. | ||
|
||
## Possible follow-ups | ||
|
||
You can set up a multi-stage delivery with Flux, same | ||
as it was done with `ArgoCD`. | ||
You can follow similar steps to the | ||
[ArgoCD multi-stage delivery with Keptn](../guides/multi-stage-application-delivery.md) | ||
user guide. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters