Wait a lab manual?
As it stands, there is an upcoming exam on devops for some very good friends of mine. And with the lack of material provided by college combined with incompetent faculties, this subject quickly became a nightmare for them.
Fear not, since I was summoned to help out this crisis. Time to put some respect on the name of devops and in the process, try and make the life of some of my friends easier.
Note : This handout will contain solutions and explanation for all 6 of the following questions:
This guide is written not only to help the people who are appearing for Course Code 22CBL54 , but for anyone who wants to get into devops in general.
Introduction
I am Akash Singh, a third year engineering student and Open Source Contributor from Bangalore.
Here is my LinkedIn, GitHub and Twitter
I go by the name SkySingh04 online.
What is DevOps Again?
DevOps is the buzzword you can't escape, and for good reason. It's a blend of Development and Operations—a methodology aimed at **streamlining the software development lifecycle (SDLC) by improving collaboration between developers and IT operations teams.
Think of DevOps as a recipe where:
- Dev = Building the dish (coding)
- Ops = Serving it piping hot to hungry users (deployment and maintenance)
Why Should You Care?
- Speed: Faster delivery of features.
- Collaboration: No more blaming each other—“It works on my machine” doesn’t fly here.
- Reliability: Fewer bugs in production (touch wood).
- Scalability: Handle user traffic like a boss.
- Security: Automate boring-but-important security checks.
Hope that makes sense? Basically if the app crashes on production, first they will come eat my head and ask me what is wrong. As a devops engineer, since I have setup the pipeline for the deployment, I should be able to look at the monitoring tools or the logs or the infra and tell them okay this is the guy who fucked up (9/10 times its the developer, 1/10 times I done the goof while setting up the infra or pipeline hehe)
Okay I hope it makes sense now, basically we devops engineers are a cross breed and think we are the main characters.
Okay now the premise is set, lets get to passing this lab internals ay?
Question 1: GIT Operations
Problem Statement
To perform Git operations like creating a repository and fetching, pulling, and branching.
Solution
Step 1: Install Git
Use your terminal like a pro:
sudo apt update
sudo apt install git
Step 2: Create a Repository
-
Navigate to your project folder:
cd ~/your_project
-
Initialize Git:
git init
-
Add and commit files:
git add . git commit -m "Initial commit"
Step 3: Fetch and Pull
-
Fetch brings changes from the remote repository but doesn't merge them:
git fetch origin
-
Pull fetches and merges changes:
git pull origin main
Step 4: Branching
-
Create a new branch:
git branch feature-branch
-
Switch to it:
git checkout feature-branch
-
Merge back to main:
git checkout main git merge feature-branch
In general, pretty self explanatory program. I hope the person reading this has once in their life used git . Is that too much to ask from 3rd year students? Wdym get a life Sky?
Question 2: The DevOps Lifecycle
The DevOps lifecycle involves a series of stages—Code, Build, Test, Configure, and Monitor—designed to streamline the software development process.
- Code: Writing and managing code collaboratively.
- Build: Compiling source code into executable artifacts.
- Test: Automating testing to ensure code quality.
- Configure: Setting up the environment for deployment.
- Monitor: Tracking application health in production.
Alright, we will build a very small web app that displays "Hello, Devops!" and then implement the above 5 steps in it.
This is kind of a very vague question and a million ways to implement and answer this. I will try to give the simplest solution that should ideally fetch the full marks or I delete my Linkedin.
Step 1: Code
-
Create the Flask Application
File: app.py
from flask import Flask app = Flask(__name__) @app.route('/') def home(): return "Hello, DevOps!" if __name__ == "__main__": app.run(host='0.0.0.0', port=5000)
-
Set Up a Virtual Environment
python3 -m venv venv source venv/bin/activate # Linux/Mac venv\Scripts\activate # Windows pip install flask pip freeze > requirements.txt
Run the Application
python app.py
Open in your browser: http://localhost:5000.
Step 2: Build
If you know, Python is an interpreted language. There is no build step so to speak, since the code gets executed line by line. So here, build refers to the preparation of the environment and making sure the app is ready to run.
-
We will write a simple shell script (build.sh) to install dependencies and ensure the app runs:
#!/bin/bash echo "Setting up environment..." python3 -m venv venv source venv/bin/activate pip install -r requirements.txt echo "Environment setup complete. Run the app with: python app.py"
-
Run the script:
bash build.sh
Step 3: Test
Now since the program demands we write some test, we will write a very simple test that checks if the app is running or not. Btw shameless plugin but if you want to write tests with 0 code, checkout my Keploy Vscode Extension xD
-
**Test File (test_app.py):
import unittest from app import app class TestApp(unittest.TestCase): def test_home(self): tester = app.test_client(self) response = tester.get('/') self.assertEqual(response.status_code, 200) self.assertEqual(response.data, b"Hello, DevOps!") if __name__ == "__main__": unittest.main()
-
Run the tests:
python -m unittest test_app.py
Step 4: Configure
Configuration here focuses on making sure the app can run in different environments. One simple approach is to use environment variables.
-
Create a .env file:
FLASK_PORT=5000
-
Modify app.py to read the environment variable:
import os from flask import Flask app = Flask(__name__) @app.route('/') def home(): return "Hello, DevOps!" if __name__ == "__main__": port = int(os.getenv("FLASK_PORT", 5000)) app.run(port=port)
-
Use python-dotenv to automatically load .env files:
pip install python-dotenv
-
Add to app.py:
from dotenv import load_dotenv load_dotenv()
Step 5: Monitor
Now monitoring is very very complicated when you bring in fancy tools like Prometheus , Grafana, Loki etc. Implementing all of that is beyond the scope of this beginner's guide . Monitoring in general means just seeing what the app is doing while it is running , like logs, health , any errors etc. We will integrate some basic monitoring without external tools:
-
Add a /health endpoint to check if the app is running:
@app.route('/health') def health(): return {"status": "up"}, 200
-
Log requests to a file:
import logging logging.basicConfig(filename='app.log', level=logging.INFO) @app.before_request def log_request(): logging.info(f"Request: {request.method} {request.path}")
Now when the faculty asks where monitoring, show him the localhost:5000/health path and the app.log file. Let him be happy because again, things can get very complicated here if we want them to be fancy.
So what exactly did we do?
- Code: Python Flask app built with basic coding practices.
- Build: Prepared the environment with a venv and a build script.
- Test: Used Python’s unittest to verify the app’s behavior.
- Configure: Used .env and python-dotenv for managing environment variables.
- Monitor: Added a /health endpoint and request logging.
And that’s how we do DevOps the easy way! 🎉
Question 3: Continuous Integration with Jenkins
Now what's this new circus Jenkins? Remember when i said that as a devops guy, I have to setup the pipeline to ensure the app / code gets deployed? Yes so that pipeline is usually built using github actions and in fancier companies, they use this tool called Jenkins
Steps to Install Jenkins :
This should already be done in your lab system, if not tell your faculty that hey jenkins not installed or look up the installation instructions online , don't memorize this. Only the command to start jenkins.
Remember you need an existing app to deploy it using jenkins, for simplicity I am using the same python app we built in Q2. It needs to be pushed to github as you'll need to connect the pipeline to your repo.
-
Install Java (required for Jenkins):
sudo apt update sudo apt install openjdk-11-jdk
-
Add the Jenkins Repository:
curl -fsSL https://pkg.jenkins.io/debian/jenkins.io.key | sudo tee \ /usr/share/keyrings/jenkins-keyring.asc > /dev/null echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \ https://pkg.jenkins.io/debian binary/ | sudo tee /etc/apt/sources.list.d/jenkins.list > /dev/null
-
Install Jenkins:
sudo apt update sudo apt install jenkins
-
Start Jenkins and Check Status:
sudo systemctl start jenkins sudo systemctl status jenkins
Configure Jenkins
-
Access Jenkins:
Open a browser and go to http://:8080 or http://localhost:8080.
-
Unlock Jenkins:
Use the admin password found in: (cant memorise , the password is unique always, you'll find it here)
cat /var/lib/jenkins/secrets/initialAdminPassword
-
Install Recommended Plugins:
Follow the on-screen setup to install necessary plugins (e.g., Git, NodeJS, Pipeline).
Jenkins Pipeline for a Web Application
- Create a New Job:
- Go to Jenkins Dashboard and click *"New Item"*.
- Choose *"Pipeline"* and give it a name.
- Configure Git Repository:
- In the Pipeline configuration, specify the repository URL:
- *Source Code Management* > Select *Git* > Enter your repository URL.
- Add credentials if needed.
- Define the Pipeline Script:
pipeline {
agent any
environment {
VIRTUAL_ENV = 'venv' // Virtual environment directory
FLASK_PORT = '5000' // Port to run the Flask app
}
stages {
stage('Clone Repository') {
steps {
echo 'Cloning the repository...'
git branch: 'main', url: 'https://github.com/ahanasrinath/myflaskapp.git'
}
}
stage('Set Up Environment') {
steps {
echo 'Setting up virtual environment and installing dependencies...'
sh '''
python3 -m venv $VIRTUAL_ENV
source $VIRTUAL_ENV/bin/activate
pip install -r requirements.txt
'''
}
}
stage('Run Tests') {
steps {
echo 'Running unit tests...'
sh '''
source $VIRTUAL_ENV/bin/activate
python -m unittest discover -s . -p "test_*.py"
'''
}
}
stage('Build and Package') {
steps {
echo 'Packaging the application...'
sh '''
tar -czf flask-app.tar.gz app.py requirements.txt .env
'''
}
}
stage('Deploy') {
steps {
echo 'Deploying the application...'
sh '''
# Copy the application to the deployment server
scp flask-app.tar.gz user@your-server:/path/to/deployment
# SSH into the server and deploy
ssh user@your-server << EOF
cd /path/to/deployment
tar -xzf flask-app.tar.gz
source $VIRTUAL_ENV/bin/activate
nohup python app.py > app.log 2>&1 &
EOF
'''
}
}
}
post {
success {
echo 'Pipeline completed successfully!'
}
failure {
echo 'Pipeline failed. Check the logs for more details.'
}
}
}
NOW HERE IS THE THING.....Since we dont have a server to begin with, there is no place for jenkins to deploy our app so to speak. Which again makes me wonder why the fuck have they told y'all to deploy something without any context whatsoever as to where it should be deployed?
Will the college give yall a dedicated server to deploy to? Very unlikely. Do they even know that they need to do so? Also unlikely. So take the last deploy stage with a grain of salt.
Hope this is not y'all in exam hall, its me everyday.
NOTE:
If they give you a random app to deploy with jenkins, *DO NOT PANICK. You'll realise whatever steps we have written in the jenkins file are the actual commands we executed in Q2. So, they can be replaced with any specific set of instructions for any specific app/ tech stack.*
For Ex:
If they give you a react app, the setup environment would be replaced with just npm install and the build will be replaced with npm run build. Similarly for java app , it might be something different like javac app.java or cpp main.c for a c file.
Point is not to panick, they dont know what they are teaching or the questions they are giving. As long as you write the steps more or less like the above, they will know that you have cooked something.
Question 4: Version Control Tools
Git is the king here, but alternatives like CVS, RCS, and Mercurial deserve mention. Each has its pros and cons:
- RCS: For single-file projects.
- CVS: Old-school, great for teams.
- Mercurial: Git’s less famous sibling, preferred by projects like Mozilla.
Chalo , lets first install these (hopefully they are already installed in the lab computers y'all will be using). Use the brew commands for Mac and apt for linux.
- RCS
sudo apt install rcs
brew install rcs
- CVS
sudo apt install cvs
brew install cvs
- Git
sudo apt install git
brew install git
- Mercurial
sudo apt install mercurial
brew install mercurial
And here are the basic basic commands for all of them:
1. Revision Control System (RCS)
# Initialize RCS directory
mkdir RCS
# Check-in a file to create its first revision
ci -u filename
# Check-out a file for editing
co -l filename
# View version history
rlog filename
2. Concurrent Versions System (CVS)
# Initialize a repository
cvs -d /path/to/repo init
# Add a project to CVS
cvs import -m "Initial import" project_name vendor_tag release_tag
# Checkout a project
cvs checkout project_name
# Commit changes
cvs commit -m "Commit message"
3. Git
# Initialize a repository
git init
# Add files to the repository
git add .
# Commit changes
git commit -m "Initial commit"
# View history
git log
# Push changes to a remote repository
git remote add origin <repository-url>
git push -u origin main
4. Mercurial
# Initialize a repository
hg init
# Add files to the repository
hg add
# Commit changes
hg commit -m "Initial commit"
# View history
hg log
# Push changes to a remote repository
hg push <repository-url>
Question 5: Docker Basics
Okay little context is required as to what this Docker is. Docker is the jaan of every Devops engineer. Without Docker, there is only chaos and suffering and pain.
What is Docker?
Docker is an open-source platform that allows you to develop, ship, and run applications inside containers. Think of a container as a lightweight, portable, and consistent environment that contains everything your application needs to run—code, libraries, dependencies, and even the runtime.
Why Use Docker?
Basically to solve the problem of "ye code mere system pe chal raha h, but not on your system". A docker container simulates the entire environment and all the dependencies, so wherever you run the container, it will run reliably and without any circus. Now you dont have to fight that rich friend who has a mac or that nerd who lives only on linux. Kahi bhi code likho, chalega ekdam aram se using Docker.
Core Concepts in Docker
- Docker Image:
- A blueprint for a container.
- Contains the app code, libraries, and dependencies.
- Example: The image we will build using the Dockerfile.
- Docker Container:
- A running instance of a Docker image.
- Isolated, lightweight, and portable.
-
Dockerfile:
- A text file containing the instructions to build a Docker image.
-
Example:
FROM python:3.8-slim WORKDIR /app COPY . . CMD ["python", "app.py"]
Docker Hub:
- A cloud-based registry for sharing Docker images.
- Think of it as "GitHub for containers."
Docker is widely used in DevOps because it:
- Simplifies app deployment.
- Works seamlessly with CI/CD pipelines.
- Allows for microservices architecture, where each service runs in its own container.
Highly recommend watching this video by Fireship on Docker and this one by Coderized
IN VERY SIMPLE TERMS : Imagine you're packing for a trip:
- The Docker Image is the suitcase, pre-packed with all the essentials (toothbrush, clothes, etc.).
- The Docker Container is you actually carrying the suitcase on your trip.
- Docker Hub is the store where you buy pre-packed suitcases (images).
Coming back to our lab program, how do we dockerise our python app? First download Docker Desktop from here
Now they have mentioned that you should run different container OS, which is brain dead easy:
1. Pull Operating System Images
Docker Hub provides a wide variety of operating system images.
-
Search for an OS image:
docker search <os-name>
Example for Ubuntu:
docker search ubuntu
-
Pull an OS image:
docker pull <os-name>:<tag>
Example for Ubuntu:
docker pull ubuntu:latest
2. Run Containers from the OS Image
-
Run an interactive container:
docker run -it <os-name>:<tag> /bin/bash
Example:
docker run -it ubuntu:latest /bin/bash
This will give you a shell prompt inside the container.
-
List running containers:
docker ps
-
List all containers (including stopped ones):
docker ps -a
-
Stop a running container:
docker stop <container-id>
-
Remove a container:
docker rm <container-id>
3. Building a Custom Application Container
Now we will dockerise your flask app! The above step 2 is not necessary for this step.
-
Create a Dockerfile in the root dir
FROM python:3.8-slim WORKDIR /app COPY requirements.txt requirements.txt RUN pip install -r requirements.txt COPY . . CMD ["python", "app.py"]
-
Build the Docker Image
docker build -t flask-devops-app .
-
Run the Container
docker run -d -p 5000:5000 flask-devops-app
Access the app at http://localhost:5000.
I will still write what each step does, feel free to skip if you can just memorise it:
FROM python:3.8-slim
- Purpose:
This specifies the base image for your container.
- python:3.8-slim is a lightweight version of the Python 3.8 image. It contains only the essential components required to run Python, making it smaller and faster to download.
WORKDIR /app
- Purpose:
This sets the working directory inside the container to /app.
- Any subsequent commands like COPY or RUN will execute relative to this directory.
- It ensures your application code and dependencies stay organized in a specific directory within the container.
COPY requirements.txt requirements.txt
- Purpose:
This copies the requirements.txt file from your local system (host) to the /app directory inside the container.
- The requirements.txt file contains all the Python libraries your app depends on.
RUN pip install -r requirements.txt
-
Purpose:
This installs all the Python dependencies specified in the requirements.txt file using pip.-
For example, if your app uses Flask, the requirements.txt might contain:
flask==2.1.2
-
COPY . .
- Purpose:
This copies all files from your local working directory (host) to the /app directory inside the container.
- It ensures your app.py and any other necessary files (e.g., templates, static assets) are available in the container.
CMD ["python", "app.py"]
- Purpose:
This specifies the command the container should run when it starts.
- Here, it tells Docker to execute python app.py, which starts your Flask application.
- The CMD command runs the app in the foreground to keep the container alive while your app is running.
THATS IT! YOUR DOCKER APP IS NOW ALIVE!!!
4. Manage Docker Containers and Images
Some extra commands because they maybe useful yk
-
View all images:
docker images
-
Remove an image:
docker rmi <image-id>
-
Stop and remove all containers:
docker stop $(docker ps -aq) docker rm $(docker ps -aq)
-
Prune unused data (stopped containers, networks, etc.):
docker system prune -a
Question 6: Deploying Apps with Docker
Congrats! If you have reached this far I am v. v. proud of you! Here is your reward: You dont have to study Question 6. Yes, question 5 and 6 are literally same to same steps! You can deploy the same flask app using the same step 3 here as well. For safety, here is the java implementation as well, you will need a simple hello world written in java btw, here it is:
Create Main.java in your project directory
public class Main {
public static void main(String[] args) {
System.out.println("Hello, Dockerized Java App!");
}
}
Then add the Dockerfile in the same folder :
Dockerfile for a Java App
FROM openjdk:11
COPY . /app
WORKDIR /app
RUN javac Main.java
CMD ["java", "Main"]
Explanation for the steps here are the same as the Dockerfile for our Flask app, only that instead of python commands we use java commands:
FROM openjdk:11
- Purpose:
This specifies the base image for your container.
- openjdk:11 is a lightweight image containing the Java Development Kit (JDK) for Java 11.
- It provides everything needed to compile and run Java applications.
- Using a specific version (11) ensures consistency and avoids issues from version mismatches.
COPY . /app
- Purpose:
This copies all files from your current working directory on the host machine to the /app directory inside the container.
- For example, this will include your Java source files (Main.java) and any other files needed to run your application.
WORKDIR /app
- Purpose:
This sets the working directory inside the container to /app.
- All subsequent commands (like RUN or CMD) will execute relative to this directory.
- It ensures your application files are in the right location before compilation or execution.
RUN javac Main.java
- Purpose:
This compiles your Main.java file using the javac (Java Compiler) command inside the container.
- The javac command generates the bytecode .class file (e.g., Main.class), which is necessary to run the Java application.
- If your application has multiple Java files or dependencies, make sure they are all copied and accessible for compilation.
CMD ["java", "Main"]
- Purpose:
This specifies the command the container should run when it starts.
- java Main executes your compiled Java program (Main.class).
- CMD runs the application in the foreground, ensuring the container remains active while the application runs.
After building and running the container, your Java application will execute and output results to the terminal (or wherever your app is programmed to send them).
Build and Run
-
Build the image:
docker build -t my-java-app .
-
Run the container:
docker run my-java-app
Output:
You should see:
Hello, World!!
Conclusion
I hope this blog helps y'all out in passing this lab exam. This was genuinely fun to write and solve , not sure what your faculty did the entire semester if he/she didn't share this material. While all of this is available online and probably written and explained better, I tried my best in explaining it in my terms and how I would solve all of them.
Then again, your lab systems will have internet. Without internet, you can't run any of the above programs anyways? So yes , its easy to google up the steps or look up this handout during the exam.
Feeling grattitude? I accept payments in the form of cadbury fuse , mango lassi and ocassionally *ganne ka juice. Yes, this is part of my work at Vance and as a devops engineer. Yes, this is a very very smol drop in the ocean of devops and things get complicated really quickly with monitoring and kubernetes and yamls etc. Yes I need to touch some grass and yes, all of you reading are *profoundly welcome <3
Thanks for reading :))
Top comments (14)
Looks like a super well documented manual! Looking forward to start implementing
Wish you all the best!
Senkyu akash-sama
You are welcome <3
For deployment, you could set up a virtual machine on your client and deploy to it.
Ofcourse that’s always an option! The point is, it didn’t come under the scope of this beginner guide!
I need a friend like you in my class haha !!
Well documented !
We cook!
Haha, all the best for your exam!
Thanks for this! It will help us a lot!
All the best!
So When should I attend the next class 😁
Thanks Bro for sharing this blog kuch kuch smajh aya ha 😉
Ab jo us ka laptop par chal raha tha mera laptop par bhi chalay ga 😂
Finally we achieve world peace!