November 25, 2025
19 min read

Junior DevOps Engineer Interview Questions: Complete Guide

interview
career-advice
job-search
entry-level
Junior DevOps Engineer Interview Questions: Complete Guide
MB

Milad Bonakdar

Author

Master essential DevOps fundamentals with comprehensive interview questions covering Linux, Git, CI/CD, Docker, cloud basics, and Infrastructure as Code for junior DevOps engineers.


Introduction

DevOps engineering bridges development and operations, focusing on automation, collaboration, and continuous improvement. As a junior DevOps engineer, you'll need foundational knowledge of Linux, version control, CI/CD pipelines, containerization, and cloud platforms.

This guide covers essential interview questions for junior DevOps engineers, organized by topic to help you prepare effectively. Each question includes detailed answers, practical examples, and hands-on code snippets.


Linux Fundamentals

1. Explain common Linux commands you use daily as a DevOps engineer.

Answer: Essential Linux commands for DevOps work:

# File operations
ls -la                    # List files with details
cd /var/log              # Change directory
cat /etc/hosts           # Display file content
tail -f /var/log/app.log # Follow log file in real-time
grep "error" app.log     # Search for patterns

# Process management
ps aux | grep nginx      # List processes
top                      # Monitor system resources
kill -9 1234            # Force kill process
systemctl status nginx   # Check service status
systemctl restart nginx  # Restart service

# File permissions
chmod 755 script.sh      # Change file permissions
chown user:group file    # Change ownership
ls -l                    # View permissions

# Disk usage
df -h                    # Disk space usage
du -sh /var/log          # Directory size
free -h                  # Memory usage

# Network
netstat -tulpn           # Show listening ports
curl https://api.com     # Make HTTP request
ping google.com          # Test connectivity
ssh user@server          # Remote login

Rarity: Very Common
Difficulty: Easy


2. How do you troubleshoot a service that's not starting on Linux?

Answer: Systematic troubleshooting approach:

# 1. Check service status
systemctl status nginx
# Look for error messages

# 2. Check logs
journalctl -u nginx -n 50
# or
tail -f /var/log/nginx/error.log

# 3. Check configuration syntax
nginx -t

# 4. Check if port is already in use
netstat -tulpn | grep :80
# or
lsof -i :80

# 5. Check file permissions
ls -l /etc/nginx/nginx.conf
ls -ld /var/log/nginx

# 6. Check disk space
df -h

# 7. Try starting manually for more details
nginx -g 'daemon off;'

# 8. Check SELinux/AppArmor (if enabled)
getenforce
ausearch -m avc -ts recent

Common issues:

  • Configuration syntax errors
  • Port already in use
  • Permission denied
  • Missing dependencies
  • Insufficient disk space

Rarity: Very Common
Difficulty: Medium


Version Control with Git

3. Explain the basic Git workflow and common commands.

Answer: Git workflow for daily DevOps tasks:

# Initialize repository
git init
git clone https://github.com/user/repo.git

# Check status
git status
git log --oneline

# Basic workflow
git add .                    # Stage changes
git commit -m "Add feature"  # Commit changes
git push origin main         # Push to remote

# Branching
git branch feature-x         # Create branch
git checkout feature-x       # Switch branch
git checkout -b feature-y    # Create and switch

# Merging
git checkout main
git merge feature-x

# Pull latest changes
git pull origin main

# Undo changes
git reset --hard HEAD        # Discard local changes
git revert abc123           # Revert specific commit

# Stash changes
git stash                    # Save changes temporarily
git stash pop               # Restore stashed changes

# View differences
git diff                     # Unstaged changes
git diff --staged           # Staged changes

Best practices:

  • Write clear commit messages
  • Commit often, push regularly
  • Use feature branches
  • Pull before pushing
  • Review changes before committing

Rarity: Very Common
Difficulty: Easy


4. How do you resolve a merge conflict in Git?

Answer: Step-by-step conflict resolution:

# 1. Attempt merge
git merge feature-branch
# Auto-merging file.txt
# CONFLICT (content): Merge conflict in file.txt

# 2. Check conflicted files
git status
# Unmerged paths:
#   both modified:   file.txt

# 3. Open conflicted file
cat file.txt
# <<<<<<< HEAD
# Current branch content
# =======
# Incoming branch content
# >>>>>>> feature-branch

# 4. Edit file to resolve conflict
# Remove conflict markers and keep desired changes

# 5. Stage resolved file
git add file.txt

