Skip to main content

Deploying an application

We have successfully bootstrapped Flux on our cluster so now we can deploy an application. To demonstrate the difference between a GitOps-based delivery of an application and other methods, we'll migrate the UI component of the sample application which is currently using the kubectl apply -k approach to the new Flux deployment approach.

First let's remove the existing UI component so we can replace it:

~$kubectl delete -k ~/environment/eks-workshop/base-application/ui

Next, clone the repository we used to bootstrap Flux in the previous section:

~$git clone ssh://${GITOPS_IAM_SSH_KEY_ID}@git-codecommit.${AWS_REGION}.amazonaws.com/v1/repos/${EKS_CLUSTER_NAME}-gitops ~/environment/flux

Now, let's get into the cloned repository and start creating our GitOps configuration. Copy the existing kustomize configuration for the UI service:

~$mkdir ~/environment/flux/apps
~$cp -R ~/environment/eks-workshop/base-application/ui ~/environment/flux/apps

We'll then need to create a kustomization in the apps directory:

~/environment/eks-workshop/modules/automation/gitops/flux/apps-kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ui

Copy this file to the Git repository directory:

~$cp ~/environment/eks-workshop/modules/automation/gitops/flux/apps-kustomization.yaml \
~/environment/flux/apps/kustomization.yaml

The last step before we push our changes is to ensure that Flux is aware of our apps directory. We do that by creating an additional file in the flux directory:

~/environment/eks-workshop/modules/automation/gitops/flux/flux-kustomization.yaml
apiVersion: kustomize.toolkit.fluxcd.io/v1beta1
kind: Kustomization
metadata:
name: apps
namespace: flux-system
spec:
interval: 1m0s
sourceRef:
kind: GitRepository
name: flux-system
path: ./apps
prune: true
validation: client

Copy this file to the Git repository directory:

~$cp ~/environment/eks-workshop/modules/automation/gitops/flux/flux-kustomization.yaml \
~/environment/flux/apps.yaml

You Git directory should now look something like this which you can validate by running tree ~/environment/flux:

.
├── apps
│   ├── kustomization.yaml
│   └── ui
│   ├── configMap.yaml
│   ├── deployment.yaml
│   ├── kustomization.yaml
│   ├── namespace.yaml
│   ├── serviceAccount.yaml
│   └── service.yaml
├── apps.yaml
└── flux-system
├── gotk-components.yaml
├── gotk-sync.yaml
└── kustomization.yaml


3 directories, 11 files

Finally we can push our configuration to CodeCommit:

~$git -C ~/environment/flux add .
~$git -C ~/environment/flux commit -am "Adding the UI service"
~$git -C ~/environment/flux push origin main

It will take Flux some time to notice the changes in CodeCommit and reconcile. You can use the Flux CLI to watch for our new apps kustomization to appear:

~$flux get kustomization --watch
NAMESPACE     NAME          AGE   READY   STATUS
flux-system   flux-system   14h   True    Applied revision: main/f39f67e6fb870eed5997c65a58c35f8a58515969
flux-system   apps          34s   True    Applied revision: main/f39f67e6fb870eed5997c65a58c35f8a58515969

You can also manually trigger Flux to reconcile like so:

~$flux reconcile source git flux-system -n flux-system

Once apps appears as indicated above use Ctrl+C to close the command. You should now have all the resources related to the UI services deployed once more. To verify, run the following commands:

~$kubectl get deployment -n ui ui
NAME   READY   UP-TO-DATE   AVAILABLE   AGE
ui     1/1     1            1           5m
~$kubectl get pod -n ui
NAME                  READY   STATUS    RESTARTS   AGE
ui-54ff78779b-qnrrc   1/1     Running   0          5m

We've now successfully migrated the UI component to deploy using Flux, and any further changes pushed to the Git repository will be automatically reconciled to our EKS cluster.