Continuous Deployment to Container Engine using Kubernetes & Jenkins

Introduction:

This tutorial shows how to achieve continuous deployment to container engine of google cloud using kubernetes and Jenkins

Kubernetes:

Kubernetes is an open-source system for automating deployment, scaling, and management of containerized applications.

 

Jenkins:

Jenkins is a self-contained, open source automation server which can be used to automate all sorts of tasks related to building, testing, and deploying software.

Continuous Deployment to Container Engine using Kubernetes &Jenkins:

Agenda:

  • Understanding a sample application
  • Deploying an application to Kubernetes
  • Uploading code to Google Code Repositories
  • Creating development pipelines in Jenkins
  • Deploying development environments
  • Deploying a canary release
  • Deploying production environments

Before you begin:

  • Select or create a Google Platform project
  • Enable billing for that project
  • Enable the Google Compute Engine, Google Container Engine APIs

Preparing your environment:

A: Setting up Jenkins on Container Engine

Objectives:

  • Creating a Kubernetes Cluster with Container Engine
  • Creating a Jenkins deployment and services
  • Configuring external Load Balancing
  • Connecting to Jenkins
  • Understanding the code

Preparing your environment:

  • First, prepare your deployment environment by activating Google Cloud Shell (Cloud Shell gives you access to the command line in Cloud Platform Console, and includes Google Cloud SDK and other tools you need for Cloud Platform development)

kub1

Creating a Kubernetes Cluster:

kub2.JPG

kub3.JPG

  • If the cluster is running, the URLs of where your Kubernetes components are accessible display.

Creating a Jenkins home volume:

To pre-populate Jenkins with the configurations discussed in Jenkins on Container Engine, you’ll need to create the volume from the supplied tarball. Container Engine mounts this volume into your Jenkins pod

kub5

Configuring Jenkins Credentials:

  • Open the jenkins/k8s/optionsfile, and replace CHANGE_ME with a new password
  • Save and exit the file
  • Enter cd ../../to return to the project directory

Next, create a Kubernetes namespace for Jenkins

kub6.JPG

Finally, create a Kubernetes secret. Kubernetes uses this object to provide Jenkins with the default username and password when Jenkins boots

kub7.JPG

Creating the Jenkins deployment and services:

  • Create the Jenkins deployment and services
  • kub8.JPG
  • The following output displays
  • kub9.JPG
  • Confirm that the pod is running and look for Runningin the STATUS column
  • kub10.JPG

Configuring external Load Balancing:

  • List the Jenkins services and confirm that jenkins-discoveryand jenkins-ui display
  • kub11.JPG

Setting up encryption:

  • If you don’t already have an SSL certificate for your domain, you can create a temporary SSL certificate and key pair by running the following command
  • kub13.JPG
  • Upload the certificate to Kubernetes as a secretobject
  • kub14.JPG

Creating the Load Balancer:

  • Create the load balancer. The command uses the ingress resource definition from the jenkins/k8s/lb/ingress.yamlfile in the sample code
  • kub15.JPG

Connecting to Jenkins:

  • Check the status of the load balancer’s health checks
  • kub16.JPG
  • From the same output, copy the [IP_ADDRESS]value of the Address This is the IP address you’ll use to connect to Jenkins. Address: [IP_ADDRESS]
  • In a web browser, navigate to the IP address from the previous step
  • Click log inon the top right of the window. Input jenkins for the User form field and the password value from the previous step for the Password form field
  • Click the log inbutton

You now have access to Jenkins and a Kubernetes cluster managed by Container Engine. To take this solution further, you could use these components in your continuous delivery pipeline.

Cleaning up:

To avoid incurring charges to your Google Cloud Platform account for the resources used in this tutorial, the easiest way is to eliminating billing is to Delete the project you created for the tutorial. To delete the project:

  • In the Cloud Platform Console, go to the Projects page.
  • In the project list, select the project you want to delete and click Delete project.
  • In the dialog, type the project ID, and then click Shut down to delete the project.

B: In Google Cloud Shell, navigate to the sample application directory

kub17.JPG

Deploying the Sample app to Kubernetes:

  • Create the Kubernetes namespace to logically isolate the production deployment
  • kub18.JPG
  • Create the dev and production deployments and services
  • kub19.JPG
  • Scale up the production environment frontends
  • kub20.JPG
  • Retrieve the external IP for the production services. It can take several minutes before you see the load balancer IP address
  • kub21.JPG
  • When complete, an IP address displays in the EXTERNAL-IPcolumn
  • Store the frontend service load balancer IP in an environment variable
  • kub22.JPG
  • Confirm that both services are working by opening the frontend external IP address in your browser
  • Open a separate terminal and poll the production endpoint’s /versionURL so you can observe rolling updates in the next section
  • kub23.JPG

Creating a repository to host the Sample app source code:

Next, create a copy of the sample app and push it to Cloud Source Repositories

  • Create the Cloud Source Repository
  • kub24.JPG
  • Initialize the local Git repository
  • kub25.JPG
  • Replace [PROJECT_ID]with your current project ID in the following command. To find your current project ID you can run gcloud config list project
  • kub26.JPG
  • Set the username and email address for your Git commits in this repository
  • Replace [EMAIL_ADDRESS]with your Git email address
  • kub27.JPG
  • Replace [USERNAME]with your Git username.
  • doc1.JPG
  • Add, commit, and push the files
  • doc2.JPG

Creating a Pipeline:

Adding your service account details:

Configure your credentials to allow Jenkins to access the code repository

  • In the Jenkins user interface, Click Credentials in the left navigation
  • Click the Jenkinslink in the Credentials table
  • doc3.JPG
  • Click Global Credentials
  • Click Add Credentials in the left navigation
  • Select Google Service Account from metadata from the Kind dropdown
  • Click OK.
  • There are now two global credentials. Make a note of second credential’s name for use later on in this tutorial.
  • doc4.JPG

Creating a Jenkins Job:

  • Click the Jenkinslink in the top left of the interface
  • Click the New Itemlink in the left navigation
  • Name the project sample-app, then choose the Multibranch Pipelineoption, then click OK
  • On the next page, click Add Sourceand select git
  • Paste the HTTPS clone URLof your sample-app repo in Cloud Source Repositories into the Project Repository Replace [PROJECT_ID] with your project ID
  • doc5.JPG
  • From the Credentialsdropdown, select the name of the credentials you created when adding your service account
  • Select the checkbox Build Periodicallyunder Build Triggers, and enter five asterisks (* * * * *) into the Schedule This ensures that Jenkins checks your code repository for changes once every minute. This field uses the CRON expression syntax to define the schedule
  • Click Save.
  • doc6
  • After you complete these steps, a job named “Branch indexing” runs. This meta-job identifies the branches in your repository and ensures changes haven’t occurred in existing branches. If you refresh Jenkins, the master branch displays this job.
  • The first run of the job fails until you make a few code changes in the next step.

Modifying the Pipeline definition:

Create a branch for the canary environment, called dev

doc7.JPG

Deploying a Dev release:

  • Open goand replace the two instances of blue with orange
  • Open goand change the version number from 1.0.0 to 2.0.0
  • doc8.JPG
  • Next, add and commit those files to your local repository
  • doc9.JPG
  • Finally, push your changes to the remote Git server
  • doc9.JPG
  • After the change is pushed to the Git repository, navigate to the Jenkins user interface where you can see that your build started
  • doc10.JPG
  • After the build is running, click the down arrow next to the build in the left navigation and select Console Output
  • doc11.JPG
  • In approximately one minute, the masterjob in the sample-app folder kicks off
  • doc12.JPG

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s