Please wait
Please wait
Comprehensive DevOps reference covering Docker, Kubernetes, Git, CI/CD, Linux, AWS, Terraform, Ansible, Jenkins, and monitoring tools. Essential commands and best practices for DevOps engineers.
Docker container lifecycle and basic operations
dockerDocker: Platform for developing, shipping, and running applications in containers
Container: Lightweight, standalone executable package with everything needed to run software
Image: Template for creating containers
# Container Lifecycle
docker run nginx # Create and start container
docker run -d nginx # Detached mode
docker run -it ubuntu bash # Interactive terminal
docker run --name web nginx # Named container
docker run -p 8080:80 nginx # Port mapping
# Container Management
docker ps # Running containers
docker ps -a # 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
docker rm -f container_id # Force remove# Container Inspection
docker logs container_id # View logs
docker logs -f container_id # Follow logs
docker exec -it container_id bash # Execute command
docker inspect container_id # Detailed info
docker stats # Resource usage
docker top container_id # Running processesBuilding, managing, and optimizing Docker images
dockerDockerfile: Text file with instructions to build Docker image
Layer: Each instruction in Dockerfile creates a layer
Registry: Storage and distribution system for Docker images (Docker Hub, ECR)
# Image Operations
docker images # List images
docker pull nginx:latest # Pull image
docker push user/image:tag # Push to registry
docker build -t myapp:1.0 . # Build image
docker tag source target # Tag image
docker rmi image_id # Remove image
docker image prune # Remove unused images# Sample Dockerfile
FROM node:16-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]
# Multi-stage Build
FROM node:16 AS builder
WORKDIR /app
COPY . .
RUN npm install && npm run build
FROM nginx:alpine
COPY /app/dist /usr/share/nginx/htmlContainer networking and data persistence
docker# Docker Networks
docker network ls # List networks
docker network create mynetwork # Create network
docker network inspect mynetwork # Network details
docker run --network mynetwork nginx # Connect to network
# Network Types
# bridge: Default, isolated network
# host: Use host's network
# none: No networking
# Docker Volumes
docker volume ls # List volumes
docker volume create myvolume # Create volume
docker volume inspect myvolume # Volume details
docker run -v myvolume:/data nginx # Mount volume
docker run -v $(pwd):/app nginx # Bind mount
# Volume Management
docker volume prune # Remove unused volumes
docker volume rm myvolume # Remove volumeMulti-container application management
dockerDocker Compose: Tool for defining and running multi-container applications
docker-compose.yml: YAML file defining services, networks, and volumes
# Docker Compose Commands
docker-compose up # Start services
docker-compose up -d # Detached mode
docker-compose down # Stop and remove
docker-compose ps # List services
docker-compose logs # View logs
docker-compose logs -f servicename # Follow logs
docker-compose exec service bash # Execute command
docker-compose build # Build images
docker-compose restart # Restart services# Sample docker-compose.yml
version: '3.8'
services:
web:
build: .
ports:
- "3000:3000"
environment:
- NODE_ENV=production
depends_on:
- db
volumes:
- ./app:/app
db:
image: postgres:14
environment:
POSTGRES_PASSWORD: secret
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:Kubernetes cluster operations and namespace management
kubernetesKubernetes (K8s): Container orchestration platform for automating deployment, scaling, and management
kubectl: Command-line tool for Kubernetes
Namespace: Virtual cluster for resource isolation
# Cluster Information
kubectl cluster-info # Cluster details
kubectl get nodes # List nodes
kubectl describe node node1 # Node details
kubectl version # Kubernetes version
kubectl api-resources # Available resources# Namespace Operations
kubectl get namespaces # List namespaces
kubectl create namespace dev # Create namespace
kubectl delete namespace dev # Delete namespace
kubectl config set-context --current --namespace=dev
kubectl get all -n dev # All resources in namespaceManaging pods and deployments in Kubernetes
kubernetesPod: Smallest deployable unit, contains one or more containers
Deployment: Manages replica sets and provides declarative updates
ReplicaSet: Ensures specified number of pod replicas are running
# Pod Operations
kubectl get pods # List pods
kubectl get pods -A # All namespaces
kubectl get pods -o wide # Detailed view
kubectl describe pod mypod # Pod details
kubectl logs mypod # Pod logs
kubectl logs -f mypod # Follow logs
kubectl logs mypod -c container # Specific container
kubectl exec -it mypod -- bash # Execute command
kubectl delete pod mypod # Delete pod# Deployment Management
kubectl get deployments # List deployments
kubectl create deployment nginx --image=nginx
kubectl scale deployment nginx --replicas=3
kubectl set image deployment/nginx nginx=nginx:1.19
kubectl rollout status deployment/nginx
kubectl rollout history deployment/nginx
kubectl rollout undo deployment/nginx
kubectl delete deployment nginxNetworking and configuration management
kubernetesService: Exposes pods to network traffic (ClusterIP, NodePort, LoadBalancer)
ConfigMap: Store configuration data as key-value pairs
Secret: Store sensitive data (passwords, tokens)
# Service Operations
kubectl get services # List services
kubectl expose deployment nginx --port=80 --type=LoadBalancer
kubectl describe service nginx # Service details
kubectl delete service nginx
# Service Types
# ClusterIP: Internal cluster access
# NodePort: External access via node port
# LoadBalancer: Cloud load balancer# ConfigMap & Secrets
kubectl create configmap app-config --from-literal=key=value
kubectl create configmap app-config --from-file=config.properties
kubectl get configmaps
kubectl describe configmap app-config
kubectl create secret generic db-secret --from-literal=password=secret
kubectl get secrets
kubectl describe secret db-secretDeclarative resource management with YAML
kubernetesDeclarative: Define desired state in YAML files
Imperative: Direct commands to make changes
Best Practice: Use declarative approach for production
# Resource Management
kubectl create -f file.yaml # Create resource
kubectl apply -f file.yaml # Apply/update resource
kubectl delete -f file.yaml # Delete resource
kubectl apply -f ./directory/ # Apply all in directory
kubectl get -f file.yaml # Get resource status# Sample Deployment YAML
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.19
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- port: 80
targetPort: 80
type: LoadBalancerVersion control fundamentals with Git
gitGit: Distributed version control system
Repository: Project directory tracked by Git
Commit: Snapshot of project at specific time
# Repository Setup
git init # Initialize repository
git clone url # Clone repository
git config --global user.name "Name"
git config --global user.email "email@example.com"
# Basic Workflow
git status # Check status
git add file.txt # Stage file
git add . # Stage all changes
git commit -m "message" # Commit changes
git commit -am "message" # Add and commit# Viewing History
git log # Commit history
git log --oneline # Compact view
git log --graph --all # Visual graph
git show commit_hash # Show commit
git diff # Unstaged changes
git diff --staged # Staged changesBranch management and merge strategies
git# Branch Operations
git branch # List branches
git branch feature # Create branch
git checkout feature # Switch branch
git checkout -b feature # Create and switch
git branch -d feature # Delete branch
git branch -D feature # Force delete
# Merging
git merge feature # Merge branch
git merge --no-ff feature # No fast-forward
git rebase main # Rebase onto main
# Remote Branches
git fetch # Fetch remote changes
git pull # Fetch and merge
git push origin main # Push to remote
git push -u origin feature # Push new branch
git push --delete origin feature # Delete remote branchStashing, cherry-picking, and history rewriting
git# Stashing Changes
git stash # Stash changes
git stash list # List stashes
git stash apply # Apply latest stash
git stash pop # Apply and remove
git stash drop # Delete stash
# Undoing Changes
git reset HEAD file.txt # Unstage file
git checkout -- file.txt # Discard changes
git reset --soft HEAD~1 # Undo commit, keep changes
git reset --hard HEAD~1 # Undo commit, discard changes
git revert commit_hash # Create reverse commit
# Cherry-picking & Tags
git cherry-pick commit_hash # Apply specific commit
git tag v1.0.0 # Create tag
git tag -a v1.0.0 -m "message" # Annotated tag
git push origin v1.0.0 # Push tagEssential Linux commands for file management
linux# Navigation & Listing
ls # List files
ls -la # Detailed list with hidden
pwd # Current directory
cd /path/to/dir # Change directory
cd .. # Parent directory
# File Operations
touch file.txt # Create file
mkdir directory # Create directory
mkdir -p path/to/dir # Create nested directories
cp source dest # Copy file
cp -r source dest # Copy directory
mv source dest # Move/rename
rm file.txt # Remove file
rm -rf directory # Remove directory recursively
# File Viewing
cat file.txt # Display content
less file.txt # Paginated view
head -n 10 file.txt # First 10 lines
tail -n 10 file.txt # Last 10 lines
tail -f file.txt # Follow file updatesUser management and file permissions
linuxPermissions: Read (r=4), Write (w=2), Execute (x=1)
Format: Owner-Group-Others (e.g., 755 = rwxr-xr-x)
# File Permissions
chmod 755 file.txt # Change permissions
chmod +x script.sh # Add execute permission
chmod -R 644 directory # Recursive
chown user:group file.txt # Change ownership
chown -R user:group directory # Recursive ownership# User Management
sudo command # Run as superuser
sudo su # Switch to root
useradd username # Create user
usermod -aG group user # Add to group
passwd username # Change password
id # User and group info
whoami # Current user
groups # User groupsManaging processes and system resources
linux# Process Management
ps aux # List all processes
ps aux | grep nginx # Find specific process
top # Real-time process view
htop # Enhanced process viewer
kill PID # Kill process
kill -9 PID # Force kill
killall processname # Kill by name
bg # Background process
fg # Foreground process
# System Information
df -h # Disk usage
du -sh directory # Directory size
free -h # Memory usage
uptime # System uptime
uname -a # System information
lscpu # CPU informationNetwork commands and text manipulation
linux# Networking
ping google.com # Test connectivity
curl https://api.example.com # HTTP request
wget https://file.com/file.zip # Download file
netstat -tulpn # Network connections
ss -tulpn # Socket statistics
ifconfig # Network interfaces
ip addr show # IP addresses
# Text Processing
grep "pattern" file.txt # Search in file
grep -r "pattern" directory # Recursive search
find /path -name "*.txt" # Find files
sed 's/old/new/g' file.txt # Replace text
awk '{print $1}' file.txt # Print first column
sort file.txt # Sort lines
uniq file.txt # Remove duplicates
wc -l file.txt # Count linesJenkins pipeline syntax and configuration
cicdPipeline: Automated build, test, and deploy workflow
Declarative: Simplified, opinionated syntax
Scripted: Groovy-based, more flexible
// Declarative Pipeline (Jenkinsfile)
pipeline {
agent any
environment {
DOCKER_REGISTRY = 'registry.example.com'
}
stages {
stage('Build') {
steps {
sh 'npm install'
sh 'npm run build'
}
}
stage('Test') {
steps {
sh 'npm test'
}
}
stage('Docker Build') {
steps {
sh 'docker build -t myapp:${BUILD_NUMBER} .'
}
}
stage('Deploy') {
steps {
sh 'kubectl apply -f k8s/'
}
}
}
post {
success {
echo 'Pipeline succeeded!'
}
failure {
echo 'Pipeline failed!'
}
}
}GitHub Actions workflow automation
cicd# .github/workflows/ci-cd.yml
name: CI/CD Pipeline
on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main ]
env:
NODE_VERSION: '16'
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: ${{ env.NODE_VERSION }}
- name: Install dependencies
run: npm ci
- name: Run tests
run: npm test
- name: Build application
run: npm run build
- name: Build Docker image
run: docker build -t myapp:${{ github.sha }} .
- name: Push to registry
run: |
docker login -u ${{ secrets.DOCKER_USER }} -p ${{ secrets.DOCKER_PASSWORD }}
docker push myapp:${{ github.sha }}Terraform basics for infrastructure provisioning
iacTerraform: Infrastructure as Code tool for building, changing, and versioning infrastructure
HCL: HashiCorp Configuration Language for defining resources
# Terraform Commands
terraform init # Initialize working directory
terraform plan # Preview changes
terraform apply # Apply changes
terraform apply -auto-approve # Skip confirmation
terraform destroy # Destroy infrastructure
terraform fmt # Format configuration
terraform validate # Validate syntax
terraform state list # List resources
terraform output # Show outputs# Sample Terraform Configuration (main.tf)
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.0"
}
}
}
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Name = "WebServer"
Environment = "Production"
}
}
output "instance_ip" {
value = aws_instance.web.public_ip
}Ansible playbooks for automation
iacAnsible: Agentless automation tool for configuration management
Playbook: YAML file defining automation tasks
Inventory: List of managed hosts
# Ansible Commands
ansible all -m ping # Test connectivity
ansible all -m shell -a "uptime" # Run command
ansible-playbook playbook.yml # Run playbook
ansible-playbook playbook.yml --check # Dry run
ansible-playbook playbook.yml -v # Verbose
ansible-inventory --list # List inventory# Sample Ansible Playbook
---
- name: Deploy Web Application
hosts: webservers
become: yes
vars:
app_port: 3000
tasks:
- name: Install Node.js
apt:
name: nodejs
state: present
update_cache: yes
- name: Copy application files
copy:
src: ./app/
dest: /opt/app/
- name: Install dependencies
npm:
path: /opt/app
- name: Start application
systemd:
name: myapp
state: started
enabled: yesAWS CLI commands for common services
cloud# AWS CLI Configuration
aws configure # Setup credentials
aws configure list # View configuration
# EC2 Operations
aws ec2 describe-instances # List instances
aws ec2 start-instances --instance-ids i-1234567890
aws ec2 stop-instances --instance-ids i-1234567890
aws ec2 terminate-instances --instance-ids i-1234567890
# S3 Operations
aws s3 ls # List buckets
aws s3 ls s3://bucket-name # List objects
aws s3 cp file.txt s3://bucket/ # Upload file
aws s3 cp s3://bucket/file.txt . # Download file
aws s3 sync ./dir s3://bucket/ # Sync directory
# IAM Operations
aws iam list-users # List users
aws iam list-roles # List roles
aws iam create-user --user-name newuserMonitoring and alerting setup
monitoringPrometheus: Time-series database for metrics collection
Grafana: Visualization and analytics platform
PromQL: Query language for Prometheus
# Sample Prometheus Configuration (prometheus.yml)
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: 'node-exporter'
static_configs:
- targets: ['localhost:9100']
- job_name: 'kubernetes-pods'
kubernetes_sd_configs:
- role: pod# Common PromQL Queries
# CPU Usage
rate(node_cpu_seconds_total[5m])
# Memory Usage
node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes
# HTTP Request Rate
rate(http_requests_total[5m])
# HTTP Error Rate
rate(http_requests_total{status=~"5.."}[5m])Security scanning and hardening
securitySecurity Scanning: Identify vulnerabilities in code and containers
Secrets Management: Never commit secrets to Git
Least Privilege: Minimum necessary permissions
# Docker Security Scanning
docker scan image:tag # Scan for vulnerabilities
trivy image nginx:latest # Trivy scanner
# Container Security
docker run --read-only nginx # Read-only filesystem
docker run --cap-drop ALL nginx # Drop capabilities
docker run --security-opt no-new-privileges nginx# Kubernetes Security
# Security Context
securityContext:
runAsNonRoot: true
runAsUser: 1000
readOnlyRootFilesystem: true
capabilities:
drop:
- ALL
# Network Policies
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: deny-all
spec:
podSelector: {}
policyTypes:
- Ingress
- Egress