0% found this document useful (0 votes)
61 views44 pages

22CBL54 Devops Lab Manual

The document outlines a DevOps laboratory course, covering essential topics such as GIT operations, continuous integration with Jenkins, version control systems, and containerization with Docker. It details the DevOps lifecycle phases, tools used at each stage, and provides practical exercises for implementing code, building, testing, and monitoring software applications. Additionally, it includes commands and procedures for managing GIT repositories and integrating various DevOps tools.

Uploaded by

anil-csbs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views44 pages

22CBL54 Devops Lab Manual

The document outlines a DevOps laboratory course, covering essential topics such as GIT operations, continuous integration with Jenkins, version control systems, and containerization with Docker. It details the DevOps lifecycle phases, tools used at each stage, and provides practical exercises for implementing code, building, testing, and monitoring software applications. Additionally, it includes commands and procedures for managing GIT repositories and integrating various DevOps tools.

Uploaded by

anil-csbs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 44

DEVOPS LABORATORY(22CB54)

Table of Contents
Sl. No. Contents Page No.

1. Introduction to DevOps 2

2. To perform the GIT operations: to create a repository and to


5
perform fetch, pull, and branching operations

3. To implement Code, Build, Test, Configure & Monitor the Software


13
Applications with DevOps.

4. Build & Test Applications with Continuous Integration: To Install and


15
Configure Jenkins to test, and deploy Java or Web Applications

5. Version Control: To Perform Version Control on websites/


Software’s using different Version control tools like 18
RCS/CVS/GIT/Mercurial

6. Virtualization & Containerization: To Install and Configure Docker for


creating Containers of different Operating System Image, deploy the 22
application in to Docker

7. To Build, deploy and manage web or Java application on Docker 25

8. Integration of all tools (GIT, Maven, Docker, Ansible and Jenkins) 26

DEPT. OF CSBS, DSCE BANGALORE 1


DEVOPS LABORATORY(22CB54)

Introduction to DevOps

● The term DevOps is a combination of two words namely Development and

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 is a software development approach through which superior quality

software can be developed quickly and with more reliability.

DevOps Life Cycle


Phases/Stages in DevOps Lifecycle:
1. Continuous Development (Plan, Code and Build)
2. Continuous Testing (Test)
3. Continuous Integration
4. Continuous Deployment (Deploy and Operate)
5. Continuous Monitoring (Monitor)
These DevOps stages are carried out on loop continuously till you achieve the desired
product quality.

Fig: Stages in DevOps Life cycle

DEPT. OF CSBS, DSCE BANGALORE 2


DEVOPS LABORATORY(22CB54)

DevOps Tools

Fig : Tools used at different levels.

1 - Continuous Development

● Involves ‘planning‘, ‘coding‘ and ‘building’ of the software


● No DevOps tools that are required for planning
● Code is maintained by using Version Control tools
● Maintaining the code is referred to as Source Code Management
● Most popular tools used for Source Code Management are Git, SVN, Mercurial, CVS,
and JIRA
● Tools like Ant, Maven, Gradle can be used in this phase for building / packaging the
code into an executable file that can be forwarded to any of the next phases

2 - Continuous Testing

● Stage where the developed software is continuously tested for bugs


● Automation testing tools like Selenium, TestNG, JUnit, etc are used
● Selenium does the automation testing, and the reports are generated by TestNG
● Entire testing phase can be automated with the help of a Continuous Integration tool
called Jenkins
● We can also schedule the execution of the test cases at predefined times

3 - Continuous Integration

● This stage is the heart of the entire DevOps life cycle


● Software developers commit changes to the source code on a daily or a weekly basis.
● Every commit is then built and this allows early detection of problems
● Jenkins is a very popular tool used in this phase. Other tools used for integration are

DEPT. OF CSBS, DSCE BANGALORE 3


DEVOPS LABORATORY(22CB54)

Bamboo and Hudson


● Whenever there is a change in the Git repository, Jenkins fetches the updated code
and it prepares a build of that code which is an executable file in the form of a war or
a jar. This build is then forwarded to the test server or the production server.

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.

DEPT. OF CSBS, DSCE BANGALORE 4


DEVOPS LABORATORY(22CB54)