# 6. Complete merge
git commit -m "Resolve merge conflict"

# Alternative: Abort merge
git merge --abort

Conflict markers:

  • <<<<<<< HEAD - Your current branch
  • ======= - Separator
  • >>>>>>> branch-name - Incoming changes

Rarity: Common
Difficulty: Easy-Medium


CI/CD Basics

5. What is CI/CD and why is it important?

Answer: CI/CD stands for Continuous Integration and Continuous Deployment/Delivery.

Continuous Integration (CI):

  • Automatically build and test code on every commit
  • Catch bugs early
  • Ensure code integrates properly

Continuous Deployment (CD):

  • Automatically deploy to production after tests pass
  • Faster release cycles
  • Reduced manual errors
Loading diagram...

Benefits:

  • Faster feedback loops
  • Reduced integration problems
  • Automated testing
  • Consistent deployments
  • Faster time to market

Rarity: Very Common
Difficulty: Easy


6. Explain a basic CI/CD pipeline using GitHub Actions.

Answer: Example GitHub Actions workflow:

# .github/workflows/ci-cd.yml
name: CI/CD Pipeline

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  build-and-test:
    runs-on: ubuntu-latest
    
    steps:
    - name: Checkout code
      uses: actions/checkout@v3
    
    - name: Setup Node.js
      uses: actions/setup-node@v3
      with:
        node-version: '18'
    
    - name: Install dependencies
      run: npm ci
    
    - name: Run tests
      run: npm test
    
    - name: Build application
      run: npm run build
    
    - name: Run linter
      run: npm run lint

  deploy:
    needs: build-and-test
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'
    
    steps:
    - name: Deploy to production
      run: |
        echo "Deploying to production..."
        # Add deployment commands here

Key concepts:

  • Triggers: When pipeline runs (push, PR, schedule)
  • Jobs: Independent tasks that can run in parallel
  • Steps: Individual commands within a job
  • Artifacts: Files passed between jobs

Rarity: Very Common
Difficulty: Medium


Docker & Containerization

7. What is Docker and why do we use containers?

Answer: Docker is a platform for developing, shipping, and running applications in containers.

Containers vs VMs:

  • Containers share host OS kernel (lightweight)
  • VMs include full OS (heavy)
  • Containers start in seconds
  • Better resource utilization

Benefits:

  • Consistency: Same environment everywhere
  • Isolation: Apps don't interfere
  • Portability: Run anywhere
  • Efficiency: Lightweight and fast
# Example Dockerfile
FROM node:18-alpine

WORKDIR /app

COPY package*.json ./
RUN npm ci --only=production

COPY . .

EXPOSE 3000

CMD ["node", "server.js"]

Rarity: Very Common
Difficulty: Easy


8. Explain common Docker commands.

Answer: Essential Docker commands:

# Images
docker pull nginx:latest          # Download image
docker images                     # List images
docker rmi nginx:latest          # Remove image
docker build -t myapp:1.0 .      # Build image

# Containers
docker run -d -p 80:80 nginx     # Run container
docker ps                         # List running containers
docker ps -a                      # List all containers
docker stop container_id          # Stop container
docker start container_id         # Start container
docker restart container_id       # Restart container
docker rm container_id            # Remove container

# Logs and debugging
docker logs container_id          # View logs
docker logs -f container_id       # Follow logs
docker exec -it container_id bash # Enter container
docker inspect container_id       # Detailed info

# Cleanup
docker system prune              # Remove unused data
docker volume prune              # Remove unused volumes

# Docker Compose
docker-compose up -d             # Start services
docker-compose down              # Stop services
docker-compose logs -f           # View logs

Rarity: Very Common
Difficulty: Easy


9. Write a docker-compose.yml for a web application with a database.

Answer: Example multi-container application:

version: '3.8'

services:
  web:
    build: .
    ports:
      - "3000:3000"
    environment:
      - NODE_ENV=production
      - DB_HOST=db
      - DB_PORT=5432
      - DB_NAME=myapp
    depends_on:
      - db
    volumes:
      - ./logs:/app/logs
    restart: unless-stopped

  db:
    image: postgres:15-alpine
    environment:
      - POSTGRES_DB=myapp
      - POSTGRES_USER=admin
      - POSTGRES_PASSWORD=secret
    volumes:
      - postgres_data:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    restart: unless-stopped

  redis:
    image: redis:7-alpine
    ports:
      - "6379:6379"
    restart: unless-stopped

volumes:
  postgres_data:

