Using Payara Platform to Rapidly Deploy Applications on Microsoft Azure
Originally published on 01 Oct 2018
Last updated on 01 Oct 2018
The Payara Platform is perfect for deploying Jakarta EE and MicroProfile applications on Microsoft Azure. One rapid option for deploying on Azure is to use Azure Application Services, especially Web App for Containers. The WebApp for Containers service allows you to rapidly deploy production Payara Micro applications onto Azure in seconds, allowing both rapid horizontal and vertical scaling on demand.
In this blog post we will walk you through creating a Docker image of your application based on Payara Micro and deploying this to Microsoft Azure. Throughout the blog post we will be using the Azure CLI for Linux.
If you follow the instructions you will need to replace the names we use in this blog with names specific to your organisation.
Preparation
First, we will create a new Resource Group called payara-app-rg in Azure to contain any resource we create for this blog post.
az group create --name payara-app-rg --location ukwest
Next, we will create a container registry within the same resource group. A container registry is needed to store our Docker images. Alternatively you can publish your image to docker hub or any other registry.
az acr create --name PayaraAppRegistry --resource-group payara-app-rg --sku Basic --location ukwest --admin-enabled true
To use the registry from Docker we will need to login to the registry. To do so we need to know the credentials which can be found by running:
az acr credential show --name PayaraAppRegistry
This will show the username and pasword required for login via docker. Note the URL for your container registry which will be shown when creating your registry.
docker login https://payaraappregistry.azurecr.io
Creating the Base Docker Image
For creating our application docker image, rather than using the published Payara Micro docker file, we shall create our own. To do this I will assume you have a web application you wish to deploy and that you have downloaded Payara Micro from the Payara website.
To make things simpler for future applications first we will create a base image for Payara Micro on Azure and then layer our application on top of the base image. This can then be reused for other applications and microservices and will reduce the image sizes we push applications. We will create the base image from the Azul Zulu Open JDK image, as Payara support customers also get support for Azul Zulu Open JDK builds.
Our Dockerfile looks like:
FROM azul/zulu-openjdk-alpine RUN mkdir -p /opt/payara/deployments RUN wget --quiet -O /opt/payara/payara-micro.jar https://search.maven.org/remotecontent?filepath=fish/payara/extras/payara-micro/5.182/payara-micro-5.182.jar EXPOSE 80 ENTRYPOINT ["java", "-jar", "/opt/payara/payara-micro.jar"] CMD ["--deploymentDir", "/opt/payara/deployments", "--nocluster", "--port", "80"]
And can be built using:
docker build . -t payara-app-service/base
Creating the Application Image
Now all we need to do is layer the war file we wish to deploy onto this image.
I’ve created a simple war file demonstrating a simple MicroProfile JAX-RS web service in this repo, once you’ve cloned the repository build the application using Maven and then build a local docker image using the command:
docker build . -t payara/azure-micro-example:1.0
You can test this image using the command:
docker run -p8080:80 payara/azure-micro-example:1.0
Once running you can test the REST service using the URL: http://127.0.0.1:8080/AzureWebAppsExample/webresources/example
Pushing the Docker Image to Azure Container Registry
Before we can use the created docker image with Azure App Services we need to upload the container to your Azure container registry. We can do this simple with docker by tagging the local image as a remote image and then using docker push:
docker tag payara/azure-micro-example:1.0 payaraappregistry.azurecr.io/azure-micro-example:latest docker push payaraappregistry.azurecr.io/azure-micro-example:latest
You can test your image has been uploaded successfully by using the azure CLI
az acr repository list --name payaraappregistry
Creating the Azure Application
Now we have a Docker image of our application on Azure Container Registry we can create an Azure Application and run it directly on Microsoft Azure. First step is to create an app service plan. The service plan determines the resources your application can use and the features available. To see more detail about what each plan level contains, see the documentation. For our application we will use the standard service plan which gives us access to autoscaling.
az appservice plan create --name payaraplan --resource-group payara-app-rg --sku S1 --is-linux
Now we have created the service plan we can create and deploy our application.
az webapp create --name payaraappexample --resource-group payara-app-rg --deployment-container-image-name azure-micro-example --plan payaraplan
We also have to configure the application to use the Azure container registry to find the application image.
az webapp config container set --name payaraappexample -g payara-app-rg -c azure-micro-example -r
https://payaraappregistry.azurecr.io -u PayaraAppRegistry -p <registry-password>
Once we have configured the container we can start the application.
az webapp start --name payaraappexample --resource-group payara-app-rg
Once the application has started you should be able to browse to the service on the URL.
http://payaraappexample.azurewebsites.net/AzureWebAppsExample/webresources/example
Pass Configuration to Your Application
If you hit the REST endpoint now you will see that the environment variable injected via the config api is not configured. To set the environment variable you can use the Azure CLI.
az webapp config appsettings set --resource-group payara-app-rg --name payaraappexample --settings exampleproperty=hello-world
Once this has been set you will need to restart the web application. App Service settings are passed to the docker container as environment variables and can be retrieved via the Microprofile config api or can be used to configure datasource connect strings, usernames and passwords.
Payara Micro Makes it Easy to Deploy MicroProfile and Java EE Apps on Azure
You can see that it is very easy to deploy MicroProfile and Java EE applications on Microsoft Azure using Azure App Services. All you need to do is create a Docker image with Payara Micro and you can be up and running rapidly with load balancing and auto scale out and auto scale up.
Related Posts
The Payara Monthly Catch - October 2024
Published on 30 Oct 2024
by Chiara Civardi
0 Comments
The Payara Monthly Catch - September 2024
Published on 30 Sep 2024
by Chiara Civardi
0 Comments