This document discusses various topics related to continuous integration/continuous delivery (CI/CD) including source control management using Git, build automation with Gradle, Jenkins pipelines for continuous delivery, using containers with Docker, orchestration with Kubernetes, monitoring with Prometheus and Grafana, self-healing and autoscaling in Kubernetes, and canary testing in Kubernetes. It provides an overview of key concepts and commands for setting up and working with each of these tools in a CI/CD pipeline.
Report
Share
Report
Share
1 of 24
Download to read offline
More Related Content
Kranthi kumar implement_ci_cd_my_notes
1. Kranthi Kumar- CI/CD Notes
1
Table of Contents
1. Source Control Management- = GIT .........................................................................................................2
2. Introducing Build Automation (GRADLE)..................................................................................................2
Commands used to install and run the Gradle Wrapper:.........................................................................3
Gradle Wrapper Command.......................................................................................................................3
Defining Tasks ...........................................................................................................................................3
Task Dependencies ...................................................................................................................................3
Plugins.......................................................................................................................................................4
Running Automated Test in Gradle...........................................................................................................4
3. Jenkins Pipelines .......................................................................................................................................4
Continuous Delivery..................................................................................................................................4
Automated Deployment in Pipeline .........................................................................................................5
4. Why Containers?.......................................................................................................................................5
Installing Docker .......................................................................................................................................6
Running with Docker in Production..........................................................................................................8
Installing Docker on Jenkins......................................................................................................................8
Jenkins Pipelines CD and a Dockerized App..............................................................................................9
Jenkins Pipelines CD and a Dockerized App..............................................................................................9
5. Orchestration..........................................................................................................................................10
Creating a Kubernetes Cluster ................................................................................................................11
Kubernetes Basics ...................................................................................................................................13
Deploying to Kubernetes with Jenkins....................................................................................................14
6. Monitoring ..............................................................................................................................................14
Cluster Monitoring..................................................................................................................................16
Application Monitoring...........................................................................................................................17
7. Kubernetes and Self-Healing..................................................................................................................17
Creating Liveness Probes in Kubernetes.................................................................................................18
8. Kubernetes and Autoscaling ...................................................................................................................20
Horizontal Pod Autoscalers in Kubernetes .............................................................................................20
9. What is Canary Testing?..........................................................................................................................21
2. Page 2 of 24
Implementing a Canary Test in Kubernetes............................................................................................23
Kubernetes Canary Testing with Jenkins Pipelines.................................................................................24
1. Source Control Management- = GIT
1. yum install git
2. git --version
3. git config --global user.name "kranthi"
4. git config --global user.email kk@kranthi.com
5. git clone repository URL
6. git status
7. git add <file name> or git add . or git add -A
8. git commit -m "< commit message >"
9. git push or git push -u < remote name, usually origin > < branch name >
10.git checkout <existing branch name>
11.git checkout -b <new branch name>
12.git branch
13.git tag <new tag name>
14.git tag
15.git pull
2. Introducing Build Automation (GRADLE)
You can find more information about installing Gradle at https://gradle.org/install/
Here are the commands used in the demo to install gradle:
Command 1 --> cd ~/
Command 2 --> wget -O ~/gradle-4.7-bin.zip https://services.gradle.org/distributions/gradle-
4.7-bin.zip
Command 3 --> sudo yum -y install unzip java-1.8.0-openjdk
Command 4 --> sudo mkdir /opt/gradle
Command 5 --> sudo unzip -d /opt/gradle/ ~/gradle-4.7-bin.zip
Command 6 --> sudo vi /etc/profile.d/gradle.sh
Command 7 --> Put this text into gradle.sh: export PATH=$PATH:/opt/gradle/gradle-4.7/bin
Command 8 --> Then set permissions on gradle.sh: sudo chmod 755 /etc/profile.d/gradle.sh
3. Page 3 of 24
Command 9 --> Finally, after logging out of the server and logging back in:gradle --version
2.1 Commands used to install and run the Gradle Wrapper:
Gradle wrapper allows gradle to install itself using just the files from your project's source
control.
Wrapper is useful because
1. it removes the need to have Gradle installed beforehand in order to run the build.
2. Ensure the project is always built with a specific version of Gradle
3. Lets you build multiple projects with different Gradle versions on one system.
4. Anyone can run the build quickly and easily
2.2 Gradle Wrapper Command
cd ~/
mkdir my-project
cd my-project
gradle wrapper
./gradlew build
2.3 Defining Tasks
The build.gradle file controls what tasks are available for your project.
You can define your own task with custom code in build.gradle:
task my Task {
Print ‘Hellow, World’}
Most of tasks you use will come built into either Gradle itself or plugins.
2.4 Task Dependencies
Gradle uses the concept of task dependencies to determine what tasks need to be run for a
particular build command:
If you run a task, any other task depend on it will also be run
Task dependencies also determines the order in which tasks get run:
A task’s dependencies will always run before that task
You can define a dependency relationship between tasks in build.gradle like this:
Task A.dependson anotherTask B
Here is the final build.gradle from the demo:
plugins {
id 'com.moowork.node' version '1.2.0'
}
task sayHello {
doLast {
println 'Hello, World!'
4. Page 4 of 24
}
}
task anotherTask {
doLast {
println 'This is another task'
}
}
If you have the gradle wrapper installed in your project (for example, by using gradle
init), you can run the build defined in this build.gradle like so:
./gradlew sayHello
2.5 Plugins
Gradle has a huge variety of plugin available, many of them contributed by the community.
Plugins add all kinds of functionality to Gradle. They usually include pre-built tasks that you can
configure to do what you need.
You can include plugins in your build.gradle like this:
Plugins {
Id “<plugin-id>” version “<plugin version>”
}
2.6 Running Automated Test in Gradle
Generally, automated test will be run in Gradle as a task that gets executed as part of the build
process:
Task build
Task test
Build.dependensOn test
Whenever ./gradlew build gets run, the test task also will run.
3. Jenkins Pipelines
Pipelines has a domain-specific-language (DSL) that is used to define the pipeline logic.
There are two types of pipeline syntax you can use
Scripted A bit more like procedural code
Declarative – Syntax describes the pipeline logic
3.1 Continuous Delivery
5. Page 5 of 24
Continuous delivery means ensuring that you are always able to deploy any version of your
code. It is necessary in Continuous deployment, where you are actually deploying your code
frequently.
In order to support continuous delivery/Deployment with a Jenkin pipelines, we need use the
Pipeline to automate the deployment process.
3.2 Automated Deployment in Pipeline
Define stages for stages of the CD process that involves deploying.
Deploy to UAT, Deploy to QA, Deploy to Staging and Deploy to
Production
In each deployment stage, define Steps that perform necessary tasks to carry out
the deployment
Copy files to a server, restart a service
We can also prompt a user for approval before performing the actual production
deployment.
sudo yum -y remove java
sudo yum -y install java-1.8.0-openjdk
sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat
-stable/jenkins.repo
sudo rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key
sudo yum -y install jenkins-2.164.2
sudo systemctl enable jenkins
sudo systemctl start jenkins
4. Why Containers?
8. Page 8 of 24
4.2 Running with Docker in Production
4.3 Installing Docker on Jenkins
Here are the commands used in the demo for this lesson:
sudo yum -y install docker
9. Page 9 of 24
sudo systemctl start docker
sudo systemctl enable docker
sudo groupadd docker
sudo usermod -aG docker jenkins
sudo systemctl restart jenkins
sudo systemctl restart docker
4.4 Jenkins Pipelines CD and a Dockerized App
You can find the source code of the train-schedule app
here: https://github.com/linuxacademy/cicd-pipeline-train-schedule-dockerdeploy
And you can find the final version of the Jenkinsfile from the demo on the example-
solution branch of that project here: https://github.com/linuxacademy/cicd-pipeline-
train-schedule-dockerdeploy/blob/example-solution/Jenkinsfile
4.5 Jenkins Pipelines CD and a Dockerized App
11. Page 11 of 24
5.1 Creating a Kubernetes Cluster
12. Page 12 of 24
You can find instructions on various ways of installing Kubernetes
here: https://kubernetes.io/docs/setup/
Installation instructions specific to kubeadm can he found
here: https://kubernetes.io/docs/setup/independent/create-cluster-kubeadm/
You can initialize the master node in kubeadm by creating a config file called kube-
config.yml with these contents:
apiVersion: kubeadm.k8s.io/v1alpha3
kind: ClusterConfiguration
networking:
podSubnet: 10.244.0.0/16
apiServerExtraArgs:
service-node-port-range: 8000-31274
Then run this command referencing that file:
kubeadm init --config kube-config.yml
Use this command to set up a pod network after initializing the master with kubeadm
init:
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/bc79dd1505b0c8681ec
e4de4c0d86c5cd2643275/Documentation/kube-flannel.yml
13. Page 13 of 24
5.2 Kubernetes Basics
You can find more detailed documentation on some of the concepts discussed
here: https://kubernetes.io/docs/concepts/#kubernetes-objects
14. Page 14 of 24
5.3 Deploying to Kubernetes with Jenkins
This demonstration uses the Kubernetes Continuous Deploy plugin to deploy to
Kubernetes from Jenkins. You can find additional documentation about that plugin
here: https://jenkins.io/doc/pipeline/steps/kubernetes-cd.
If you want to follow along, you can find the sample source code
here: https://github.com/linuxacademy/cicd-pipeline-train-schedule-kubernetes.
6. Monitoring
For more information about these monitoring tools, check out their official sites:
Prometheus - https://prometheus.io/
Grafana - https://grafana.com/
The first step toward using Prometheus and Grafana to gather metrics within
Kubernetes is to install them. walks you through the process of installing Prometheus
and Grafana in your Kubernetes cluster. After completing this lesson, you will know how
to quickly install Prometheus and Grafana using Helm.
Since there are quite a few commands involved in this installation, here is a reference
guide for the commands used to perform the installation in this lesson:
15. Page 15 of 24
curl https://raw.githubusercontent.com/kubernetes/helm/master/scripts/get > /tmp/get_
helm.sh
chmod 700 /tmp/get_helm.sh
DESIRED_VERSION=v2.8.2 /tmp/get_helm.sh
helm init --wait
kubectl --namespace=kube-system create clusterrolebinding add-on-cluster-admin --clus
terrole=cluster-admin --serviceaccount=kube-system:default
helm ls
cd ~/
git clone https://github.com/kubernetes/charts
cd charts
git checkout efdcffe0b6973111ec6e5e83136ea74cdbe6527d
cd ../
vi prometheus-values.yml
prometheus-values.yml:
alertmanager:
persistentVolume:
enabled: false
server:
persistentVolume:
enabled: false
Then run:
helm install -f prometheus-values.yml charts/stable/prometheus --name prometheus --na
mespace prometheus
vi grafana-values.yml
grafana-values.yml:
adminPassword: password
Then run:
helm install -f grafana-values.yml charts/stable/grafana/ --name grafana --namespace
grafana
vi grafana-ext.yml
grafana-ext.yml:
16. Page 16 of 24
kind: Service
apiVersion: v1
metadata:
namespace: grafana
name: grafana-ext
spec:
type: NodePort
selector:
app: grafana
ports:
- protocol: TCP
port: 3000
nodePort: 8080
Then run:
kubectl apply -f grafana-ext.yml
You can check on the status of the prometheus and grafana pods with these
commands:
kubectl get pods -n prometheus
kubectl get pods -n grafana
When setting up your dastasource in grafana, use this url:
http://prometheus-server.prometheus.svc.cluster.local
6.1 Cluster Monitoring
17. Page 17 of 24
6.2 Application Monitoring
Here is the sample source code for the train schedule app that was used in the
demo: https://github.com/linuxacademy/cicd-pipeline-train-schedule-monitoring
And here is a direct link to the Kubernetes template yml file that was used to deploy the
app: https://github.com/linuxacademy/cicd-pipeline-train-schedule-
monitoring/blob/master/train-schedule-kube.yml
7. Kubernetes and Self-Healing
For more information, check out the Kubernetes documentation on pod lifecycle and
restart policies: https://kubernetes.io/docs/concepts/workloads/pods/pod-
lifecycle/#restart-policy.
18. Page 18 of 24
7.1 Creating Liveness Probes in Kubernetes
For more information on Kubernetes liveness probes, check out their
documentation: https://kubernetes.io/docs/tasks/configure-pod-container/configure-
liveness-readiness-probes/.
Here is the final Kubernetes template file used in the demo (train-schedule-kube.yml):
kind: Service
apiVersion: v1
metadata:
name: train-schedule-service
annotations:
prometheus.io/scrape: 'true'
spec:
type: NodePort
selector:
app: train-schedule
ports:
20. Page 20 of 24
periodSeconds: 10
8. Kubernetes and Autoscaling
8.1 Horizontal Pod Autoscalers in Kubernetes
full HPA documentation: https://kubernetes.io/docs/tasks/run-application/horizontal-pod-
autoscale/
Here is the GitHub repo for the source code used in the demo. Check the example-
solution branch for the demo's final state: https://github.com/linuxacademy/cicd-
pipeline-train-schedule-autoscaling
21. Page 21 of 24
Here are the commands used to set up the autoscaler in the demo:
cd ~/
git clone https://github.com/kubernetes-incubator/metrics-server.git
cd metrics-server/
kubectl create -f deploy/1.8+/
kubectl get --raw /apis/metrics.k8s.io/
cd ~/
git clone
cd cicd-pipeline-train-schedule-autoscaling/
vi train-schedule-kube.yml
kubectl apply -f train-schedule-kube.yml
You can find the changes made to train-schedule-kube.yml in the example-
solution branch of the GitHub repo. Once you have deployed the app and the
HPA, you can generate CPU load to test it by spinning up a busybox shell:
kubectl run -i --tty load-generator --image=busybox /bin/sh
Then run this in the busybox shell to create load:
while true; do wget -q -O- http://:8080/generate-cpu-load; done
9. What is Canary Testing?
23. Page 23 of 24
8.2 Implementing a Canary Test in Kubernetes
You can find the sample source code that was used in this demo
here: https://github.com/linuxacademy/cicd-pipeline-train-schedule-canary
Be sure to check out the example-solution branch to find the canary deployment
template in train-schedule-kube-canary.yml.
Here are the commands used to deploy the stable and canary pods in the demo:
cd ~/
git clone
cd cicd-pipeline-train-schedule-canary/
24. Page 24 of 24
kubectl apply -f train-schedule-kube.yml
vi train-schedule-kube-canary.yml
kubectl apply -f train-schedule-kube-canary.yml
8.3 Kubernetes Canary Testing with Jenkins Pipelines