Key concepts:

  • services: Define containers
  • depends_on: Service dependencies
  • volumes: Persistent data storage
  • environment: Environment variables
  • ports: Port mapping
  • restart: Restart policy

Rarity: Common
Difficulty: Medium


Cloud Basics

10. Explain the difference between IaaS, PaaS, and SaaS.

Answer: Cloud service models:

IaaS (Infrastructure as a Service):

  • Provides: Virtual machines, storage, networks
  • You manage: OS, runtime, applications
  • Examples: AWS EC2, Azure VMs, Google Compute Engine
  • Use case: Full control over infrastructure

PaaS (Platform as a Service):

  • Provides: Runtime environment, databases, middleware
  • You manage: Applications and data
  • Examples: AWS Elastic Beanstalk, Heroku, Google App Engine
  • Use case: Focus on code, not infrastructure

SaaS (Software as a Service):

  • Provides: Complete applications
  • You manage: User data and settings
  • Examples: Gmail, Salesforce, Office 365
  • Use case: Ready-to-use applications
Loading diagram...

Rarity: Common
Difficulty: Easy


11. What are the basic AWS services a DevOps engineer should know?

Answer: Essential AWS services:

Compute:

  • EC2: Virtual servers
  • Lambda: Serverless functions
  • ECS/EKS: Container orchestration

Storage:

  • S3: Object storage
  • EBS: Block storage for EC2
  • EFS: Shared file storage

Networking:

  • VPC: Virtual private cloud
  • Route 53: DNS service
  • CloudFront: CDN
  • ELB: Load balancing

Database:

  • RDS: Managed relational databases
  • DynamoDB: NoSQL database

DevOps Tools:

  • CodePipeline: CI/CD service
  • CodeBuild: Build service
  • CloudWatch: Monitoring and logging
  • IAM: Access management

Example: Launch EC2 instance with AWS CLI:

aws ec2 run-instances \
  --image-id ami-0abcdef1234567890 \
  --instance-type t2.micro \
  --key-name my-key-pair \
  --security-group-ids sg-0123456789abcdef0 \
  --subnet-id subnet-0123456789abcdef0 \
  --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=WebServer}]'

Rarity: Very Common
Difficulty: Medium


Infrastructure as Code

12. What is Infrastructure as Code (IaC) and why is it important?

Answer: IaC is managing infrastructure through code rather than manual processes.

Benefits:

  • Version Control: Track infrastructure changes
  • Reproducibility: Create identical environments
  • Automation: Reduce manual errors
  • Documentation: Code serves as documentation
  • Consistency: Same configuration everywhere

Popular IaC tools:

  • Terraform: Multi-cloud provisioning
  • Ansible: Configuration management
  • CloudFormation: AWS-specific
  • Pulumi: Code-based IaC

Example Terraform:

# main.tf
provider "aws" {
  region = "us-east-1"
}

resource "aws_instance" "web" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"

  tags = {
    Name = "WebServer"
    Environment = "Production"
  }
}

