Building Kubernetes Applications with Acorn

Nov 4, 2022 by Sameer Kulkarni
Building Kubernetes Applications with Acorn

In this blog post, I would talk about the challenges faced by developers while building applications for Kubernetes. How developers in the world of cloud computing, are required to pick up tools and technologies that traditionally were required by DevOps engineers, irrespective of their choice. I’ll also talk about how Acorn attempts to solve this issue by abstracting away the platform-level details & letting developers focus on application code instead.

Introduction

Kubernetes is an open-source container orchestration engine that offers a host of benefits to run applications on it. It offers benefits such as self-healing, auto-scaling, service discovery, load-balancing, etc. OOTB. Hence more and more developers are working on creating applications for Kubernetes. Although, the process for developing such apps is cumbersome with a lot of steps. Developers also need Kubernetes knowledge to be able to develop & test the applications.

Acorn abstracts away the Kubernetes concepts, giving the developers the choice to dive into Kubernetes if they want to. It also simplifies the application development process. It does that by minimizing the steps required from writing the code to troubleshooting the application on Kubernetes.

The usual app development cycle for developers

This is how a typical application development cycle looks like when your target deployment platform is Kubernetes.

Screenshot-from-2022-10-21-14-16-56.png

  1. Write application code or make the required code updates
  2. Build your code & container image
  3. Push the container image to a container registry, such as Docker hub
  4. Create or update Kubernetes manifest YAML
  5. Apply the Kubernetes manifest to the Kubernetes cluster
  6. Check if the application updates have taken effect as intended. In case you don’t see any changes to the previous state of the application, check if you’ve pushed the image or not & if Kubernetes pod is using the correct image or not
  7. Use Kubernetes commands to look at container logs
  8. Troubleshoot application & K8s deployment/configuration issues.

Many steps in this cycle are time-consuming. Depending on the size of the image being built, pushing & pulling the image each time can feel like a drag. Especially if you’re trying to troubleshoot an issue whose root cause seems to elude you, these steps can quickly become stressful.

However, there are certain tweaks you could do to lessen the stress, such as:

  • Host a local container registry to lessen the network delay in the push/pull of container images
  • Write a script to build the image, push the image to the registry, update the new image tag in K8s YAML & update the K8s

Although, there aren’t any tools available to elegantly tackle this issue. Plus these are just the core steps & not the full list of required things you need to do. To host a local container registry yourself, you need to learn the process to do so & figure out how your Kubernetes cluster would fetch images from it. You’d also need to learn various Kubernetes concepts, irrespective of your choice. K8s concepts like pods, deployments, stateful sets, various kinds of services & their use cases, K8s config objects like config maps & secrets, Kubernetes volumes, endpoints & maybe more depending on the application you are working on.

Acorn to the rescue

We saw how the whole process of building k8s app is complex. The developers need to understand a lot of concepts & deal with multiple interfaces. This considerably increases the time to market and has a steep learning curve. But if you’re starting out with building K8s app, you don’t need to do all this. Acorn takes all this burden off of your shoulders with a fraction of the effort.

Acorn is an application packaging & deployment solution, which aims at simplifying the process of developing applications for Kubernetes. Acorn offers a simple CLI, which along with a CUE-based Acornfile can run your applications on a Kubernetes cluster, without needing to know any Kubernetes concepts. Its development mode makes application updates & debugging seamless for developers, which otherwise can seem daunting.

It is a thin layer of abstraction that sits on top of Kubernetes & lets you use its advantages without diving into the nitty-gritty details of the technology. It also takes care of hosting a container registry & as well as an ingress controller on some Kubernetes distributions which don’t run it by default. See Acorn documentation for more details.

Acorn in Action

Let’s look at this closer by running a sample application with Acorn!

The setup

First, we need a Kubernetes cluster. I am running my Kubernetes cluster using Docker Desktop. You can choose a different one if you like. Although, for the time being, I would recommend using one of Rancher Desktop, Docker Desktop & Minikube. These either come with all the Acorn dependencies pre-installed or Acorn has tighter integration with them to install the dependencies for you. See Acorn documentation for more details.

Install Acorn on your local machine as well as on your Kubernetes cluster using the install instructions. With Acorn installed on Docker Desktop Kubernetes cluster, you can see that Acorn is running the following pods in acorn-system namespace.

$ kubectl get pods -n acorn-system NAME READY STATUS RESTARTS AGE acorn-api-75947f657c-wf2sk 1/1 Running 0 2d22h acorn-controller-59bdd9bd6b-6ffjs 1/1 Running 0 2d22h buildkitd-64c547f65b-6cfz4 2/2 Running 0 2d22h containerd-config-path-w45ld 1/1 Running 0 2d22h traefik-5bcc97dd7d-xrfnp 1/1 Running 0 2d22h

