DEV Community

Paul Yu for Microsoft Azure

Posted on • Originally published at paulyu.dev on

Effortlessly Deploy to AKS with Open Source Tools Draft and Acorn

In this post, I'll walk you through deploying a web application to Azure Kubernetes Service (AKS) without having to write any Docker or Kubernetes manifest files.

Using open-source command-line tools Draft and Acorn, we'll containerize and deploy to AKS in just a few steps! Let's go 🚀

Pre-requisites

Before you begin, make sure you have access to an Azure Subscription. You will also need to have the following tools installed on your machine.

If you already have these tools installed, feel free to jump to Step 1.

I ran the commands below from a terminal on macOS. If you are on a Linux or Windows machine, please refer to each tool's documentation page (linked above) for OS-specific installation instructions.

# Install Azure CLI
brew update && brew install azure-cli

# Install Git CLI
brew install git

# Install Draft CLI
brew tap azure/draft
brew install draft

# Install Acorn CLI
brew install acorn-io/cli/acorn
Enter fullscreen mode Exit fullscreen mode

Step 1: Create an AKS cluster and install Acorn

Creating an AKS cluster can take a few minutes, so let's get the process started.

# Make sure you are logged into Azure
az login

# Make sure you are working with the proper Azure subscription
az account --set <YOUR_SUBSCRIPTION_NAME>

# Change this to your preferred Azure region
location=westus3

# To avoid any naming conflicts, store a random number which will be used in deployments
rand=$RANDOM

# Create a resource group
az group create --name rg-demoapp$rand \
  --location $location

# Create an AKS cluster
az aks create --name aks-demoapp$rand \
  --resource-group rg-demoapp$rand \
  --enable-addons web_application_routing # <-- this installs a managed nginx ingress controller
Enter fullscreen mode Exit fullscreen mode

For this demo, the only requirement is the Kubernetes cluster must have an ingress controller for Acorn to expose web applications with publicly accessible endpoints. Rather than installing an ingress controller manually, we'll leverage the Web Application Routing add-on for AKS.

AKS clusters are highly configurable, so I recommend you explore the documentation linked above or visit some of the OSS Labs repo that I've been contributing to.

With the AKS cluster deployed, you'll need to grab the credentials so we can connect to the cluster using kubectl and install Acorn into it.

If you don't have kubectl installed, run the following command to install it.

az aks install-cli

Pull down the kubectl credentials with the following command.

az aks get-credentials --name aks-demoapp$rand \
  --resource-group rg-demoapp$rand
Enter fullscreen mode Exit fullscreen mode

Verify you have access to the cluster.

kubectl cluster-info
Enter fullscreen mode Exit fullscreen mode

Installing Acorn on the cluster.

acorn install
Enter fullscreen mode Exit fullscreen mode

If you are interested in learning about what gets installed with the command above, visit the Acorn docs to learn more. If you are familiar with Kubernetes and want to see all the objects that gets created in your cluster, you can run the following command to output the manifest files.

acorn install -o yaml
Enter fullscreen mode Exit fullscreen mode

Step 2: Package your Acorn app

We need an app to deploy, so clone a version of the Azure Voting App, which I've re-written using Rust (I'm learning Rust so be kind 😅).

git clone --branch postgres https://github.com/pauldotyu/azure-voting-app-rust.git
cd azure-voting-app-rust
Enter fullscreen mode Exit fullscreen mode

Acorn will take care of creating your Kubernetes manifest files but it expects that you have a Dockerfile ready to containerize your app. Instead of writing a Dockerfile from scratch, we'll use the Draft CLI to generate one for us. Draft is able to detect languages used within your project and generate the appropriate Docker manifest to containerize your app.

Make sure you are in the azure-voting-app-rust directory before running the draft command.

draft create --dockerfile-only
Enter fullscreen mode Exit fullscreen mode

You will be prompted to enter the port to expose in the container. Enter 8080 as the port.

[Draft] --- Dockerfile Creation ---
✔ Please Enter the port exposed in the application: 8080
Enter fullscreen mode Exit fullscreen mode

Draft may not always produce a perfect Dockerfile and may require tweaking. However, it does provide a solid starting point. As an example, if you inspect the newly created Dockerfile, you'll notice it's using an older version of Rust (1.58.0). Let's update the first line of the Dockerfile to use the latest version; which at the time of this writing is 1.64.0.

sed -i -e 's/rust:1.58.0/rust:1.64.0/' Dockerfile
Enter fullscreen mode Exit fullscreen mode

