DEV Community

Cover image for A Lab Manual to Devops
Akash Singh
Akash Singh

Posted on

A Lab Manual to Devops

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:

Image description

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

Sky Singh

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?

  1. Speed: Faster delivery of features.
  2. Collaboration: No more blaming each other—“It works on my machine” doesn’t fly here.
  3. Reliability: Fewer bugs in production (touch wood).
  4. Scalability: Handle user traffic like a boss.
  5. 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
Enter fullscreen mode Exit fullscreen mode

Step 2: Create a Repository

  1. Navigate to your project folder:

    cd ~/your_project
    
  2. Initialize Git:

    git init
    
  3. 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

  1. 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)
    
  2. 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
    
  3. Run the Application
    python app.py

  4. 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.

  1. 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"
    
  2. 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

  1. **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()
    
  2. 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.

  1. Create a .env file:

    FLASK_PORT=5000
    
  2. 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)
    
  3. Use python-dotenv to automatically load .env files:

    pip install python-dotenv
    
  4. 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:

  1. Add a /health endpoint to check if the app is running:

    @app.route('/health')
    def health():
        return {"status": "up"}, 200
    
  2. 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.

  1. Install Java (required for Jenkins):

    sudo apt update
    sudo apt install openjdk-11-jdk
    
  2. 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
    
  3. Install Jenkins:

    sudo apt update
    sudo apt install jenkins
    
  4. Start Jenkins and Check Status:

    sudo systemctl start jenkins
    sudo systemctl status jenkins
    

Configure Jenkins

  1. Access Jenkins:

    Open a browser and go to http://:8080 or http://localhost:8080.

  2. 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
    
  3. Install Recommended Plugins:

    Follow the on-screen setup to install necessary plugins (e.g., Git, NodeJS, Pipeline).


Jenkins Pipeline for a Web Application

  1. Create a New Job:
-   Go to Jenkins Dashboard and click *"New Item"*.
-   Choose *"Pipeline"* and give it a name.
Enter fullscreen mode Exit fullscreen mode
  1. Configure Git Repository:
-   In the Pipeline configuration, specify the repository URL:
    -   *Source Code Management* > Select *Git* > Enter your repository URL.
-   Add credentials if needed.
Enter fullscreen mode Exit fullscreen mode
  1. 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.'
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

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.

GitHub - ahmednuaman/meme-says-jenkins: A meme based Jenkins CI monitor

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
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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

  1. Docker Image:
    • A blueprint for a container.
    • Contains the app code, libraries, and dependencies.
    • Example: The image we will build using the Dockerfile.
  2. Docker Container:
    • A running instance of a Docker image.
    • Isolated, lightweight, and portable.
  3. 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"]
      
  4. Docker Hub:

-   A cloud-based registry for sharing Docker images.
-   Think of it as "GitHub for containers."
Enter fullscreen mode Exit fullscreen mode

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.

  1. Search for an OS image:

    docker search <os-name>

    Example for Ubuntu:

    docker search ubuntu

  2. Pull an OS image:

    docker pull <os-name>:<tag>

    Example for Ubuntu:

    docker pull ubuntu:latest


2. Run Containers from the OS Image

  1. 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.

  2. List running containers:

    docker ps

  3. List all containers (including stopped ones):

    docker ps -a

  4. Stop a running container:

    docker stop <container-id>

  5. 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.

  1. 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"]
    
  2. Build the Docker Image

    docker build -t flask-devops-app .

  3. Run the Container

    docker run -d -p 5000:5000 flask-devops-app

  4. 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!!!

It's Alive Media

4. Manage Docker Containers and Images

Some extra commands because they maybe useful yk

  1. View all images:

    docker images

  2. Remove an image:

    docker rmi <image-id>

  3. Stop and remove all containers:

    docker stop $(docker ps -aq)
    docker rm $(docker ps -aq)
    
  4. 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!");
    }
}
Enter fullscreen mode Exit fullscreen mode

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"]
Enter fullscreen mode Exit fullscreen mode

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

  1. Build the image:

    docker build -t my-java-app .
    
  2. Run the container:

    docker run my-java-app
    
  3. 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)

Collapse
 
mandyy_1305 profile image
Manish Suresh

Looks like a super well documented manual! Looking forward to start implementing

Collapse
 
skysingh04 profile image
Akash Singh

Wish you all the best!

Collapse
 
areebahmeddd profile image
Areeb

Senkyu akash-sama

Collapse
 
skysingh04 profile image
Akash Singh

You are welcome <3

Collapse
 
sephyi profile image
Sephyi

For deployment, you could set up a virtual machine on your client and deploy to it.

Collapse
 
skysingh04 profile image
Akash Singh

Ofcourse that’s always an option! The point is, it didn’t come under the scope of this beginner guide!

Collapse
 
slashexx profile image
Dhruv

I need a friend like you in my class haha !!

Well documented !

Collapse
 
skysingh04 profile image
Akash Singh

We cook!

Collapse
 
priyanshukumarsinha profile image
Priyanshu Kumar Sinha

Image description

Collapse
 
skysingh04 profile image
Akash Singh

Haha, all the best for your exam!

Collapse
 
shreyashsri profile image
Shreyash Srivastava

Thanks for this! It will help us a lot!

Collapse
 
skysingh04 profile image
Akash Singh

All the best!

Collapse
 
ahmad-mamoon profile image
Muhammad Ahmad

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 😂

Collapse
 
skysingh04 profile image
Akash Singh

Finally we achieve world peace!