Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Latest commit

 

History

History
205 lines (133 loc) · 7.91 KB

deploy-to-AKS-blueegreen-using-jenkins.md

File metadata and controls

205 lines (133 loc) · 7.91 KB

Deploy to AKS using Jenkins and blue/green deployment strategy

This document shows you how to deploy the todo app java project to AKS using Jenkins and blue/green deployment strategy.

Fork the todo-app-java-on-azure repo

  1. 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
  2. Change directories so that you are working from the cloned directory.

Create Azure services

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.

Create AKS

  1. login your Azure CLI, and set your subscription id

    az login
    az account set -s <your-subscription-id>
  2. 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>
  3. Create AKS

    az aks create -g <your-resource-group-name> -n <your-kubernetes-cluster-name> --node-count 2
  4. Install kubectl and jq, a lightweight command-line JSON processor.

Setting up AKS

Because we are doing blue/green deployment, we need to do some initial setup. You have two choices.

Run the set up script

  1. 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>
  2. Run the script.

Set up manually

  1. Download the Kubernetes configuration to your profile folder.

    az aks get-credentials -g <your-resource-group-name> -n <your-kubernetes-cluster-name> --admin
  2. 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
    
  3. 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

    Public IP

    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 and todoapp-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.

Create Azure Container Registry

  1. 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
  2. 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>

Prepare Jenkins server

  1. Deploy a Jenkins Master on Azure [https://aka.ms/jenkins-on-azure]

  2. 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 the docker commands.

  3. Install additional tools needed for this example:

    sudo apt-get install jq
    
  4. Install the plugins in Jenkins. Click 'Manage Jenkins' -> 'Manage Plugins' -> 'Available', then search and install the following plugins: Azure Container Service Plugin.

  5. Add dd a Credential in type "Microsoft Azure Service Principal" with your service principal.

  6. Add a Credential in type "Username with password" with your account of docker registry.

Edit the Jenkinsfile

  1. In your own repo, navigate to /deploy/aks/ and open Jenkinsfile

  2. 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>'
    

Create job

  1. Add a new job in type "Pipeline".

  2. Choose "Pipeline script from SCM" in "Pipeline" -> "Definition".

  3. Fill in the SCM repo url your forked repo and script path deploy/aks/Jenkinsfile

Run it

  1. Verify you can run your project successfully in your local environment. (Run project on local machine)

  2. 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.

  3. 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

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.

Additional information

For more on zero-downtime deployment, please check out this quickstart template.

Clean up

Delete the Azure resources you just created by running below command:

az group delete -y --no-wait -n <your-resource-group-name>