July 17, 2018
Hot Topics:

Using Jenkins with Kubernetes AWS, Part 2

  • March 2, 2018
  • By Deepak Vohra
  • Send Email »
  • More Articles »

In the first of three articles on automating Kubernetes installation with Jenkins, " Using Jenkins with Kubernetes AWS, Part 1," we created the pre-requisite artifacts and created a Jenkins node. In this continuation article, we shall configure a Jenkinsfile for a Jenkins pipeline and create a Jenkins pipeline. This article has following sections:

Creating a Jenkinsfile

A Jenkins Pipeline is configured in a text file called Jenkinsfile in Groovy syntax. The Jenkinsfile consists of steps. A "step" is a build step, an instruction for Jenkins to implement. Two kinds of steps are supported: node and stage. A "node" is a top-level step that selects an executor/s on agent/s to run code on. A node is a machine (master or agent) and a label within the node step should match a label on the machine for the node to select the machine. The "node" step creates a workspace, which is a file directory, for a specific Jenkins job for resource intensive processing. The "node" step also schedules the steps defined within it on an executor slot by adding them to the Jenkins build queue. When the executor slot frees up the scheduled steps run from the build queue.

Create a file called Jenkinsfile (without any suffix). A file without a suffix is created on Windows with the following command from command prompt. Note the "." At the end of the command, which is to be included.

>notepad Jenkinsfile.

In the dialog "Cannot find the Jenkinsfile. file. Do you want to create a new file?" click Yes. A Jenkinsfile file gets created. In the Jenkinsfile, create a top-level step called "node" in which the "jenkins" label is the same as the Labels value configured in the Jenkins agent.

node('jenkins') {

Within the node step, add other steps to install a Kubernetes cluster. Add stage steps for installing Kubernetes. The procedure for installing Kubernetes is well documented and won't be discussed again. For reference, "Getting Started with Kubernetes on Amazon Web Services (AWS)" discusses the installation procedure in detail. Shell commands are run with "sh". User input for variables such as number of workers and instance type may be prompted for.

The kube-aws init command to initialize the CloudFormation stack has the cluster name hard-coded as "kubernetes-coreos-cluster" (arbitrary name). The --external-dns-name is set to a domain name, NOSQLSEARCH.COM, which would be different for different users. The EC2 key pair is set with --key-name to kubernetes-coreos, which was created earlier. The KMS Key is set to the KeyMetadata.Arn string generated earlier with the aws kms command in the --kms-key option. The generated Jenkinsfile is listed below:

node('jenkins') {

stage 'set env'

sh "sudo yum install gnupg2"
sh "gpg2 --keyserver pgp.mit.edu --recv-key FC8A365E"

sh "gpg2 --fingerprint FC8A365E"

sh "wget https://github.com/coreos/coreos-kubernetes/releases/
sh "wget https://github.com/coreos/coreos-kubernetes/releases/

sh "gpg2 --verify kube-aws-linux-amd64.tar.gz.sig kube-aws-linux-

sh "tar zxvf kube-aws-linux-amd64.tar.gz"

sh "sudo mv linux-amd64/kube-aws /usr/local/bin"

sh "export AWS_SECRET_ACCESS_KEY=7BaiUETep3zPYrhrzKYpBdwkwVV16

sh "aws ec2 create-volume --availability-zone us-east-1c --size 10
   --volume-type gp2"

stage 'kube-aws init'

sh "mkdir coreos-cluster"
sh "cd coreos-cluster"

sh "kube-aws init

stage "kube-aws render"
WORKER_COUNT = input message: 'Number of Nodes',
   parameters: [[$class: 'StringParameterDefinition',
   defaultValue: '3', description: '', name: 'WORKER_COUNT']]
INSTANCE_TYPE = input message: 'Instance Type',
   parameters: [[$class: 'StringParameterDefinition',
   defaultValue: 't2.micro', description: '',
   name: 'INSTANCE_TYPE']]

sh "kube-aws render"
sh "sed -i '''s/#workerCount: 1/workerCount:
   '''$WORKER_COUNT'''/''' cluster.yaml"
sh "sed -i '''s/#workerInstanceType: m3.medium/workerInstanceType:
   '''$INSTANCE_TYPE'''/''' cluster.yaml"
sh "kube-aws validate"

stage "Archive CFN"
step([$class: 'ArtifactArchiver', artifacts: 'cluster.yaml,
   fingerprint: true])

   stage "Deploy Cluster"
   shouldDeploy = input message: 'Should Deploy Cluster?',
      parameters: [[$class: 'ChoiceParameterDefinition',
      choices: 'yes\no', description: '', name: 'Deploy']]
   if(shouldDeploy == "yes") {
      echo "Deploying Kubernetes cluster"
      sh "kube-aws up"
      sh "kube-aws status"
      step([$class: 'ArtifactArchiver', artifacts: 'kubeconfig',
         fingerprint: true])

Creating a Jenkins Pipeline

In the Jenkins Dashboard, click create a new jobs to create a Jenkins Pipeline, as shown in Figure 1.

Select "Create new jobs" to create a Jenkins job
Figure 1: Select "create new jobs" to create a Jenkins job

In the user interface, specify a pipeline name (install-kubernetes), for example, and select Pipeline, as shown in Figure 2. Click OK.

Selecting Pipeline
Figure 2: Selecting Pipeline

The pipeline configuration wizard gets started, as shown in Figure 3.

Jenkins Pipeline Configuration Wizard
Figure 3: Jenkins Pipeline Configuration Wizard

Select the Pipeline tab. In the Definition field, select the Pipeline script option, as shown in Figure 4.

Pipeline tab
Figure 4: Pipeline tab

Copy and paste the Jenkinsfile listed earlier. Click Save, as shown in Figure 5.

Configuring Pipeline Script
Figure 5: Configuring Pipeline Script

A new Jenkins Pipeline gets created (see Figure 6).

Jenkins Pipeline created
Figure 6: Jenkins Pipeline created

Initially, the pipeline is not running and the Status should not list any Build having being started or run, as shown in Figure 7.

Jenkins Pipeline Status
Figure 7: Jenkins Pipeline Status


In this article, we configured a Jenkinsfile and used the Jenkinsfile to create a Jenkins pipeline. In the 3rd and concluding article on automating Kubernetes installation with Jenkins, we shall run the Jenkins pipeline created in this article to install Kubernetes. Subsequently, we shall test the installed Kubernetes cluster.

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.


Thanks for your registration, follow us on our social networks to keep up-to-date