1. To perform the GIT operations: to create a repository and to perform


fetch, pull, and branching operations

Prerequisite
● Install GIT on your local machine: https://git-scm.com/downloads
● Create/Login to an account on github.com: https://github.com/

Git - Distributed Version Control System. [https://git-scm.com/doc]


Github - GitHub is a code hosting platform (website) for collaboration and version control and
lets you (and others) work together on projects. [https://help.github.com]
Online tool to learn git: https://learngitbranching.js.org/
Sources: https://git-scm.com/book/en/v2/Getting-Started-About-Version-Control

What is Version Control System?

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.

Types of Version control systems:

● Local Version Control Systems


● Centralized Version Control Systems
● Distributed Version Control Systems

Local Version Control System

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

DEPT. OF CSBS, DSCE BANGALORE 5


DEVOPS LABORATORY(22CB54)

time by adding up all the patches.

Distributed Version Control Systems

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.

DEPT. OF CSBS, DSCE BANGALORE 6


DEVOPS LABORATORY(22CB54)

Git Architecture

Git cheat sheet: https://github.github.com/training-kit/downloads/github-git-cheat-sheet.pdf

DEPT. OF CSBS, DSCE BANGALORE 7


DEVOPS LABORATORY(22CB54)

Delete Saved Credentials (Previous User)

Credential manager → Windows Credentials → Generic Credentials → git (remove)

Git Commands (type following command in the git bash)

To check git version and list common git commands

git --version

To configure git for all repositories

git config --global user.name "xyz"


git config --global user.email "[email protected]"

To configure git for single repository

git config user.name "xyz"


git config user.email "[email protected]"

Creating local repository (Project Originating on your PC)

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 link remote repository to local repository

git remote add origin “URL-of-Remote-Repository”


git pull origin master // ** If remote repository is non empty

DEPT. OF CSBS, DSCE BANGALORE 8


DEVOPS LABORATORY(22CB54)

To remove remote repository

git remote remove origin

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

Display status of files (Working Directory, Staging area, Repository)

git status

Add file to staging area

git add filename.txt // add specific file

git add –A OR git add . // add all files

Remove files from Staging Area

git reset filename.txt

git reset //Remove all files

***** Make changes in one file ******

View changes made after last commit / edit

git diff

Save changes to local repo

git commit –m “commit message”

Get Commit Details

git log

Pull remote files

git pull origin master

DEPT. OF CSBS, DSCE BANGALORE 9


DEVOPS LABORATORY(22CB54)

Push changes to the remote repo (always Pull first)

git push origin master

Restore previous version (Commit)

git checkout -b newbranch commit-hash // Will create a new branch on the commit hash
specified

OR

git checkout commit-hash // Commit you want to go to

git branch newbranch // Make a new branch on the commit

git checkout newbranch // Go to new branch

******* Work on Ongoing Project ******

Clone remote Repo

git clone “URL-of-Remote-Repository” // use current directory

git clone “URL-of-Remote-Repository” desktop/gitrepo1 // use specified directory

Clone remote Repo

git clone “URL-of-Remote-Repository” // use current directory

git clone “URL-of-Remote-Repository” desktop/gitrepo1 // use specified directory

View all branches

git branch

git branch –a // display all branches remote and local

Create new branch


git branch mergebranch
git branch rebasebranch

Go to New branch (to make changes)

git checkout mergebranch

***** add one file and commit ******

DEPT. OF CSBS, DSCE BANGALORE 10


DEVOPS LABORATORY(22CB54)

Push branch to remote repo

git push origin mergebranch


git push --all origin // Push all Branches to remote

git branch –a // Display all the branches

Merge Branches (into master - Use online tool to demonstrate)

git checkout master // Go to master branch

git pull origin master // Pull changes if any

git branch --merged // Display Merged branches if any

git merge mergebranch // Merge merge branch into master

Rebase Branches (into master - Use online tool to demonstrate)

git checkout master


***** add one file and commit ******
git rebase rebasebranch

Deleting a Branch

git branch --merged // Display Merged branches if any

git branch –d mergebranch // Delete Branch on local repo

git branch –a // Display Branches

git push origin -–delete mergebranch // Delete Branch on remote repo

Undo the changes made (in staging area but not commited)

git checkout filename.txt

Edit the commit message (changes the hash – recommended if changes made are not pushed
to central repo)

git commit --stat // See the Stats

git commit --amend -m “Updated Message” // Change only the commit message

** Make changes in a file and add one file **

git commit –-amend // to add files modified or newly added to previous commit

Move commit to a different branch (Use online tool to demonstrate)

git checkout newbranch // Go to that branch

DEPT. OF CSBS, DSCE BANGALORE 11


DEVOPS LABORATORY(22CB54)

git cherry-pick commit-hash

** delete commit from original branch **

git reset --soft commit-hash //check the log to see changes (Hash should be one commit old)

git reset commit-hash // mixed (moves changes to working directory)

git reset –-hard commit-hash // (Hash should be one commit old)

git clean –df // gets rid of untracked directories and files

Get back files before being collected by the garbage collector

git reflog

git checkout commit-hash

git log

git branch backup

git checkout master

git branch

git checkout backup

Revert commit (undo changes that others have pulled)

git revert commit-hash // save and exit the message

git diff commit-hash revert-hash

Stash Command (when not ready to commit but save changes)

git stash save “Stash-message”

git stash list

git stash apply stash-code // Restore changes but keep stash (applicable between branches)

git checkout -- . // Go back to state before apply

git stash pop // Restore changes most recent and remove stash (applicable between
branches)

DEPT. OF CSBS, DSCE BANGALORE 12


DEVOPS LABORATORY(22CB54)

git stash drop stash-code // Delete specific stash

git stash clear // Clear all stashes

Use Gitdiff tool (Many other diff tools available online for better UI)

git difftool master // Changes made in local repository


git difftool commit1-hash commit2-hash // View exact changes between the two commits
git difftool origin/master // Difference between local and remote repository

DEPT. OF CSBS, DSCE BANGALORE 13


DEVOPS LABORATORY(22CB54)

2. To implement Code, Build, Test, Configure & Monitor the Software


Applications with DevOps.

Pre-requisites: Install necessary tools:


● Git

● Jenkins (or set up GitHub Actions)

● 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

● Build Tool: Maven/Gradle

● CI/CD Tool: Jenkins/GitHub Actions

● Containerization Tool: Docker

1. Build Code (Version Control with Git): Clone the sample repository:
git clone <repository_url>
cd <project_directory>

2. Modify or create a simple code file (Assume HelloWorld.java)


public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, DevOps!");
}
}

