Ukieweb

Diary

I write what I learn here

Continuous Delivery Using Jenkins for Docker & Kubernetes [Part 2]

Continuous Delivery Using Jenkins for Docker & Kubernetes [Part 2]

In my previous blog I wrote about continuous delivery with Jenkins where I discussed what DevOps is and the various stages involved. I also went through the process of installing Jenkinson an Ubuntuserver. Today I will be describing step by step the process of creating a pipeline in Jenkins to implement continuous delivery for Docker and Kubernetes

Prerequisite

  1. Setup Jenkinsas described in my last blog post
  2. Setup Kubernetes cluster. Here is a full guide on how to set up a K8 cluster on AWS.
  3. Understand basic Linux commands
  4. Understand basic Kubernetescommands

What You Will Learn

  • Creating a deployment pipeline in Jenkins
  • Creating a Jenkinsfie
  • Running your first build/deployment
  • Best practices for DevOps in Jenkins

1. Creating Jenkins Pipeline

Login to Jenkins and navigate to Jenkins dashboard. Click on New item as shown in the figure below

Jenkins dashboard

Select on pipeline and enter a new pipeline name

New pipeline

Change the pipeline definition to pipeline script from SCM. This file will include all the commands we want Jenkins to run.

Jenkinsfile

Configure the Source Control Management (SCM) as Git. Provide URL to the GitHub repository you want to use. Select the credentials that you configured before getting to this stage. Don’t forget to specify the GitHub branch you want to build from.

Github source

Save the pipeline and leave the script path to Jenkinsfile

save pipeline

2. Creating a Jenkinsfile

A Jenkinsfile contains all the commands you need to run in your deployment. This can be tweaked depending on the programming language you are using. In this blog I will use Java meaning that I will need to package the raw Java code into a Jar file before creating an Image.

Below is a sample Jenkinsfile for a pipeline. It's defined in stages with each stage running one specific task. The stages are;

  • Pull all the required dependencies for building a Java project

  • MVN command to create a package jar

  • Build and tag an image. Include --no-cache tag to prevent the build from caching the previous image. Notice the version v_0_0_1 which specifies the version of the image.

  • Login to Docker Hub. Ideally you should have the username and password saved as environment variables.

  • Push the image to Docker Hub

  • Deployment to Kubernetes cluster. Not the two commands in that stage

    Kubectl apply -f your_yaml_file Use this for the first time to create your deployment and a service

    Kubectl rolling-update service_name --image Use this for updating the pods running your services.

In the next blog I will introduce you to creating Dockerfile and Yaml deployment files. All the best as you implement your DevOps tasks on Docker and Kubernetes.

Sample Jenkinsfile

0 Comments

To make a comment you have to login