DevOps Shack
DevOps Shack
Mastering Core DevOps Scenarios: A
Comprehensive Guide
Introduction
As organizations aim to achieve faster development cycles, continuous delivery,
and enhanced security, the role of a DevOps engineer becomes critical. This
document delves into five key scenarios every DevOps engineer must master,
covering automation, infrastructure as code, monitoring, scaling, and security.
1
DevOps Shack
We will explore:
1. Automating CI/CD Pipelines.
2. Infrastructure as Code (IaC) Implementation.
3. Monitoring and Incident Management.
4. Scaling Applications with Kubernetes.
5. Security and Compliance in the DevOps Pipeline.
Each section includes implementation details, code examples, and best practices
to help you master these scenarios.
Scenario 1: Automating CI/CD Pipelines
Objective
Automating Continuous Integration (CI) and Continuous Deployment (CD)
pipelines is crucial to streamline software delivery and ensure high-quality
releases. In this section, we will create a multi-stage CI/CD pipeline using Jenkins,
Docker, and Kubernetes.
Steps to Implement CI/CD Pipeline:
1. Set Up Jenkins: First, set up Jenkins on an EC2 instance or a Kubernetes
pod.
Jenkins Setup Script (EC2 example):
sudo apt update
sudo apt install openjdk-11-jdk -y
wget -q -O - [Link] | sudo apt-key add -
sudo sh -c 'echo deb [Link] binary/ >
/etc/apt/[Link].d/[Link]'
sudo apt update
sudo apt install jenkins -y
2
DevOps Shack
sudo systemctl start jenkins
sudo systemctl enable jenkins
2. Install Required Plugins: Install the necessary Jenkins plugins like Git,
Docker Pipeline, and Kubernetes CLI Plugin for integration.
3. Create a Jenkinsfile for Your Application: A Jenkinsfile defines the steps in
your CI/CD pipeline. Here is an example for a simple Java application with
Docker.
Example Jenkinsfile:
pipeline {
agent any
stages {
stage('Clone Repository') {
steps {
git '[Link]
}
}
stage('Build Docker Image') {
steps {
script {
[Link]('your-app-image')
}
}
}
stage('Deploy to Kubernetes') {
steps {
3
DevOps Shack
sh 'kubectl apply -f k8s/[Link]'
}
}
}
post {
always {
archiveArtifacts artifacts: '**/target/*.jar', allowEmptyArchive: true
}
}
}
4. Deploy the Application to Kubernetes: Define a Kubernetes deployment
manifest to deploy the built image.
Sample Kubernetes [Link]:
apiVersion: apps/v1
kind: Deployment
metadata:
name: your-app
spec:
replicas: 3
selector:
matchLabels:
app: your-app
template:
metadata:
4
DevOps Shack
labels:
app: your-app
spec:
containers:
- name: your-app
image: your-app-image:latest
ports:
- containerPort: 8080
5. Trigger the Pipeline: Push code changes to your Git repository, and Jenkins
will automatically trigger the pipeline.
Best Practices for CI/CD:
• Use Multi-Stage Pipelines: Define separate stages for building, testing, and
deploying to avoid bottlenecks.
• Automate Rollbacks: Implement automatic rollback strategies in case of
failures.
• Utilize Caching: Use Docker layer caching to speed up the build process.
Scenario 2: Infrastructure as Code (IaC) Implementation
Objective
Automating cloud infrastructure provisioning with Infrastructure as Code (IaC)
ensures consistency and reduces manual errors. We'll use Terraform for
provisioning and Ansible for configuration management.
Steps to Implement Infrastructure as Code:
1. Install Terraform: Download and install Terraform on your local machine or
CI environment.
Install Terraform (Linux):
5
DevOps Shack
bash
Copy code
sudo apt-get update && sudo apt-get install -y gnupg software-properties-
common curl
curl -fsSL [Link] | sudo apt-key add -
sudo apt-add-repository "deb [arch=amd64] [Link]
$(lsb_release -cs) main"
sudo apt-get update && sudo apt-get install terraform
2. Create Terraform Configuration Files: Define a simple Terraform
configuration for deploying a Virtual Private Cloud (VPC) and an EC2
instance.
Terraform Configuration ([Link]):
provider "aws" {
region = "us-west-2"
}
resource "aws_vpc" "my_vpc" {
cidr_block = "[Link]/16"
}
resource "aws_instance" "my_instance" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "[Link]"
tags = {
6
DevOps Shack
Name = "MyInstance"
}
}
3. Initialize and Apply Terraform Configuration: Use the following commands
to initialize and apply the Terraform configuration.
Terraform Commands:
terraform init
terraform apply
4. Install and Use Ansible for Configuration: Once the infrastructure is
provisioned, use Ansible to install packages or configure the instance.
Ansible Playbook ([Link]):
- hosts: all
become: yes
tasks:
- name: Install Nginx
apt:
name: nginx
state: present
5. Run the Ansible Playbook: Run the playbook to configure the EC2 instance.
Command:
ansible-playbook -i inventory [Link]
Best Practices for IaC:
• Modularize Code: Use Terraform modules to break down infrastructure
into reusable components.
7
DevOps Shack
• Version Control: Always use version control systems like Git for your IaC
files to track changes.
• Automate Testing: Use tools like terratest or InSpec to automatically test
your infrastructure code.
Scenario 3: Monitoring and Incident Management
Objective
Setting up robust monitoring and incident management is crucial for maintaining
high availability in production systems. In this scenario, we'll use Prometheus and
Grafana for monitoring, along with Alertmanager for incident management.
Steps to Implement Monitoring:
1. Set Up Prometheus: Install and configure Prometheus to scrape metrics
from your application or infrastructure.
Prometheus Configuration ([Link]):
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'node_exporter'
static_configs:
- targets: ['localhost:9100']
Run Prometheus:
./prometheus --[Link]=[Link]
2. Set Up Grafana for Visualization: Install Grafana and configure it to use
Prometheus as a data source for visualizing metrics.
Grafana Docker Command:
8
DevOps Shack
docker run -d -p 3000:3000 --name=grafana grafana/grafana
3. Configure Alertmanager: Set up Alertmanager to receive alerts from
Prometheus and route them to email, Slack, or other platforms.
Alertmanager Configuration ([Link]):
global:
smtp_smarthost: '[Link]'
smtp_from: 'your-email@[Link]'
smtp_auth_username: 'your-email@[Link]'
smtp_auth_password: 'your-password'
route:
receiver: 'email-alert'
receivers:
- name: 'email-alert'
email_configs:
- to: 'admin@[Link]'
4. Create Alert Rules in Prometheus: Define alert rules in Prometheus to
trigger alerts based on metrics.
Prometheus Alert Rule:
groups:
- name: example
rules:
- alert: InstanceDown
expr: up == 0
9
DevOps Shack
for: 5m
labels:
severity: critical
annotations:
summary: "Instance {{ $[Link] }} down"
description: "{{ $[Link] }} of job {{ $[Link] }} has been down for
more than 5 minutes."
5. Respond to Alerts: When an incident occurs, Alertmanager will trigger
notifications. Make sure to have an incident response plan in place.
Best Practices for Monitoring:
• Use Service-Level Objectives (SLOs): Define clear SLOs and metrics that
align with business objectives.
• Alert on Symptoms, Not Causes: Focus on customer-facing symptoms
rather than internal issues.
• Automate Incident Responses: Automate responses where possible, such
as auto-scaling or restarting failed services.
Scenario 4: Scaling Applications with Kubernetes
Objective
As applications grow, scaling becomes critical to handling increased user load. In
this scenario, we'll use Kubernetes Horizontal Pod Autoscaler (HPA) to scale a
web application based on CPU usage.
Steps to Implement Kubernetes Scaling:
1. Deploy the Application: Start by deploying a web application to a
Kubernetes cluster.
Deployment Manifest ([Link]):
10
DevOps Shack
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-app
spec:
replicas: 2
selector:
matchLabels:
app: web-app
template:
metadata:
labels:
app: web-app
spec:
containers:
- name: web-app
image: web-app-image
resources:
requests:
cpu: "100m"
limits:
cpu: "200m"
ports:
- containerPort: 80
11
DevOps Shack
2. Enable Horizontal Pod Autoscaler (HPA): Define an HPA to scale the
application based on CPU usage.
HPA Manifest ([Link]):
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: web-app-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: web-app
minReplicas: 2
maxReplicas: 10
targetCPUUtilizationPercentage: 50
3. Apply the HPA Configuration: Apply the HPA configuration to the cluster.
Command:
kubectl apply -f [Link]
4. Test Scaling: Simulate high load to observe the autoscaler in action. You
can use tools like Apache JMeter or wrk to load test the application.
5. Monitor Scaling Events: Use kubectl get hpa to monitor scaling events and
ensure that the application is scaling correctly based on CPU usage.
Command:
kubectl get hpa
Best Practices for Scaling:
12
DevOps Shack
• Optimize Resource Requests and Limits: Define appropriate CPU and
memory requests/limits for your pods.
• Use Metrics Wisely: You can scale based on other metrics like memory,
custom metrics, or even external metrics.
• Avoid Over-Scaling: Set a reasonable maximum limit for your autoscaler to
prevent unnecessary resource consumption.
Scenario 5: Security and Compliance in the DevOps Pipeline
Objective
Ensuring security and compliance is a key responsibility in the DevOps lifecycle.
We'll integrate security scanning tools like Snyk and SonarQube into a CI/CD
pipeline to detect vulnerabilities early in the development process.
Steps to Implement Security in CI/CD:
1. Integrate Snyk into Jenkins Pipeline: Use Snyk to scan your application
dependencies for known vulnerabilities.
Add Snyk Stage to Jenkinsfile:
pipeline {
agent any
stages {
stage('Snyk Security Scan') {
steps {
sh 'snyk test --all-projects'
}
}
}
}
13
DevOps Shack
Snyk Command (for local testing):
snyk test --all-projects
2. Integrate SonarQube for Code Quality and Security: Set up SonarQube to
analyze code quality and security vulnerabilities.
Add SonarQube Stage to Jenkinsfile:
pipeline {
agent any
stages {
stage('SonarQube Analysis') {
steps {
withSonarQubeEnv('SonarQube') {
sh 'mvn clean verify sonar:sonar'
}
}
}
}
}
3. Run Compliance Checks: For infrastructure compliance, you can use
Terraform Sentinel or tools like OPA (Open Policy Agent) to enforce
policies during IaC provisioning.
Sentinel Example:
import "tfplan"
import "strings"
main = rule {
14
DevOps Shack
strings.has_prefix(tfplan.resource_changes[*].address, "aws_instance")
}
4. Automate Security Tests: Ensure that security tests are part of every
pipeline run, with automatic blocking for critical vulnerabilities.
5. Review and Respond to Reports: Regularly review the security reports
from Snyk and SonarQube and address the critical issues in a timely
manner.
Best Practices for Security:
• Shift Left on Security: Start security testing early in the development
process to identify issues sooner.
• Use Dependency Scanning: Regularly scan your project dependencies for
known vulnerabilities.
• Implement Automated Security Gates: Block deployments that fail security
tests or have critical vulnerabilities.
Conclusion
In this document, we covered five essential scenarios for any DevOps engineer:
automating CI/CD pipelines, implementing infrastructure as code, monitoring
systems, scaling applications, and securing the DevOps lifecycle. Each scenario
comes with code examples and best practices to help you get started and
continuously improve your workflows.
Mastering these scenarios ensures that you can handle core aspects of the
DevOps lifecycle, from automation to security, providing value to any organization
focused on speed, reliability, and security in software delivery.
15