3. Push changes to the repository:


git add .
git commit -m "Added HelloWorld program"
git push origin main

4. Build (Automate with Maven/Gradle)


● Create a pom.xml (for Maven) or build.gradle (for Gradle) file in the project directory.

● Run the build process: mvn clean install

● Verify the generated artifact (.jar or .war file).

5. Test (Unit Testing)

DEPT. OF CSBS, DSCE BANGALORE 14


DEVOPS LABORATORY(22CB54)

● Write a simple unit test (HelloWorldTest.java).

● Run tests using Maven/Gradle

● mvn test

6. Configure (CI/CD Pipeline with Jenkins or GitHub Actions)


Jenkins Setup:
● Create a new Jenkins job.

● Link it to the GitHub repository.


Add build steps:
● Build using Maven mvn clean install

● Execute unit tests: mvn test

7. GitHub Actions Setup:


Add a .github/workflows/ci.yml file:
name: CI Pipeline
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Java
uses: actions/setup-java@v3
with:
java-version: '11'
- name: Build with Maven
run: mvn clean install
- name: Run Tests
run: mvn test

8. Deploy and Monitor (Containerization and Monitoring)


Containerization with Docker: Create a Dockerfile:
FROM openjdk:11-jre-slim
COPY target/HelloWorld.jar /app/HelloWorld.jar
CMD ["java", "-jar", "/app/HelloWorld.jar"]
Build and run the Docker image:
docker build -t helloworld-app .
docker run -p 8080:8080 helloworld-app

DEPT. OF CSBS, DSCE BANGALORE 15


DEVOPS LABORATORY(22CB54)

9. Monitoring with Prometheus/Grafana:


● Expose application metrics.

