This article is a quick guide to demonstrate how to deploy a MuleSoft (Mule) app in a container on Azure AKS (Azure Container Service – Kubernetes).
Apart from assuming that you have (very) basic knowledge about Azure, containers and MuleSoft, below are the other pre-requisites:
- SSH key pair – you will need a SSH Key Pair in order to login into the VMs created by AKS. If you don’t have one, don’t know how to generate one or have no idea what I’m talking about, have a look a this guide from Azure.
- Service Principal – If you don’t know what it is, check this guide: https://goo.gl/2WHkIE
- Azure CLI 2.0 – version 2.0.21 or later is required to use Azure AKS functionalities.
- Kubernetes command line client (kubectl) – it is used to manage the Kubernet cluster. If you have the Azure CLI installed, just run this command: az aks install-cli
- Docker Hub account – If you don’t have an account, just go to https://hub.docker.com and create one
- Mulesoft Anypoint Studio – If you don’t know what it is, check this page: https://www.mulesoft.com/platform/studio
- Anypoint Platform Account – you will need an account to download the ‘hello-word’ example used in this guide (unless you want to use your own app). If you don’t have an account, create one at: https://anypoint.mulesoft.com
- Project folder – We also need to create a folder named ‘hello-docker’ to save the files for this lab.
Creating Azure AKS cluster
The first thing we need to do is to create our Azure AKS (Kubernetes) cluster. Click New ontop left of you Azure Portal screen, and type container in the search box then click on ‘Azure Container Service – AKS’
In the Basics blade, enter the following information:
- Cluster Name – MuleCluster
- DNS prefix – leave blank
- Kubernetes version – 1.8.1
- Subscription – choose the subscription you want to use for this lab
- Resource group – create a new one named K8S-MULE-RG
- Location – choose your preferred location (Ive chosen ‘Central US at its been the most stable for me during the preview period)
Click on create button and fill the following information in the Configuration blade:
- User name – muleuser (or any other name you would prefer)
- SSH public key – use your own ssh public (not private!) key as mentioned in the pre-requisites
- Service principal client ID –
- Service principal client secret –
- Nodes count – number of nodes you want to run in your cluster
- Node virtual machine type – Ive used the cheapest one available for this lab (Standard_A2_v2)
- OS disk size – leave blank
Note: In real life scenarios, the last three properties above must represent the requirements outcomed from your sizing exercise, which are based on the App(s)/Container(s) that you are going to spread across the cluster. One particular point to pay attention is the OS disk size as AKS cluster nodes dont have data disks because of their stateless nature (obviously!).
Just click OK and check your configuration on the next blade. If you’re happy, click OK again and Azure will start to create your cluster.
Configure your local machine to connect to Azure AKS
In order to connect to your Azure AKS cluster, you need to download its credentials. Run the following command it the credentials will be downloaded to ./kube under your user home folder.
az aks get-credentials -g K8S-MULE-RG -n MuleCluster
To test the configuration, use the following command:
kubectl get nodes
You should see somthing like this:
NAME STATUS ROLES AGE VERSION
aks-agentpool-35569548-0 Ready agent 1d v1.8.1
aks-agentpool-35569548-1 Ready agent 1d v1.8.1
aks-agentpool-35569548-2 Ready agent 1d v1.8.1
Note: This command will overwrite any existing ~/.kube/config file. I find it useful to rename the configuration file to a name that can identify the target cluster. For example, if you rename our created ‘config’ file to ‘config-aks-mule’, you can add the configuration file details to your kubectl command like this:
kubectl --kubeconfig=.kube/config-aks-mule get nodes
Prepare your Mule app
In order to keep this lab simple, we are going to use the ‘Hello-World’ example from Anypoint Exchange.
Open your Anypoint Studio and click on File -> New -> Project from Example
In order to download an example you must be logged in on the Anypoint Platform. So, if youre not logged, click on the Login Button at the top right of the Exchange screen.
Enter your login credentials, then search and select the project Hello World.
Click on ‘Open’ button and the project will be downloaded to your workspace.
Before we create a deployable archive, we need to set a port for the application to listen.
Now select the HTTP listener configuration and click on the ‘Edit’ button.
Change the port value from ‘${http.port}’ to ‘8085′.
Right-click on the project root and select ‘Export’
Select ‘Mule deployable archive’ and click next
Choose the ‘hello-docker’ folder as the destination for our archive (zip) file
Create your Docker image
Now it is time to create a docker image that contains both Mulesoft app and runtime. In our hello-docker folder, create a file named ‘Dockerfile’ with the content below. It is out of the scope of this post to explain what a Dockerfile is, but for now you just need to understand that it is using an existing base image (java:openjdk-8-jdk) that contains the MuleSoft runtime, downloads the standalone runtime version and add our app (Hello Word) to the target image.
FROM java:openjdk-8-jdk
MAINTAINER rodrigo@rnascimento.com
# Install Mule standalone 3.9.0
RUN cd ~ && wget https://repository-master.mulesoft.org/nexus/content/repositories/releases/org/mule/distributions/mule-standalone/3.9.0/mule-standalone-3.9.0.tar.gz && echo "39b773bf20702f614faf30b2ffca4716 mule-standalone-3.9.0.tar.gz" | md5sum -c
RUN cd /opt && tar xvzf ~/mule-standalone-3.9.0.tar.gz && rm ~/mule-standalone-3.9.0.tar.gz && ln -s /opt/mule-standalone-3.9.0 /opt/mule
#Define environment variables.# Copy hello world app
COPY hello-world_1.4.0.zip "/opt/mule/apps"
# Define working directory.
WORKDIR /opt/mule
# Command to start the application
CMD [ "/opt/mule/bin/mule" ]
# Default http port
EXPOSE 8085:8085
Now that we described what our image will contain, its time to build our image. In the hello-docker folder, we will use the following syntax to build our image:
docker build -t $DOCKER_ID_USER/my_image .
In our case it will be:
docker build -t rodrigocmn/hello-docker:latest .
Push your image to Docker Hub
Now it is time to send your docker image to a repository. For this lab we will use Docker Hub as it is free. If you don’t have an account, just go to https://hub.docker.com and create one. Use the following command to login into your account:
docker login
Enter your user and password, then you should get a ‘Login Succeeded’ message. To push your image, just use the following syntax:
docker push $DOCKER_ID_USER/my_image
For example:
docker push rodrigocmn/hello-docker
If you go to your docker hub repository, you should see it published:
Deploying the Hello World app
In order to deploy our Hello World app to Kubernetes, we need to create a deployment file. In our hello-docker folder, create a file named ‘deployment.yaml’ and paste the content below. Again I’m not going into the details of this manifest file, but I just would like to mention that it will create a Kubernetes deployment that will be responsible for creating a POD running a container with the image we created (our ‘Hello World’ app) and a Service (Load Balancer) that will expose the app to the external world.
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: mule-hello-world
spec:
replicas: 1
template:
metadata:
labels:
app: mule-hello-world
spec:
containers:
- name: mule-hello-world
image: rodrigocmn/hello-docker:latest
ports:
- containerPort: 8085
---
apiVersion: v1
kind: Service
metadata:
name: mule-hello-world
spec:
type: LoadBalancer
ports:
- port: 8085
selector:
app: mule-hello-world
To create the deployment in our cluster use the following command:
kubectl create -f hello-docker.yaml
Checking if everything is working
It is time to see if our efforts paid off. As the container will take some time to be ready, we can start looking at Azure AKS (Kubernetes) dashboard to see the deployment progress. Run the following command to open a tunnel to your cluster and browse the dashboard:
az aks browse -g K8S-MULE-RG -n MuleCluster
Your default browser will open the overview page, which should look something like this:
Click on ‘Deployments’ in the left menu and it will show the deployment progress.
Click on ‘Services’ in the left menu to see the external endpoint we are going to use to access the app.
We can even look at the application logs to see if it was successfully deployed into MuleSoft runtime. Click on ‘Pods’ in the left menu and then select the ‘mule-hello-world-xxxxx’ pod, then click on the ‘Logs’ button in the top right menu. You should see a screen like this:
We can see that the runtime engine successfully identified the new app and started it. Now it is time to check if we can access it from the Internet. Using the external endpoint from the ‘Services’ page, we just need to append ‘/HelloWorld’ and see the following result:
Probably not the most exciting page to look at but, considering all the good stuff we learned through this lab, I hope you are satisfied with the achievement!