Build Docker image from local directory in Azure Container Registry then deploy to Azure Kubernetes Service using Jenkins
This document shows how to deploy this todo app java project to Kubernetes cluster using Jenkins. Instead of installing Docker on the build agent, you can use Azure ACR Plugin to build your Docker image in Azure Container Registry with your Maven packaged jar
file.
On the Jenkins machine, it clones the toao-app-java-on-azure to local with Git Plugin, uses Maven Plugin to build out a jar
file.
Using Azure ACR Plugin, Jenkins uploads the jar
together with Dockerfile
to Azure Container Registry. ACR Quick Build will build a docker image and host it when receiving the Dockerfile
and jar
file.
After ACR Quick Build finishes pushing docker image. Jenkins will use Azure Container Agents Plugin to apply two Kubernetes resource yaml files to Azure Kubernetes Service.
This deployment instruction will include Maven package in the Dockerfile. If you want to do the Maven package on your Jenkins Server instead during the docker build, please go to Build Docker image from git repo in Azure Container Registry then deploy to Azure Kubernetes Service using Jenkins.
Verify you can run your project successfully in your local environment. (Run project on local machine)
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 eastus
-
Create a service principal and configure its access to all Azure resources under this subscription. Note all the information as service principal.
az ad sp create-for-rbac
-
Create Kubernetes cluster
az aks create -g <your-resource-group-name> -n <your-kubernetes-cluster-name> --generate-ssh-keys
-
Install
kubectl
on your local machineaz aks install-cli
-
Get access credentials for a managed Kubernetes cluster and save to local machine.
az aks get-credentials -g <your-resource-group-name> -n <your-kubernetes-cluster-name>
-
Get access credentials for a managed Kubernetes cluster. Note the yaml output as
kubeconfig
.az aks get-credentials -g <your-resource-group-name> -n <your-kubernetes-cluster-name> -f -
-
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.
-
Connect to the server with SSH and install the build tools:
sudo apt-get install git maven
-
Install the plugins in Jenkins.
Click 'Manage Jenkins' -> 'Manage Plugins' -> 'Available', then search and install the following plugins: EnvInject, Azure Container Agents Plugin, Azure Container Registry Tasks Plugin.
-
Add a Credential in type "Microsoft Azure Service Principal" with the service principal you created. Note the ID as
AZURE_CRED
. -
Add a Credential in type "Username with password" with your account of docker registry. Note the ID as
ACR_CRED
. -
Add a Credential in type "Kubernetes configuration (kubeconfig)" -> "Enter directly", with the kubeconfig you noted when creating AKS.
-
Add a new job in type "Pipeline".
-
Enable "Prepare an environment for the run", and put the following environment variables in "Properties Content":
AZURE_CRED_ID=[your Azure Credential ID] ACR_RES_GROUP=[your ACR resource group] ACR_NAME=[your ACR name] ACR_USERNAME=[your registry username] ACR_REGISTRY=[your ACR registry url, without http schema] ACR_CREDENTIAL_ID=[your credential id of ACR account] ACR_SECRET=[secret name you will created in AKS to store ACR credential] AKS_RES_GROUP=[your AKS resource group] AKS_NAME=[your AKS name] IMAGE_NAME=[image name you will push to ACR, without registry prefix] DOCUMENTDB_URI=[your documentdb uri] DOCUMENTDB_KEY=[your documentdb key] DOCUMENTDB_DBNAME=[your documentdb databasename]
-
Choose "Pipeline script from SCM" in "Pipeline" -> "Definition".
-
Fill in the SCM repo url, branch and script path.
For this example, you should put:
https://github.com/Microsoft/todo-app-java-on-azure
as "Repository URL"*/master
as "Branches to build"doc/resources/jenkins/Jenkinsfile-acr
as "Script Path"
In the
Jenkinsfile-acr
, it defines the pipeline step logic:- stage('init') - Checkout to the scm
- stage('build') - Use
Maven
to build out a jar file. Upload the jar andDockerfile
to ACR to build the docker image. - stage('deploy') - Apply a deployment to AKS with the new built docker image. Then expose the deployment to external.
-
Run jenkins job.
-
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>