22CBL54 Devops Lab Manual
22CBL54 Devops Lab Manual
Table of Contents
Sl. No. Contents Page No.
1. Introduction to DevOps 2
Introduction to DevOps
Operations. DevOps is a practice that allows a single team to manage the entire
application development life cycle, that is, development, testing, deployment,
operations.
DevOps Tools
1 - Continuous Development
2 - Continuous Testing
3 - Continuous Integration
4 - Continuous Deployment
● This is the stage where the code is deployed to the production servers
● Configuration management and Containerization tools involve in this stage
● Configuration management
○ It is the act of releasing deployments to servers, scheduling updates on all
servers and keeping the configurations consistent across all the servers.
○ Some popular tools that are used here are Puppet, Chef, SaltStack, and
Ansible.
● Containerization
○ Docker and Vagrant are the popular tools used for this purpose.
○ These tools help produce consistency across Development, Test, Staging and
Production environments. Besides this, they also help in scaling-up and
scaling-down of instances swiftly.
5 - Continuous Monitoring
● Vital information about the use of the software is recorded. This information is
processed to recognize the proper functionality of the application. The system errors
such as low memory, server not reachable, etc are resolved in this phase.
● The root cause of any issue is determined in this phase.
● This practice involves the participation of the Operations team who will monitor the
user activity for bugs or any improper behavior of the system.
● The popular tools used for this are Splunk, ELK Stack, Nagios, NewRelic and Sensu.
● These tools help you monitor the application’s performance and the servers closely
and also enable you to check the health of the system proactively.
● Any major issues if found are reported to the development team so that it can be
fixed in the continuous development phase. This leads to a faster resolution of the
problems.
Prerequisite
● Install GIT on your local machine: https://git-scm.com/downloads
● Create/Login to an account on github.com: https://github.com/
Version control is a system that records changes to a file or set of files over time so that you
can recall specific versions later. A Version Control System (VCS) is a very wise thing to use. It
allows you to revert selected files back to a previous state, revert the entire project back to a
previous state, compare changes over time, see who last modified something that might be
causing a problem, who introduced an issue and when, and more. Using a VCS also generally
means that if you screw things up or lose files, you can easily recover. In addition, you get all
this for very little overhead.
Many people’s version-control method of choice is to copy files into another directory
(perhaps a time-stamped directory). This approach is very common because it is so simple,
but it is also incredibly error prone. It is easy to forget which directory you’re in and
accidentally write to the wrong file or copy over files you don’t mean to.
To deal with this issue, programmers long ago developed local VCSs that had a simple
database that kept all the changes to files under revision control.
One of the most popular VCS tools was a system called RCS, which is still distributed with
many computers today. RCS works by keeping patch sets (that is, the differences between
files) in a special format on disk; it can then re-create what any file looked like at any point in
Whenever you have the entire history of the project in a single place, you risk losing
everything. This is where Distributed Version Control Systems (DVCSs) step in.
In a DVCS (such as Git, Mercurial, Bazaar or Darcs), clients don’t just check out the latest
snapshot of the files; rather, they fully mirror the repository, including its full history. Thus, if
any server dies, and these systems were collaborating via that server, any of the client
repositories can be copied back up to the server to restore it. Every clone is really a full
backup of all the data.
Furthermore, many of these systems deal pretty well with having several remote repositories
they can work with, so you can collaborate with different groups of people in different ways
simultaneously within the same project. This allows you to set up several types of workflows
that aren’t possible in centralized systems, such as hierarchical models.
Git Architecture
git --version
Right click on the folder for which you want to create repository and then select Git Bash
Here command and start typing the following commands:
git init
Untrack Directory
rm –rf .git
To check remote
git remote –v
Make two text files (one to ignore other to track). Put some text in both
vi newfile.txt >> Press ‘Insert’ or ‘a’ >> insert some text >> Press ‘Esc’ >> ‘:wq’
vi ignore.txt >> Press ‘Insert’ or ‘a’ >> insert some text >> Press ‘Esc’ >> ‘:wq’
OR
Use notepad to create files “newfile.txt” and “ignore.txt”
Add file name to “.gitignore” file to ignore that particular file
git status
git diff
git log
git checkout -b newbranch commit-hash // Will create a new branch on the commit hash
specified
OR
git branch
Deleting a Branch
Undo the changes made (in staging area but not commited)
Edit the commit message (changes the hash – recommended if changes made are not pushed
to central repo)
git commit --amend -m “Updated Message” // Change only the commit message
git commit –-amend // to add files modified or newly added to previous commit
git reset --soft commit-hash //check the log to see changes (Hash should be one commit old)
git reflog
git log
git branch
git stash apply stash-code // Restore changes but keep stash (applicable between branches)
git stash pop // Restore changes most recent and remove stash (applicable between
branches)
Use Gitdiff tool (Many other diff tools available online for better UI)
● Docker
● Basic understanding of Git, Docker, Jenkins, and a programming language like Java
or Python.
● Set up a sample project repository on GitHub.
Tools Required
● Version Control: Git
1. Build Code (Version Control with Git): Clone the sample repository:
git clone <repository_url>
cd <project_directory>
● mvn test
What is Jenkins ?
Continuous Integration :
Renaming Project:
Requirements:
This setup offers many advantages, especially over local VCSs. For example, everyone knows
to a certain degree what everyone else on the project is doing. Administrators have fine-
grained control over who can do what, and it’s far easier to administer a CVCS than it is to
deal with local databases on every client.
However, this setup also has some serious downsides. The most obvious is the single point of
failure that the centralized server represents. If that server goes down for an hour, then
during that hour nobody can collaborate at all or save versioned changes to anything they’re
working on. If the hard disk the central database is on becomes corrupted, and proper
backups haven’t been kept, you lose absolutely everything — the entire history of the project
except whatever single snapshots people happen to have on their local machines. Local VCS
systems suffer from this same problem — whenever you have the entire history of the project
On Server side:
On Client Side:
cd helloworld
cd trunk
svn status
svn commit code.txt -m “added file code.txt to repository” // Commit changes to central
repository
Creating Branches:
// Go to “helloworld” folder
ls
cd informal // go to informal folder
nano code.txt
// add to the end of file
# Comment
svn commit code.txt -m “Added Comment” // Commit new changes to the branch (Go to
trunk and check new commit not added to trunk)
svn up
// Go to trunk folder
svn commit -m “Merged branch into Trunk” // Merge the Branch into the trunk
svn up
svn rm informal
// go to “Documents” folder
rm -rf helloworld
svn co svn://192.168.56.101/helloworld
cd helloworld/branches
ls
svn co -r 4 svn://192.168.56.101/helloworld
Requirements:
An Account with Docker Hub
Enable hypervisor for windows steps here
(Note: If you have Virtualbox running then you already have hypervisor ON)
Docker for Windows (Link in Resources): Installation steps here
Installation on Ubuntu:
sudo apt install docker.io
systemctl start docker
Resources:
Download Docker for windows: https://hub.docker.com/editions/community/docker-ce-
desktop-windows/
User manual for docker windows: https://docs.docker.com/docker-for-windows/
Docker tutorials: https://www.tutorialspoint.com/docker/index.htm
Introduction:
Docker is a container management service. The keywords of Docker are develop, ship and run
anywhere. The whole idea of Docker is for developers to easily develop applications, ship
them into containers which can then be deployed anywhere.
The initial release of Docker was in March 2013 and since then, it has become the buzzword
for modern world development, especially in the face of Agile-based projects.
Features of Docker:
● Docker has the ability to reduce the size of development by providing a smaller
Components of Docker:
1. Docker for Mac - It allows one to run Docker containers on the Mac OS.
2. Docker for Linux - It allows one to run Docker containers on the Linux OS.
3. Docker for Windows - It allows one to run Docker containers on the Windows OS.
4. Docker Engine - It is used for building Docker images and creating Docker containers.
5. Docker Hub - This is the registry which is used to host various Docker images.
6. Docker Compose - This is used to define applications using multiple Docker
containers.
Docker Images:
Docker Container:
A container is a standard unit of software that packages up code and all its dependencies so
the application runs quickly and reliably from one computing environment to another.
Container images become containers at runtime and in the case of Docker containers -
images become containers when they run on Docker Engine. Available for both Linux and
Windows-based applications, containerized software will always run the same, regardless of
the infrastructure. Containers isolate software from its environment and ensure that it works
uniformly despite differences for instance between development and staging.
Docker Hub:
Docker Hub is a registry service on the cloud that allows you to download Docker images that
are built by other communities. You can also upload your own Docker built images to Docker
hub.
sDocker Commands:
docker –version
docker help
docker run -it -d image-id // Create container from system (will pull if unavailable)
docker exec -it container-id bash // Run a command (application) in a running container (try
update and nano text editor)
mkdir python-app
cd python-app
nano app.py
print("Hello from python file")
.. save file ctrl + x -> Y
nano Dockerfile
// Contents of Dockerfile
FROM python
COPY . /src
CMD ["python", "/src/app.py"]
.. save file ctrl + x -> Y
Go to amazon EC2 -> create Ubuntu server instance (Free) -> Keep default options for
“Instance type, Instance Details, Add Storage, Add Tags” -> In “Configure security Groups”
Add Rule “Type = Custom TCP Rule”, “Port Range = 8080” and “Source as Custom ‘0.0.0.0/0,
::/0’” -> Create a new Key pair and name it “ubuntu-pass” -> Download the private key
“ubuntu-pass.pem”
sudo ssh -i ubuntu-pass.pem [email protected] // Log into remote server via SSH
OR for Windows
On Integration Server:
Install Git -> Install jdk8 (Prerequisite for jenkins) -> Install Jenkins -> Install Ansible -> Install
Docker
Create a Java Netbeans project (Web Application using Maven) on your Computer and Upload
it to your gitHub Repository (“allinone”)
Step 2: Create a New Jenkins Pipeline Project and Configure it to Poll SCM (GitHub):
In Jenkins:
1. Create “new Item” -> name it “AllInOneApp”
2. Select type as “Pipeline” -> Click “OK”
3. In Configurations give “Description”
4. Go to pipeline tab in script make following changes
pipeline{
agent any
stages{
stage('Polling SCM'){
steps{
// Code 1
}
}
}
}
For Code 1:
1. Click on ”Pipeline Syntax” link outside Script box
2. Click on “Snippet Generator”
3. In sample step select “git:Git”
4. Provide URL and Branch details
5. For credentials
a. Click on Add
// End Result
pipeline{
agent any
stages{
stage('Polling SCM'){
steps{
git branch: 'main', credentialsId: 'GitHub', url: 'https://github.com/__.git'
}
}
}
}
In Jenkins:
1. Go to “manage jenkins”
2. In “Global Tool Configuration” tab
3. Go to “Maven” section
4. Click “Maven Installation” -> “Add Maven”
5. Give name as “maven3”
6. Click on “Install Automatically”
7. Keep the rest as default
8. Click on ”Save”
// End Result
pipeline{
agent any
tools{
maven ‘maven3’
}
stages{
stage('Polling SCM'){
steps{
git branch: 'main', credentialsId: 'GitHub', url: 'https://github.com/__.git'
}
}
stage(‘Build using Maven’){
steps{
sh “mvn clean package”
}
}
}
}
FROM tomcat:8.0
COPY target/*.war /usr/local/tomcat/webapps/webapp.war
pipeline{
agent any
tools{
maven ‘maven3’
}
environment {
DOCKER_TAG = getVersion()
}
stages{
stage('Polling SCM'){
steps{
git branch: 'main', credentialsId: 'GitHub', url: 'https://github.com/___.git'
}
}
stage(‘Build using Maven’){
steps{
sh “mvn clean package”
}
}
stage(‘Build Docker Image’){
steps{
sh "docker build . -t ninadg89/devopsapp:${DOCKER_TAG}"
}
}
}
}
def getVersion(){
def commitHash = // Code 2
return commitHash
}
For Code 2:
1. Go to “Snippet Generator”
2. Select sample step as “sh:Shell Script”
3. In the text box type the command “git rev-parse --short HEAD”
4. Click on “Advanced”
5. Check the “Return Standard output” checkbox
6. Click “Generate Pipeline Script”
7. Copy the script generated in place of “Code 2”
// End Result
pipeline{
agent any
tools{
maven ‘maven3’
}
environment {
DOCKER_TAG = getVersion()
}
stages{
stage('Polling SCM'){
steps{
git branch: 'main', credentialsId: 'GitHub', url: 'https://github.com/_______.git '
}
}
stage(‘Build using Maven’){
steps{
def getVersion(){
def commitHash = sh returnStdout: true, script: 'git rev-parse --short HEAD'
return commitHash
}
stages{
stage('Polling SCM'){
steps{
git branch: 'main', credentialsId: 'GitHub', url:
'https://github.com/gaikwadninad89/allinone.git'
}
}
stage(‘Build using Maven’){
steps{
sh “mvn clean package”
}
}
stage(‘Build Docker Image’){
steps{
sh "docker build . -t ninadg89/devopsapp:${DOCKER_TAG}"
}
}
stage(‘Push image to DockerHub’){
steps{
withCredentials([string(credentialsId: 'docker-hub', variable: 'dockerHubPwd')]) {
sh "docker login -u ninadg89 -p ${dockerHubPwd}"
}
sh "docker push ninadg89/devopsapp:${DOCKER_TAG}"
}
}
}
}
def getVersion(){
def commitHash = sh returnStdout: true, script: 'git rev-parse --short HEAD'
return commitHash }
In Jenkins:
1. Go to “Manage Jenkins” -> “Global Tool Configuration”
2. In ansible section click “Add”
3. Set name as “ansible”
4. Set path to executable as “/usr/bin/”
5. Click “Save”
[dev]
54.146.13.210 ansible_user=ubuntu
- hosts: dev
become: yes
become_method: sudo
tasks:
- name: Update apt repository
apt: update_cache=yes
- name: Install python pip
apt:
name: python3-pip
state: present
- name: Install Docker
apt:
name: docker.io
state: present
- name: Start Docker
service:
name: docker
state: started
- "8080:8080"
pipeline{
agent any
tools{
maven ‘maven3’
}
environment {
DOCKER_TAG = getVersion()
}
stages{
stage('Polling SCM'){
steps{
git branch: 'main', credentialsId: 'GitHub', url: 'https://github.com_____”
}
}
stage(‘Build using Maven’){
steps{
sh “mvn clean package”
}
}
stage(‘Build Docker Image’){
steps{
sh "docker build . -t napp/devopsapp:${DOCKER_TAG}"
}
}
stage(‘Push image to DockerHub’){
steps{
withCredentials([string(credentialsId: 'docker-hub', variable: 'dockerHubPwd')]) {
sh "docker login -u napp -p ${dockerHubPwd}"
}
sh "docker push ninadg89/devopsapp:${DOCKER_TAG}"
}
}
stage(‘Deploy webapp using ansible’){
steps{
// Code 3
}
}
}
}
def getVersion(){
def commitHash = sh returnStdout: true, script: 'git rev-parse --short HEAD'
return commitHash }
For Code 3:
1. Go to “Snippet Generator”
2. Select sample step as “ansible Playbook:”
3. Write Playbook file path in workspace as “deploy-docker.yml”
4. Write Inventory file path in workspace as “inventory”
5. For Credentials:
6. Click “Add” -> “Jenkins”
a. Select kind as “SSH username and private key”
b. Give id as “dev-server”
c. Description as “dev-server”
d. Username as “ubuntu”
e. In Private key select radio button “Enter directly”
f. Paste the RSA key present in “ubuntu-pass.pem” file
g. Click on “Add”
7. Select the SSH Credentials created
8. Select “Disable the host SSH key check” checkbox
9. Add extra parameters as “-e DOCKER_TAG=${DOCKER_TAG}”
// End Result
pipeline{
agent any
tools{
maven ‘maven3’
}
environment {
DOCKER_TAG = getVersion()
}
stages{
stage('Polling SCM'){
steps{
git branch: 'main', credentialsId: 'GitHub', url: 'https://github.com/_____'
}
}
stage(‘Build using Maven’){
steps{
sh “mvn clean package”
}
}
stage(‘Build Docker Image’){
steps{
sh "docker build . -t napp/devopsapp:${DOCKER_TAG}"
}
}
stage(‘Push image to DockerHub’){
steps{
pipeline{
agent any tools{
maven 'maven3'
}
environment {
DOCKER_TAG = getVersion()
}
stages{
stage('Polling SCM'){
steps{
git branch: 'main', credentialsId: 'GitHub', url: 'https://github.com/___'
}
}
stage('Build using Maven'){
steps{
sh "mvn clean package"
}
}
stage('Build Docker Image'){
steps{
sh "docker build . -t napp/devopsapp:${DOCKER_TAG}"
} }
stage('Push image to DockerHub'){
steps{
withCredentials([string(credentialsId: 'docker-hub', variable: 'dockerHubPwd')]) {
sh "docker login -u napp -p ${dockerHubPwd}"
}
sh "docker push napp/devopsapp:${DOCKER_TAG}"
}
}
stage('Deploy webapp usinng ansible'){
steps{
ansiblePlaybook credentialsId: 'dev-server', disableHostKeyChecking: true, extras: '-e
DOCKER_TAG=${DOCKER_TAG}', installation: 'ansible', inventory: 'inventory', playbook:
'deploy-docker.yml'
} } }}
def getVersion(){
def commitHash = sh returnStdout: true, script: 'git rev-parse --short HEAD'
return commitHash }