项目作者: fluxcd

项目描述 :
Managing Helm releases with Flux Helm Operator
高级语言: HTML
项目地址: git://github.com/fluxcd/helm-operator-get-started.git
创建时间: 2018-02-19T11:00:39Z
项目社区:https://github.com/fluxcd/helm-operator-get-started

开源协议:MIT License

下载


Managing Helm releases the GitOps way

We are moving to Flux v2

⚠️ Please note: In preparation of Flux v2 GA this repository with Flux v1 examples has been archived. The Flux v2 equivalent of what is shown here can be found at flux2-kustomize-helm-example.

Thanks a lot for your interest.

What is GitOps?

GitOps is a way to do Continuous Delivery, it works by using Git as a source of truth for declarative infrastructure and workloads.
For Kubernetes this means using git push instead of kubectl create/apply or helm install/upgrade.

In a traditional CICD pipeline, CD is an implementation extension powered by the
continuous integration tooling to promote build artifacts to production.
In the GitOps pipeline model, any change to production must be committed in source control
(preferable via a pull request) prior to being applied on the cluster.
This way rollback and audit logs are provided by Git.
If the entire production state is under version control and described in a single Git repository, when disaster strikes,
the whole infrastructure can be quickly restored from that repository.

To better understand the benefits of this approach to CD and what the differences between GitOps and
Infrastructure-as-Code tools are, head to the Weaveworks website and read GitOps - What you need to know article.

In order to apply the GitOps pipeline model to Kubernetes you need three things:

  • a Git repository with your workloads definitions in YAML format, Helm charts and any other Kubernetes custom resource that defines your cluster desired state (I will refer to this as the config repository)
  • a container registry where your CI system pushes immutable images (no latest tags, use semantic versioning or git commit sha)
  • an operator that runs in your cluster and does a two-way synchronization:
    • watches the registry for new image releases and based on deployment policies updates the workload definitions with the new image tag and commits the changes to the config repository
    • watches for changes in the config repository and applies them to your cluster

I will be using GitHub to host the config repo, Docker Hub as the container registry and Flux as the GitOps Kubernetes Operator.

gitops

Prerequisites

You’ll need a Kubernetes cluster v1.11 or newer, a GitHub account, git and kubectl installed locally.

Install Helm v3 and fluxctl for macOS with Homebrew:

  1. brew install helm fluxctl

On Windows you can use Chocolatey:

  1. choco install kubernetes-helm fluxctl

On Linux you can download the helm
and fluxctl binaries from GitHub.

Install Flux

The first step in automating Helm releases with Flux is to create a Git repository with your charts source code.

On GitHub, fork this repository and clone it locally
(replace fluxcd with your GitHub username):

  1. git clone https://github.com/fluxcd/helm-operator-get-started
  2. cd helm-operator-get-started

*If you fork, update the release definitions with your Docker Hub repository and GitHub username located in
\releases(dev/stg/prod)\podinfo.yaml in your master branch before proceeding.

Add FluxCD repository to Helm repos:

  1. helm repo add fluxcd https://charts.fluxcd.io

Create the fluxcd namespace:

  1. kubectl create ns fluxcd

Install Flux by specifying your fork URL (replace fluxcd with your GitHub username):

  1. helm upgrade -i flux fluxcd/flux --wait \
  2. --namespace fluxcd \
  3. --set git.url=git@github.com:fluxcd/helm-operator-get-started

Install the HelmRelease Kubernetes custom resource definition:

  1. kubectl apply -f https://raw.githubusercontent.com/fluxcd/helm-operator/master/deploy/crds.yaml

Install Flux Helm Operator with Helm v3 support:

  1. helm upgrade -i helm-operator fluxcd/helm-operator --wait \
  2. --namespace fluxcd \
  3. --set git.ssh.secretName=flux-git-deploy \
  4. --set helm.versions=v3