● Configure Prometheus to scrape metrics.

3. Build & Test Applications with Continuous Integration: To Install and


Configure Jenkins to test, and deploy Java or Web Applications

Prerequisites: Java version 8 OR 11 installed


Jenkins Installation procedure:
https://www.youtube.com/watch?v=1y8RsUbxtAw&t=512s // For Windows
https://www.youtube.com/watch?v=YyPMhKdBCxw // For Ubuntu

What is Jenkins ?

Jenkins is a powerful application that allows continuous


integration and continuous delivery of projects, regardless of
the platform you are working on. It is a free source that can
handle any kind of build or continuous integration. You can
integrate Jenkins with a number of testing and deployment
technologies.

Continuous Integration :

Continuous Integration is a development practice that requires developers to integrate code


into a shared repository at regular intervals. This concept was meant to remove the problem
of finding later occurrence of issues in the build lifecycle. Continuous integration requires the
developers to have frequent builds. The common practice is that whenever a code commit
occurs, a build should be triggered.

Jenkins Dashboard: ( Default Location: localhost:8080 )

DEPT. OF CSBS, DSCE BANGALORE 16


DEVOPS LABORATORY(22CB54)

Creating a new job:

Job 1: JavaHomeBuild (Running at Periodic Intervals)

1. Select "New job" on the dashboard


2. Enter name as "JavaHomeBuild"
a. Select “Freestyle Project”
b. click "ok"
3. In "General" tab
a. Add "description" as “Build Project for Developer”
4. In "SCM" (source code management) tab
a. select "none" (default)
5. In "Triggers" tab
a. Select "Periodically"
b. Enter the value "* * * * *"
6. In "Build" tab
a. select "Execute Windows Batch Command" // “Execute Shell” for Ubuntu
users
b. Type "cd <path-of-java-file-saved>"
c. Type "javac HelloHome.java"
7. Click on "Save"

Chaining jobs together:

DEPT. OF CSBS, DSCE BANGALORE 17


DEVOPS LABORATORY(22CB54)

Job 2: HelloHomeRun (Job Configuration copied from other job)


1. Select "New job" on the dashboard
2. Enter name as "HelloHomeRun"
a. Select “Copy from”
b. In text box enter “HelloHomeBuild”
c. Click "ok"
3. In "General" tab
a. Edit "description" as “Run Project for Intern”
4. In "SCM" (source code management) tab
a. select "none" (default)
5. In "Triggers" tab
a. Select "After other projects"
b. In the text box "HelloHomeBuild"
6. In "Build" tab
a. select "Execute Windows Batch Command" // “Execute Shell” for Ubuntu
users
b. Type "cd <path-of-java-file-saved>"
c. Type "java HelloHome"
7. Click on "Save"

Job 3: TestHomeMsg (Simple job)


1. Select "New job" on the dashboard
2. Enter name as "TestHomeMsg"
a. Select “Freestyle Project”
b. Click "ok”
3. In "General" tab
a. Edit "description" as “Display Message for tester”
4. In "SCM" (source code management) tab
a. select "none" (default)
5. In "Triggers" tab
a. Select "After other projects"
b. In the text box "HelloHomeRun"
6. In "Build" tab
a. select "Execute Windows Batch Command" // “Execute Shell” for Ubuntu
users
b. Type "echo ----- Successful Execution -----"
7. Click on "Save"

Running Jenkins Project:

DEPT. OF CSBS, DSCE BANGALORE 18


DEVOPS LABORATORY(22CB54)

● Click on the arrow appearing in front of the project name on Dashboard


● Select “Build Now” (Execution will start automatically for jobs scheduled to build
periodically)

Viewing Console Output:

● Click on the project_name on Dashboard


● Click on the respective build number
● Click on “Console Output” in the left hand options pane

Renaming Project:

● Click on the arrow appearing in front of the project name on Dashboard


● Select “Rename”
● Enter the “New_name” in text box and click on Rename

DEPT. OF CSBS, DSCE BANGALORE 19


DEVOPS LABORATORY(22CB54)

4. Version Control: To Perform Version Control on websites/ Software’s


using different Version control tools like RCS/CVS/GIT/Mercurial

Requirements:

