We recently added support in Habitat Builder for Azure Container Registry (ACR). With this integration you can export to Azure Container Registry as part of your build job and use those containers inside AKS.
In this walkthrough we'll show you how to setup the ACR integration in Habitat Builder and how to configure Azure Kubernetes Services to run the a sample Habitat application.
What are the Azure Container Registry and Azure Kubernetes Service ?
Azure Container Registry is a managed Docker container registry service used for storing private Docker container images. It’s a fully managed Azure resource and gives you local, network-close storage of your container images when deploying to Azure.
Azure Kubernetes Service (AKS) is a fully managed hosted Kubernetes environment that makes it quick and easy to deploy and manage containerized applications without container orchestration expertise. It also eliminates the burden of ongoing operations and maintenance by provisioning, upgrading, and scaling resources on demand, without taking your applications offline.
Using both of them together allows you to quickly stand up a Kubernetes cluster along with a private repository to hold the containers.
We're going to use the
kubectl command-line tools throughout this example. If you're using the Azure Cloud Shell both of these tools are already installed.
If you choose to install and use the CLI locally see Install Azure CLI. Once you have the Azure CLI installed it can download
kubectl, the Kubernetes command line client, to manage the cluster:
All the code used in this example is available on GitHub at https://github.com/jamesc/azure-habitat-example.
Create AKS Cluster and ACR registry
First off we need to create a new cluster and registry. You can use the following script that will:
- Create a Azure Kubernetes Service Cluster
- Create a Azure Container Registry
- Grant access to the Registry for the service principal associated with the cluster
- Create a new Service Principal to be used by Habitat Builder to publish to the registry
The credentials for the Service Principal will be outputted by the script. These will be used in the next step to configure Habitat Builder.
Configure builder to export to the registry
Now that you have setup the registry you need to use the Habitat Builder user interface to add the Azure Container Registry to your origin. This uses the configuration details outputted by the setup script.
Connect your packages to the registry
The final step to be carried out in the Habitat Builder user interface is to connect a plan file (we're using the
mysql plans from the GitHub repository) and configure it to export it to our container registry. First we do this for the
wordpress package which has its plan file at
You can then trigger a new build of the package and follow progress in the live build output window of Habitat Builder. You'll see a container being built and pushed to the registry.
Repeat these steps for the
mysql package with a plan file at
mysql/plan.sh. You can verify that the packages have been pushed using the
Deploy the Habitat Operator to Kubernetes
The Habitat Operator for Kubernetes is the recommended way to integrate Habitat and Kubernetes into a unified whole. It leverages the Kubernetes API to unify communication between your Kubernetes clusters and Habitat Builder as it builds and manages your applications, and enables you to use both
kubectl and the
hab cli and keep all of your components in sync.
The following Kubernetes Deployment object installs the Habitat Operator in our cluster.
Deploy your application to AKS
Finally we're ready to deploy our application. Our example Wordpress application consists of two components - the Wordpress server and the backend MySQL database. The following Kubernetes configuration will:
- Create a Kubernetes secret with our MySQL credentials
- Deploy two Habitat packages (
habitat/mysql) from ACR
- Setup a Kubernetes load balancer to export our wordpress application port.
Again we deploy this using
kubectl apply. After a few minutes both pods should be running:
We can retrieve the external IP of the load balancer from Kubernetes:
And finally we can check that WordPress is up and running on the external IP.
In this walkthrough you saw how to setup Habitat Builder, Azure Container Registry and Azure Container Services,creating a complete continuous deployment pipeline for your application. This pattern can applied to any application packaged with Habitat creating a standard mechanism for you to run all your applications.
- Ask and answer questions on the Habitat forums
- Chat with the Habitat Community on Slack
- Learn more about Habitat