Build Docker image from git repo 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 Github repo url.
On the Jenkins machine, it uses Azure ACR PLugin to queue an Azure Container Registry Quick build to build a todo-app-java-on-azure docker image, then apply the docker image to an Azure Kubernetes Service cluster with Azure Container Agents Plugin.
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 local directory 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
-
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] ACR_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" in "Pipeline" -> "Definition".
-
Fill the Script part with content in Jenkinsfile-acr-with-git
In the
Jenkinsfile-acr-with-git
, it defines the pipeline step logic:- stage('build') - Send the GitHub URL to ACR to queue a build.
- 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>