fluxcd - getting started

Ensure you have installed the followings 

- kubernetes 

- flux cli 

- github account 

Fluxcd will integrate with your github and apply any changes in the repository to deploy application to your k8s cluster. 

In order to do that, we have to bootstrap your application first. This will install flux core component such as source-controller, helm controller, kustomization controller and notification-controller.

To deploy an app from git you need a repository. Flux can create that for you 



flux bootstrap github --owner=mitzenjeremywoo --repository=fleet-infra --branch=main
--path=./clusters/my-cluster --personal

Then you need to enter your PAT tokens for github. Once you have done that, please enter the following commands to start working with your repository.


git clone https://github.com/mitzenjeremywoo/fleet-infra
cd fleet-infra

Creating your GitRepository source - this will be your source code or codebase where your application will be deploying and in this instance it is using "https://github.com/stefanprodan/podinfo"


flux create source git podinfo
--url=https://github.com/stefanprodan/podinfo -branch=master
--interval=1m --export > ./clusters/my-cluster/podinfo-source.yaml

which is similiar to this yaml below:-

apiVersion: source.toolkit.fluxcd.io/v1
kind: GitRepository
metadata:
  name: podinfo
  namespace: flux-system
spec:
  interval: 1m
  ref:
    branch: master
  url: https://github.com/stefanprodan/podinfo

Committing your code change.


git add -A && git commit -m "Add podinfo GitRepository"
git push



Next, create a deployment/implementation yaml by running the following command. It is saying use kustomize to do the deployment and as you will see below, there's a few options that you can configure for example to bump up the replicas.



flux create kustomization podinfo --target-namespace=default
--source=podinfo --path="./kustomize" --prune=true --wait=true
--interval=30m --retry-interval=2m --health-check-timeout=3m --export
> ./clusters/my-cluster/podinfo-kustomization.yaml

 This will create the equivalent yaml.

apiVersion: kustomize.toolkit.fluxcd.io/v1
kind: Kustomization
metadata:
  name: podinfo
  namespace: flux-system
spec:
  interval: 30m0s
  path: ./kustomize
  prune: true
  retryInterval: 2m0s
  sourceRef:
    kind: GitRepository
    name: podinfo
  targetNamespace: default
  timeout: 3m0s
  wait: true

Then to test your code changes:-


git add -A && git commit -m "Add podinfo Kustomization"
git push

Flux will auto-sync and deploy your app.

You can also check the flux sync status by running the following command (really confusing tho) - when i looked at it, i was under the impression that my deployment didn't work - mainly because of the displaced text outputs.

flux get kustomizations --watch

Your podinfo will be deployed to the default namespace. You can run the following command to get those details

kubectl get pods -n default


Let's say you wanted to change this to a helm release instead. You can instead change the yaml to the followings.


Convert git repository to helm repository


---
apiVersion: source.toolkit.fluxcd.io/v1
kind: HelmRepository
metadata:
  name: podinfo
  namespace: flux-system
spec:
  interval: 5m
  url: https://stefanprodan.github.io/podinfo

Then change kustomization to helm release. Commenting out the code changes that we had earlier.

---
apiVersion: helm.toolkit.fluxcd.io/v2
kind: HelmRelease
metadata:
  name: podinfo
  namespace: flux-system
spec:
  # patches:
  #   - patch: |-
  #       apiVersion: autoscaling/v2
  #       kind: HorizontalPodAutoscaler
  #       metadata:
  #         name: podinfo
  #       spec:
  #         minReplicas: 3
  #     target:
  #       name: podinfo
  #       kind: HorizontalPodAutoscaler
  releaseName: podinfo
  interval: 1m0s
  #path: ./kustomize
  chart:
    spec:
      chart: podinfo # Path to the chart directory in the Git repo
      sourceRef:
        kind: HelmRepository
        name: podinfo
  #prune: true
  #retryInterval: 2m0s
  # sourceRef:
  #   kind: GitRepository
  #   name: podinfo
  targetNamespace: default
  timeout: 3m0s
  #wait: true
  values:
    replicaCount: 2

Commit this and wait for it to sync. You should have 2 podinfo replica now.

Some tips on debugging. 

Sometimes it can be confusing when the deployment doesn't work. By committing and waiting is not an ideal way of dealing with it. 

What you can do is run kubectl apply to test out those HelmRepository or even HelmRelease before even committing it and always checks to logs from HelmRelease controller. If it is kustomization, you need to check Kustomization controller. 







Comments

Popular posts from this blog

The specified initialization vector (IV) does not match the block size for this algorithm