The Flux Helm operator provides an extension to Flux that automates Helm Chart releases for it.
A Chart release is described through a Kubernetes custom resource named HelmRelease.
The Flux daemon synchronizes these resources from git to the cluster,
and the Flux Helm operator makes sure Helm charts are released as specified in the resources.

Note that Flux Helm Operator works with Kubernetes 1.11 or newer.

At startup, Flux generates a SSH key and logs the public key. Find the public key with:

  1. fluxctl identity --k8s-fwd-ns fluxcd

In order to sync your cluster state with Git you need to copy the public key and
create a deploy key with write access on your GitHub repository.

Open GitHub, navigate to your fork, go to Setting > Deploy keys click on Add deploy key, check
Allow write access, paste the Flux public key and click Add key.

GitOps pipeline example

The config repo has the following structure:

  1. ├── charts
  2. └── podinfo
  3. ├── Chart.yaml
  4. ├── README.md
  5. ├── templates
  6. └── values.yaml
  7. ├── hack
  8. ├── Dockerfile.ci
  9. └── ci-mock.sh
  10. ├── namespaces
  11. ├── dev.yaml
  12. └── stg.yaml
  13. └── releases
  14. ├── dev
  15. └── podinfo.yaml
  16. └── stg
  17. └── podinfo.yaml

I will be using podinfo to demonstrate a full CI/CD pipeline including promoting releases between environments.

I’m assuming the following Git branching model:

  • dev branch (feature-ready state)
  • stg branch (release-candidate state)
  • master branch (production-ready state)

When a PR is merged in the dev or stg branch will produce a immutable container image as in repo/app:branch-commitsha.

Inside the hack dir you can find a script that simulates the CI process for dev and stg.
The ci-mock.sh script does the following:

  • pulls the podinfo source code from GitHub
  • generates a random string and modifies the code
  • generates a random Git commit short SHA
  • builds a Docker image with the format: yourname/podinfo:branch-sha
  • pushes the image to Docker Hub

Let’s create an image corresponding to the dev branch (replace stefanprodan with your Docker Hub username):

  1. $ cd hack && ./ci-mock.sh -r stefanprodan/podinfo -b dev
  2. Sending build context to Docker daemon 4.096kB
  3. Step 1/15 : FROM golang:1.13 as builder
  4. ....
  5. Step 9/15 : FROM alpine:3.10
  6. ....
  7. Step 12/15 : COPY --from=builder /go/src/github.com/stefanprodan/k8s-podinfo/podinfo .
  8. ....
  9. Step 15/15 : CMD ["./podinfo"]
  10. ....
  11. Successfully built 71bee4549fb2
  12. Successfully tagged stefanprodan/podinfo:dev-kb9lm91e
  13. The push refers to repository [docker.io/stefanprodan/podinfo]
  14. 36ced78d2ca2: Pushed

Inside the charts directory there is a podinfo Helm chart.
Using this chart I want to create a release in the dev namespace with the image I’ve just published to Docker Hub.
Instead of editing the values.yaml from the chart source, I create a HelmRelease definition (located in /releases/dev/podinfo.yaml):

  1. apiVersion: helm.fluxcd.io/v1
  2. kind: HelmRelease
  3. metadata:
  4. name: podinfo-dev
  5. namespace: dev
  6. annotations:
  7. fluxcd.io/automated: "true"
  8. filter.fluxcd.io/chart-image: glob:dev-*
  9. spec:
  10. releaseName: podinfo-dev
  11. chart:
  12. git: git@github.com:fluxcd/helm-operator-get-started
  13. path: charts/podinfo
  14. ref: master
  15. values:
  16. image:
  17. repository: stefanprodan/podinfo
  18. tag: dev-kb9lm91e
  19. replicaCount: 1

Flux Helm release fields:

  • metadata.name is mandatory and needs to follow Kubernetes naming conventions
  • metadata.namespace is optional and determines where the release is created
  • spec.releaseName is optional and if not provided the release name will be $namespace-$name
  • spec.chart.path is the directory containing the chart, given relative to the repository root
  • spec.values are user customizations of default parameter values from the chart itself

