Skip to content

Commit

Permalink
Add content to CI-Flix
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuriy Bezsonov committed Sep 26, 2023
1 parent 2fe4244 commit c70a8c2
Show file tree
Hide file tree
Showing 9 changed files with 297 additions and 108 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
218 changes: 218 additions & 0 deletions website/docs/automation/gitops/flux/assets/ci.drawio

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
186 changes: 78 additions & 108 deletions website/docs/automation/gitops/flux/ci.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@ title: 'Continuous Integration and GitOps'
sidebar_position: 50
---

We have successfully bootstrapped Flux on our cluster so now we can deploy an application. To demonstrate how to make changes in the source code and leverage GitOps to deploy a new image to a cluster we introduce Continuous Integration pipeline.
We have successfully bootstrapped Flux on EKS cluster and deployed the application. To demonstrate how to make changes in the source code an application, build a new container images and leverage GitOps to deploy a new image to a cluster we introduce Continuous Integration pipeline. We will leverage AWS Developer Tools and [DevOps principles](https://aws.amazon.com/devops/what-is-devops/) to build [multi-architecture container images](https://aws.amazon.com/blogs/containers/introducing-multi-architecture-container-images-for-amazon-ecr/) for Amazon ECR.

Next, clone the repository for the application sources:
We created Continuous Integration Pipeline during the prepare environment step and now we need to make it up and running.

![CI](assets/ci-multi-arch.png)

First, clone CodeCommit repository for the application sources:

```bash
$ git clone ssh://${GITOPS_IAM_SSH_KEY_ID}@git-codecommit.${AWS_REGION}.amazonaws.com/v1/repos/${EKS_CLUSTER_NAME}-retail-store-sample ~/environment/retail-store-sample-codecommit
```

Next, populate the CodeCommit repository with the sources from the public repository of the [Sample application](https://github.com/aws-containers/retail-store-sample-app):

```bash
$ git clone https://github.com/aws-containers/retail-store-sample-app ~/environment/retail-store-sample-app

Expand All @@ -20,37 +26,72 @@ $ cp -R retail-store-sample-app/src retail-store-sample-codecommit
$ cp -R retail-store-sample-app/images retail-store-sample-codecommit
```

We use AWS CodeBuild and define `buildspec.yml` to build new `x86_64` and `arm64` images in parallel.

```file
manifests/modules/automation/gitops/flux/buildspec.yml
```

```bash
$ cp ~/environment/eks-workshop/modules/automation/gitops/flux/buildspec.yml ~/environment/retail-store-sample-codecommit/buildspec.yml
```

We use AWS CodeBuild also to build `Image Index` for `multi-architecture image` using `buildspec-manifest.yml`

```file
manifests/modules/automation/gitops/flux/buildspec-manifest.yml
```

```bash
$ cp ~/environment/eks-workshop/modules/automation/gitops/flux/buildspec-manifest.yml ~/environment/retail-store-sample-codecommit/buildspec-manifest.yml
```

Now we are ready to push our changes to CodeCommit and start the CodePipeline

```bash
$ git -C ~/environment/retail-store-sample-codecommit add .
$ git -C ~/environment/retail-store-sample-codecommit commit -am "initial commit"
$ git -C ~/environment/retail-store-sample-codecommit push --set-upstream origin main
```
You can navigate to `CodePipeline` in AWS Console and explore `eks-workshop-retail-store-sample` pipeline.

![ci-start](assets/ci-start.png)

As a result of a CodePipeline run with CodeBuild you will have a new image in ECR

![ci-start](assets/ecr.png)

While we are waiting for pipeline to create new images, let's [automate image updates to Git](https://fluxcd.io/flux/guides/image-update/) using Flux Image Automation Controller.

First, we need to install Flux components.

```bash
$ flux install --components-extra=image-reflector-controller,image-automation-controller
```
Edit file

Next, edit file `deployment.yaml` and add placeholder for new container image url

```bash
$ git -C ~/environment/flux pull
$ vi ~/environment/flux/apps/ui/deployment.yaml
```

Change
Change to:

image: "public.ecr.aws/aws-containers/retail-store-sample-ui:0.4.0" `# {"$imagepolicy": "flux-system:ui"}`

Commit changes to deployment:

```bash
$ git -C ~/environment/flux add .
$ git -C ~/environment/flux commit -am "Adding ImagePolicy"
$ git -C ~/environment/flux push
```

We need to deploy custom resource definitions (ImageRepository, ImagePolicy, ImageUpdateAutomation) for Flux to enable monitoring of new container images in ECR and automated deployment using GitOps.

```bash
$ echo $IMAGE_URI_UI
$ cat <<EOF | envsubst | kubectl create -f -
apiVersion: image.toolkit.fluxcd.io/v1beta2
kind: ImageRepository
Expand Down Expand Up @@ -119,6 +160,12 @@ EOF
imageupdateautomation.image.toolkit.fluxcd.io/ui created
```

We created the following architecture:

![ci-eks-gitops](assets/ci-eks-gitops.png)

Now, lets reconcile the changes.

```bash
$ flux reconcile image repository ui
$ flux reconcile source git flux-system
Expand All @@ -128,10 +175,14 @@ $ git -C ~/environment/flux pull
$ kubectl -n ui get pods
```

We can check that `image:` in the `deployment` has been updated to a new tag.

```bash
$ kubectl -n ui describe deployment ui | grep Image
```

To access `UI` using a browser we need to expose it using `Ingress` and `Load Balancer`.

Let's create an Ingress resource with the following manifest:

```file
Expand All @@ -152,30 +203,42 @@ NAME CLASS HOSTS ADDRESS PORTS
ui alb * k8s-ui-ui-1268651632.us-west-2.elb.amazonaws.com 80 15s
```

Check the UI page using url of the ingress
We wait 2-5 minutes until Application Load Balancer will be provisioned and check the UI page using url of the ingress.

```bash
$ export UI_URL=$(kubectl get ingress -n ui ui -o jsonpath="{.status.loadBalancer.ingress[*].hostname}{'\n'}")
$ curl $UI_URL/home | grep "Retail Store Sample"
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
<title>Retail Store Sample App</title>
100 <a class="navbar-brand" href="/home">Retail Store Sample</a>
13254 100 13254 0 0 236k 0 --:--:-- --:--:-- --:--:-- 239k
```

Edit file
![ui-before](assets/ui-before.png)

Let's introduce changes to the source code of the Sample Application.

Edit the file:

```bash
$ vi ~/environment/retail-store-sample-codecommit/src/ui/src/main/resources/templates/fragments/layout.html
```

Change
Change line 24

`<a class="navbar-brand" href="/home">Retail Store Sample</a>` to `<a class="navbar-brand" href="/home">Retail Store Sample New</a>`

Commit changes.

```bash
$ git -C ~/environment/retail-store-sample-codecommit status
$ git -C ~/environment/retail-store-sample-codecommit add .
$ git -C ~/environment/retail-store-sample-codecommit commit -am "Update UI src"
$ git -C ~/environment/retail-store-sample-codecommit push
```

Wait until CI will build the new image and Flux will deploy it
Wait until CodePipeline will build the new image and Flux will deploy it.

```bash
$ kubectl -n ui describe deployment ui | grep Image
Expand All @@ -197,106 +260,13 @@ $ kubectl -n ui describe deployment ui | grep Image
$ export UI_URL=$(kubectl get ingress -n ui ui -o jsonpath="{.status.loadBalancer.ingress[*].hostname}{'\n'}")
$ while [[ $(curl -s -o /dev/null -w "%{http_code}" $UI_URL/home) != "200" ]]; do sleep 1; done
$ curl $UI_URL/home | grep "Retail Store Sample"
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
10 <title>Retail Store Sample App</title>
<a class="navbar-brand" href="/home">Retail Store Sample New</a>
0 13258 100 13258 0 0 289k 0 --:--:-- --:--:-- --:--:-- 294k
```

Work in progress ...

-------

<!-- First let's remove the existing UI component so we can replace it:
```bash
$ kubectl delete -k /workspace/manifests/ui
```
Next, clone the repository we used to bootstrap Flux in the previous section:
```bash
$ git clone ssh://${GITOPS_IAM_SSH_KEY_ID}@git-codecommit.${AWS_DEFAULT_REGION}.amazonaws.com/v1/repos/${EKS_CLUSTER_NAME}-gitops ~/environment/gitops
```
Now, let's get into the cloned repository and start creating our GitOps configuration. Copy the existing kustomize configuration for the UI service:
```bash
$ mkdir ~/environment/gitops/apps
$ cp -R /workspace/manifests/ui ~/environment/gitops/apps
```
We'll then need to create a kustomization in the `apps` directory:
Copy this file to the Git repository directory:
```bash
$ cp /workspace/modules/automation/gitops/flux/apps-kustomization.yaml ~/environment/gitops/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:
Copy this file to the Git repository directory:
```bash
$ cp /workspace/modules/automation/gitops/flux/flux-kustomization.yaml ~/environment/gitops/apps.yaml
```
You Git directory should now look something like this which you can validate by running `tree ~/environment/gitops`:
```
.
├── 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:
```bash
$ (cd ~/environment/gitops && \
git add . && \
git commit -am "Adding the UI service" && \
git 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:
```bash test=false
$ 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:
```bash wait=30 hook=flux-deployment
$ 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:
```bash
$ 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
```
After successful build and deployment (5-10 minutes) we will have the new version of UI application up and running.

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. -->
![ui-after](assets/ui-after.png)
1 change: 1 addition & 0 deletions website/docs/automation/gitops/flux/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ $ prepare-environment automation/gitops/flux
This will make the following changes to your lab environment:
- Create an AWS CodeCommit repository
- Create an IAM user with access to the CodeCommit repository
- Create Continuous Integration Pipeline for [Sample Application UI](https://github.com/aws-containers/retail-store-sample-app)

You can view the Terraform that applies these changes [here](https://github.com/VAR::MANIFESTS_OWNER/VAR::MANIFESTS_REPOSITORY/tree/VAR::MANIFESTS_REF/manifests/modules/networking/custom-networking/.workshop/terraform).

Expand Down

0 comments on commit c70a8c2

Please sign in to comment.