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/ download/v0.7.1/kube-aws-linux-amd64.tar.gz" sh "wget https://github.com/coreos/coreos-kubernetes/releases/ download/v0.7.1/kube-aws-linux-amd64.tar.gz.sig" sh "gpg2 --verify kube-aws-linux-amd64.tar.gz.sig kube-aws-linux- amd64.tar.gz" sh "tar zxvf kube-aws-linux-amd64.tar.gz" sh "sudo mv linux-amd64/kube-aws /usr/local/bin" sh "export AWS_ACCESS_KEY_ID=AKIAJGFCP4HUFH4453FA" sh "export AWS_SECRET_ACCESS_KEY=7BaiUETep3zPYrhrzKYpBdwkwVV16 BTT+pt2/EXF" sh "aws ec2 create-volume --availability-zone us-east-1c --size 10 --volume-type gp2" stage 'kube-aws init' deleteDir() sh "mkdir coreos-cluster" sh "cd coreos-cluster" sh "kube-aws init --cluster-name=kubernetes-coreos-cluster --external-dns-name=NOSQLSEARCH.COM --region=us-east-1 --availability-zone=us-east-1c --key-name=kubernetes-coreos --kms-key-arn='arn:aws:kms:us-east-1:672593526685:key/ f380f8b3-e93d-4a37-b87f-9ad1dbe909be '" 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, stack-template.json,credentials/*,userdata/*', fingerprint: true]) stage "Deploy Cluster" shouldDeploy = input message: 'Should Deploy Cluster?', parameters: [[$class: 'ChoiceParameterDefinition', choices: 'yesno', 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.
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.
Figure 2: Selecting Pipeline
The pipeline configuration wizard gets started, as shown in Figure 3.
Figure 3: Jenkins Pipeline Configuration Wizard
Select the Pipeline tab. In the Definition field, select the Pipeline script option, as shown in Figure 4.
Figure 4: Pipeline tab
Copy and paste the Jenkinsfile listed earlier. Click Save, as shown in Figure 5.
Figure 5: Configuring Pipeline Script
A new Jenkins Pipeline gets created (see Figure 6).
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.
Figure 7: Jenkins Pipeline Status
Conclusion
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.