The options specified in the HelmRelease spec.values will override the ones in values.yaml from the chart source.

With the fluxcd.io/automated annotations I instruct Flux to automate this release.
When a new tag with the prefix dev is pushed to Docker Hub, Flux will update the image field in the yaml file,
will commit and push the change to Git and finally will apply the change on the cluster.

gitops-automation

When the podinfo-dev HelmRelease object changes inside the cluster,
Kubernetes API will notify the Flux Helm Operator and the operator will perform a Helm release upgrade.

  1. $ helm -n dev history podinfo-dev
  2. REVISION STATUS CHART DESCRIPTION
  3. 1 superseded podinfo-0.2.0 Install complete
  4. 2 deployed podinfo-0.2.0 Upgrade complete

The Flux Helm Operator reacts to changes in the HelmRelease collection but will also detect changes in the charts source files.
If I make a change to the podinfo chart, the operator will pick that up and run an upgrade.

gitops-chart-change

  1. $ helm -n dev history podinfo-dev
  2. REVISION STATUS CHART DESCRIPTION
  3. 1 superseded podinfo-0.2.0 Install complete
  4. 2 superseded podinfo-0.2.0 Upgrade complete
  5. 3 deployed podinfo-0.2.1 Upgrade complete

Now let’s assume that I want to promote the code from the dev branch into a more stable environment for others to test it.
I would create a release candidate by merging the podinfo code from dev into the stg branch.
The CI would kick in and publish a new image:

  1. $ cd hack && ./ci-mock.sh -r stefanprodan/podinfo -b stg
  2. Successfully tagged stefanprodan/podinfo:stg-9ij63o4c
  3. The push refers to repository [docker.io/stefanprodan/podinfo]
  4. 8f21c3669055: Pushed

Assuming the staging environment has some sort of automated load testing in place,
I want to have a different configuration than dev:

  1. apiVersion: helm.fluxcd.io/v1
  2. kind: HelmRelease
  3. metadata:
  4. name: podinfo-rc
  5. namespace: stg
  6. annotations:
  7. fluxcd.io/automated: "true"
  8. filter.fluxcd.io/chart-image: glob:stg-*
  9. spec:
  10. releaseName: podinfo-rc
  11. chart:
  12. git: git@github.com:fluxcd/helm-operator-get-started
  13. path: charts/podinfo
  14. ref: master
  15. values:
  16. image:
  17. repository: stefanprodan/podinfo
  18. tag: stg-9ij63o4c
  19. replicaCount: 2
  20. hpa:
  21. enabled: true
  22. maxReplicas: 10
  23. cpu: 50
  24. memory: 128Mi

With Flux Helm releases it’s easy to manage different configurations per environment.
When adding a new option in the chart source make sure it’s turned off by default so it will not affect all environments.

If I want to create a new environment, let’s say for hotfixes testing, I would do the following:

  • create a new namespace definition in namespaces/hotfix.yaml
  • create a dir releases/hotfix
  • create a HelmRelease named podinfo-hotfix
  • set the automation filter to glob:hotfix-*
  • make the CI tooling publish images from my hotfix branch to stefanprodan/podinfo:hotfix-sha

Production promotions with sem ver

For production, instead of tagging the images with the Git commit, I will use Semantic Versioning.

Let’s assume that I want to promote the code from the stg branch into master and do a production release.
After merging stg into master via a pull request, I would cut a release by tagging master with version 0.4.10.

When I push the git tag, the CI will publish a new image in the repo/app:git_tag format:

  1. $ cd hack && ./ci-mock.sh -r stefanprodan/podinfo -v 0.4.10
  2. Successfully built f176482168f8
  3. Successfully tagged stefanprodan/podinfo:0.4.10

