Kubernetes Application: CI/CD pipeline - Part II
Overview
Teaching: 0 min
Exercises: 0 minQuestions
Objectives
Being able to deploy a Jenkins server inside Kubernetes and integrate with GitHub for automated building/testing.
1. Updated Jenkins launch
- Launch an experiment from the
csc468lngo
profile using thekubernetes-jenkins-cdci
branch.- This branch is from kubernetes-jenkins-cdci.
- Once the experiment is fully deployed, and all Startup Finished running, SSH into the head node. You don’t have to do anything else.
- The launching of the overlay network is now fully automated and is integrated into the
kube_manager.sh
file.
2. Setup Jenkins
- All normal commands to launch Jenkins have been integrated into
launch_jenkins.sh
.$ bash /local/repository/launch_jenkins.sh
- To get the
initialAdminPassword
, you can run the following command directly:$ kubectl exec $(kubectl get pods -n jenkins | grep jenkins | awk '{print $1}') -n jenkins -- cat /var/jenkins_home/secrets/initialAdminPassword
kubectl exec
allows users to run a bash command directly inside the specified pod.$(kubectl get pods -n jenkins | grep jenkins | awk '{print $1}')
is a sequence of pipe commands:
$(kubectl get pods -n jenkins
get all pods| grep jenkins
parses the line containing the jenkins pod names| awk '{print $1}')
gets the first column, which is the pod ID only.- Configure Jenkins via the web interface as shown in slide 3 and 4 of Kubernetes Application: CI/CD pipeline - Part I.
- Add the following plugins to Jenkins:
- Kubernetes
- SSH Agent
3. Configure Jenkins
In the subsequent slides, we are revisiting the configuration of Jenkins in a more organized manner.
- Configure SSH credentials
- Configure one single executor to support remote SSH execution
- Configure Kubernetes access for deploying Jenkins container-agents.
- Configure pod- and container-templates
4. Configure Jenkins: SSH credentials
- On the CloudLab head node, run
ssh-keygen
(do not enter any password when asked).- Run
cat .ssh/id_rsa.pub >> .ssh/authorized_keys
- Run
cat ~/.ssh/id_rsa
and copy the displayed text, including the starting and ending dashes without any extra spaces.- On Jenkins Dashboard, go to
Manage Jenkins
/Manage Credentials
.
- Click on
Jenkins
underStores scoped to Jenkins
, thenGlobal credentials (unrestricted)
.- Click on
Add Credentials
.- Fill in the boxes as follows:
Kind
: SSH Username with private nameScope
: Global (Jenkins, nodes, items, all child items, etc)ID
: cloudlabUsername
: Enter your CloudLab login username here.Private Key
: CheckEnter directly
, clickAdd
, then paster the previously copied private key to this box.- Click
OK
.
5. Configure Jenkins: Single executor
- On Jenkins Dashboard, go to
Manage Jenkins
/Manage Nodes and Clouds
.
- Click on the gear icon for
Built-In Node
- Fill in the boxes as follows:
Number of executors
: 1Labels
: deployUsage
: Only build jobs with label expressions matching this node
6. Configure Jenkins: Kubernetes
- On Jenkins Dashboard, go to
Manage Jenkins
/Manage Nodes and Clouds
/Configure Clouds
.- Select
Kubernetes
fromAdd a new cloud
dropbox.- Click on
Kubernetes Cloud Details
.- Fill in the boxes as follows:
Kubernetes Name
: kubernetesKubernetes URL
: Information of theKubernetes control plane
gotten from runningkubectl cluster-info
on the CloudLab head node.- Check
Direction Connection
box.- Click
Test Connection
to confirm connection.
7. Configure Jenkins: Pod Templates
- Continue on the
Configure Clouds
from the previous slide.- Click
Add Pod Template
thenPod Template details
- Fill in the boxes as follows:
Name
: agent-templateNamespace
: jenkinsUsage
: Only build jobs with label expressions matching this node- Do not add container yet
- Click on
Add Volume
:
- Select
Host Path Volume
- Enter
/var/run/docker.sock
for bothHost path
andMount path
.- This is to enable the building and pushing of Docker images.
8. Configure Jenkins: Container Templates
In the scope of
Pod Template
- Click
Add Container
- Fill in the boxes as follows:
Container Template Name
: golangDocker image
: golang- Click
Add Container
Container Template Name
: dockerDocker image
: docker- Click
Add Environment Variable
for thedocker
container template
- Prior to this, go to
hub.docker.com
and login to your Docker Hub account.
- Go to Account Settings
- Go to
Security
.- Click on
New Access Token
.- Enter a short description for this token, allow
Access permission
to beRead, Write, Delete
, and then clickGenerate
.- Store this key some where safe.
- First environment variable:
Key
: DOCKER_TOKENValue
: the access token copied from before.- Second environment variable:
Key
: DOCKER_REGISTRYValue
: YOUR_DOCKERHUB_USERNAME/go_server- Third environment variable:
Key
: DOCKER_USERValue
: YOUR_DOCKERHUB_USERNAME- Click
Apply
and thenSave
.
9. Setup the app
- Create a branch called
go_app
on yourhello
repository (from the hands-on in the Jenkins’ eposide).- The
go_app
branch should have the same contents as https://github.com/CSC468-WCU/hello/tree/go_app- Setup the
webhook
for thego_app
to point to the Jenkins server in the previous slide.- The composition of the files in this branch is:
main.go
: The Go file that serves as the web server (the application to be deployed).main_test.go
: The Go file that serves as the test file (part of the CD process).Jenkinsfile
: Setup the pipeline for Jenkins to build, test, and push and deploy (if test is passed) the Go app.
- Edit the
registry
(line 4) to change toYOUR_DOCKERHUB_USERNAME/go_server
.- Edit the
registry
(line 5) to change toYOUR_DOCKERHUB_USERNAME
.- Edit the
registry
(line 73, 74, 75):
- Change my username
lngo
to your CloudLab username.- Be careful of capitalization in your CloudLab username. It has to match exactly.
- Change the IP address to the correct IP address of your head node.
Dockerfile
: The Docker image that will package the web server.deployment.yml
andservice.yml
: K8 configuration files.
10. Setup the Jenkins pipeline
- Login to the Jenkins server.
- Select
New Item
, and create a newPipeline
namedgo_server
.
- On
Build Triggers
tab, selectGitHub hook trigger for GITScm polling
,- On
Pipeline
tab, select the followings:
Definition
: Pipeline script from SCM (this will open new options)SCM
: GitBranch Specifier
:go_app
- Click
Save
- Click
Build Now
to activate the first build
- Open a new browser tab and visit the IP address of
head
at port 32000 to see the running server
11. CI/CD
- Edit
main.go
ingo_app
to introduce and error.- Observe that the build failed, but the web server is still running.
- Change
main.go
and alsomain_test.go
so that the build and test can pass.- Observe the webserver updated after the build completes successfully.
Key Points