Scaling DevOps at the Edge with Kubernetes, Acorn and GitOps – Part 2

by | May 17, 2023

Spread the word

Configuring Continuous Deployment for Acorn Applications through GitOps

In the last tutorial, we explored how to use Flux to deploy the Acorn framework at scale on multiple Kubernetes clusters running at the edge. Continuing the series, I will walk you through the steps for automating the deployment of Acorns running our application code through Flux. We will also configure image automation to update Acorn apps as soon as a new version of the image is pushed to the registry. 

Step 1 – Exploring the Cluster Environment  

Assuming you completed all the steps introduced in the last tutorial, you should have a Kubernetes cluster running MetalLB, NGINX ingress, and Acorn deployment framework. 

The local working directory should have the below structure:

If your environment doesn’t reflect this state, ensure you complete the configuration before moving to the next step. 

Step 2 – Building and Pushing a Simple Acorn Image

Let’s build one of the simplest Acorns based on the NGINX web server that prints hello world. Create an Acorn file in the root directory of your repo. 

containers: {
 web: {
  image: "nginx"
  ports: publish: "80/http"
  files: {
   "/usr/share/nginx/html/index.html": "<h1>My First Acorn - v1.0.0!</h1>"
  }
 }
}

Build and push the image to Docker Hub.

export DOCKER_HUB_USER=YOUR_USERNAME
export DOCKER_HUB_PASS=YOUR_PASSWORD
acorn build -t index.docker.io/$DOCKER_HUB_USER/simple-acorn-app:v1.0.0 .

Log in to Docker Hub before pushing the image.

docker login -u $DOCKER_HUB_USER -p $DOCKER_HUB_PASS

Let’s push the Acorn image

acorn login -u $DOCKER_HUB_USER -p $DOCKER_HUB_PASS index.docker.io

Let’s run and validate the application.

acorn run -p 80 index.docker.io/$DOCKER_HUB_USER/simple-acorn-app:v1.0.0
curl  http://web-sparkling-waterfall-5a7ab8bd0ab2.1wyryr.alpha.on-acorn.io

Now that the app is working, let’s delete it and push it through GitOps.

acorn rm sparkling-waterfall

Step 3 – Deploying Acorn App Through GitOps

Switch to the clusters/cluster-1 directory of the repo and generate the YAML manifest for the simple app we built in the previous step. 

acorn run -o yaml -p 80 index.docker.io/$DOCKER_HUB_USER/simple-acorn-app:v1.0.0 > simple-app.yaml

This emits the below YAML file containing the definition of the Acorn:

apiVersion: api.acorn.io/v1
kind: App
metadata:
  name: simple-app
  namespace: acorn
spec:
  image: index.docker.io/janakiramm/simple-acorn-app:v1.0.0
  ports:
  - publish: true
    targetPort: 80

This definition will deploy the Acorn app after the Acorn deployment framework is ready. 

Let’s commit and push the changes to GitHub. 

git add .
git commit -m "added Acorn app"
git push

Force a reconciliation for Flux, and you should see the Acorn app getting deployed.

flux reconcile source git flux-system

You can send a curl request to see the output of the app.

 curl http://web-simple-app-2b2ff0af08ec.1wyryr.alpha.on-acorn.io

Step 4 – Implementing Image Automation for Acorn App

Let’s configure image update automation to update the app to the latest version as soon as we push the image to Docker Hub. 

For this, we need to refresh the Flux installation to include the required controllers and also enable read/write to commit new files to the GitHub repository. 

flux bootstrap github \
  --owner=YOUR_GITHUB_USERNAME \
  --repository=YOUR_GITHUB_REPO \
 --path=clusters/cluster-1 \
 --personal \
 --read-write-key \
 --components-extra=image-reflector-controller,image-automation-controller

Once it is done, we need to generate a set of artifacts to configure image automation. Under the clusters/cluster-1 directory, run the below commands:

flux create image repository app-repo \
--image=index.docker.io/$DOCKER_HUB_USER/simple-acorn-app \
--interval=1m \
--export > app-registry.yaml
flux create image policy app-policy \
--image-ref=app-repo \
--select-semver=1.0.x \
--export > app-policy.yaml

These commands associate image update automation control with the controller, which will keep scanning the repository for new images. 

Next, we need to enable Flux to write updated YAML file to the GitHub repository. For this, run the below command:

flux create image update flux-system \
--git-repo-ref=flux-system \
--git-repo-path="clusters/cluster-1" \
--checkout-branch=main \
--push-branch=main \
--author-name=fluxcdbot \
[email protected] \
--commit-template="{{range .Updated.Images}}{{println .}}{{end}}" \
--export > flux-system-automation.yaml

To enable updation, we need to add a comment to the application YAML file to link the updation policy created earlier.

apiVersion: api.acorn.io/v1
kind: App
metadata:
  name: simple-app
  namespace: acorn
spec:
  image: index.docker.io/janakiramm/simple-acorn-app:v1.0.0  # {"$imagepolicy": "flux-system:app-policy"}
  ports:
  - publish: true
    targetPort: 80

Commit and push the changes to GitHub repository. Force a reconcile and run the below command to see the status of the image updation controller. 

flux reconcile source git flux-system
flux get images all

Let’s change the Acornfile and push the image with a new tag.

containers: {
 web: {
  image: "nginx"
  ports: publish: "80/http"
  files: {
   "/usr/share/nginx/html/index.html": "<h1>My First Acorn - v1.0.1!</h1>"
  }
 }
}
acorn build -t index.docker.io/$DOCKER_HUB_USER/simple-acorn-app:v1.0.1 .
acorn push index.docker.io/$DOCKER_HUB_USER/simple-acorn-app:v1.0.1

In a few seconds, Flux detects the new image.

Force a reconcile and pull the repo to see the Acorn app YAML file getting updated.

The Acorn app will be updated to the latest image with tag v1.0.1

We have successfully deployed an Acorn app through GitOps and implemented image automation to roll out new versions as soon as the repo is updated.

Janakiram is a practicing architect, analyst, and advisor focusing on emerging infrastructure technologies. He provides strategic advisory to hyperscalers, technology platform companies, startups, ISVs, and enterprises. As a practitioner working with a diverse Enterprise customer base across cloud native, machine learning, IoT, and edge domains, Janakiram gains insight into the enterprise challenges, pitfalls, and opportunities involved in emerging technology adoption. Janakiram is an Amazon, Microsoft, and Google certified cloud architect, as well as a CNCF Ambassador and Microsoft Regional Director. He is an active contributor at Gigaom Research, Forbes, The New Stack, and InfoWorld. You can follow him on twitter.

Photo by Karsten Würth on Unsplash


Spread the word