normalian blog

Let's talk about Microsoft Azure, ASP.NET and Java!

How to deploy your Java applications into Azure Container Service

As you know, Azure Container Service(ACS) has some orchestrators such like DC/OS, Swarm and Kubernetes. In this post, you can get below knowledge.

What's requirement to use ACS for Java

In this post, I use my Windows machine for Docker client. Please prepare below environment at first to deploy your Java applications into ACS Kubernetes.

How to make Java application Docker images

Run below maven command in general command prompt. You can find your Java web application in your current directory. Please change "groupId" and "artifactId" for your environment.

mvn archetype:generate -DgroupId=com.mydomain -DartifactId=helloworld -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

Please edit helloworld\src\main\webapp\index.jsp like below if you need.

  <title>Docker deployed apps</title>
  <h2>Hello World! on Docker</h2>

Create Dockerfile for your application in your Java project folder like below.

FROM jboss/wildfly
MAINTAINER normalian

ADD ./target/helloworld.war /opt/jboss/wildfly/standalone/deployments


This Dockerfile pull public "jboss/wildfly" Docker image, deploy your application into WildFly and expose 8080 port for web accesses.

Create a Docker image with your application to run below command in your Java project folder.

helloworld>docker build -t normalian/wildfly-helloworld .
Sending build context to Docker daemon  35.33kB
Step 1/4 : FROM jboss/wildfly
 ---> ec96c1eb76d4
Step 2/4 : MAINTAINER normalian
 ---> Using cache
 ---> f445b07e38f8
Step 3/4 : ADD ./target/helloworld.war /opt/jboss/wildfly/standalone/deployments
 ---> Using cache
 ---> 92fd373f0f55
Step 4/4 : EXPOSE 8080
 ---> Using cache
 ---> 13a52eeb004a
Successfully built 13a52eeb004a
Successfully tagged normalian/helloworld-test:latest
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.

You will find a warning, but you can ignore the warning in this post. Please consider it depending on your environment. After that you can check your docker image and run your application with the image.

helloworld>docker images
REPOSITORY                                                  TAG                 IMAGE ID            CREATED             SIZE
normalian/wildfly-helloworld                                latest              9c25b0840c8c        9 minutes ago       584MB

helloworld>docker run -it --rm -p 8080:8080 normalian/wildfly-helloworld

Please access http://localhost:8080/helloworld/index.jsp with your browser. You can watch your jsp file of your application. After that, please stop your container, and the container will remove caused by "--rm" option.

How to push your Docker images into "Container registries" in Microsoft Azure

Access and create your "Container registries". Please refer this article if you need. After creating "Container registries", you can get "Registry name", "Login server" and "password" via the portal. Please refer below image.

Run below command to push your docker image into your "Container registries" in Microsoft Azure.

helloworld> docker login --username=<username> --password=<password>
helloworld> docker tag normalian/wildfly-helloworld
helloworld> docker push

After that, you can watch your image in your "Container registries" like below.

Please read this section at first if you haven't setup ssh in your client machine. Launch "Bash on Ubuntu on Windows" in your client machine, and run below commands.

normalian@k8s-master-2E00DAB3-0:~$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/normalian/.ssh/id_rsa):
Created directory '/home/normalian/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/normalian/.ssh/id_rsa.
Your public key has been saved in /home/normalian/.ssh/
The key fingerprint is:
The key's randomart image is:
+---[RSA 2048]----+
|                 |
|      o          |
|   . o o      . .|
|  + B *  .   . = |
|E+ X X +S o . + o|
|o O =.=  . = + . |
| +...o... o +    |
|oo ... ...       |
|O .oo...         |

Refer this article, and follow steps "until Login to the master node of ACS".

How to deploy and run your Java applications on ACS

Make a yaml file to define your application in master node of Kubernetes, and run below commands to publish your docker image.

normalian@k8s-master-2E00DAB3-0:~$ vi java-app-deployment.yaml
normalian@k8s-master-2E00DAB3-0:~$ cat java-app-deployment.yaml
apiVersion: apps/v1beta1
kind: Deployment
  name: my-java-app01
  replicas: 1
        app: my-java-app01
      - name: my-java-app01
        - containerPort: 8080
          name: wildfly

normalian@k8s-master-2E00DAB3-0:~$ kubectl create -f java-app-deployment.yaml
normalian@k8s-master-2E00DAB3-0:~$ kubectl get deployments
my-java-app01   1         1         1            1           4h

normalian@k8s-master-2E00DAB3-0:~$ kubectl expose deployment my-java-app01 --type=LoadBalancer

You can't access via internet without "kubectl expose deployment my-java-app01 --type=LoadBalancer", because Kubernetes deploy docker images into its own network. After this, run below commands to get IP to access from internet.

normalian@k8s-master-2E00DAB3-0:~$ kubectl get service
NAME            CLUSTER-IP   EXTERNAL-IP   PORT(S)          AGE
kubernetes     <none>        443/TCP          4h
my-java-app01   <pending>     8080:30226/TCP   2m
normalian@k8s-master-2E00DAB3-0:~$ kubectl get service
NAME            CLUSTER-IP   EXTERNAL-IP   PORT(S)          AGE
kubernetes     <none>        443/TCP          4h
my-java-app01   13.91.1.xx    8080:30226/TCP   3m

Input "http://13.91.1.xx:8080/helloworld" in your browser, and you can access your Java application.