This document shows how to deploy this todo app java project to Kubernetes cluster using Maven plugin. It firstly uses Spotify Docker Maven Plugin to build a docker image and push the image to a private Azure Container Registry. Then, it uses Fabric8 Maven Plugin to generate Kubernetes resource yaml file and apply the yaml file to your cluster.
You can create the Azure Services using Azure CLI 2.0.
-
login your Azure CLI, and set your subscription id
az login az account set -s <your-subscription-id>
-
Create a resource group
az group create -n <your-resource-group-name> -l westeurope
-
Create Kubernetes cluster in Azure Container Service (AKS)
az aks create -g <your-resource-group-name> -n <your-kubernetes-cluster-name> --generate-ssh-keys
-
Connect to the cluster, this command download the Kubernetes configuration to your profile folder. The Fabric8 Maven Plugin and kubectl will use this configure file to interact with your Kubernetes cluster.
az aks get-credentials -g <your-resource-group-name> -n <your-kubernetes-cluster-name>
-
Install the kubectl command line
az acs kubernetes install-cli
-
Run below command to create an Azure Container Registry. After creation, use
login server
as Docker registry URL in the next section.az acr create -n <your-registry-name> -g <your-resource-group-name> --sku <sku-name>
where `<sku-name>` is one of the following: `{Basic,Managed_Basic,Managed_Standard,Managed_Premium}`.
-
Run below command to show your Azure Container Registry credentials. You will use Docker registry username and password in the next section.
az acr credential show -n <your-registry-name>
-
In the Maven settings file
~/.m2/settings.xml
, add a new<server>
element with your container registry credentials from previous steps.<server> <id>put-your-docker-registry-url</id> <username>put-your-docker-username</username> <password>put-your-docker-key</password> <configuration> <email>put-your-email</email> </configuration> </server>
-
In
<properties>
section of the project'spom.xml
, replace value in<docker.image.prefix>
element with your docker registry URL.<docker.image.prefix>put-your-docker-registry-url</docker.image.prefix>
-
Create Kubernetes resource yaml file fragments.
The Fabric8 Maven Plugin supports putting Kubernetes yaml files in the
src/main/fabric8
folder. And this plugin will aggregate these yaml file into one Kubernetes resource list file, which can be apply to cluster directly.-
Copy fabric8/deployment.yml to
src/main/fabric8/deployment.yml
. This file defines a deployment with the docker image you pushed to your Azure Container Registry. -
Copy fabric8/service.yml to
src/main/fabric8/service.yml
. This file defines a service to expose the deployment to external internet. -
Copy fabric8/secrets.yml to
src/main/fabric8/secrets.yml
. This file defines a docker-registry secrets, which will be used to pull image from your Azure Container Registry.
-
-
Verify you can run your project successfully in your local environment. (Run project on local machine)
-
Build the project package
mvn clean package
-
Build the docker image and push it to your Azure Container Registry.
mvn docker:build docker:push
-
Deploy image to your Kubernetes cluster.
mvn fabric8:resource fabric8:apply
NOTE: You can also run above commands as a one-liner:
mvn clean package docker:build docker:push fabric8:resource fabric8:apply
-
Get the external IP address. This may take a few minutes to wait the deploy success. Before finishing, the
external-ip
field should showpending
.kubectl get svc -w
-
Open the url you obtained in last step in your browser, you will find the todo app has been deployed to your Kubernetes cluster.
Delete the Azure resources you just created by running below command:
az group delete -y --no-wait -n <your-resource-group-name>