Post

DevOps Sample codes for Interview Preparation

These are simple, easy-to-memorize examples of common DevOps configuration files that you might be asked to write during an interview. Each example covers the basics while demonstrating good practices.

DevOps Sample codes for Interview Preparation

1. Dockerfile Example

A basic Dockerfile for a Python application:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
FROM ubuntu:20.04

# Set environment variables
ENV APP_HOME /app

# Create app directory
WORKDIR $APP_HOME

# Install dependencies
RUN apt-get update && apt-get install -y \
    python3 \
    python3-pip \
    && rm -rf /var/lib/apt/lists/*

# Copy application files
COPY . .

# Install Python dependencies
RUN pip3 install -r requirements.txt

# Expose port
EXPOSE 5000

# Command to run the application
CMD ["python3", "app.py"]

2. Docker Compose Example

A simple docker-compose.yml file for a web application with database:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
version: '3'

services:
  app:
    build: .
    ports:
      - "5000:5000"
    volumes:
      - ./:/app
    environment:
      - DB_HOST=db
      - DB_NAME=mydb
      - DB_USER=user
      - DB_PASSWORD=password
    depends_on:
      - db
  
  db:
    image: postgres:13
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_DB=mydb
      - POSTGRES_USER=user
      - POSTGRES_PASSWORD=password
    volumes:
      - postgres_data:/var/lib/postgresql/data

volumes:
  postgres_data:

3. Jenkinsfile Example

A basic Jenkins pipeline with build, test, and deploy stages:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
pipeline {
    agent any
    
    stages {
        stage('Build') {
            steps {
                echo 'Building the application...'
                sh 'docker build -t myapp:latest .'
            }
        }
        
        stage('Test') {
            steps {
                echo 'Running tests...'
                sh 'docker run myapp:latest python3 -m unittest'
            }
        }
        
        stage('Deploy') {
            steps {
                echo 'Deploying the application...'
                sh 'docker-compose up -d'
            }
        }
    }
    
    post {
        success {
            echo 'Pipeline completed successfully!'
        }
        failure {
            echo 'Pipeline failed!'
        }
    }
}

4. GitLab CI Configuration Example

A simple .gitlab-ci.yml file with build, test, and deploy stages:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
stages:
  - build
  - test
  - deploy

build:
  stage: build
  script:
    - echo "Building the application..."
    - docker build -t myapp:latest .
  tags:
    - docker

test:
  stage: test
  script:
    - echo "Running tests..."
    - docker run myapp:latest python3 -m unittest
  tags:
    - docker

deploy:
  stage: deploy
  script:
    - echo "Deploying to production..."
    - docker-compose up -d
  only:
    - main
  tags:
    - docker

5. Terraform Example

A basic Terraform configuration to list VPC and EC2 instances:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# Provider configuration
provider "aws" {
  region = "us-east-1"
}

# Data source to get all VPCs
data "aws_vpcs" "all_vpcs" {}

# Data source to get all EC2 instances
data "aws_instances" "all_instances" {}

# Output VPC IDs
output "vpc_ids" {
  value = data.aws_vpcs.all_vpcs.ids
}

# Output EC2 Instance IDs
output "instance_ids" {
  value = data.aws_instances.all_instances.ids
}

# Output EC2 Instance details
output "instance_details" {
  value = [
    for instance in data.aws_instances.all_instances.instances :
    {
      id        = instance.id
      type      = instance.instance_type
      state     = instance.state
      private_ip = instance.private_ip
    }
  ]
}

6. Bash Script Example

A basic bash script for server health monitoring:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/bin/bash

# Basic Server Health Check Script

echo "========== Server Health Check =========="
echo "Date: $(date)"
echo ""

# Check disk space
echo "--- Disk Space ---"
df -h | grep -vE '^Filesystem|tmpfs|cdrom'
echo ""

# Check memory usage
echo "--- Memory Usage ---"
free -m
echo ""

# Check CPU load
echo "--- CPU Load ---"
uptime
echo ""

# Check active processes
echo "--- Top 5 CPU-consuming processes ---"
ps aux --sort=-%cpu | head -6
echo ""

# Check system uptime
echo "--- System Uptime ---"
uptime -p
echo ""

echo "========== End of Health Check =========="
This post is licensed under CC BY 4.0 by the author.