If I want to automate the production deployment based on version tags, I would use semver filters instead of glob:

  1. apiVersion: helm.fluxcd.io/v1
  2. kind: HelmRelease
  3. metadata:
  4. name: podinfo-prod
  5. namespace: prod
  6. annotations:
  7. fluxcd.io/automated: "true"
  8. filter.fluxcd.io/chart-image: semver:~0.4
  9. spec:
  10. releaseName: podinfo-prod
  11. chart:
  12. git: git@github.com:fluxcd/helm-operator-get-started
  13. path: charts/podinfo
  14. ref: master
  15. values:
  16. image:
  17. repository: stefanprodan/podinfo
  18. tag: 0.4.10
  19. replicaCount: 3

Now if I release a new patch, let’s say 0.4.11, Flux will automatically deploy it.

  1. $ cd hack && ./ci-mock.sh -r stefanprodan/podinfo -v 0.4.11
  2. Successfully tagged stefanprodan/podinfo:0.4.11

gitops-semver

Managing Kubernetes secrets

In order to store secrets safely in a public Git repo you can use the Bitnami Sealed Secrets controller
and encrypt your Kubernetes Secrets into SealedSecrets.
The SealedSecret can be decrypted only by the controller running in your cluster.

The Sealed Secrets Helm chart is available on Helm Hub,
so I can use the Helm repository instead of a git repo. This is the sealed-secrets controller release:

  1. apiVersion: helm.fluxcd.io/v1
  2. kind: HelmRelease
  3. metadata:
  4. name: sealed-secrets
  5. namespace: adm
  6. spec:
  7. releaseName: sealed-secrets
  8. chart:
  9. repository: https://kubernetes-charts.storage.googleapis.com/
  10. name: sealed-secrets
  11. version: 1.6.1

Note that this release is not automated, since this is a critical component I prefer to update it manually.

Install the kubeseal CLI:

  1. brew install kubeseal

At startup, the sealed-secrets controller generates a RSA key and logs the public key.
Using kubeseal you can save your public key as pub-cert.pem,
the public key can be safely stored in Git, and can be used to encrypt secrets without direct access to the Kubernetes cluster:

  1. kubeseal --fetch-cert \
  2. --controller-namespace=adm \
  3. --controller-name=sealed-secrets \
  4. > pub-cert.pem

You can generate a Kubernetes secret locally with kubectl and encrypt it with kubeseal:

  1. kubectl -n dev create secret generic basic-auth \
  2. --from-literal=user=admin \
  3. --from-literal=password=admin \
  4. --dry-run \
  5. -o json > basic-auth.json
  6. kubeseal --format=yaml --cert=pub-cert.pem < basic-auth.json > basic-auth.yaml

This generates a custom resource of type SealedSecret that contains the encrypted credentials:

  1. apiVersion: bitnami.com/v1alpha1
  2. kind: SealedSecret
  3. metadata:
  4. name: basic-auth
  5. namespace: adm
  6. spec:
  7. encryptedData:
  8. password: AgAR5nzhX2TkJ.......
  9. user: AgAQDO58WniIV3gTk.......

Delete the basic-auth.json file and push the pub-cert.pem and basic-auth.yaml to Git:

  1. rm basic-auth.json
  2. mv basic-auth.yaml /releases/dev/
  3. git commit -a -m "Add basic auth credentials to dev namespace" && git push

Flux will apply the sealed secret on your cluster and sealed-secrets controller will then decrypt it into a
Kubernetes secret.

SealedSecrets

To prepare for disaster recovery you should backup the sealed-secrets controller private key with:

  1. kubectl get secret -n adm sealed-secrets-key -o yaml --export > sealed-secrets-key.yaml

To restore from backup after a disaster, replace the newly-created secret and restart the controller:

  1. kubectl replace secret -n adm sealed-secrets-key -f sealed-secrets-key.yaml
  2. kubectl delete pod -n adm -l app=sealed-secrets

Getting Help" class="reference-link">Getting Help

If you have any questions about Helm Operator and continuous delivery:

Your feedback is always welcome!