The first three pods are required by acorn to build & deploy acorn applications to the Kubernetes cluster. The containerd-config-path pod is the local container registry, which acorn uses to push the built images to & deploy the same on the Kubernetes cluster. The last pod is the Traefik v2 Ingress Controller which it explicitly deploys on the Docker Desktop Kubernetes cluster since this K8s distribution doesn’t come with an inbuilt Ingress Controller.

The Application Code

Next, we will create a simple Python Hello World web application. For that, let’s first create an app.py file in an empty project directory & paste the below code into it. Refer to the git repository for the complete code files.

from flask import Flask, render_template_string # HTML Jinja2 Template which will be shown in the browser page_template = ''' <div style="margin: auto; text-align: center;"> <h1>Hello World!</h1> </div> ''' # Defining the Flask Web App app = Flask(__name__) # Define the root path to display Hello World! @app.route('/') def root(): return render_template_string(page_template)

Next, create a requirements.txt file with just one entry.

flask

Create a Dockerfile & past below code in the same.

FROM python:3-alpine WORKDIR /app ENV FLASK_APP=app.py ENV FLASK_RUN_HOST=0.0.0.0 RUN apk add --no-cache gcc musl-dev linux-headers ADD requirements.txt . RUN pip install -r requirements.txt COPY . . EXPOSE 5000 CMD ["flask", "run"]

Finally, create an Acornfile & paste the below code in it.

containers: { app: { build: "." env: { if args.dev { "FLASK_DEBUG": "1" } } if args.dev { dirs: "/app": "./" } ports: publish: "5000/http" } }

Run the application and access it on your browser with the given link.

$ acorn run . [+] Building 2.0s (11/11) FINISHED => [internal] load build definition from Dockerfile 0.1s => => transferring dockerfile: 272B 0.1s ... => => pushing layers 0.0s => => pushing manifest for 127.0.0.1:5000/acorn/acorn:latest@sha256:c488db1f94 0.0s silent-river STATUS: ENDPOINTS[] HEALTHY[] UPTODATE[] STATUS: ENDPOINTS[] HEALTHY[0] UPTODATE[0] pending ... STATUS: ENDPOINTS[http://app-silent-river-72275335.local.on-acorn.io => app:5000] HEALTHY[1] UPTODATE[1] OK

Screenshot-from-2022-10-20-14-59-07.png

Note that, after writing the application, neither did we need to know any Kubernetes concepts nor did we write any Kubernetes manifests nor run any Kubernetes commands. Acorn gives you a seamless experience of writing an application while taking care of making it ready for Kubernetes.

Run the application in debug mode

You can run your application in debug mode using Acorn. Debug mode lets you view the updated application while you make the code changes. Acorn takes care of building the application in real-time, pushing the new image to the container registry & updating the Kubernetes manifest for the same. All you need to do is update the code in your project directory & refresh the application on the browser.

$ acorn run --debug . [+] Building 3.7s (11/11) FINISHED ... app-bcfcbdd96-76kmq: * Serving Flask app 'app.py' app-bcfcbdd96-76kmq: * Debug mode: on app-bcfcbdd96-76kmq: WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. ... app-bcfcbdd96-76kmq: * Debugger is active! app-bcfcbdd96-76kmq: (sync): Start syncing ... STATUS: ENDPOINTS[http://app-weathered-brook-2f76dfd5.local.on-acorn.io => app:5000] HEALTHY[1] UPTODATE[1] OK

Update the browser URL to the new one & Update app.py file to add a new HTML line below Hello World! line.

... <div style="margin: auto; text-align: center;"> <h1>Hello World!</h1> <h2>My first Acorn!</h2> </div> ...

Observe the Acorn CLI update the application in real-time & reload the browser.

Screenshot-from-2022-10-20-15-17-33.png

Thus, we do not need to follow the usual steps to update/debug the application, but only need to keep working on the application itself. Acorn takes care of building the application & updating the same in Kubernetes.

How to enable application debugging in Acorn?

This is enabled by two things in particular.

  1. --dev flag in the acorn run command, which enables the Acorn development mode
  2. Enabling development mode in Acornfile 2.1. Since it’s a Flask app, enable the FLASK_DEBUG mode by setting it to 1, when Acorn development mode is on. 2.2 Enable hot reloading of the codebase by mounting the project directory into the container using dirs directive.

See the code snippet below from Acornfile for more context

... env: { if args.dev { "FLASK_DEBUG": "1" } } if args.dev { dirs: "/app": "./" } ...

Conclusion

Acorn offers an elegant solution to the difficulties faced by developers while building applications for Kubernetes. It abstracts away the Kubernetes components enabling the developers to focus on the application details. It deploys additional required components enabling faster application development & testing. Its development mode lets you update & troubleshoot applications without any additional steps.

Along with the above-mentioned capabilities, Acorn also has other abilities such as shipping the entire application as a single container image to container registries, deploying the application to dev or production environments, etc. Learn more about Acorn at https://docs.acorn.io. Learn more by joining a hands on training class.