Kubernetes Application: CI/CD pipeline - Part I

Overview

Teaching: 0 min
Exercises: 0 min
Questions
Objectives
  • Being able to deploy a Jenkins server inside Kubernetes and integrate with GitHub for automated building/testing.

1. Preparation: individual!

  • Each student should launch an experiment of their own.
  • Visit Dr. Ngo’s Kubernetes-Jenkins branch and create a copy of this branch in your GitHub CloudLab profile repository.
  • You can overwrite the existing main branch, or, even better, create a new branch.
  • Once the experiment is fully deployed, and all Startup Finished running:
  • SSH into the head node and run the followings
$ cd
$ bash /local/repository/launch_network.sh
$ kubectl get nodes

2. Introduction to Jenkins

  • Jenkins
  • Open-source automation server that allows continuous integration:
    • Recognized whenever source code is changed and/or updated.
    • Automatic building and testing of updated codes.

3. Deploy Jenkins on Kubernetes

  • SSH to the headnode of your Kubernetes cluster.
$ kubectl create namespace jenkins
$ kubectl create -f /local/repository/jenkins.yaml --namespace jenkins
$ kubectl get pods -n jenkins
  • Repeat the kubectl get pods -n command a few time until you see that the jenkins pod is up and running.

  • What did we just deploy: jenkins.yaml.
  • Next, run the commands to create Service.
    • A Service is an abstraction that defines a logical set of Pods and a policy by which to access them.
    • The set of pods targeted by a service is determined by a selector.
    • It allows the decoupling of the microservices provided by the pods and the actual pods themselves (which could be created and destroyed as needed).
  • The service file for the Jenkins’ pod.
$ kubectl create -f /local/repository/jenkins-service.yaml --namespace jenkins
$ kubectl get services --namespace jenkins

$ kubectl get nodes -o wide
  • Grab one of the values of INTERNAL-IP, open a browser and go to INTERNAL-IP:30000

  • To find the default password:
    • Write down the NAME value from the first command and use that for the second command.
$ kubectl get pods -n jenkins
$ kubectl logs jenkins-794699f9bc-45tkq -n jenkins

  • Copy and paste the initial admin password to the Jenkins’ page and hit Continue.

4. Configure Jenkins

  • Click on Install selected plugins

  • You can either try to create the first admin user or Skip and continue as admin.

  • Click Save and Finish, then Start using Jenkins

5. Integrate Git and Jenkins

  • Create a new public GitHub repository named hello with the contents similar to this repository
  • In your hello repo, go to Settings/Webhooks and add a new webhook with the settings similar to the screenshot.
    • NOTE: The Payload URL should be YOUR jenkin server URL.
    • Click Add webhook when done.

  • On your Jenkins dashboard, select New Item, then setup the new project with the settings similar to the screenshot below. Clicl OK when done.

  • Click Source Code Management:

  • Click Build Triggers and select GitHub hook trigger for GITScm polling.
  • Click Build/Add build step and select Execute shell.

  • Enter make in the Command box, then click Save.

  • Make an edit to your C file in the hello repo and observe how the jenkins server launches a build.

  • Explore the interface to learn more about the details of the build.
  • Try to push an incorrect edit to the C file and see how the Jenkins server update the build.

6. Setup Jenkins agents to run on the Kubernetes cluster

  • Reminder: If you are launching a new experiment
$ bash /local/repository/launch_network.sh
  • Expand NodePort range by editing /etc/kubernetes/manifests/kube-apiserver.yaml and add the following line:
    • service-node-port-range=30000-50000

  • sa stands for service account.
$ kubectl create namespace jenkins
$ kubectl create clusterrolebinding permissive-binding --clusterrole=cluster-admin --user=admin --user=kubelet --group=system:serviceaccounts
$ kubectl -n kube-system create sa jenkins
$ kubectl create clusterrolebinding jenkins --clusterrole cluster-admin --serviceaccount=jenkins:jenkins
$ kubectl create -f /local/repository/jenkins.yaml --namespace jenkins
$ kubectl create -f /local/repository/jenkins-service.yaml --namespace jenkins

After setup Jenkins (see slides 4), let’s add Kubernetes support

  • Go to Manage Jenkins, then Manage Nodes and Clouds, go to the setting of the built-in node and set the number of executors to 0.

  • Go to Manage Jenkins, then Manage Plugins
    • Type Kubernetes into the search box and select the Kubernetes plugin.
    • Click Install without Restart.
    • Scroll to the bottom of the following page and check the Restart Jenkins after installing box.
    • Wait until Jenkins restart and log back in.

  • Manage Jenkins, then Manage Nodes and Clouds, then Configure Cloud.
  • Select Kubernetes from Add a new cloud dropbox.

7. Setup Jenkins/Kubernetes: configure Kubernetes Cloud details

  • Run kubectl cluster-info to get the information about the Kubernetes control plane.
$ kubectl cluster-info

  • Enter the information as shown in the figure below:
    • Kubernetes URL: use the information from Kubernetes control plane.
    • Check Direct Connection box.
    • Click Test Connection to confirm successful connection.

8. Setup Jenkins/Kubernetes: configure Pod Templates

  • Click Pod Templates.
    • Click Add Pod Template.
    • Click Add Container.
  • Fill in information about the template for Pod/Container as shown below

  • Click Save.

9. Launch a new pipeline

  • Create a branch from your hello repository and named it hello_kube. Make sure that this hello_kube branch is copied from the instructor’s hello repo’s hello_kube branch

  • In Jenkins, create a New Item of type Pipeline and name it hello_kube.
  • Under Build Triggers and check GitHub hook trigger for GITScm polling.
  • Under Pipeline select Pipeline script from SCM.
  • Once SCM appears, select Git and provide the Repository URL for your hello repo.
  • Under Branch to build, change */master to */hello_kube.
  • Make sure that Script Path, enter Jenkinsfile.
    • This is the Jenkinsfile in the hello_kube branch.
    • Click Apply.

  • Click Save.

10. Where the wild things are

Key Points