resource "aws_security_group" "web_sg" {
  name        = "web-sg"
  description = "Allow HTTP traffic"

  ingress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

Rarity: Very Common
Difficulty: Medium


13. Explain basic Terraform workflow.

Answer: Terraform workflow steps:

# 1. Initialize Terraform
terraform init
# Downloads providers and modules

# 2. Format code
terraform fmt
# Formats .tf files

# 3. Validate configuration
terraform validate
# Checks syntax

# 4. Plan changes
terraform plan
# Shows what will be created/modified/destroyed

# 5. Apply changes
terraform apply
# Creates/updates infrastructure
# Prompts for confirmation

# 6. View state
terraform show
# Shows current state

# 7. Destroy infrastructure
terraform destroy
# Removes all resources

Terraform file structure:

project/
├── main.tf          # Main configuration
├── variables.tf     # Input variables
├── outputs.tf       # Output values
├── terraform.tfvars # Variable values
└── .terraform/      # Provider plugins

Example variables:

# variables.tf
variable "instance_type" {
  description = "EC2 instance type"
  type        = string
  default     = "t2.micro"
}

variable "environment" {
  description = "Environment name"
  type        = string
}

# terraform.tfvars
environment = "production"
instance_type = "t2.small"

Rarity: Common
Difficulty: Medium


Monitoring & Logging

14. What metrics would you monitor for a web application?

Answer: Key monitoring metrics:

Application Metrics:

  • Response time / latency
  • Request rate (requests per second)
  • Error rate (4xx, 5xx errors)
  • Throughput

System Metrics:

  • CPU usage
  • Memory usage
  • Disk I/O
  • Network I/O

Infrastructure Metrics:

  • Container/pod status
  • Service availability
  • Load balancer health

Example Prometheus query:

# Average response time
rate(http_request_duration_seconds_sum[5m])
/ rate(http_request_duration_seconds_count[5m])

# Error rate
sum(rate(http_requests_total{status=~"5.."}[5m]))
/ sum(rate(http_requests_total[5m]))

# CPU usage
100 - (avg by (instance) (irate(node_cpu_seconds_total{mode="idle"}[5m])) * 100)

Alerting thresholds:

  • Response time > 500ms
  • Error rate > 1%
  • CPU usage > 80%
  • Memory usage > 85%
  • Disk usage > 90%

Rarity: Common
Difficulty: Medium


15. How do you centralize logs from multiple servers?

Answer: Centralized logging architecture:

Loading diagram...

Common stack (ELK):

  • Elasticsearch: Store and index logs
  • Logstash/Fluentd: Collect and process logs
  • Kibana: Visualize and search logs

Example Filebeat configuration:

# filebeat.yml
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /var/log/app/*.log
  fields:
    app: myapp
    environment: production

output.elasticsearch:
  hosts: ["elasticsearch:9200"]
  index: "app-logs-%{+yyyy.MM.dd}"

processors:
  - add_host_metadata: ~
  - add_cloud_metadata: ~

Best practices:

  • Use structured logging (JSON)
  • Include correlation IDs
  • Set retention policies
  • Index strategically
  • Monitor log volume

Rarity: Common
Difficulty: Medium


Kubernetes Basics

16. What is Kubernetes and what are its basic components?

Answer: Kubernetes is a container orchestration platform that automates deployment, scaling, and management of containerized applications.

Basic Components:

Control Plane:

  • API Server: Entry point for all commands
  • etcd: Key-value store for cluster data
  • Scheduler: Assigns pods to nodes
  • Controller Manager: Maintains desired state

Worker Nodes:

  • kubelet: Manages pods on the node
  • kube-proxy: Network routing
  • Container Runtime: Runs containers (Docker, containerd)
Loading diagram...

Basic Kubernetes Objects:

1. Pod:

apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  labels:
    app: nginx
spec:
  containers:
  - name: nginx
    image: nginx:latest
    ports:
    - containerPort: 80

2. Deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.21
        ports:
        - containerPort: 80

3. Service:

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
  type: LoadBalancer

Common kubectl Commands:

# Get resources
kubectl get pods
kubectl get deployments
kubectl get services

# Describe resource
kubectl describe pod nginx-pod

# Create from file
kubectl apply -f deployment.yaml

# Delete resource
kubectl delete pod nginx-pod

# View logs
kubectl logs nginx-pod

# Execute command in pod
kubectl exec -it nginx-pod -- /bin/bash

# Port forwarding
kubectl port-forward pod/nginx-pod 8080:80

Rarity: Very Common
Difficulty: Easy


Configuration Management

17. Explain Ansible basics and write a simple playbook.

Answer: Ansible is an agentless configuration management tool that uses SSH to configure servers.

Key Concepts:

  • Inventory: List of servers to manage
  • Playbook: YAML file defining tasks
  • Modules: Reusable units of work
  • Roles: Organized collection of tasks

Inventory File:

# inventory.ini
[webservers]
web1.example.com
web2.example.com

[databases]
db1.example.com

[all: vars]
ansible_user=ubuntu
ansible_ssh_private_key_file=~/.ssh/id_rsa

Simple Playbook:

# playbook.yml
---
- name: Setup Web Servers
  hosts: webservers
  become: yes
  
  vars:
    app_port: 8080
    app_user: webapp
  
  tasks:
    - name: Update apt cache
      apt:
        update_cache: yes
        cache_valid_time: 3600
    
    - name: Install required packages
      apt:
        name:
          - nginx
          - python3
          - git
        state: present
    
    - name: Create application user
      user:
        name: "{{ app_user }}"
        shell: /bin/bash
        create_home: yes
    
    - name: Copy nginx configuration
      template:
        src: templates/nginx.conf.j2
        dest: /etc/nginx/sites-available/default
      notify: Restart nginx
    
    - name: Ensure nginx is running
      service:
        name: nginx
        state: started
        enabled: yes
    
    - name: Deploy application
      git:
        repo: https://github.com/example/app.git
        dest: /var/www/app
        version: main
      become_user: "{{ app_user }}"
  
  handlers:
    - name: Restart nginx
      service:
        name: nginx
        state: restarted

Template Example:

# templates/nginx.conf.j2
server {
    listen {{ app_port }};
    server_name {{ ansible_hostname }};
    
    location / {
        proxy_pass http://localhost:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

Running Playbooks:

# Check syntax
ansible-playbook playbook.yml --syntax-check

# Dry run (check mode)
ansible-playbook playbook.yml --check

# Run playbook
ansible-playbook -i inventory.ini playbook.yml

# Run with specific tags
ansible-playbook playbook.yml --tags "deploy"

# Limit to specific hosts
ansible-playbook playbook.yml --limit webservers

Ansible Roles Structure:

roles/
└── webserver/
    ├── tasks/
    │   └── main.yml
    ├── handlers/
    │   └── main.yml
    ├── templates/
    │   └── nginx.conf.j2
    ├── files/
    ├── vars/
    │   └── main.yml
    └── defaults/
        └── main.yml

Using Roles:

---
- name: Setup Infrastructure
  hosts: all
  become: yes
  
  roles:
    - common
    - webserver
    - monitoring

Ad-hoc Commands:

# Ping all hosts
ansible all -i inventory.ini -m ping

# Run command on all hosts
ansible all -i inventory.ini -a "uptime"

# Install package
ansible webservers -i inventory.ini -m apt -a "name=nginx state=present" --become

# Copy file
ansible all -i inventory.ini -m copy -a "src=/local/file dest=/remote/file"

# Restart service
ansible webservers -i inventory.ini -m service -a "name=nginx state=restarted" --become

Rarity: Common
Difficulty: Medium


Scripting & Automation

18. Write a bash script to automate a common DevOps task.

Answer: Bash scripting is essential for automation in DevOps.

Example 1: Backup Script

#!/bin/bash

# Database backup script with rotation
set -e  # Exit on error
set -u  # Exit on undefined variable

# Configuration
DB_NAME="myapp"
DB_USER="backup_user"
BACKUP_DIR="/var/backups/mysql"
RETENTION_DAYS=7
DATE=$(date +%Y%m%d_%H%M%S)
BACKUP_FILE="${BACKUP_DIR}/${DB_NAME}_${DATE}.sql.gz"
LOG_FILE="/var/log/mysql_backup.log"

# Function to log messages
log() {
    echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}

# Function to send notification
send_notification() {
    local status=$1
    local message=$2
    
    # Send to Slack
    curl -X POST -H 'Content-type: application/json' \
        --data "{\"text\":\"Backup ${status}: ${message}\"}" \
        "$SLACK_WEBHOOK_URL"
}

# Create backup directory if it doesn't exist
mkdir -p "$BACKUP_DIR"

# Start backup
log "Starting backup of database: $DB_NAME"

# Perform backup
if mysqldump -u "$DB_USER" -p"$DB_PASSWORD" "$DB_NAME" | gzip > "$BACKUP_FILE"; then
    log "Backup completed successfully: $BACKUP_FILE"
    
    # Get file size
    SIZE=$(du -h "$BACKUP_FILE" | cut -f1)
    log "Backup size: $SIZE"
    
    # Remove old backups
    log "Removing backups older than $RETENTION_DAYS days"
    find "$BACKUP_DIR" -name "${DB_NAME}_*.sql.gz" -mtime +$RETENTION_DAYS -delete
    
    # Upload to S3 (optional)
    if command -v aws &> /dev/null; then
        log "Uploading backup to S3"
        aws s3 cp "$BACKUP_FILE" "s3://my-backups/mysql/" --storage-class GLACIER
    fi
    
    send_notification "SUCCESS" "Database $DB_NAME backed up successfully ($SIZE)"
else
    log "ERROR: Backup failed"
    send_notification "FAILED" "Database $DB_NAME backup failed"
    exit 1
fi

log "Backup process completed"

Example 2: Health Check Script

#!/bin/bash

# Service health check script
SERVICES=("nginx" "mysql" "redis")
ENDPOINTS=("http://localhost:80" "http://localhost:8080/health")
ALERT_EMAIL="ops@example.com"

check_service() {
    local service=$1
    
    if systemctl is-active --quiet "$service"; then
        echo "✓ $service is running"
        return 0
    else
        echo "✗ $service is NOT running"
        return 1
    fi
}

check_endpoint() {
    local url=$1
    local response=$(curl -s -o /dev/null -w "%{http_code}" "$url")
    
    if [ "$response" -eq 200 ]; then
        echo "✓ $url is healthy (HTTP $response)"
        return 0
    else
        echo "✗ $url is unhealthy (HTTP $response)"
        return 1
    fi
}

check_disk_space() {
    local threshold=80
    local usage=$(df -h / | awk 'NR==2 {print $5}' | sed 's/%//')
    
    if [ "$usage" -lt "$threshold" ]; then
        echo "✓ Disk usage: ${usage}%"
        return 0
    else
        echo "✗ Disk usage critical: ${usage}%"
        return 1
    fi
}

# Main health check
echo "=== System Health Check ==="
echo "Date: $(date)"
echo

failed_checks=0

# Check services
echo "Checking services..."
for service in "${SERVICES[@]}"; do
    if ! check_service "$service"; then
        ((failed_checks++))
    fi
done
echo

# Check endpoints
echo "Checking endpoints..."
for endpoint in "${ENDPOINTS[@]}"; do
    if ! check_endpoint "$endpoint"; then
        ((failed_checks++))
    fi
done
echo

# Check disk space
echo "Checking disk space..."
if ! check_disk_space; then
    ((failed_checks++))
fi
echo

# Report results
if [ $failed_checks -eq 0 ]; then
    echo "All checks passed!"
    exit 0
else
    echo "Failed checks: $failed_checks"
    # Send alert email
    echo "Health check failed. $failed_checks issues detected." | \
        mail -s "Health Check Alert" "$ALERT_EMAIL"
    exit 1
fi

Example 3: Deployment Script

#!/bin/bash

# Application deployment script
APP_NAME="myapp"
APP_DIR="/var/www/$APP_NAME"
REPO_URL="https://github.com/example/myapp.git"
BRANCH="main"
BACKUP_DIR="/var/backups/deployments"

deploy() {
    echo "Starting deployment of $APP_NAME"
    
    # Create backup
    echo "Creating backup..."
    BACKUP_FILE="${BACKUP_DIR}/${APP_NAME}_$(date +%Y%m%d_%H%M%S).tar.gz"
    tar -czf "$BACKUP_FILE" -C "$APP_DIR" .
    
    # Pull latest code
    echo "Pulling latest code from $BRANCH..."
    cd "$APP_DIR" || exit 1
    git fetch origin
    git checkout "$BRANCH"
    git pull origin "$BRANCH"
    
    # Install dependencies
    echo "Installing dependencies..."
    npm ci --production
    
    # Run database migrations
    echo "Running migrations..."
    npm run migrate
    
    # Build application
    echo "Building application..."
    npm run build
    
    # Restart application
    echo "Restarting application..."
    pm2 restart "$APP_NAME"
    
    # Health check
    echo "Performing health check..."
    sleep 5
    if curl -f http://localhost:3000/health > /dev/null 2>&1; then
        echo "✓ Deployment successful!"
        return 0
    else
        echo "✗ Health check failed. Rolling back..."
        rollback "$BACKUP_FILE"
        return 1
    fi
}

rollback() {
    local backup_file=$1
    echo "Rolling back to previous version..."
    
    cd "$APP_DIR" || exit 1
    rm -rf ./*
    tar -xzf "$backup_file" -C .
    
    pm2 restart "$APP_NAME"
    echo "Rollback completed"
}

# Run deployment
deploy

Best Practices:

  • Use set -e to exit on errors
  • Use set -u to catch undefined variables
  • Add logging and error handling
  • Make scripts idempotent
  • Use functions for reusability
  • Add comments and documentation
  • Validate inputs
  • Use meaningful variable names

Rarity: Very Common
Difficulty: Medium


Conclusion

Preparing for a junior DevOps engineer interview requires hands-on experience with core tools and concepts. Focus on:

  1. Linux fundamentals: Command line proficiency and troubleshooting
  2. Version control: Git workflows and collaboration
  3. CI/CD: Understanding automation pipelines
  4. Containers: Docker basics and orchestration
  5. Cloud platforms: AWS/Azure/GCP core services
  6. IaC: Terraform or Ansible basics
  7. Monitoring: Metrics and centralized logging

Practice these concepts in real projects, set up your own CI/CD pipelines, and deploy applications to cloud platforms. Hands-on experience is the best preparation. Good luck!

Related Posts

Recent Posts

Weekly career tips that actually work

Get the latest insights delivered straight to your inbox