Now here's the "gotcha"... you don't need to write Kubernetes manifest files to deploy your app. Instead, you will need to create another type of manifest file named Acornfile. This file is written in JSON-like syntax, feels very similar to a Docker Compose or Helm, and is used to package your application as an Acorn app.

Let's create an Acornfile that describes how our application should be deployed to Kubernetes. The Acornfile will deploy two containers with environment variables for each; one database container with the password set in plain text, and one web app container with a connection string to the database. It will also expose the web application over HTTP using the Kubnernetes ingress controller that has been deployed in your cluster.

The password is in plain text for demo purposes but you should take a look at Acorn's secrets management system.

cat << EOF > Acornfile
containers: db: {
  image: "postgres:15.0-alpine"
  env: {
    "POSTGRES_PASSWORD": "mypassword"
  }
  ports: "5432/tcp"
}
containers: web: {
  build: "."
  env: {
    "DATABASE_URL": "postgres://postgres:mypassword@db"
  }
  ports: publish: "8080/http"
  dependsOn: ["db"]
}
EOF
Enter fullscreen mode Exit fullscreen mode

The full reference for Acornfile schema can be found here

Step 3: Deploy the Acorn app to AKS

Now, you could run the acorn run command and have Acorn build your container and deploy to AKS at the same time. Instead, we'll publish the Acorn app artifacts to Azure Container Registry (ACR) and deploy it from there.

If you do not have an ACR resource yet, create one using the following command.

az acr create --name acrdemoapp$rand \
  --resource-group rg-demoapp$rand \
  --sku Standard \
  --admin-enabled
Enter fullscreen mode Exit fullscreen mode

Log into the container registry by grabbing the admin credentials and passing it into the acorn login command.

az acr credential show --name acrdemoapp$rand --query "passwords[0].value" -o tsv | acorn login acrdemoapp$rand.azurecr.io -u acrdemoapp$rand --password-stdin
Enter fullscreen mode Exit fullscreen mode

Build the Acorn artifact.

acorn build -t acrdemoapp$rand.azurecr.io/azure-vote:v0.1 .
Enter fullscreen mode Exit fullscreen mode

Push the Acorn artifact to ACR.

acorn push acrdemoapp$rand.azurecr.io/azure-vote:v0.1
Enter fullscreen mode Exit fullscreen mode

Deploy the Acorn app to AKS and wait for the endpoints to become healthy.

acorn run --name azure-vote-$rand acrdemoapp$rand.azurecr.io/azure-vote:v0.1
Enter fullscreen mode Exit fullscreen mode

You can also run the following command to view the status of the app. Once the app is fully deployed you will be able to navigate to the endpoint URL listed. Below is an example of what the output should look like.

$ acorn apps
NAME               IMAGE                                        HEALTHY   UP-TO-DATE   CREATED   ENDPOINTS                                                                       MESSAGE
azure-vote-10981   acrdemoapp10981.azurecr.io/azure-vote:v0.1   2         2            78s ago   http://web-azure-vote-10981-d70891c5f27d.v9lov9.alpha.on-acorn.io => web:8080   OK
Enter fullscreen mode Exit fullscreen mode

Finally, you can browse to the endpoint URL and submit your vote!

Summary

Overall, I think the Draft and Acorn projects are awesome tools to get developers up and running as quick as possible on Kubernetes.

One of the initial hurdles developers face when considering Kubernetes as a hosting platform is knowing how to package their applications into containers. Learning how to author a Dockerfile can be a challenge but with Draft CLI, you can automate most of this. Draft does more than just create Docker manifests... it can also create Kubernetes manifests, Helm charts, GitHub automation workflows, and publish your containers to Azure Container Registry. There is also an extension for Visual Studio Code that will provide a graphical user interface for some of the draft CLI commands.

When it comes to Kubernetes, the Acorn packaging framework aims to abstract much of Kubernetes away from you. Granted you will need to become familiar with the Acornfile schema and acorn CLI commands but it may be a more approachable way to onboarding and operating your apps on any Kubernetes cluster.

Lastly, with the AKS Web Application Routing add-on, even ingress controller installation and management can be abstracted away from you as Azure will take care of this.

I'd love to hear how this approach to onboarding to Kubernetes and/or AKS works for you. If you have any feedback or questions, feel free to let me know in the comments below or on social channels (Twitter, Mastodon, or Linkedin). Cheers!

Clean up

When you're done testing, you should tear down the deployment using the following commands.

# Stop and remove the Acorn app deployment
acorn rm $(acorn apps -qa)

# Delete the Azure resources
az group delete --name rg-demoapp$rand -y --no-wait
Enter fullscreen mode Exit fullscreen mode

Resources

Top comments (0)