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

Latest commit

 

History

History
125 lines (83 loc) · 3.98 KB

deploy-to-azure-container-service-using-jenkins.md

File metadata and controls

125 lines (83 loc) · 3.98 KB

Deploy to Azure Container Service using Jenkins

This document shows how to deploy this todo app java project to Kubernetes cluster using Jenkins.

Create Azure services

You can create the Azure Services using Azure CLI 2.0.

Create Azure Container Service

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

    az login
    az account set -s <your-subscription-id>
  2. Create a resource group

    az group create -n <your-resource-group-name> -l westeurope
  3. Create Kubernetes cluster

    az acs create --orchestrator-type kubernetes -g <your-resource-group-name> -n <your-kubernetes-cluster-name> --generate-ssh-keys
  4. Connect to the cluster, this command download the Kubernetes configuration to your profile folder. The kubectl will use this configure file to interact with your Kubernetes cluster.

    az acs kubernetes get-credentials -g <your-resource-group-name> -n <your-kubernetes-cluster-name>
  5. Install the kubectl command line

    az acs kubernetes install-cli

Create Azure Container Registry

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

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

    sudo apt-get install git maven docker.io
    
  3. Install the plugins in Jenkins. Click 'Manage Jenkins' -> 'Manage Plugins' -> 'Available', then search and install the following plugins: EnvInject, Azure Kubernetes CD Plugin.

  4. Add a Credential in type "SSH Username with private key" with your Kubernetes login credential.

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

Create job

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

  2. Enable "Prepare an environment for the run", and put the following environment variables in "Properties Content":

    ACS_SERVER=[your Azure Container Service server]
    ACS_CRED_ID=[your credential id of ACS login credential]
    ACR_SERVER=[your Azure Container Registry]
    ACR_CRED_ID=[your credential id of ACR account]
    WEB_APP=[the name of the app]
    DOCUMENTDB_URI=[your documentdb uri]
    DOCUMENTDB_KEY=[your documentdb key]
    DOCUMENTDB_DBNAME=[your documentdb databasename]
    
  3. Choose "Pipeline script from SCM" in "Pipeline" -> "Definition".

  4. Fill in the SCM repo url and script path.

  5. Create Kubernetes resource yaml file fragments.

    • Create src/main/jenkins/jenkins-sample.yml with yaml content from this file. This file defines a deployment and a service with the docker image you pushed to your Azure Container Registry.

Run it

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

  2. Run jenkins job.

  3. Get the external IP address. This may take a few minutes to wait the deploy success. Before finishing, the external-ip field should show pending.

    kubectl get svc -w
  4. Open the url you obtained in last step in your browser, you will find the todo app has been deployed to your Kubernetes cluster.

Clean up

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

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