Two Lubuntu / Ubuntu systems (Server and Client)


Download Link for Lubuntu Desktop: https://lubuntu.net/downloads/

Centralized Version Control System

To collaborate with developers on other systems, Centralized Version Control Systems


(CVCSs) were developed. These systems (such as CVS, Subversion, and Perforce) have a single
server that contains all the versioned files, and a number of clients that check out files from
that central place. For many years, this has been the standard for version control.

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

DEPT. OF CSBS, DSCE BANGALORE 20


DEVOPS LABORATORY(22CB54)

in a single place, you risk losing everything.

On Server side:

sudo apt-get install subversion // Will install SVN on server

sudo mkdir -p /svn/repos // Make Directory to store repositories

sudo svnadmin create /svn/repos/helloworld // Make SVN repository

sudo apt-get install nano


sudo nano /svn/repos/helloworld/conf/svnserve.conf // Open configuration file to edit
● anon-access = none
● auth-access = write
● password-db = passwd

sudo nano svn/repos/helloworld/conf/passwd // Open password file to add new username


and password
● ninad = ninad // Username = Password

sudo svnserve -d -r /svn/repos // Start Server


sudo kill -9 TaskID // To stop server (TaskId available in Task Manager)

On Client Side:

sudo apt-get install subversion // Install SVN on client machine

mkdir helloworld // Making a folder to track

cd helloworld // go inside the local repository


mkdir trunk // Main branch
mkdir branches // Branches folder
mkdir tags // Name to stable releases
cd .. // Go outside helloworld repository

svn import helloworld/ svn://192.168.56.101/helloword // Import Local repository files into


a Server and add a commit message.

SVN will prompt for Username and Password

DEPT. OF CSBS, DSCE BANGALORE 21


DEVOPS LABORATORY(22CB54)

rm -rf helloworld // Delete untracked helloworld folder

svn co svn://192.168.56.101/helloworld // Checkout the server folder. Will create a local


folder

cd helloworld

ls -a //Should contain a .svn file

cd trunk

sudo nano code.txt //edit greetings.py to add following


This is some code
// save pressing “ctrl+x” → “y” enter → enter

svn status

svn add code.txt // add file to track

svn commit code.txt -m “added file code.txt to repository” // Commit changes to central
repository

To delete saved Credentials:

rm -r ~/.subversion/ // Remove .subversion file from home folder


nano code.txt // Edit code.txt file by adding following line
# This is a comment // Add a comment to file and save the file
// save pressing “ctrl+x” → “y” enter → enter

svn commit -m “Comment added” // Will ask for login information

Creating Branches:

// Go to “helloworld” folder

svn copy svn://192.168.56.101/helloworld/trunk


svn://192.168.56.101/helloworld/branches/informal //Copying trunk data to new branch
named “informal”

cd branches // go to branches folder


ls
svn up // Update working directory

DEPT. OF CSBS, DSCE BANGALORE 22


DEVOPS LABORATORY(22CB54)

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)

Merge Branches (Does not copy commit info):

// Go to branch folder “informal”

svn up

svn merge ^/trunk // Merge branch in trunk

// Go to trunk folder

svn merge --reintegrate ^/branches/informal // Reintegrate informal branch in trunk

svn commit -m “Merged branch into Trunk” // Merge the Branch into the trunk

svn up

// To delete branch “informal” → go to “branches” folder

svn rm informal

svn commit -m “deleted informal branch”

// go to “Documents” folder

rm -rf helloworld

svn co svn://192.168.56.101/helloworld

cd helloworld/branches

ls

DEPT. OF CSBS, DSCE BANGALORE 23


DEVOPS LABORATORY(22CB54)

// To Checkout to a previous revision

svn co -r 4 svn://192.168.56.101/helloworld

DEPT. OF CSBS, DSCE BANGALORE 24


DEVOPS LABORATORY(22CB54)

5. Virtualization & Containerization: To Install and Configure Docker for


creating Containers of different Operating System Image, deploy the
application in to Docker

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

DEPT. OF CSBS, DSCE BANGALORE 25


DEVOPS LABORATORY(22CB54)

footprint of the operating system via containers.


