|
| 1 | +## 🚀 **16. Hands-on Projects (Python for DevOps)** |
| 2 | + |
| 3 | +These projects combine all the concepts learned throughout the course — from Python fundamentals to DevOps-specific automation. |
| 4 | +Each project focuses on **real-world DevOps automation**, helping you practice working with AWS, Docker, Kubernetes, monitoring, and CI/CD systems. |
| 5 | + |
| 6 | +--- |
| 7 | + |
| 8 | +### ✅ **1. Automate AWS EC2 Instance Start/Stop using boto3** |
| 9 | + |
| 10 | +**Goal:** |
| 11 | +Automatically start or stop EC2 instances based on time or load conditions. |
| 12 | + |
| 13 | +**Skills Covered:** |
| 14 | +AWS automation, boto3, IAM permissions, scheduling. |
| 15 | + |
| 16 | +**Example Code:** |
| 17 | + |
| 18 | +```python |
| 19 | +import boto3 |
| 20 | + |
| 21 | +ec2 = boto3.client('ec2') |
| 22 | + |
| 23 | +def start_instances(): |
| 24 | + ec2.start_instances(InstanceIds=['i-0abcd1234ef567890']) |
| 25 | + print("✅ EC2 instance started") |
| 26 | + |
| 27 | +def stop_instances(): |
| 28 | + ec2.stop_instances(InstanceIds=['i-0abcd1234ef567890']) |
| 29 | + print("🛑 EC2 instance stopped") |
| 30 | + |
| 31 | +# Call the function manually or via cron/scheduler |
| 32 | +start_instances() |
| 33 | +``` |
| 34 | + |
| 35 | +--- |
| 36 | + |
| 37 | +### ✅ **2. Log File Analyzer Script** |
| 38 | + |
| 39 | +**Goal:** |
| 40 | +Parse and analyze log files (e.g., `/var/log/syslog`, Nginx logs) to extract errors, warnings, and statistics. |
| 41 | + |
| 42 | +**Skills Covered:** |
| 43 | +File handling, regex (`re`), automation, reporting. |
| 44 | + |
| 45 | +**Example Code:** |
| 46 | + |
| 47 | +```python |
| 48 | +import re |
| 49 | + |
| 50 | +error_count = 0 |
| 51 | +with open("/var/log/syslog", "r") as log: |
| 52 | + for line in log: |
| 53 | + if re.search(r"error|failed|critical", line, re.IGNORECASE): |
| 54 | + error_count += 1 |
| 55 | + |
| 56 | +print(f"⚠️ Total errors found: {error_count}") |
| 57 | +``` |
| 58 | + |
| 59 | +--- |
| 60 | + |
| 61 | +### ✅ **3. Automated Backup using Python** |
| 62 | + |
| 63 | +**Goal:** |
| 64 | +Backup important files or directories to a remote server or S3 bucket automatically. |
| 65 | + |
| 66 | +**Skills Covered:** |
| 67 | +File handling, `shutil`, `boto3`, `paramiko`. |
| 68 | + |
| 69 | +**Example Code:** |
| 70 | + |
| 71 | +```python |
| 72 | +import shutil, datetime, boto3 |
| 73 | + |
| 74 | +# Create local backup |
| 75 | +backup_file = f"/tmp/backup_{datetime.date.today()}.zip" |
| 76 | +shutil.make_archive("/tmp/backup", "zip", "/etc") |
| 77 | + |
| 78 | +# Upload to S3 |
| 79 | +s3 = boto3.client('s3') |
| 80 | +s3.upload_file(backup_file, "devops-backup-bucket", "backup.zip") |
| 81 | +print("✅ Backup completed and uploaded to S3.") |
| 82 | +``` |
| 83 | + |
| 84 | +--- |
| 85 | + |
| 86 | +### ✅ **4. CI/CD Pipeline Trigger via Python Script** |
| 87 | + |
| 88 | +**Goal:** |
| 89 | +Trigger Jenkins or GitLab CI/CD pipelines programmatically using REST APIs. |
| 90 | + |
| 91 | +**Skills Covered:** |
| 92 | +REST API, `requests`, authentication, DevOps integration. |
| 93 | + |
| 94 | +**Example Code (Jenkins Example):** |
| 95 | + |
| 96 | +```python |
| 97 | +import requests |
| 98 | + |
| 99 | +jenkins_url = "http://jenkins-server:8080/job/deploy/build" |
| 100 | +response = requests.post(jenkins_url, auth=('admin', 'password')) |
| 101 | +print("✅ CI/CD Pipeline Triggered:", response.status_code) |
| 102 | +``` |
| 103 | + |
| 104 | +--- |
| 105 | + |
| 106 | +### ✅ **5. Docker Image Cleaner Automation** |
| 107 | + |
| 108 | +**Goal:** |
| 109 | +Automatically delete old or unused Docker images to save disk space. |
| 110 | + |
| 111 | +**Skills Covered:** |
| 112 | +Docker SDK, automation, system monitoring. |
| 113 | + |
| 114 | +**Example Code:** |
| 115 | + |
| 116 | +```python |
| 117 | +import docker |
| 118 | + |
| 119 | +client = docker.from_env() |
| 120 | +for image in client.images.list(): |
| 121 | + if "<none>" in image.tags: |
| 122 | + client.images.remove(image.id, force=True) |
| 123 | + print(f"🧹 Removed dangling image: {image.id}") |
| 124 | +``` |
| 125 | + |
| 126 | +--- |
| 127 | + |
| 128 | +### ✅ **6. Kubernetes Pod Status Checker** |
| 129 | + |
| 130 | +**Goal:** |
| 131 | +Monitor the status of pods in a Kubernetes cluster and report failed pods. |
| 132 | + |
| 133 | +**Skills Covered:** |
| 134 | +Kubernetes Python client, automation, alerting. |
| 135 | + |
| 136 | +**Example Code:** |
| 137 | + |
| 138 | +```python |
| 139 | +from kubernetes import client, config |
| 140 | + |
| 141 | +config.load_kube_config() |
| 142 | +v1 = client.CoreV1Api() |
| 143 | +pods = v1.list_pod_for_all_namespaces() |
| 144 | + |
| 145 | +for pod in pods.items: |
| 146 | + if pod.status.phase != "Running": |
| 147 | + print(f"⚠️ Pod {pod.metadata.name} in {pod.metadata.namespace} is {pod.status.phase}") |
| 148 | +``` |
| 149 | + |
| 150 | +--- |
| 151 | + |
| 152 | +### ✅ **7. Server Health Monitoring Tool** |
| 153 | + |
| 154 | +**Goal:** |
| 155 | +Monitor CPU, memory, and disk usage, and trigger alerts if thresholds are crossed. |
| 156 | + |
| 157 | +**Skills Covered:** |
| 158 | +`psutil`, alerting, system monitoring, automation. |
| 159 | + |
| 160 | +**Example Code:** |
| 161 | + |
| 162 | +```python |
| 163 | +import psutil, smtplib |
| 164 | + |
| 165 | +cpu = psutil.cpu_percent() |
| 166 | +memory = psutil.virtual_memory().percent |
| 167 | + |
| 168 | +if cpu > 80 or memory > 80: |
| 169 | + print("⚠️ High usage detected!") |
| 170 | +else: |
| 171 | + print(f"✅ CPU: {cpu}% | Memory: {memory}%") |
| 172 | +``` |
| 173 | + |
| 174 | +--- |
| 175 | + |
| 176 | +## 🧠 **Learning Outcomes** |
| 177 | + |
| 178 | +By completing these projects, you’ll gain hands-on experience in: |
| 179 | + |
| 180 | +* Automating cloud (AWS) operations |
| 181 | +* Managing infrastructure with Docker & Kubernetes |
| 182 | +* Integrating Python with DevOps CI/CD pipelines |
| 183 | +* Building monitoring and backup scripts |
| 184 | +* Applying automation in real-world DevOps workflows |
| 185 | + |
| 186 | +--- |
0 commit comments