This document shows you how to deploy the todo app java project to AKS using Jenkins and blue/green deployment strategy.
-
Use git to download a copy of the application to your development environment.
git clone https://github.com/microsoft/todo-app-java-on-azure.git
-
Change directories so that you are working from the cloned directory.
You can create the Azure Services using Azure CLI 2.0. For AKS, please make sure Azure CLI is version 2.0.25 or later.
AKS is still in preview at the time when these instructions are created. You may need to enable the preview for your Azure subscription. Please refer to this for more details.
-
login your Azure CLI, and set your subscription id
az login az account set -s <your-subscription-id>
-
Create a resource group. While AKS is in preview, only some location options are available.
az group create -n <your-resource-group-name> -l <your-location>
-
Create AKS
az aks create -g <your-resource-group-name> -n <your-kubernetes-cluster-name> --node-count 2
-
Install kubectl and jq, a lightweight command-line JSON processor.
Because we are doing blue/green deployment, we need to do some initial setup. You have two choices.
-
Edit set up script and update
<your-resource-group-name>
,<your-kubernetes-cluster-name>
,<your-location>
and<your-dns-name-suffix>
respectively:resource_group=<your-resource-group-name> location=<your-location> aks_name=<your-kubernetes-cluster-name> dns_name_suffix=<your-dns-name-suffix>
-
Run the script.
-
Download the Kubernetes configuration to your profile folder.
az aks get-credentials -g <your-resource-group-name> -n <your-kubernetes-cluster-name> --admin
-
Change directory to /deploy/aks/setup. Run the following kubectl commands to setup the services for the public end point and the two test end points:
kubectl apply -f service-green.yml kubectl apply -f test-endpoint-blue.yml kubectl apply -f test-endpoint-green.yml
-
Update the public and test end points DNS names. When AKS is created, an additional resource group is created. Look for resource group:
MC_<your-resource-group-name>_<your-kubernetes-cluster-name>_<your-location>
.Locate the public ip's in the resource group
For each of the services, find the external IP address by running:
kubectl get service todoapp-service
Update the DNS name for the corresponding IP address:
az network public-ip update --dns-name aks-todoapp --ids /subscriptions/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/resourceGroups/MC_<resourcegroup>_<aks>_<location>/providers/Microsoft.Network/publicIPAddresses/kubernetes-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
Repeat for
todoapp-test-blue
andtodoapp-test-green
:az network public-ip update --dns-name todoapp-blue --ids /subscriptions/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/resourceGroups/MC_<resourcegroup>_<aks>_<location>/providers/Microsoft.Network/publicIPAddresses/kubernetes-BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB az network public-ip update --dns-name todoapp-green --ids /subscriptions/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/resourceGroups/MC_<resourcegroup>_<aks>_<location>/providers/Microsoft.Network/publicIPAddresses/kubernetes-CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
Note that the dns name needs to be unique in your subscription.
<your-dns-name-suffix>
can be used to ensure the uniqueness.
-
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> --admin-enabled true
-
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>
-
Deploy a Jenkins Master on Azure [https://aka.ms/jenkins-on-azure]
-
Connect to the server with SSH and install the build tools on the server where you will run your build:
sudo apt-get install git maven
Install Docker by following the steps here. Make sure the user
jenkins
has permission to run thedocker
commands. -
Install additional tools needed for this example:
sudo apt-get install jq
-
Install the plugins in Jenkins. Click 'Manage Jenkins' -> 'Manage Plugins' -> 'Available', then search and install the following plugins: Azure Container Service Plugin.
-
Add dd a Credential in type "Microsoft Azure Service Principal" with your service principal.
-
Add a Credential in type "Username with password" with your account of docker registry.
-
In your own repo, navigate to /deploy/aks/ and open
Jenkinsfile
-
Update:
def servicePrincipalId = '<your-service-principal>' def resourceGroup = '<your-resource-group-name>' def aks = '<your-kubernetes-cluster-name>' def cosmosResourceGroup = '<your-cosmodb-resource-group>' def cosmosDbName = '<your-cosmodb-name>' def dbName = '<your-dbname>' def dockerRegistry = '<your-acr-name>.azurecr.io'
And update ACR credential id
def dockerCredentialId = '<your-acr-credential-id>'
-
Add a new job in type "Pipeline".
-
Choose "Pipeline script from SCM" in "Pipeline" -> "Definition".
-
Fill in the SCM repo url
your forked repo
and script pathdeploy/aks/Jenkinsfile
-
Verify you can run your project successfully in your local environment. (Run project on local machine)
-
Run jenkins job. If you run this for the first time, Jenkins will deploy the todo app to the Blue environment which is the default inactive environment.
-
To verify, open the urls:
- Public end point:
http://aks-todoapp<your-dns-name-suffix>.<your-location>.cloudapp.azure.com
- Blue end point -
http://aks-todoapp-blue<your-dns-name-suffix>.<your-location>.cloudapp.azure.com
- Green end point -
http://aks-todoapp-green<your-dns-name-suffix>.<your-location>.cloudapp.azure.com
- Public end point:
The public and the Blue test end points have the same update while the Green end point shows the default tomcat image.
If you run the build more than once, it will cycle through Blue and Green deployments. In other words, if the current environment is Blue, the job will deploy/test the Green environment and then update the application public endpoint to route traffic to the Green environment if all is good with testing.
For more on zero-downtime deployment, please check out this quickstart template.
Delete the Azure resources you just created by running below command:
az group delete -y --no-wait -n <your-resource-group-name>