● With containers, it becomes easier for teams across different units, such as
development, QA and Operations to work seamlessly across applications.
● You can deploy Docker containers anywhere, on any physical and virtual machines
and even on the cloud.
● Since Docker containers are pretty lightweight, they are very easily scalable.

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:

In Docker, everything is based on Images. An image is a combination of a file system and


parameters. A Docker container image is a lightweight, standalone, executable package of
software that includes everything needed to run an application: code, runtime, system tools,
system libraries and settings.

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

DEPT. OF CSBS, DSCE BANGALORE 26


DEVOPS LABORATORY(22CB54)

docker help

docker images // List images with image-id

docker pull ubuntu // pull ubuntu image from DockerHub

docker ps // List active Containers with container-id


docker ps -a // List all Containers with container-id

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)

docker diff container-id // Inspect changes to files or directories on a container's filesystem


docker commit container-id new-image-name // Create a new image from a container's
changes preferably username/imagename

docker restart container-id // Restart one or more containers


docker rename container-id new-container-name // Rename a container

docker stop container-id // Stop one or more running containers


docker kill container-id // Kill one or more running containers (Abruptly Stop container)

docker rm container-id // Remove one or more containers


docker container prune // Remove all stopped containers
docker rmi image-id // Remove one or more images

docker login // Log in to a Docker registry


docker logout // Log out from a Docker registry

docker push image-name // Push an image or a repository to a registry

docker export container-id // Export a container's file system as a tar archive


docker import file-path-with-filename // Import the contents from a tarball to create a
filesystem image

DEPT. OF CSBS, DSCE BANGALORE 27


DEVOPS LABORATORY(22CB54)

6. To Build, deploy and manage web or application on Docker.

Creating python app image file

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

docker build . -t python_app


docker images
docker run python_app

DEPT. OF CSBS, DSCE BANGALORE 28


DEVOPS LABORATORY(22CB54)

Open End Experiment


Integration of all tools (GIT, Maven, Docker, Ansible and Jenkins)
For Remote Host [preferably Amazon ec2 instance]: Two - Integration server and Deployment
/ Production server

Ubuntu Server (free version) on amazon EC2:


https://aws.amazon.com/console/

Create Ubuntu server on Amazon EC2 [Video]:


https://www.youtube.com/watch?v=a8CBE_WN7rA

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”

Test the SSH connection with EC2 host:

sudo chmod 400 ubuntu-pass.pem // Gives read only permission

sudo ssh -i ubuntu-pass.pem [email protected] // Log into remote server via SSH

OR for Windows

ssh -i ubuntu-pass.pem [email protected]

On Integration Server:

Install Git -> Install jdk8 (Prerequisite for jenkins) -> Install Jenkins -> Install Ansible -> Install
Docker

Step 1: Create a Java Web Application in Netbeans

Create a Java Netbeans project (Web Application using Maven) on your Computer and Upload
it to your gitHub Repository (“allinone”)

DEPT. OF CSBS, DSCE BANGALORE 29


DEVOPS LABORATORY(22CB54)

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

DEPT. OF CSBS, DSCE BANGALORE 30


DEVOPS LABORATORY(22CB54)

b. Select kind as “Username and Password”


c. Provide Username and Password in the allotted Textbox
d. Give id as “GitHub”
e. Add Description as “GitHub”
f. Click on “Add”
6. Select created GitHub credentials from the list
7. Click on “Generate Pipeline Script”
8. Copy the generated script as “Code 1”

// End Result
pipeline{
agent any

stages{
stage('Polling SCM'){
steps{
git branch: 'main', credentialsId: 'GitHub', url: 'https://github.com/__.git'
}
}
}
}

Step 3: Build the code using Maven

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”

On Integration Server shell:


Go to “/var/lib/jenkins/tools/” and list the files and folders you should be able to see maven
installation folder

Change pipeline Script to the following:

DEPT. OF CSBS, DSCE BANGALORE 31


DEVOPS LABORATORY(22CB54)

// 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”
}
}
}
}

Step 4: Build a Tomcat8 container image with War file deployed in it

Create “Dockerfile” on Github repository with contents:

