Set Up a CI/CD Pipeline with a Jenkins Pod in Kubernetes (Part 2)

In Part 1 of our series, we installed Docker, Minikube, and kubectl. We also got an image repository running on our cluster in Minikube, and tested the process of building and pushing an image using our Hello-Kenzan app.

In this article, we will set up continuous delivery for our application by running Jenkins in its own pod in Kubernetes. We will create a pipeline using a Jenkins 2.0 Pipeline script that automates building our Hello-Kenzan image, pushing it to the repository, and deploying it into Kubernetes. That’s right: we are deploying pods from a pod repository using other pods. While this may sound convoluted, once the infrastructure and application components are all running on Kubernetes, it makes the management of these pieces easy since they’re all under one ecosystem.

You may be asking, when will we get back to running the Kr8sswordz Puzzle app that we promised to showcase in Part 1? Good question! With Part 2 we’re laying the last bit of infrastructure we need so that we can run our Kr8sswordz Puzzle in Part 3.

Exercise 1: Creating and Building a Pipeline in Jenkins

Before you begin, you’ll want to make sure you’ve run through the steps in Part 1, in which we set up our image repository running in a pod. If you previously stopped Minikube, you’ll need to start it up again. Enter the following terminal command, and wait for the cluster to start:

minikube start --memory 8000 --cpus 2 --kubernetes-version v1.6.0

If you’d like, you can check the cluster status and view all the pods that are running with the following commands:

kubectl cluster-infokubectl get pods --all-namespaces

Now let’s start the interactive tutorial for Part 2 with the following terminal commands:

cd ~/kubernetes-ci-cdnpm run part2

Remember, you don’t actually have to type the commands below—just press Enter at each step and the script will enter the command for you!

1. Install Jenkins, which we’ll use to create our automated CI/CD pipeline. It will take the pod a minute or two to roll out.

kubectl apply -f manifests/jenkins.yml; kubectl rollout status deployment/jenkins

You can open a second terminal window and inspect all the pods that are running by entering the following command. You’ll see a pod for Jenkins now.

kubectl get pods --all-namespaces

2. Open the Jenkins UI in a web browser.

minikube service jenkins

3. Display the Jenkins admin password and copy it by right-clicking.BE CAREFUL NOT TO PRESS CTRL-C TO COPY THE PASSWORD AS THIS WILL STOP THE SCRIPT.

kubectl exec -it `kubectl get pods --selector=app=jenkins --output=jsonpath={.items..metadata.name}` cat /root/.jenkins/secrets/initialAdminPassword

4. Paste the Jenkins admin password in the box and click Continue. Click Install suggested plugins and wait for the process to complete.

5. Create an admin user and credentials, and click Save and Finish (make sure to remember these credentials as you will need them for repeated logins). Click Start using Jenkins.

6. We now want to create a new pipeline for use with our Hello-Kenzan app. On the left, click New Item.

BTf8sPj7dH0xW4DmVY9RlUC9NTth_SkSnPPsf4wc

Enter the item name as Hello-Kenzan Pipeline, selectPipeline, and clickOK.

4If4KfHDUj8hGFn8kkaavcX9H8sboABcODIkrVL3

7. Under the Pipeline section at the bottom, change the Definition to be Pipeline script from SCM.

8. Change the SCM to Git.

9. Change the Repository URL to be the URL for your forked Git repository, such as https://github.com/[Git User Name]/kubernetes-ci-cd.

P54j1L0WTmqTlzix-4ZE3oGUsZ_bSpe7vQR19tZq

Click Save. On the left, click Build Nowto run the new pipeline. You should see it run through the build, push, and deploy steps in a few seconds.

p8t29kTg4C9lVua3BEKM5R3oUkBbR7K21ocG8Ptx

10. Now view the Hello-Kenzan application.

minikube service hello-kenzan

You might notice that you’re not seeing the change you previously made to index.html. That’s because Jenkins wasn’t using your local code. Instead, Jenkins pulled the code from your forked repo on GitHub, used that code to build the image, pushed it, and then deployed it. We’ll take advantage of this in the next exercise.

Exercise 2: Pushing Code Changes Through the Pipeline

Thanks to the work we did in our last exercise, our Jenkins pipeline is up and running. In this exercise, we’ll see how easy it is to push and deploy code changes with Kubernetes and a CI/CD pipeline.

Before we get back to the tutorial, let’s try changing theindex.htmlin our Hello-Kenzan app, then building again to verify that the Jenkins build process works. Leaving the tutorial running in one terminal window, open another terminal window and do the following.

a. Open ~/kubernetes-ci-cd/applications/hello-kenzan/index.html in a text editor.

nano ~/kubernetes-ci-cd/applications/hello-kenzan/index.html

b. Add the following html at the end of the file (or any other html you like). (Tip: You can right-click in nano and choose Paste.)

<p>For more from Kenzan, check out our <a href="http://techblog.kenzan.com/">blog</a>.</p>

c. Press Ctrl+X to close the file—you’ll be prompted to save your changes.

d. Commit the changed file to your Git repo (you’ll need to enter your GitHub credentials):

cd ~/kubernetes-ci-cdgit commit -am "Added message to index.html"git push

e. In the Jenkins UI, click Build Now to run the build again.

XHbEIycRkuBFfAEFElMegMcNIz-9O5Kaw7NraK2L

Great—now let’s get back to the interactive tutorial. Just pressEnter to run the next step.

11. View the updated Hello-Kenzan application. You should see the message you added to index.html. (If you don’t, hold down Shift and refresh your browser to force it to reload.)

minikube service hello-kenzan

h1VOalTPpzt5R6Vjs6V1fQu8OYEDgKNzK2qKdsJD

And that’s it! You’ve successfully used your pipeline to automatically pull the latest code from your Git repository, build and push a container image to your cluster, and then deploy it in a pod. And you did it all with one click—that’s the power of a CI/CD pipeline.

If you’re done working in Minikube for now, you can go ahead and stop the cluster by entering the following command:

minikube stop

Up Next

In Part 2, we set up CI/CD by running Jenkins in a pod, and we are now successfully building, pushing, and then deploying from our image repository using a Jenkins pipeline script. We have all the groundwork laid for running and building the promised Kr8sswordz Puzzle app.

In Parts 3 and 4, we will make good on that promise by running the app. We will demonstrate features such as caching with etcd and scaling the app for load with Kubernetes APIs.

Kenzan is a software engineering and full service consulting firm that provides customized, end-to-end solutions that drive change through digital transformation. Combining leadership with technical expertise, Kenzan works with partners and clients to craft solutions that leverage cutting-edge technology, from ideation to development and delivery. Specializing in application and platform development, architecture consulting, and digital transformation, Kenzan empowers companies to put technology first.

Want to learn more about Kubernetes? Get unlimited access to the new Kubernetes Fundamentals training course for one year for $199. Sign up now!


Leave a Reply