FROM tomcat:8.0
COPY target/*.war /usr/local/tomcat/webapps/webapp.war

In Shell of Integration Server:


1. Type “sudo usermod -a -G docker jenkins”
2. Type “sudo systemctl restart jenkins” OR “sudo service jenkins restart”

DEPT. OF CSBS, DSCE BANGALORE 32


DEVOPS LABORATORY(22CB54)

3. Type “chkconfig docker on”


4. Type “sudo service docker start”

Change the pipeline script as:

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}"
}
}
}
}

DEPT. OF CSBS, DSCE BANGALORE 33


DEVOPS LABORATORY(22CB54)

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{

DEPT. OF CSBS, DSCE BANGALORE 34


DEVOPS LABORATORY(22CB54)

sh “mvn clean package”


}
}
stage(‘Build Docker Image’){
steps{
sh "docker build . -t ninadg89/devopsapp:${DOCKER_TAG}"
}
}
}
}

def getVersion(){
def commitHash = sh returnStdout: true, script: 'git rev-parse --short HEAD'
return commitHash
}

Step 5: Push the built Docker image to DockerHub

For logging to DockerHub with credentials in Jenkins:


1. Go to “Snippet Generator”
2. Select sample step as “withcredentials”
3. In Bindings click on “Add” -> choose “secret text”
4. Name the variable as “dockerHubPwd”
5. In Credentials click on “Add”
a. Click on “Jenkins”
b. Select kind as “Secret Text”
c. In secret text box type password
d. Write id as “docker-hub”
e. Write description as “docker-hub”
f. Click on “Add”
6. In credentials select the docker-hub credential created
7. Click “Generate Pipeline Script”
8. Copy the script generated as below:

withCredentials([string(credentialsId: 'docker-hub', variable: 'dockerHubPwd')]) {


// some block

DEPT. OF CSBS, DSCE BANGALORE 35


DEVOPS LABORATORY(22CB54)

Replace “// some block” to produce:


// 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/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’){

DEPT. OF CSBS, DSCE BANGALORE 36


DEVOPS LABORATORY(22CB54)

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 }

Step 6: Deploy Docker Container on Production server

In Integration server shell


1. Type “ansible --version”
2. Copy executable location “/usr/bin/”

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”

Create “inventory” file on git repository with contents:

[dev]
54.146.13.210 ansible_user=ubuntu

Create ansible playbook “deploy-docker.yml” on git repository with contents:

- hosts: dev
become: yes

DEPT. OF CSBS, DSCE BANGALORE 37


DEVOPS LABORATORY(22CB54)

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

- name: Install docker-py python module


pip:
name: docker-py
state: present
- name: Start the Container
docker_container:
name: devopsapp
image: "napp/devopsapp:{{DOCKER_TAG}}"
state: started
restart: yes
ports:

DEPT. OF CSBS, DSCE BANGALORE 38


DEVOPS LABORATORY(22CB54)

- "8080:8080"

Add Stage ‘Deploy webapp using ansible':

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’){

DEPT. OF CSBS, DSCE BANGALORE 39


DEVOPS LABORATORY(22CB54)

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}”

DEPT. OF CSBS, DSCE BANGALORE 40


DEVOPS LABORATORY(22CB54)

10. Click “Generate Pipeline Script”


11. Copy generated script as Code 3

// 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{

DEPT. OF CSBS, DSCE BANGALORE 41


DEVOPS LABORATORY(22CB54)

withCredentials([string(credentialsId: 'docker-hub', variable: 'dockerHubPwd')]) {


sh "docker login -u ninadg89 -p ${dockerHubPwd}"
}
sh "docker push ninadg89/devopsapp:${DOCKER_TAG}"
}
}
stage(‘Deploy webapp using 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
}

Final pipeline Script:

pipeline{
agent any tools{
maven 'maven3'
}
environment {
DOCKER_TAG = getVersion()
}
stages{
stage('Polling SCM'){

DEPT. OF CSBS, DSCE BANGALORE 42


DEVOPS LABORATORY(22CB54)

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 }

DEPT. OF CSBS, DSCE BANGALORE 43


DEVOPS LABORATORY(22CB54)

Final Repository Files:

Build the Jenkins Project:

Output on Production / Operations server:

DEPT. OF CSBS, DSCE BANGALORE 44

You might also like