<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Daniel Macdonald on Medium]]></title>
        <description><![CDATA[Stories by Daniel Macdonald on Medium]]></description>
        <link>https://medium.com/@daniel_macdonald?source=rss-797248f9bb88------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*nDKxq3sG6HTHRD3y-hjFsw.jpeg</url>
            <title>Stories by Daniel Macdonald on Medium</title>
            <link>https://medium.com/@daniel_macdonald?source=rss-797248f9bb88------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 01:32:41 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@daniel_macdonald/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Automated Docker Deployment Script — Flask Web Application.]]></title>
            <link>https://medium.com/@daniel_macdonald/automated-docker-deployment-script-flask-web-application-9efd68446114?source=rss-797248f9bb88------2</link>
            <guid isPermaLink="false">https://medium.com/p/9efd68446114</guid>
            <dc:creator><![CDATA[Daniel Macdonald]]></dc:creator>
            <pubDate>Tue, 09 Dec 2025 16:04:07 GMT</pubDate>
            <atom:updated>2025-12-09T20:35:01.463Z</atom:updated>
            <content:encoded><![CDATA[<h3>Automated Docker Deployment Script — Flask Web Application.</h3><p>How I built a production-grade POSIX-compliant deployment automation workflow.</p><p>Introduction: <em>This is a robust bash script used for the end-to-end deployment of a Dockerized application on a remote Linux server using SSH. This bash script handles everything from cloning your git repository, setting up Docker, Docker Compose, and Nginx reverse proxy for a production-ready deployment.</em></p><p>Modern DevOps emphasizes:</p><ul><li><strong>Consistency:</strong> same process everywhere</li><li><strong>Idempotency:</strong> running deployment twice should give same result</li><li><strong>Security:</strong> no hard-coded secrets</li><li><strong>Recoverability:</strong> clear logs and predictable behaviors</li><li><strong>Speed:</strong> one command to deploy the entire stack</li></ul><p>Manual steps introduce human error. Automated deployments eliminate it.</p><p>This script gives you:</p><ul><li>End-to-end automated Docker deployment</li><li>Automated Git pull/clone</li><li>Automated remote setup via SSH</li><li>Automated Docker build and compose-up</li><li>Optional cleanup (--cleanup)</li><li>Full logging with timestamps</li><li>Safe exit codes and signal handling</li><li>POSIX compliance (sh, dash, bash)</li></ul><p>The Deployment Script:</p><pre>#!/bin/sh<br><br>################################################################################<br># deploy.sh - Production-Grade Dockerized Application Deployment Script<br>################################################################################<br># Author: Macdonald Daniel<br># Version: 1.0.0<br># Description: Automates setup, deployment, and configuration of Dockerized<br>#              applications on remote Linux servers with full POSIX compliance<br>#<br># Usage:<br>#   ./deploy.sh           # Normal deployment<br>#   ./deploy.sh --cleanup # Remove all deployed resources<br>#   ./deploy.sh --help    # Show usage information<br>#<br># Requirements:<br>#   - POSIX-compliant shell (sh, dash, bash, etc.)<br>#   - git, ssh, scp/rsync, curl<br>#   - SSH key-based authentication to remote server<br>#<br># Exit Codes:<br>#   0  - Success<br>#   1  - General error<br>#   2  - Input validation failed<br>#   3  - SSH connection failed<br>#   4  - Deployment failed<br>#   5  - Configuration error<br>#   130 - Interrupted by user (Ctrl+C)<br>################################################################################<br><br># ============================================================================<br># STRICT MODE AND SAFETY SETTINGS<br># ============================================================================<br># Exit on error, treat unset variables as errors<br>set -e<br>set -u<br><br># Set Internal Field Separator to newline and tab only<br># This prevents word splitting issues with filenames containing spaces<br>IFS=&#39;<br> &#39;<br><br># ============================================================================<br># GLOBAL CONSTANTS AND CONFIGURATION<br># ============================================================================<br><br># Script metadata<br>readonly SCRIPT_NAME=&quot;$(basename &quot;$0&quot;)&quot;<br>readonly SCRIPT_VERSION=&quot;1.0.0&quot;<br>readonly SCRIPT_DIR=&quot;$(CDPATH=&#39;&#39; cd -- &quot;$(dirname -- &quot;$0&quot;)&quot; &amp;&amp; pwd)&quot;<br><br># Exit codes (semantic error codes for better debugging)<br>readonly EXIT_SUCCESS=0<br>readonly EXIT_GENERAL_ERROR=1<br>readonly EXIT_VALIDATION_ERROR=2<br>readonly EXIT_SSH_ERROR=3<br>readonly EXIT_DEPLOYMENT_ERROR=4<br>readonly EXIT_CONFIG_ERROR=5<br>readonly EXIT_INTERRUPTED=130<br><br># Logging configuration<br>readonly TIMESTAMP=&quot;$(date +%Y%m%d_%H%M%S)&quot;<br>readonly LOG_FILE=&quot;${SCRIPT_DIR}/deploy_${TIMESTAMP}.log&quot;<br><br># Color codes for terminal output (POSIX-compliant using printf)<br>readonly COLOR_RED=&quot;$(printf &#39;\033[0;31m&#39;)&quot;<br>readonly COLOR_GREEN=&quot;$(printf &#39;\033[0;32m&#39;)&quot;<br>readonly COLOR_YELLOW=&quot;$(printf &#39;\033[1;33m&#39;)&quot;<br>readonly COLOR_BLUE=&quot;$(printf &#39;\033[0;34m&#39;)&quot;<br>readonly COLOR_CYAN=&quot;$(printf &#39;\033[0;36m&#39;)&quot;<br>readonly COLOR_RESET=&quot;$(printf &#39;\033[0m&#39;)&quot;<br><br># Emoji/symbols for output (use ASCII fallbacks if terminal doesn&#39;t support)<br>readonly SYMBOL_SUCCESS=&quot;✅&quot;<br>readonly SYMBOL_ERROR=&quot;❌&quot;<br>readonly SYMBOL_WARNING=&quot;⚠️&quot;<br>readonly SYMBOL_INFO=&quot;ℹ️&quot;<br>readonly SYMBOL_ROCKET=&quot;🚀&quot;<br>readonly SYMBOL_PACKAGE=&quot;📦&quot;<br>readonly SYMBOL_NETWORK=&quot;🌐&quot;<br>readonly SYMBOL_FOLDER=&quot;📁&quot;<br>readonly SYMBOL_WRENCH=&quot;🔧&quot;<br>readonly SYMBOL_CHECK=&quot;🩺&quot;<br>readonly SYMBOL_CELEBRATE=&quot;🎉&quot;<br><br># Deployment configuration (populated from user input)<br>GIT_REPO=&quot;&quot;<br>GIT_PAT=&quot;&quot;<br>GIT_BRANCH=&quot;main&quot;<br>SSH_USER=&quot;&quot;<br>SERVER_IP=&quot;&quot;<br>SSH_KEY_PATH=&quot;&quot;<br>APP_PORT=&quot;&quot;<br>CLEANUP_MODE=0<br><br># Derived paths<br>LOCAL_CLONE_DIR=&quot;&quot;<br>REMOTE_APP_DIR=&quot;/opt/app_deploy&quot;<br>NGINX_CONFIG_PATH=&quot;/etc/nginx/sites-available/app.conf&quot;<br>NGINX_ENABLED_PATH=&quot;/etc/nginx/sites-enabled/app.conf&quot;<br><br># ============================================================================<br># LOGGING AND OUTPUT FUNCTIONS<br># ============================================================================<br><br># Initialize logging system<br># Creates log file and writes header information<br>initialize_logging() {<br>    # Create log file with header<br>    {<br>        printf &quot;================================================================================\n&quot;<br>        printf &quot;Deployment Script Log\n&quot;<br>        printf &quot;================================================================================\n&quot;<br>        printf &quot;Script Version: %s\n&quot; &quot;$SCRIPT_VERSION&quot;<br>        printf &quot;Execution Date: %s\n&quot; &quot;$(date &#39;+%Y-%m-%d %H:%M:%S&#39;)&quot;<br>        printf &quot;Log File: %s\n&quot; &quot;$LOG_FILE&quot;<br>        printf &quot;================================================================================\n\n&quot;<br>    } &gt; &quot;$LOG_FILE&quot;<br>    <br>    # Display log location to user<br>    printf &quot;%s Log file: %s%s%s\n&quot; &quot;$SYMBOL_INFO&quot; &quot;$COLOR_CYAN&quot; &quot;$LOG_FILE&quot; &quot;$COLOR_RESET&quot;<br>    printf &quot;================================================================================\n\n&quot;<br>}<br><br># Logging function that writes to both console and log file<br># Arguments:<br>#   $1 - Log level (INFO|SUCCESS|WARN|ERROR)<br>#   $2+ - Message to log<br># Usage:<br>#   log &quot;INFO&quot; &quot;Starting deployment&quot;<br>#   log &quot;ERROR&quot; &quot;Failed to connect&quot;<br>log() {<br>    level=&quot;$1&quot;<br>    shift<br>    message=&quot;$*&quot;<br>    timestamp=&quot;$(date &#39;+%Y-%m-%d %H:%M:%S&#39;)&quot;<br>    <br>    # Write to log file with timestamp<br>    printf &quot;[%s] [%s] %s\n&quot; &quot;$timestamp&quot; &quot;$level&quot; &quot;$message&quot; &gt;&gt; &quot;$LOG_FILE&quot;<br>    <br>    # Display to console with colors and symbols<br>    case &quot;$level&quot; in<br>        INFO)<br>            printf &quot;%s%s[INFO]%s %s\n&quot; &quot;$COLOR_BLUE&quot; &quot;$SYMBOL_INFO&quot; &quot;$COLOR_RESET&quot; &quot;$message&quot;<br>            ;;<br>        SUCCESS)<br>            printf &quot;%s%s[SUCCESS]%s %s\n&quot; &quot;$COLOR_GREEN&quot; &quot;$SYMBOL_SUCCESS&quot; &quot;$COLOR_RESET&quot; &quot;$message&quot;<br>            ;;<br>        WARN)<br>            printf &quot;%s%s[WARN]%s %s\n&quot; &quot;$COLOR_YELLOW&quot; &quot;$SYMBOL_WARNING&quot; &quot;$COLOR_RESET&quot; &quot;$message&quot;<br>            ;;<br>        ERROR)<br>            printf &quot;%s%s[ERROR]%s %s\n&quot; &quot;$COLOR_RED&quot; &quot;$SYMBOL_ERROR&quot; &quot;$COLOR_RESET&quot; &quot;$message&quot;<br>            ;;<br>        *)<br>            printf &quot;[%s] %s\n&quot; &quot;$level&quot; &quot;$message&quot;<br>            ;;<br>    esac<br>}<br><br># Print section header for better visual organization<br># Arguments:<br>#   $1 - Section title<br>print_section_header() {<br>    title=&quot;$1&quot;<br>    printf &quot;\n&quot;<br>    printf &quot;================================================================================\n&quot;<br>    printf &quot;%s %s\n&quot; &quot;$SYMBOL_ROCKET&quot; &quot;$title&quot;<br>    printf &quot;================================================================================\n&quot;<br>    log &quot;INFO&quot; &quot;=== $title ===&quot;<br>}<br><br># ============================================================================<br># ERROR HANDLING AND CLEANUP<br># ============================================================================<br><br># Error handler called when script exits with error<br># Arguments:<br>#   $1 - Exit code<br>error_handler() {<br>    exit_code=&quot;$1&quot;<br>    <br>    if [ &quot;$exit_code&quot; -ne 0 ]; then<br>        log &quot;ERROR&quot; &quot;Script failed with exit code: $exit_code&quot;<br>        printf &quot;\n%s Deployment failed. Check log file for details: %s\n&quot; &quot;$SYMBOL_ERROR&quot; &quot;$LOG_FILE&quot;<br>        printf &quot;================================================================================\n&quot;<br>    fi<br>}<br><br># Handle interrupt signals (Ctrl+C)<br>interrupt_handler() {<br>    log &quot;WARN&quot; &quot;Deployment interrupted by user&quot;<br>    printf &quot;\n%s Deployment cancelled by user\n&quot; &quot;$SYMBOL_WARNING&quot;<br>    exit $EXIT_INTERRUPTED<br>}<br><br># Cleanup function for --cleanup flag<br># Removes all deployed resources from remote server<br>cleanup_deployment() {<br>    print_section_header &quot;Cleanup Mode - Removing Deployed Resources&quot;<br>    <br>    # Collect SSH information if not already set<br>    if [ -z &quot;${SSH_USER:-}&quot; ]; then<br>        printf &quot;Enter SSH username: &quot;<br>        read -r SSH_USER<br>    fi<br>    <br>    if [ -z &quot;${SERVER_IP:-}&quot; ]; then<br>        printf &quot;Enter server IP address: &quot;<br>        read -r SERVER_IP<br>    fi<br>    <br>    if [ -z &quot;${SSH_KEY_PATH:-}&quot; ]; then<br>        printf &quot;Enter SSH key path: &quot;<br>        read -r SSH_KEY_PATH<br>    fi<br>    <br>    # Validate SSH key exists<br>    if [ ! -f &quot;$SSH_KEY_PATH&quot; ]; then<br>        log &quot;ERROR&quot; &quot;SSH key not found: $SSH_KEY_PATH&quot;<br>        exit $EXIT_VALIDATION_ERROR<br>    fi<br>    <br>    log &quot;INFO&quot; &quot;Connecting to remote server: $SERVER_IP&quot;<br>    <br>    # Execute cleanup commands on remote server<br>    ssh -i &quot;$SSH_KEY_PATH&quot; -o BatchMode=yes -o StrictHostKeyChecking=accept-new \<br>        &quot;$SSH_USER@$SERVER_IP&quot; sh &lt;&lt;&#39;CLEANUP_EOF&#39;<br>        <br>        echo &quot;Stopping and removing containers...&quot;<br>        <br>        # Stop docker-compose managed containers<br>        if [ -f &quot;$HOME/app_deploy/docker-compose.yml&quot; ]; then<br>            cd &quot;$HOME/app_deploy&quot;<br>            sudo docker-compose down -v 2&gt;/dev/null || true<br>        fi<br>        <br>        # Stop standalone containers<br>        container_ids=$(sudo docker ps -aq 2&gt;/dev/null || true)<br>        if [ -n &quot;$container_ids&quot; ]; then<br>            printf &quot;%s&quot; &quot;$container_ids&quot; | xargs sudo docker stop 2&gt;/dev/null || true<br>            printf &quot;%s&quot; &quot;$container_ids&quot; | xargs sudo docker rm 2&gt;/dev/null || true<br>        fi<br>        <br>        # Prune unused resources<br>        sudo docker container prune -f 2&gt;/dev/null || true<br>        sudo docker network prune -f 2&gt;/dev/null || true<br>        sudo docker volume prune -f 2&gt;/dev/null || true<br>        <br>        # Remove deployment directory<br>        sudo rm -rf &quot;$HOME/app_deploy&quot; /opt/app_deploy 2&gt;/dev/null || true<br>        <br>        # Remove Nginx configuration<br>        sudo rm -f /etc/nginx/sites-available/app.conf 2&gt;/dev/null || true<br>        sudo rm -f /etc/nginx/sites-enabled/app.conf 2&gt;/dev/null || true<br>        <br>        # Reload Nginx<br>        if sudo systemctl is-active --quiet nginx 2&gt;/dev/null; then<br>            sudo systemctl reload nginx 2&gt;/dev/null || true<br>        fi<br>        <br>        echo &quot;Cleanup completed successfully&quot;<br>CLEANUP_EOF<br>    <br>    log &quot;SUCCESS&quot; &quot;Cleanup completed successfully&quot;<br>    exit $EXIT_SUCCESS<br>}<br><br># Set up signal traps<br># These ensure proper cleanup and error reporting<br>trap &#39;saved_exit=$?; error_handler $saved_exit&#39; EXIT<br>trap &#39;interrupt_handler&#39; INT TERM<br><br># ============================================================================<br># INPUT VALIDATION FUNCTIONS<br># ============================================================================<br><br># Validate that input is not empty<br># Arguments:<br>#   $1 - Field name (for error messages)<br>#   $2 - Value to validate<br># Returns:<br>#   0 if valid, exits script if invalid<br>validate_not_empty() {<br>    field_name=&quot;$1&quot;<br>    value=&quot;$2&quot;<br>    <br>    if [ -z &quot;$value&quot; ]; then<br>        log &quot;ERROR&quot; &quot;$field_name cannot be empty&quot;<br>        exit $EXIT_VALIDATION_ERROR<br>    fi<br>}<br><br># Validate URL format (basic check for http(s):// or git@)<br># Arguments:<br>#   $1 - URL to validate<br># Returns:<br>#   0 if valid, 1 if invalid<br>validate_url() {<br>    url=&quot;$1&quot;<br>    <br>    case &quot;$url&quot; in<br>        https://*|http://*|git@*|ssh://*)<br>            return 0<br>            ;;<br>        *)<br>            return 1<br>            ;;<br>    esac<br>}<br><br># Validate IP address format<br># Arguments:<br>#   $1 - IP address to validate<br># Returns:<br>#   0 if valid, 1 if invalid<br>validate_ip() {<br>    ip=&quot;$1&quot;<br>    <br>    # Check for invalid characters<br>    case &quot;$ip&quot; in<br>        *[!0-9.]*)<br>            return 1<br>            ;;<br>    esac<br>    <br>    # Split IP into octets and validate each<br>    octet_count=0<br>    old_ifs=&quot;$IFS&quot;<br>    IFS=&#39;.&#39;<br>    <br>    for octet in $ip; do<br>        octet_count=$((octet_count + 1))<br>        <br>        # Check if octet is a valid number<br>        case &quot;$octet&quot; in<br>            &#39;&#39;|*[!0-9]*)<br>                IFS=&quot;$old_ifs&quot;<br>                return 1<br>                ;;<br>        esac<br>        <br>        # Check octet range (0-255)<br>        if [ &quot;$octet&quot; -lt 0 ] || [ &quot;$octet&quot; -gt 255 ] 2&gt;/dev/null; then<br>            IFS=&quot;$old_ifs&quot;<br>            return 1<br>        fi<br>    done<br>    <br>    IFS=&quot;$old_ifs&quot;<br>    <br>    # IP must have exactly 4 octets<br>    if [ &quot;$octet_count&quot; -ne 4 ]; then<br>        return 1<br>    fi<br>    <br>    return 0<br>}<br><br># Validate port number (1-65535)<br># Arguments:<br>#   $1 - Port number to validate<br># Returns:<br>#   0 if valid, 1 if invalid<br>validate_port() {<br>    port=&quot;$1&quot;<br>    <br>    # Check if it&#39;s a number<br>    case &quot;$port&quot; in<br>        &#39;&#39;|*[!0-9]*)<br>            return 1<br>            ;;<br>    esac<br>    <br>    # Check port range<br>    if [ &quot;$port&quot; -ge 1 ] &amp;&amp; [ &quot;$port&quot; -le 65535 ]; then<br>        return 0<br>    else<br>        return 1<br>    fi<br>}<br><br># Validate file exists and is readable<br># Arguments:<br>#   $1 - File path to validate<br># Returns:<br>#   0 if valid, 1 if invalid<br>validate_file() {<br>    file=&quot;$1&quot;<br>    <br>    if [ -f &quot;$file&quot; ] &amp;&amp; [ -r &quot;$file&quot; ]; then<br>        return 0<br>    else<br>        return 1<br>    fi<br>}<br><br># ============================================================================<br># USER INPUT COLLECTION<br># ============================================================================<br><br># Collect all deployment parameters from user with validation<br>collect_deployment_parameters() {<br>    print_section_header &quot;Collecting Deployment Parameters&quot;<br>    <br>    # Git Repository URL<br>    while true; do<br>        printf &quot;Enter Git Repository URL: &quot;<br>        read -r GIT_REPO<br>        <br>        if validate_url &quot;$GIT_REPO&quot;; then<br>            log &quot;SUCCESS&quot; &quot;Repository URL validated: $GIT_REPO&quot;<br>            break<br>        else<br>            log &quot;ERROR&quot; &quot;Invalid URL format. Use https://, http://, git@, or ssh://&quot;<br>        fi<br>    done<br>    <br>    # Personal Access Token (optional for public repos)<br>    printf &quot;Enter Personal Access Token (press Enter if public repo): &quot;<br>    stty -echo 2&gt;/dev/null  # Hide input<br>    read -r GIT_PAT<br>    stty echo 2&gt;/dev/null   # Restore echo<br>    printf &quot;\n&quot;<br>    <br>    if [ -n &quot;$GIT_PAT&quot; ]; then<br>        log &quot;INFO&quot; &quot;PAT configured (hidden for security)&quot;<br>    else<br>        log &quot;INFO&quot; &quot;No PAT provided (assuming public repository)&quot;<br>    fi<br>    <br>    # Branch name with default<br>    printf &quot;Enter branch name [default: main]: &quot;<br>    read -r input_branch<br>    GIT_BRANCH=&quot;${input_branch:-main}&quot;<br>    log &quot;SUCCESS&quot; &quot;Branch set to: $GIT_BRANCH&quot;<br>    <br>    # SSH Username<br>    while true; do<br>        printf &quot;Enter SSH username: &quot;<br>        read -r SSH_USER<br>        <br>        if [ -n &quot;$SSH_USER&quot; ]; then<br>            log &quot;SUCCESS&quot; &quot;SSH username: $SSH_USER&quot;<br>            break<br>        else<br>            log &quot;ERROR&quot; &quot;Username cannot be empty&quot;<br>        fi<br>    done<br>    <br>    # Server IP Address<br>    while true; do<br>        printf &quot;Enter server IP address: &quot;<br>        read -r SERVER_IP<br>        <br>        if validate_ip &quot;$SERVER_IP&quot;; then<br>            log &quot;SUCCESS&quot; &quot;Server IP validated: $SERVER_IP&quot;<br>            break<br>        else<br>            log &quot;ERROR&quot; &quot;Invalid IP address format (expected: xxx.xxx.xxx.xxx)&quot;<br>        fi<br>    done<br>    <br>    # SSH Key Path<br>    while true; do<br>        printf &quot;Enter SSH private key path [default: ~/.ssh/id_rsa]: &quot;<br>        read -r input_key<br>        SSH_KEY_PATH=&quot;${input_key:-$HOME/.ssh/id_rsa}&quot;<br>        <br>        # Expand tilde<br>        case &quot;$SSH_KEY_PATH&quot; in<br>            \~/*) SSH_KEY_PATH=&quot;$HOME/${SSH_KEY_PATH#\~/}&quot; ;;<br>        esac<br>        <br>        if validate_file &quot;$SSH_KEY_PATH&quot;; then<br>            log &quot;SUCCESS&quot; &quot;SSH key found: $SSH_KEY_PATH&quot;<br>            break<br>        else<br>            log &quot;ERROR&quot; &quot;SSH key not found or not readable: $SSH_KEY_PATH&quot;<br>        fi<br>    done<br>    <br>    # Application Port<br>    while true; do<br>        printf &quot;Enter application internal port: &quot;<br>        read -r APP_PORT<br>        <br>        if validate_port &quot;$APP_PORT&quot;; then<br>            log &quot;SUCCESS&quot; &quot;Application port: $APP_PORT&quot;<br>            break<br>        else<br>            log &quot;ERROR&quot; &quot;Invalid port (must be 1-65535)&quot;<br>        fi<br>    done<br>    <br>    # Set local clone directory based on repo name<br>    repo_name=&quot;${GIT_REPO##*/}&quot;<br>    repo_name=&quot;${repo_name%.git}&quot;<br>    LOCAL_CLONE_DIR=&quot;${SCRIPT_DIR}/${repo_name}&quot;<br>    <br>    # Display configuration summary<br>    printf &quot;\n&quot;<br>    printf &quot;================================================================================\n&quot;<br>    printf &quot;%s Configuration Summary\n&quot; &quot;$SYMBOL_WRENCH&quot;<br>    printf &quot;================================================================================\n&quot;<br>    printf &quot;Repository:     %s\n&quot; &quot;$GIT_REPO&quot;<br>    printf &quot;Branch:         %s\n&quot; &quot;$GIT_BRANCH&quot;<br>    printf &quot;Server IP:      %s\n&quot; &quot;$SERVER_IP&quot;<br>    printf &quot;SSH User:       %s\n&quot; &quot;$SSH_USER&quot;<br>    printf &quot;SSH Key:        %s\n&quot; &quot;$SSH_KEY_PATH&quot;<br>    printf &quot;App Port:       %s\n&quot; &quot;$APP_PORT&quot;<br>    printf &quot;Local Clone:    %s\n&quot; &quot;$LOCAL_CLONE_DIR&quot;<br>    printf &quot;================================================================================\n&quot;<br>    <br>    # Confirmation prompt<br>    printf &quot;\nProceed with deployment? (y/n): &quot;<br>    read -r confirm<br>    <br>    case &quot;$confirm&quot; in<br>        y|Y|yes|YES)<br>            log &quot;SUCCESS&quot; &quot;User confirmed deployment&quot;<br>            ;;<br>        *)<br>            log &quot;WARN&quot; &quot;Deployment cancelled by user&quot;<br>            exit $EXIT_SUCCESS<br>            ;;<br>    esac<br>}<br><br># ============================================================================<br># GIT OPERATIONS<br># ============================================================================<br><br># Clone repository or update if it already exists (idempotent)<br>clone_or_update_repository() {<br>    print_section_header &quot;Preparing Git Repository&quot;<br>    <br>    # Construct authenticated URL for HTTPS repos<br>    auth_url=&quot;&quot;<br>    case &quot;$GIT_REPO&quot; in<br>        https://*)<br>            if [ -n &quot;$GIT_PAT&quot; ]; then<br>                # Inject PAT into URL for authentication<br>                auth_url=&quot;$(printf &quot;%s&quot; &quot;$GIT_REPO&quot; | sed &quot;s|https://|https://${GIT_PAT}@|&quot;)&quot;<br>                log &quot;INFO&quot; &quot;Using authenticated HTTPS URL&quot;<br>            else<br>                auth_url=&quot;$GIT_REPO&quot;<br>                log &quot;INFO&quot; &quot;Using public HTTPS URL&quot;<br>            fi<br>            ;;<br>        git@*|ssh://*)<br>            auth_url=&quot;$GIT_REPO&quot;<br>            log &quot;INFO&quot; &quot;Using SSH URL&quot;<br>            ;;<br>        *)<br>            log &quot;ERROR&quot; &quot;Unsupported repository URL format&quot;<br>            exit $EXIT_VALIDATION_ERROR<br>            ;;<br>    esac<br>    <br>    # Check if repository already exists locally<br>    if [ -d &quot;$LOCAL_CLONE_DIR&quot; ]; then<br>        log &quot;INFO&quot; &quot;Repository directory exists, updating...&quot;<br>        <br>        cd &quot;$LOCAL_CLONE_DIR&quot; || {<br>            log &quot;ERROR&quot; &quot;Failed to enter repository directory&quot;<br>            exit $EXIT_GENERAL_ERROR<br>        }<br>        <br>        # Check current branch<br>        current_branch=&quot;$(git rev-parse --abbrev-ref HEAD 2&gt;/dev/null)&quot; || current_branch=&quot;&quot;<br>        <br>        if [ &quot;$current_branch&quot; != &quot;$GIT_BRANCH&quot; ]; then<br>            log &quot;INFO&quot; &quot;Switching to branch: $GIT_BRANCH&quot;<br>            git fetch origin &quot;$GIT_BRANCH&quot; &gt;&gt; &quot;$LOG_FILE&quot; 2&gt;&amp;1 || true<br>            git checkout &quot;$GIT_BRANCH&quot; &gt;&gt; &quot;$LOG_FILE&quot; 2&gt;&amp;1 || {<br>                log &quot;ERROR&quot; &quot;Failed to checkout branch: $GIT_BRANCH&quot;<br>                exit $EXIT_GENERAL_ERROR<br>            }<br>        fi<br>        <br>        # Pull latest changes<br>        log &quot;INFO&quot; &quot;Pulling latest changes from origin/$GIT_BRANCH&quot;<br>        if git pull origin &quot;$GIT_BRANCH&quot; &gt;&gt; &quot;$LOG_FILE&quot; 2&gt;&amp;1; then<br>            log &quot;SUCCESS&quot; &quot;Repository updated successfully&quot;<br>        else<br>            log &quot;ERROR&quot; &quot;Failed to pull changes from origin&quot;<br>            exit $EXIT_GENERAL_ERROR<br>        fi<br>    else<br>        # Clone repository for the first time<br>        log &quot;INFO&quot; &quot;Cloning repository...&quot;<br>        <br>        if git clone --branch &quot;$GIT_BRANCH&quot; &quot;$auth_url&quot; &quot;$LOCAL_CLONE_DIR&quot; &gt;&gt; &quot;$LOG_FILE&quot; 2&gt;&amp;1; then<br>            log &quot;SUCCESS&quot; &quot;Repository cloned successfully&quot;<br>        else<br>            log &quot;ERROR&quot; &quot;Failed to clone repository&quot;<br>            exit $EXIT_GENERAL_ERROR<br>        fi<br>        <br>        cd &quot;$LOCAL_CLONE_DIR&quot; || {<br>            log &quot;ERROR&quot; &quot;Failed to enter cloned repository&quot;<br>            exit $EXIT_GENERAL_ERROR<br>        }<br>    fi<br>    <br>    # Clear PAT from memory for security<br>    GIT_PAT=&quot;&quot;<br>    <br>    # Display current commit<br>    current_commit=&quot;$(git rev-parse --short HEAD 2&gt;/dev/null)&quot; || current_commit=&quot;unknown&quot;<br>    log &quot;INFO&quot; &quot;Current commit: $current_commit&quot;<br>}<br><br># Verify Docker configuration files exist<br>verify_docker_configuration() {<br>    print_section_header &quot;Verifying Docker Configuration&quot;<br>    <br>    log &quot;INFO&quot; &quot;Current directory: $(pwd)&quot;<br>    <br>    # Check for Docker configuration files<br>    if [ -f &quot;Dockerfile&quot; ]; then<br>        log &quot;SUCCESS&quot; &quot;Found Dockerfile&quot;<br>        <br>        # Try to detect application type<br>        if grep -qi &quot;node&quot; Dockerfile 2&gt;/dev/null; then<br>            log &quot;INFO&quot; &quot;Detected: Node.js application&quot;<br>        elif grep -qi &quot;python&quot; Dockerfile 2&gt;/dev/null; then<br>            log &quot;INFO&quot; &quot;Detected: Python application&quot;<br>        elif grep -qi &quot;java&quot; Dockerfile 2&gt;/dev/null; then<br>            log &quot;INFO&quot; &quot;Detected: Java application&quot;<br>        else<br>            log &quot;INFO&quot; &quot;Detected: Generic Docker application&quot;<br>        fi<br>    elif [ -f &quot;docker-compose.yml&quot; ] || [ -f &quot;docker-compose.yaml&quot; ]; then<br>        log &quot;SUCCESS&quot; &quot;Found docker-compose.yml&quot;<br>    else<br>        log &quot;ERROR&quot; &quot;No Dockerfile or docker-compose.yml found&quot;<br>        log &quot;ERROR&quot; &quot;Cannot proceed without Docker configuration&quot;<br>        exit $EXIT_VALIDATION_ERROR<br>    fi<br>    <br>    # Display project structure<br>    log &quot;INFO&quot; &quot;Project structure (top 10 items):&quot;<br>    ls -1 | head -n 10 &gt;&gt; &quot;$LOG_FILE&quot;<br>}<br><br># ============================================================================<br># SSH AND NETWORK OPERATIONS<br># ============================================================================<br><br># Test SSH connectivity to remote server<br>test_ssh_connectivity() {<br>    print_section_header &quot;Testing SSH Connectivity&quot;<br>    <br>    # Optional: Test network connectivity with ping (not reliable, some hosts block ICMP)<br>    log &quot;INFO&quot; &quot;Testing network connectivity to $SERVER_IP&quot;<br>    if ping -c 2 -W 5 &quot;$SERVER_IP&quot; &gt;&gt; &quot;$LOG_FILE&quot; 2&gt;&amp;1; then<br>        log &quot;SUCCESS&quot; &quot;Network connectivity confirmed (ping successful)&quot;<br>    else<br>        log &quot;WARN&quot; &quot;Ping failed or blocked (this is often normal)&quot;<br>    fi<br>    <br>    # Test SSH authentication<br>    log &quot;INFO&quot; &quot;Testing SSH authentication&quot;<br>    if ssh -i &quot;$SSH_KEY_PATH&quot; -o BatchMode=yes -o ConnectTimeout=10 \<br>           -o StrictHostKeyChecking=accept-new &quot;$SSH_USER@$SERVER_IP&quot; \<br>           &quot;echo &#39;SSH_CONNECTION_OK&#39;&quot; &gt;&gt; &quot;$LOG_FILE&quot; 2&gt;&amp;1; then<br>        log &quot;SUCCESS&quot; &quot;SSH authentication successful&quot;<br>    else<br>        log &quot;ERROR&quot; &quot;SSH authentication failed&quot;<br>        log &quot;ERROR&quot; &quot;Ensure your SSH key is authorized on the server&quot;<br>        exit $EXIT_SSH_ERROR<br>    fi<br>    <br>    # Gather remote system information<br>    log &quot;INFO&quot; &quot;Gathering remote system information&quot;<br>    ssh -i &quot;$SSH_KEY_PATH&quot; -o BatchMode=yes &quot;$SSH_USER@$SERVER_IP&quot; sh &lt;&lt;&#39;REMOTE_INFO_EOF&#39;<br>        printf &quot;Hostname: %s\n&quot; &quot;$(hostname)&quot;<br>        printf &quot;OS: %s\n&quot; &quot;$(cat /etc/os-release 2&gt;/dev/null | head -n1 || uname -a)&quot;<br>        printf &quot;Kernel: %s\n&quot; &quot;$(uname -r)&quot;<br>        printf &quot;Architecture: %s\n&quot; &quot;$(uname -m)&quot;<br>        <br>        if command -v docker &gt;/dev/null 2&gt;&amp;1; then<br>            printf &quot;Docker: %s\n&quot; &quot;$(docker --version 2&gt;/dev/null || echo &#39;installed&#39;)&quot;<br>        else<br>            printf &quot;Docker: not installed\n&quot;<br>        fi<br>REMOTE_INFO_EOF<br>}<br><br># Execute command on remote server with error handling<br># Arguments:<br>#   $1 - Command to execute<br># Returns:<br>#   0 on success, 1 on failure<br>remote_exec() {<br>    command=&quot;$1&quot;<br>    <br>    log &quot;INFO&quot; &quot;Executing remotely: $command&quot;<br>    <br>    if ssh -i &quot;$SSH_KEY_PATH&quot; -o BatchMode=yes &quot;$SSH_USER@$SERVER_IP&quot; &quot;$command&quot; &gt;&gt; &quot;$LOG_FILE&quot; 2&gt;&amp;1; then<br>        return 0<br>    else<br>        log &quot;ERROR&quot; &quot;Remote command failed: $command&quot;<br>        return 1<br>    fi<br>}<br><br># ============================================================================<br># REMOTE ENVIRONMENT PREPARATION<br># ============================================================================<br><br># Install Docker, Docker Compose, and Nginx on remote server<br>prepare_remote_environment() {<br>    print_section_header &quot;Preparing Remote Environment&quot;<br>    <br>    log &quot;INFO&quot; &quot;Installing required software on remote server&quot;<br>    log &quot;INFO&quot; &quot;This may take several minutes...&quot;<br>    <br>    # Execute installation script on remote server<br>    ssh -i &quot;$SSH_KEY_PATH&quot; -o BatchMode=yes &quot;$SSH_USER@$SERVER_IP&quot; sh &lt;&lt;&#39;REMOTE_SETUP_EOF&#39;<br>        set -e<br>        <br>        echo &quot;Detecting package manager...&quot;<br>        <br>        # Function to install Docker<br>        install_docker() {<br>            if command -v docker &gt;/dev/null 2&gt;&amp;1; then<br>                echo &quot;Docker already installed&quot;<br>                docker --version<br>                return 0<br>            fi<br>            <br>            echo &quot;Installing Docker...&quot;<br>            <br>            if command -v apt-get &gt;/dev/null 2&gt;&amp;1; then<br>                # Debian/Ubuntu<br>                sudo apt-get update -qq<br>                sudo apt-get install -y apt-transport-https ca-certificates curl gnupg lsb-release<br>                <br>                # Add Docker GPG key and repository<br>                curl -fsSL https://download.docker.com/linux/ubuntu/gpg | \<br>                    sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg 2&gt;/dev/null || true<br>                <br>                echo &quot;deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable&quot; | \<br>                    sudo tee /etc/apt/sources.list.d/docker.list &gt;/dev/null<br>                <br>                sudo apt-get update -qq<br>                sudo apt-get install -y docker-ce docker-ce-cli containerd.io<br>                <br>            elif command -v yum &gt;/dev/null 2&gt;&amp;1; then<br>                # RHEL/CentOS<br>                sudo yum install -y yum-utils<br>                sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo<br>                sudo yum install -y docker-ce docker-ce-cli containerd.io<br>                <br>            elif command -v dnf &gt;/dev/null 2&gt;&amp;1; then<br>                # Fedora<br>                sudo dnf -y install dnf-plugins-core<br>                sudo dnf config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo<br>                sudo dnf install -y docker-ce docker-ce-cli containerd.io<br>            else<br>                echo &quot;Unsupported package manager&quot;<br>                return 1<br>            fi<br>            <br>            # Enable and start Docker<br>            sudo systemctl enable docker 2&gt;/dev/null || true<br>            sudo systemctl start docker<br>            <br>            echo &quot;Docker installed successfully&quot;<br>            docker --version<br>        }<br>        <br>        # Function to install Docker Compose<br>        install_docker_compose() {<br>            if command -v docker-compose &gt;/dev/null 2&gt;&amp;1; then<br>                echo &quot;Docker Compose already installed&quot;<br>                docker-compose --version<br>                return 0<br>            fi<br>            <br>            echo &quot;Installing Docker Compose...&quot;<br>            <br>            # Download latest version<br>            sudo curl -L &quot;https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)&quot; \<br>                -o /usr/local/bin/docker-compose<br>            <br>            sudo chmod +x /usr/local/bin/docker-compose<br>            <br>            echo &quot;Docker Compose installed successfully&quot;<br>            docker-compose --version<br>        }<br>        <br>        # Function to install Nginx<br>        install_nginx() {<br>            if command -v nginx &gt;/dev/null 2&gt;&amp;1; then<br>                echo &quot;Nginx already installed&quot;<br>                nginx -v<br>                return 0<br>            fi<br>            <br>            echo &quot;Installing Nginx...&quot;<br>            <br>            if command -v apt-get &gt;/dev/null 2&gt;&amp;1; then<br>                sudo apt-get update -qq<br>                sudo apt-get install -y nginx<br>            elif command -v yum &gt;/dev/null 2&gt;&amp;1; then<br>                sudo yum install -y nginx<br>            elif command -v dnf &gt;/dev/null 2&gt;&amp;1; then<br>                sudo dnf install -y nginx<br>            else<br>                echo &quot;Unsupported package manager&quot;<br>                return 1<br>            fi<br>            <br>            # Enable and start Nginx<br>            sudo systemctl enable nginx 2&gt;/dev/null || true<br>            sudo systemctl start nginx<br>            <br>            echo &quot;Nginx installed successfully&quot;<br>            nginx -v<br>        }<br>        <br>        # Install all components<br>        install_docker<br>        install_docker_compose<br>        install_nginx<br>        <br>        # Add user to docker group (requires re-login to take effect)<br>        if ! getent group docker &gt;/dev/null 2&gt;&amp;1; then<br>            sudo groupadd docker || true<br>        fi<br>        sudo usermod -aG docker &quot;$USER&quot; || true<br>        <br>        echo &quot;Remote environment preparation completed&quot;<br>REMOTE_SETUP_EOF<br>    <br>    log &quot;SUCCESS&quot; &quot;Remote environment prepared successfully&quot;<br>    <br>    # Verify installations<br>    log &quot;INFO&quot; &quot;Verifying installations...&quot;<br>    remote_exec &quot;docker --version &amp;&amp; docker-compose --version &amp;&amp; nginx -v&quot;<br>}<br><br># ============================================================================<br># APPLICATION DEPLOYMENT<br># ============================================================================<br><br># Transfer application files to remote server<br>transfer_application_files() {<br>    print_section_header &quot;Transferring Application Files&quot;<br>    <br>    log &quot;INFO&quot; &quot;Creating remote directory: $REMOTE_APP_DIR&quot;<br>    remote_exec &quot;sudo mkdir -p $REMOTE_APP_DIR &amp;&amp; sudo chown $SSH_USER:$SSH_USER $REMOTE_APP_DIR&quot;<br>    <br>    log &quot;INFO&quot; &quot;Transferring files from $LOCAL_CLONE_DIR to $SERVER_IP:$REMOTE_APP_DIR&quot;<br>    <br>    # Try rsync first (more efficient), fall back to scp if not available<br>    if command -v rsync &gt;/dev/null 2&gt;&amp;1; then<br>        log &quot;INFO&quot; &quot;Using rsync for efficient transfer&quot;<br>        <br>        if rsync -avz --delete --exclude=&#39;.git&#39; \<br>           -e &quot;ssh -i $SSH_KEY_PATH -o BatchMode=yes&quot; \<br>           &quot;$LOCAL_CLONE_DIR/&quot; &quot;$SSH_USER@$SERVER_IP:$REMOTE_APP_DIR/&quot; &gt;&gt; &quot;$LOG_FILE&quot; 2&gt;&amp;1; then<br>            log &quot;SUCCESS&quot; &quot;Files transferred successfully using rsync&quot;<br>        else<br>            log &quot;ERROR&quot; &quot;File transfer failed&quot;<br>            exit $EXIT_GENERAL_ERROR<br>        fi<br>    else<br>        log &quot;INFO&quot; &quot;rsync not available, using scp&quot;<br>        <br>        if scp -i &quot;$SSH_KEY_PATH&quot; -o BatchMode=yes -r \<br>           &quot;$LOCAL_CLONE_DIR/&quot;* &quot;$SSH_USER@$SERVER_IP:$REMOTE_APP_DIR/&quot; &gt;&gt; &quot;$LOG_FILE&quot; 2&gt;&amp;1; then<br>            log &quot;SUCCESS&quot; &quot;Files transferred successfully using scp&quot;<br>        else<br>            log &quot;ERROR&quot; &quot;File transfer failed&quot;<br>            exit $EXIT_GENERAL_ERROR<br>        fi<br>    fi<br>}<br><br># Build and deploy Docker containers<br>deploy_docker_containers() {<br>    print_section_header &quot;Deploying Docker Containers&quot;<br>    <br>    log &quot;INFO&quot; &quot;Building and starting containers on remote server&quot;<br>    <br>    # Pass necessary variables to remote execution<br>    ssh -i &quot;$SSH_KEY_PATH&quot; -o BatchMode=yes &quot;$SSH_USER@$SERVER_IP&quot; \<br>        &quot;REMOTE_DIR=&#39;$REMOTE_APP_DIR&#39; APP_PORT=&#39;$APP_PORT&#39;&quot; sh &lt;&lt;&#39;DEPLOY_EOF&#39;<br>        set -e<br>        <br>        cd &quot;$REMOTE_DIR&quot; || exit 1<br>        <br>        echo &quot;Current directory: $(pwd)&quot;<br>        echo &quot;Stopping existing containers (idempotent)...&quot;<br>        <br>        # Stop docker-compose managed containers<br>        if [ -f docker-compose.yml ] || [ -f docker-compose.yaml ]; then<br>            sudo docker-compose down 2&gt;/dev/null || true<br>        fi<br>        <br>        # Stop any containers using the target port<br>        port_containers=$(sudo docker ps -q --filter &quot;publish=$APP_PORT&quot; 2&gt;/dev/null || true)<br>        if [ -n &quot;$port_containers&quot; ]; then<br>            printf &quot;%s&quot; &quot;$port_containers&quot; | xargs sudo docker stop 2&gt;/dev/null || true<br>        fi<br>        <br>        echo &quot;Building and starting new containers...&quot;<br>        <br>        # Deploy based on available configuration<br>        if [ -f docker-compose.yml ] || [ -f docker-compose.yaml ]; then<br>            echo &quot;Using docker-compose for deployment&quot;<br>            sudo docker-compose build --no-cache<br>            sudo docker-compose up -d --remove-orphans<br>            <br>        elif [ -f Dockerfile ]; then<br>            echo &quot;Using standalone Dockerfile for deployment&quot;<br>            <br>            # Generate app name from directory<br>            APP_NAME=$(basename &quot;$PWD&quot;)<br>            <br>            # Build image<br>            sudo docker build -t &quot;$APP_NAME:latest&quot; .<br>            <br>            # Remove old container if exists<br>            if sudo docker ps -a --format &#39;{{.Names}}&#39; | grep -x &quot;$APP_NAME&quot; &gt;/dev/null 2&gt;&amp;1; then<br>                sudo docker rm -f &quot;$APP_NAME&quot; || true<br>            fi<br>            <br>            # Run new container<br>            sudo docker run -d \<br>                --name &quot;$APP_NAME&quot; \<br>                --restart unless-stopped \<br>                -p &quot;127.0.0.1:$APP_PORT:$APP_PORT&quot; \<br>                &quot;$APP_NAME:latest&quot;<br>        else<br>            echo &quot;Error: No Dockerfile or docker-compose.yml found&quot;<br>            exit 1<br>        fi<br>        <br>        echo &quot;Waiting for containers to stabilize...&quot;<br>        sleep 10<br>        <br>        echo &quot;Container deployment completed&quot;<br>DEPLOY_EOF<br>    <br>    log &quot;SUCCESS&quot; &quot;Containers deployed successfully&quot;<br>}<br><br># Verify container health and status<br>verify_container_health() {<br>    print_section_header &quot;Verifying Container Health&quot;<br>    <br>    log &quot;INFO&quot; &quot;Checking container status...&quot;<br>    <br>    # Check running containers<br>    ssh -i &quot;$SSH_KEY_PATH&quot; -o BatchMode=yes &quot;$SSH_USER@$SERVER_IP&quot; sh &lt;&lt;&#39;VERIFY_EOF&#39;<br>        echo &quot;Running containers:&quot;<br>        sudo docker ps --format &#39;table {{.Names}}\t{{.Status}}\t{{.Ports}}&#39;<br>        <br>        echo &quot;&quot;<br>        echo &quot;Checking container logs (last 20 lines):&quot;<br>        container_ids=$(sudo docker ps -q)<br>        if [ -n &quot;$container_ids&quot; ]; then<br>            for container_id in $container_ids; do<br>                echo &quot;--- Container: $container_id ---&quot;<br>                sudo docker logs --tail 20 &quot;$container_id&quot; 2&gt;&amp;1 || true<br>            done<br>        fi<br>VERIFY_EOF<br>    <br>    log &quot;SUCCESS&quot; &quot;Container health check completed&quot;<br>}<br><br># ============================================================================<br># NGINX CONFIGURATION<br># ============================================================================<br><br># Configure Nginx as reverse proxy<br>configure_nginx_proxy() {<br>    print_section_header &quot;Configuring Nginx Reverse Proxy&quot;<br>    <br>    log &quot;INFO&quot; &quot;Creating Nginx configuration (port 80 -&gt; $APP_PORT)&quot;<br>    <br>    # Create Nginx configuration on remote server<br>    # Note: EOF is NOT quoted so variables expand<br>    ssh -i &quot;$SSH_KEY_PATH&quot; -o BatchMode=yes &quot;$SSH_USER@$SERVER_IP&quot; \<br>        &quot;APP_PORT=&#39;$APP_PORT&#39;&quot; sh &lt;&lt;EOF<br>        set -e<br>        <br>        echo &quot;Writing Nginx configuration...&quot;<br>        <br>        # Create configuration file<br>        sudo tee /etc/nginx/sites-available/app.conf &gt; /dev/null &lt;&lt;NGINX_CONFIG<br>server {<br>    listen 80;<br>    listen [::]:80;<br>    server_name _;<br><br>    # Logging<br>    access_log /var/log/nginx/app_access.log;<br>    error_log /var/log/nginx/app_error.log;<br><br>    # Main application proxy<br>    location / {<br>        proxy_pass http://127.0.0.1:\$APP_PORT;<br>        proxy_http_version 1.1;<br>        <br>        # WebSocket support<br>        proxy_set_header Upgrade \\\$http_upgrade;<br>        proxy_set_header Connection &#39;upgrade&#39;;<br>        <br>        # Standard proxy headers<br>        proxy_set_header Host \\\$host;<br>        proxy_set_header X-Real-IP \\\$remote_addr;<br>        proxy_set_header X-Forwarded-For \\\$proxy_add_x_forwarded_for;<br>        proxy_set_header X-Forwarded-Proto \\\$scheme;<br>        <br>        # Timeouts<br>        proxy_connect_timeout 60s;<br>        proxy_send_timeout 60s;<br>        proxy_read_timeout 60s;<br>        <br>        # Buffering<br>        proxy_buffering off;<br>        proxy_cache_bypass \\\$http_upgrade;<br>    }<br><br>    # Health check endpoint<br>    location /health {<br>        access_log off;<br>        return 200 &quot;healthy\n&quot;;<br>        add_header Content-Type text/plain;<br>    }<br><br>    # Security headers<br>    add_header X-Frame-Options &quot;SAMEORIGIN&quot; always;<br>    add_header X-Content-Type-Options &quot;nosniff&quot; always;<br>    add_header X-XSS-Protection &quot;1; mode=block&quot; always;<br>}<br>NGINX_CONFIG<br><br>        echo &quot;Enabling site configuration...&quot;<br>        <br>        # Create symbolic link to enable site<br>        sudo ln -sf /etc/nginx/sites-available/app.conf /etc/nginx/sites-enabled/app.conf<br>        <br>        # Remove default site if it exists<br>        sudo rm -f /etc/nginx/sites-enabled/default<br>        <br>        echo &quot;Testing Nginx configuration...&quot;<br>        sudo nginx -t<br>        <br>        echo &quot;Reloading Nginx...&quot;<br>        if sudo systemctl is-active --quiet nginx; then<br>            sudo systemctl reload nginx<br>        else<br>            sudo systemctl start nginx<br>        fi<br>        <br>        echo &quot;Nginx configuration completed&quot;<br>EOF<br>    <br>    log &quot;SUCCESS&quot; &quot;Nginx configured and reloaded successfully&quot;<br>}<br><br># ============================================================================<br># DEPLOYMENT VALIDATION<br># ============================================================================<br><br># Perform comprehensive deployment validation<br>validate_deployment() {<br>    print_section_header &quot;Validating Deployment&quot;<br>    <br>    # Check Docker service<br>    log &quot;INFO&quot; &quot;Checking Docker service status...&quot;<br>    if remote_exec &quot;sudo systemctl is-active docker&quot;; then<br>        log &quot;SUCCESS&quot; &quot;Docker service is running&quot;<br>    else<br>        log &quot;ERROR&quot; &quot;Docker service is not running&quot;<br>        exit $EXIT_DEPLOYMENT_ERROR<br>    fi<br>    <br>    # Check Nginx service<br>    log &quot;INFO&quot; &quot;Checking Nginx service status...&quot;<br>    if remote_exec &quot;sudo systemctl is-active nginx&quot;; then<br>        log &quot;SUCCESS&quot; &quot;Nginx service is running&quot;<br>    else<br>        log &quot;ERROR&quot; &quot;Nginx service is not running&quot;<br>        exit $EXIT_CONFIG_ERROR<br>    fi<br>    <br>    # Check container status<br>    log &quot;INFO&quot; &quot;Verifying containers are running...&quot;<br>    if ssh -i &quot;$SSH_KEY_PATH&quot; -o BatchMode=yes &quot;$SSH_USER@$SERVER_IP&quot; \<br>           &quot;sudo docker ps | grep -q &#39;Up&#39; 2&gt;/dev/null&quot;; then<br>        log &quot;SUCCESS&quot; &quot;Application containers are running&quot;<br>    else<br>        log &quot;WARN&quot; &quot;No running containers detected&quot;<br>    fi<br>    <br>    # Test local endpoint on remote server<br>    log &quot;INFO&quot; &quot;Testing application endpoint (remote localhost)...&quot;<br>    if remote_exec &quot;curl -sf -m 10 http://127.0.0.1:$APP_PORT &gt;/dev/null 2&gt;&amp;1 || nc -z 127.0.0.1 $APP_PORT&quot;; then<br>        log &quot;SUCCESS&quot; &quot;Application is responding on port $APP_PORT&quot;<br>    else<br>        log &quot;WARN&quot; &quot;Application may not be fully ready on port $APP_PORT&quot;<br>    fi<br>    <br>    # Test Nginx proxy<br>    log &quot;INFO&quot; &quot;Testing Nginx reverse proxy...&quot;<br>    if remote_exec &quot;curl -sf -m 10 http://127.0.0.1 &gt;/dev/null 2&gt;&amp;1&quot;; then<br>        log &quot;SUCCESS&quot; &quot;Nginx proxy is working correctly&quot;<br>    else<br>        log &quot;WARN&quot; &quot;Nginx proxy may not be fully operational&quot;<br>    fi<br>    <br>    # Test external access<br>    log &quot;INFO&quot; &quot;Testing external access to http://$SERVER_IP&quot;<br>    if curl -sf -m 10 &quot;http://$SERVER_IP&quot; &gt;/dev/null 2&gt;&amp;1; then<br>        log &quot;SUCCESS&quot; &quot;Application is accessible from external network&quot;<br>    else<br>        log &quot;WARN&quot; &quot;Application may not be accessible externally (check firewall rules)&quot;<br>    fi<br>    <br>    # Display service status summary<br>    log &quot;INFO&quot; &quot;Deployment validation completed&quot;<br>    <br>    # Fetch and display recent Nginx logs<br>    log &quot;INFO&quot; &quot;Recent Nginx access logs:&quot;<br>    remote_exec &quot;sudo tail -n 5 /var/log/nginx/app_access.log 2&gt;/dev/null || echo &#39;No logs yet&#39;&quot;<br>}<br><br># ============================================================================<br># MAIN EXECUTION FLOW<br># ============================================================================<br><br># Display usage information<br>show_usage() {<br>    cat &lt;&lt;USAGE<br>================================================================================<br>$SCRIPT_NAME - Production-Grade Docker Deployment Script<br>================================================================================<br><br>USAGE:<br>    ./$SCRIPT_NAME [OPTIONS]<br><br>OPTIONS:<br>    (no options)    Run normal deployment workflow<br>    --cleanup       Remove all deployed resources from remote server<br>    --help          Display this help message<br>    --version       Display script version<br><br>DESCRIPTION:<br>    Automates the complete deployment lifecycle of Dockerized applications<br>    to remote Linux servers. Handles:<br>    <br>    - Git repository cloning/updating<br>    - SSH connectivity testing<br>    - Remote environment preparation (Docker, Docker Compose, Nginx)<br>    - Container building and deployment<br>    - Nginx reverse proxy configuration<br>    - Comprehensive deployment validation<br><br>REQUIREMENTS:<br>    Local:  git, ssh, scp/rsync, curl<br>    Remote: Linux server with SSH access (key-based authentication)<br><br>EXAMPLES:<br>    # Normal deployment<br>    ./$SCRIPT_NAME<br>    <br>    # Cleanup existing deployment<br>    ./$SCRIPT_NAME --cleanup<br>    <br>    # Show help<br>    ./$SCRIPT_NAME --help<br><br>EXIT CODES:<br>    0   - Success<br>    1   - General error<br>    2   - Input validation failed<br>    3   - SSH connection failed<br>    4   - Deployment failed<br>    5   - Configuration error<br>    130 - Interrupted by user<br><br>For more information, check the generated log file after execution.<br><br>================================================================================<br>USAGE<br>}<br><br># Main function - orchestrates entire deployment process<br>main() {<br>    # Parse command line arguments<br>    while [ $# -gt 0 ]; do<br>        case &quot;$1&quot; in<br>            --cleanup)<br>                CLEANUP_MODE=1<br>                shift<br>                ;;<br>            --help|-h)<br>                show_usage<br>                exit $EXIT_SUCCESS<br>                ;;<br>            --version|-v)<br>                printf &quot;%s version %s\n&quot; &quot;$SCRIPT_NAME&quot; &quot;$SCRIPT_VERSION&quot;<br>                exit $EXIT_SUCCESS<br>                ;;<br>            *)<br>                printf &quot;%s Error: Unknown option: %s\n&quot; &quot;$SYMBOL_ERROR&quot; &quot;$1&quot;<br>                printf &quot;Run &#39;./%s --help&#39; for usage information\n&quot; &quot;$SCRIPT_NAME&quot;<br>                exit $EXIT_VALIDATION_ERROR<br>                ;;<br>        esac<br>    done<br>    <br>    # Initialize logging<br>    initialize_logging<br>    <br>    # Handle cleanup mode<br>    if [ &quot;$CLEANUP_MODE&quot; -eq 1 ]; then<br>        cleanup_deployment<br>        # cleanup_deployment exits, so we won&#39;t reach here<br>    fi<br>    <br>    # Display welcome banner<br>    printf &quot;\n&quot;<br>    printf &quot;================================================================================\n&quot;<br>    printf &quot;%s DOCKER DEPLOYMENT AUTOMATION SCRIPT\n&quot; &quot;$SYMBOL_ROCKET&quot;<br>    printf &quot;================================================================================\n&quot;<br>    printf &quot;Version: %s\n&quot; &quot;$SCRIPT_VERSION&quot;<br>    printf &quot;Started: %s\n&quot; &quot;$(date &#39;+%Y-%m-%d %H:%M:%S&#39;)&quot;<br>    printf &quot;================================================================================\n\n&quot;<br>    <br>    log &quot;INFO&quot; &quot;Starting deployment workflow&quot;<br>    <br>    # Execute deployment steps in sequence<br>    collect_deployment_parameters<br>    clone_or_update_repository<br>    verify_docker_configuration<br>    test_ssh_connectivity<br>    prepare_remote_environment<br>    transfer_application_files<br>    deploy_docker_containers<br>    verify_container_health<br>    configure_nginx_proxy<br>    validate_deployment<br>    <br>    # Display success summary<br>    printf &quot;\n&quot;<br>    printf &quot;================================================================================\n&quot;<br>    printf &quot;%s DEPLOYMENT COMPLETED SUCCESSFULLY\n&quot; &quot;$SYMBOL_CELEBRATE&quot;<br>    printf &quot;================================================================================\n&quot;<br>    printf &quot;\n&quot;<br>    printf &quot;%s Access your application at: %shttp://%s%s\n&quot; \<br>           &quot;$SYMBOL_NETWORK&quot; &quot;$COLOR_GREEN&quot; &quot;$SERVER_IP&quot; &quot;$COLOR_RESET&quot;<br>    printf &quot;%s Nginx reverse proxy: Port 80 -&gt; Port %s\n&quot; \<br>           &quot;$SYMBOL_INFO&quot; &quot;$APP_PORT&quot;<br>    printf &quot;%s Log file: %s%s%s\n&quot; \<br>           &quot;$SYMBOL_INFO&quot; &quot;$COLOR_CYAN&quot; &quot;$LOG_FILE&quot; &quot;$COLOR_RESET&quot;<br>    printf &quot;\n&quot;<br>    printf &quot;To remove this deployment, run:\n&quot;<br>    printf &quot;  ./%s --cleanup\n&quot; &quot;$SCRIPT_NAME&quot;<br>    printf &quot;\n&quot;<br>    printf &quot;================================================================================\n&quot;<br>    <br>    log &quot;SUCCESS&quot; &quot;Deployment workflow completed successfully&quot;<br>}<br><br># ============================================================================<br># SCRIPT ENTRY POINT<br># ============================================================================<br><br># Execute main function with all command line arguments<br>main &quot;$@&quot;<br><br># Explicit success exit (trap will handle logging)<br>exit $EXIT_SUCCESS</pre><p>Script Breakdown of Major Components</p><p><strong>1. Strict Mode and Safety Guardrails</strong></p><p>The script begins with:</p><pre>set -e   # exit on error<br>set -u   # undefined variables treated as errors<br>IFS=&#39;\n\t&#39;</pre><p>This ensures:</p><ul><li>Bash immediately exits if a command fails (returns a non-zero exit status). Necessary to prevent silent failures. Without this, if a command such as cp or mkdir fails during execution, the script may continue executing the lines, leading to undesirable results.</li><li>Tells bash to treat any variable not initialized as an error and exits the script.</li><li>Safe handling of spaces in file paths</li></ul><p>This alone can help prevent about 70% of common Bash script bugs.</p><p><strong>2. Semantic Exit Codes</strong></p><p>Clear exit codes allow predictable CI/CD behavior:</p><ul><li>0 = success</li><li>1 = general error</li><li>2 = validation error</li><li>3 = SSH error</li><li>4 = deployment failure</li><li>5 = configuration error</li><li>130 = user interruption</li></ul><p>This makes logs and debugging significantly easier.</p><p><strong>3. Logging System for Both Console and File</strong></p><p>Every action is logged with timestamps:</p><pre>log &quot;INFO&quot; &quot;Message here&quot;</pre><p>Output goes to:</p><ul><li>Terminal (with color + emoji)</li><li>deploy_YYYYMMDD.log file</li></ul><p>This ensures traceability — essential for production-grade deployments.</p><p><strong>4. Fully Interactive User Input with Validation</strong></p><p>The script asks for:</p><ul><li>Git repo</li><li>Branch</li><li>SSH username</li><li>Server IP</li><li>SSH key path</li><li>Application internal port</li></ul><p>Validation functions check:</p><ul><li>Valid IP format</li><li>Valid port numbers</li><li>URL format</li><li>File existence</li></ul><p>This prevents invalid deployments.</p><p><strong>5. Secure Git Clone with Optional PAT</strong></p><p>Supports HTTPS or SSH cloning:</p><pre>git clone --branch &quot;$GIT_BRANCH&quot; ...</pre><p>If a Personal Access Token is used, it is;</p><ul><li>Injected securely</li><li>Cleared from memory after cloning</li></ul><p><strong>6. Verifying Dockerfile or docker-compose</strong></p><p>The script ensures the project contains:</p><ul><li>Dockerfile</li><li>Or docker-compose.yml</li></ul><p>This ensures deployments do not fail midway.</p><p><strong>7. SSH Connectivity Test</strong></p><p>Before deploying, it verifies:</p><ul><li>SSH authentication</li><li>Network connectivity</li><li>Remote OS, kernel, architecture</li><li>Whether Docker is installed</li></ul><p>No deployment begins without this.</p><p><strong>8. Remote Server Preparation</strong></p><p>The script can install:</p><ul><li>Docker</li><li>Docker Compose</li><li>Nginx (for reverse-proxying)</li></ul><p>This ensures the environment is consistent across servers.</p><p><strong>9. Deployment Process</strong></p><p>The script performs:</p><ul><li>Upload of project to server</li><li>Building Docker images remotely</li><li>Starting services via docker-compose</li><li>Setting up Nginx reverse proxy</li><li>Automatic reload</li></ul><p>In one command:</p><pre>./deploy.sh</pre><p><strong>10. Cleanup Mode</strong></p><p>Run:</p><pre>./deploy.sh --cleanup</pre><p>This:</p><ul><li>Stops containers</li><li>Removes containers, volumes, networks</li><li>Removes deployment folders</li><li>Removes Nginx config</li><li>Reloads Nginx</li></ul><p>Perfect for rollback or fresh re-deployment.</p><p>Using the Script:</p><p><strong>1. Normal Deployment</strong></p><pre>./deploy.sh</pre><p><strong>2. Cleanup All Deployed Resources</strong></p><pre>./deploy.sh --cleanup</pre><p><strong>3. Show Help</strong></p><pre>./deploy.sh --help</pre><p>Folder Structure Example:</p><pre>project/<br>├── Dockerfile<br>├── docker-compose.yml<br>├── deploy.sh<br>└── app/</pre><p><strong>When To Use This Script?</strong></p><p>This script is ideal when you manage:</p><ol><li>Single-server Docker deployments</li><li>Multiple microservices packaged via Docker Compose</li><li>Environments where you want simple but reliable deployments</li><li>Remote servers accessed via SSH</li><li>Zero-downtime patch deployments</li></ol><p>As an extra tip, the deployment script can be integrated with your CICD workflows by plugging into;</p><ul><li>Github Actions</li><li>Gitlab CI</li><li>Jenkins</li><li>Circle CI</li><li>and ArgoCD workflows etc.</li></ul><p>Just pass environment variables and call:</p><pre>sh deploy.sh</pre><p>You instantly have a simple but powerful end-to-end CI/CD pipeline with automated deployment.</p><p><strong>Conclusion</strong></p><p>This automated deployment script gives DevOps engineers a repeatable, and secure way to deploy Dockerized applications with:</p><ul><li>Zero manual steps</li><li>Full logging</li><li>Safety checks</li><li>Validation</li><li>SSH automation</li><li>Nginx provisioning</li><li>Cleanup support</li></ul><p>It bridges the gap between manual Docker commands and full-blown enterprise CI/CD systems.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9efd68446114" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Zero-Downtime Deployments: A Beginner’s Guide to Blue/Green Deployment with Nginx]]></title>
            <link>https://medium.com/@daniel_macdonald/zero-downtime-deployments-a-beginners-guide-to-blue-green-deployment-with-nginx-b675bb9a0985?source=rss-797248f9bb88------2</link>
            <guid isPermaLink="false">https://medium.com/p/b675bb9a0985</guid>
            <dc:creator><![CDATA[Daniel Macdonald]]></dc:creator>
            <pubDate>Tue, 09 Dec 2025 11:20:57 GMT</pubDate>
            <atom:updated>2025-12-09T11:30:35.446Z</atom:updated>
            <content:encoded><![CDATA[<h3>Zero-Downtime Deployments: A Beginner’s Guide to Blue/Green Deployment with Nginx .</h3><p>Building a production-ready deployment system that never lets your users see errors!!!</p><p><strong>The Problem We’re Solving</strong></p><p>Picture this: You’ve built an amazing web application. It’s running in production, serving your users. Now you need to deploy a new version. The traditional approach? Stop the old version, start the new one. And then boom — your users start to see error pages.</p><p>Not great, right?</p><p>This is where <strong>blue/green deployment</strong> comes in. It’s a simple but powerful technique that lets you switch between two identical environments instantly, with <strong>zero downtime</strong>. Even better, if something goes wrong, you can switch back in seconds.</p><p>By the end of this guide, you’ll have built a system that:</p><ul><li>Automatically switches traffic when your primary server fails</li><li>Sends you Slack alerts when problems occur</li><li>Handles errors gracefully without your users ever noticing</li></ul><p>And the best part? You don’t need to be a DevOps expert to understand it.</p><p><strong>What is Blue/Green Deployment?</strong></p><p>Imagine you run a coffee shop with two identical kitchens: the <strong>Blue Kitchen</strong> and the <strong>Green Kitchen</strong>.</p><ul><li>Your customers always order at the same counter (that’s your <strong>Nginx server</strong>)</li><li>Normally, all orders go to the Blue Kitchen (your <strong>active version</strong>)</li><li>The Green Kitchen is ready, but standing by (your <strong>standby version</strong>)</li></ul><p>Now, let’s say you want to upgrade your espresso machine. Instead of closing the shop:</p><ol><li>You upgrade the Green Kitchen while Blue keeps serving customers</li><li>When Green is ready, you simply redirect all new orders to Green</li><li>Customers never notice the switch — no downtime!</li><li>If Green has issues, you instantly switch back to Blue</li></ol><p>That’s blue/green deployment. Simple concept, powerful results.</p><p><strong>The Architecture We’re Building</strong></p><p>Before we dive into code, let’s understand what we’re creating:</p><pre>Internet → [Nginx Reverse Proxy] → [Blue App (Primary)]<br>                                  → [Green App (Standby)]<br>                                  → [Monitor (Watches Everything)]</pre><p><strong>The Players:</strong></p><ol><li><strong>Nginx</strong> — Your traffic cop. It determines whether requests go to Blue or Green.</li><li><strong>Blue App</strong> — Your currently active application (version 1.0)</li><li><strong>Green App</strong> — Your standby application (could be version 1.0 or 2.0)</li><li><strong>Monitor</strong> — A Python script that watches logs and sends Slack alerts</li></ol><p>The Magic: When Nginx detects that Blue isn’t responding (maybe it crashed, or you deployed broken code), it automatically sends traffic to Green. Your users never see an error page.</p><p><strong>Prerequisites: What You Need</strong></p><p>Don’t worry — we’re starting from the beginning. You’ll need:</p><p><strong>1. An AWS Account</strong></p><ul><li>Sign up at aws.amazon.com (free tier is fine)</li><li>We’ll be using the free-tier EC2 server (Ubuntu)</li></ul><p><strong>2. Basic Command Line Knowledge</strong></p><ul><li>If you can type commands and hit enter, then we’re good to go!</li></ul><p><strong>3. A Slack Workspace</strong></p><ul><li>Free to create at slack.com</li><li>We’ll use this for alerts</li></ul><p><strong>4. 1–2 Hours of Time</strong></p><ul><li>Grab a plate of jollof rice, we’re building something cool</li></ul><p><strong>Step 1: Setting Up Your Server</strong></p><p>First, let’s set up a server on AWS.</p><p><strong>Launch an EC2 Instance</strong></p><ol><li><strong>Log in to AWS Console</strong></li></ol><ul><li>Go to EC2 Dashboard</li><li>Click “Launch Instance”</li></ul><p><strong>2. Choose Your Settings:</strong></p><pre> Name: blue-green-demo<br>   OS: Ubuntu 22.04 LTS (free tier eligible)<br>   Instance Type: t3.medium (we need a bit of power)<br>   Storage: 20 GB</pre><p><strong>3. Create a Key Pair</strong></p><ul><li>Name it blue-green-key</li><li>Download the .pem file</li><li>Don’t lose this file! It’s your only way to access the server</li></ul><p><strong>4. Configure Security Group.</strong> This is your firewall. Add these rules:</p><pre>SSH (Port 22) - Your IP only (for security)<br>   Custom TCP (Port 8080) - Anywhere (for users)<br>   Custom TCP (Port 8081) - Your IP only (for testing)<br>   Custom TCP (Port 8082) - Your IP only (for testing)</pre><p><strong>5. Launch!</strong></p><ul><li>Click “Launch Instance”</li><li>Wait for 2 minutes for it to boot</li></ul><p><strong>Connect to Your Server</strong></p><p>Open your terminal and connect:</p><pre># On Mac/Linux<br>chmod 400 blue-green-key.pem<br>ssh -i blue-green-key.pem ubuntu@YOUR-EC2-IP<br><br># On Windows (use PowerShell)<br>ssh -i blue-green-key.pem ubuntu@YOUR-EC2-IP</pre><p>You’ll see something like:</p><pre>ubuntu@ip-172-31-45-67:~$<br></pre><p>You’re in. Congratulations! 🎉</p><p><strong>Step 2: Installing Docker</strong></p><p>Docker lets us run applications in isolated containers. Think of it like shipping containers for code — everything the app needs is packaged inside.</p><p><strong>Run these commands one by one:</strong></p><pre># Update the system<br>sudo apt-get update<br>sudo apt-get upgrade -y<br><br># Install Docker (this script does all the heavy lifting)<br>curl -fsSL https://get.docker.com -o get-docker.sh<br>sudo sh get-docker.sh<br><br># Let yourself use Docker without typing &#39;sudo&#39; every time<br>sudo usermod -aG docker $USER<br><br># Install Docker Compose (for managing multiple containers)<br>sudo apt-get install docker-compose-plugin -y<br><br># Install helpful tools<br>sudo apt-get install curl jq bc -y</pre><p>Now, log out and log back in:</p><pre>exit<br># Then reconnect with the SSH command from earlier</pre><p>Verify it worked:</p><pre>docker --version<br># Should show: Docker version 24.x.x<br><br>docker compose version<br># Should show: Docker Compose version 2.x.x</pre><p>If you see version numbers, you’re golden! ✨</p><p><strong>Step 3: Setting Up Slack Alerts</strong></p><p>We want to know immediately when something goes wrong. Slack is perfect for this.</p><p><strong>Create a Slack Webhook</strong></p><ol><li>Go to your Slack workspace</li><li>Click on your workspace name → Settings &amp; Administration → Manage Apps</li><li>Search for “Incoming Webhooks”</li><li>Click “Add to Slack”</li><li>Choose a channel (create one called #deployment-alerts)</li><li>Copy the Webhook URL</li></ol><ul><li>It looks like: <a href="https://hooks.slack.com/services/T00000000/B00000000/XXXX">https://hooks.slack.com/services/T00000000/B00000000/XXXX</a></li><li><strong>Save this!</strong> We’ll need it soon</li></ul><p>Test it works:</p><pre>curl -X POST &quot;YOUR_WEBHOOK_URL&quot; \<br>  -H &#39;Content-Type: application/json&#39; \<br>  -d &#39;{&quot;text&quot;:&quot;Hello from my blue/green deployment! 🚀&quot;}&#39;</pre><p>Check Slack — you should see your message! If you do, perfect. If not, double-check you copied the full webhook URL.</p><p><strong>Step 4: Project Setup</strong></p><p>Let’s organize our code. This is like setting up folders before starting a big project.</p><pre># Create the main project folder<br>mkdir -p ~/blue-green-deployment<br>cd ~/blue-green-deployment<br><br># Create subfolders<br>mkdir -p nginx/templates nginx/conf.d<br>mkdir -p monitoring<br>mkdir -p scripts tests docs logs<br><br># Check your structure<br>tree -L 2</pre><p>You should see:</p><pre>blue-green-deployment/<br>├── nginx/<br>│   ├── templates/<br>│   └── conf.d/<br>├── monitoring/<br>├── scripts/<br>├── tests/<br>├── docs/<br>└── logs/</pre><p>Beautiful! Now let’s fill these folders with magic. ✨</p><p><strong>Step 5: Understanding the Configuration</strong></p><p>Before we write code, let’s understand what we’re configuring. We’ll use a file called .env to store all our settings.</p><p><strong>Create Your Configuration File</strong></p><pre>nano .env</pre><p>Paste this in (we’ll explain each part):</p><pre># Which Docker images to use for your apps<br># (Use the pre-built images from the project requirements)<br>BLUE_IMAGE=your-registry/blue-green-app:latest<br>GREEN_IMAGE=your-registry/blue-green-app:latest<br><br># Which pool is active? (blue or green)<br>ACTIVE_POOL=blue<br><br># Version labels (helps you track what&#39;s deployed)<br>RELEASE_ID_BLUE=v1.0.0-blue<br>RELEASE_ID_GREEN=v1.0.0-green<br><br># What port does your app run on?<br>PORT=3000<br><br># IMPORTANT: Your Slack webhook from Step 3<br>SLACK_WEBHOOK_URL=https://hooks.slack.com/services/YOUR/WEBHOOK/HERE<br><br># Alert Settings<br>ERROR_RATE_THRESHOLD=2.0    # Alert if errors exceed 2%<br>WINDOW_SIZE=200              # Check last 200 requests<br>ALERT_COOLDOWN_SEC=300       # Wait 5 min between similar alerts<br>FAILOVER_COOLDOWN_SEC=60     # Wait 1 min between failover alerts<br><br># Maintenance mode (set to true to silence alerts during deployments)<br>MAINTENANCE_MODE=false<br><br># Debug mode (set to true to see detailed logs)<br>DEBUG=false</pre><p>Save and exit: Press Ctrl+X, then Y, then Enter</p><p><strong>What Does This All Mean?</strong></p><p>Let me break it down:</p><ul><li><strong>BLUE_IMAGE / GREEN_IMAGE</strong>: These are your application versions. Think of them like installers for your app.</li><li><strong>ACTIVE_POOL</strong>: Which version is currently serving users? Start with blue.</li><li><strong>RELEASE_ID</strong>: Labels so you know which version is which (like “v1.0” vs “v2.0”)</li><li><strong>PORT</strong>: Your Node.js app listens on port 3000 (standard for Node apps)</li><li><strong>SLACK_WEBHOOK_URL</strong>: Where to send alerts (from Step 3)</li><li><strong>ERROR_RATE_THRESHOLD</strong>: If more than 2% of requests fail, sound the alarm!</li><li><strong>WINDOW_SIZE</strong>: Look at the last 200 requests to calculate the error rate</li><li><strong>ALERT_COOLDOWN</strong>: Don’t spam Slack — wait between similar alerts</li><li><strong>MAINTENANCE_MODE</strong>: Turn this on when doing planned work (no alerts needed)</li></ul><p><strong>Step 6: The Nginx Configuration</strong></p><p>Nginx is our traffic cop. It needs to know:</p><ol><li>Where are Blue and Green?</li><li>How to detect when one fails?</li><li>How to switch traffic automatically?</li></ol><p><strong>Create the Nginx Template</strong></p><pre>nano nginx/templates/nginx.conf.template</pre><p>I’m going to give you the config in chunks, with explanations:</p><p><strong>Part 1: The Basics</strong></p><pre>worker_processes auto;<br>error_log /var/log/nginx/error.log warn;<br>pid /var/run/nginx.pid;<br><br>events {<br>    worker_connections 1024;<br>}<br><br>http {<br>    include /etc/nginx/mime.types;<br>    default_type application/octet-stream;</pre><p>This is boilerplate — just telling Nginx to start up properly. Nothing to worry about here.</p><p><strong>Part 2: The Smart Logging</strong></p><pre># This format logs everything we need to monitor<br>    log_format monitoring escape=json &#39;{&#39;<br>        &#39;&quot;timestamp&quot;:&quot;$time_iso8601&quot;,&#39;<br>        &#39;&quot;remote_addr&quot;:&quot;$remote_addr&quot;,&#39;<br>        &#39;&quot;request_method&quot;:&quot;$request_method&quot;,&#39;<br>        &#39;&quot;request_uri&quot;:&quot;$request_uri&quot;,&#39;<br>        &#39;&quot;status&quot;:$status,&#39;<br>        &#39;&quot;request_time&quot;:$request_time,&#39;<br>        &#39;&quot;upstream_addr&quot;:&quot;$upstream_addr&quot;,&#39;<br>        &#39;&quot;upstream_status&quot;:&quot;$upstream_status&quot;,&#39;<br>        &#39;&quot;pool&quot;:&quot;$upstream_http_x_app_pool&quot;,&#39;<br>        &#39;&quot;release&quot;:&quot;$upstream_http_x_release_id&quot;&#39;<br>    &#39;}&#39;;<br><br>    access_log /var/log/nginx/access.log monitoring;</pre><p><strong>Why JSON?</strong> Our Python monitoring script can easily read JSON logs. Each request gets logged with:</p><ul><li>Which pool served it (blue or green)</li><li>How long it took</li><li>Whether it succeeded or failed</li></ul><p><strong>Part 3: The Magic — Upstream Configuration</strong></p><pre>upstream app_backend {<br>        # This is where the blue/green magic happens!<br>        <br>        # Blue is primary (when ACTIVE_POOL=blue)<br>        ${BLUE_PRIMARY}<br>        <br>        # Green is backup (switches automatically if Blue fails)<br>        ${GREEN_BACKUP}<br>        <br>        # These get swapped when ACTIVE_POOL=green<br>        ${GREEN_PRIMARY}<br>        ${BLUE_BACKUP}<br>    }</pre><p><strong>What’s happening here?</strong></p><ul><li>${BLUE_PRIMARY} is a placeholder that gets replaced with the actual server config</li><li>One server is marked as primary, the other as backup</li><li>The backup server only receives traffic if the primary fails</li></ul><p><strong>Part 4: Failover Settings</strong></p><pre>server {<br>        listen 80;<br>        server_name _;<br>        <br>        location / {<br>            proxy_pass http://app_backend;<br>            <br>            # KEY SETTINGS FOR ZERO-DOWNTIME:<br>            proxy_connect_timeout 2s;    # Wait max 2s to connect<br>            proxy_read_timeout 3s;       # Wait max 3s for response<br>            <br>            # What counts as a failure?<br>            proxy_next_upstream error timeout http_500 http_502 http_503 http_504;<br>            <br>            # How many times to retry?<br>            proxy_next_upstream_tries 2;<br>            <br>            # Headers to preserve<br>            proxy_set_header Host $host;<br>            proxy_set_header X-Real-IP $remote_addr;<br>            proxy_pass_header X-App-Pool;<br>            proxy_pass_header X-Release-Id;<br>        }<br>    }<br>}</pre><p><strong>The Critical Parts Explained:</strong></p><ol><li>Timeouts (2–3 seconds): If Blue doesn’t respond in 2–3 seconds, try Green immediately.</li><li>proxy_next_upstream: This line is the secret sauce. It says “if you get an error, timeout, or 5xx status code, try the next server.”</li><li>proxy_next_upstream_tries 2: Try primary, then backup. That’s 2 tries total.</li><li>Headers: We preserve special headers from the app so we know which pool served each request.</li></ol><p>Save this file: Ctrl+X, Y, Enter</p><p><strong>Step 7: The Monitoring Script</strong></p><p>This Python script is your watchdog. It sits there 24/7, reading Nginx logs and watching for problems.</p><p><strong>Create the Watcher Script</strong></p><pre>nano monitoring/watcher.py</pre><p>I’ll explain the key concepts (the full script is in the artifacts, but let’s understand the logic):</p><p><strong>What It Does:</strong></p><ol><li>Reads Nginx logs in real-time (like tail -f but smarter)</li><li>Tracks metrics:</li></ol><ul><li>How many requests to Blue?</li><li>How many requests to Green?</li><li>What’s the error rate over the last 200 requests?</li></ul><p>3. Detects events:</p><ul><li>Did traffic switch from Blue to Green? (Failover!)</li><li>Is the error rate above 2%? (Problem!)</li><li>Did everything return to normal? (Recovery!)</li></ul><p>4. Sends Slack alerts (but not too many — rate limiting prevents spam)</p><p><strong>Key Concepts in the Code:</strong></p><pre># Configuration from environment variables<br>SLACK_WEBHOOK_URL = os.getenv(&#39;SLACK_WEBHOOK_URL&#39;)<br>ERROR_RATE_THRESHOLD = float(os.getenv(&#39;ERROR_RATE_THRESHOLD&#39;, &#39;2.0&#39;))<br><br># Sliding window for error tracking<br>request_window = deque(maxlen=200)  # Last 200 requests<br><br># Add each request to the window<br>request_window.append({<br>    &#39;status&#39;: 200,  # or 500 if error<br>    &#39;pool&#39;: &#39;blue&#39;,<br>    &#39;is_error&#39;: False<br>})<br><br># Calculate error rate<br>errors = sum(1 for req in request_window if req[&#39;is_error&#39;])<br>error_rate = (errors / len(request_window)) * 100<br><br># Send alert if threshold exceeded<br>if error_rate &gt; ERROR_RATE_THRESHOLD:<br>    send_slack_alert(&quot;Error rate is 3.5%!&quot;)</pre><p><strong>The Sliding Window Concept:</strong></p><p>Imagine you have a window that can see 200 requests at a time. As new requests come in, old ones slide out the back. This gives you a real-time view of “what’s happening right now” without being affected by ancient history.</p><pre>Old requests → [||||||||||||||||||||] ← New requests<br>                  200-request window<br>                  <br>Calculate: How many of these are errors?<br>If &gt;2% are errors → Alert!</pre><p>Copy the full watcher.py script from the earlier artifacts into this file.</p><p><strong>Save it:</strong> Ctrl+X, Y, Enter</p><p><strong>Create Python Requirements</strong></p><pre>nano monitoring/requirements.txt</pre><pre>requests==2.31.0</pre><p>This is the only external library we need — for sending HTTP requests to Slack.</p><p><strong>Save it:</strong> Ctrl+X, Y, Enter</p><p><strong>Create the Watcher Dockerfile</strong></p><pre>nano monitoring/Dockerfile</pre><pre>FROM python:3.11-slim<br><br>WORKDIR /app<br><br># Install dependencies<br>COPY requirements.txt .<br>RUN pip install --no-cache-dir -r requirements.txt<br><br># Copy the monitoring script<br>COPY watcher.py .<br>RUN chmod +x watcher.py<br><br># Run as non-root for security<br>RUN useradd -m -u 1000 watcher &amp;&amp; chown -R watcher:watcher /app<br>USER watcher<br><br>CMD [&quot;python3&quot;, &quot;-u&quot;, &quot;watcher.py&quot;]</pre><p><strong>What’s This?</strong> It’s a recipe for building a container that runs our Python script. Docker will:</p><ol><li>Start with a base Python image</li><li>Install the requests library</li><li>Copy our script</li><li>Run it</li></ol><p>Save it: Ctrl+X, Y, Enter</p><p><strong>Step 8: Docker Compose — Orchestrating Everything</strong></p><p>Docker Compose is like a conductor for an orchestra. It starts all our containers in the right order and connects them together.</p><pre>nano docker-compose.yml</pre><pre>version: &#39;3.8&#39;<br><br>services:<br>  # The Blue application<br>  app_blue:<br>    image: ${BLUE_IMAGE}<br>    container_name: app_blue<br>    environment:<br>      - APP_POOL=blue<br>      - RELEASE_ID=${RELEASE_ID_BLUE}<br>      - PORT=${PORT:-3000}<br>    ports:<br>      - &quot;8081:3000&quot;  # Blue runs on port 8081<br>    networks:<br>      - app_network<br>    healthcheck:<br>      test: [&quot;CMD&quot;, &quot;curl&quot;, &quot;-f&quot;, &quot;http://localhost:3000/healthz&quot;]<br>      interval: 5s<br>      timeout: 3s<br>      retries: 3<br>    restart: unless-stopped<br><br>  # The Green application (identical to Blue)<br>  app_green:<br>    image: ${GREEN_IMAGE}<br>    container_name: app_green<br>    environment:<br>      - APP_POOL=green<br>      - RELEASE_ID=${RELEASE_ID_GREEN}<br>      - PORT=${PORT:-3000}<br>    ports:<br>      - &quot;8082:3000&quot;  # Green runs on port 8082<br>    networks:<br>      - app_network<br>    healthcheck:<br>      test: [&quot;CMD&quot;, &quot;curl&quot;, &quot;-f&quot;, &quot;http://localhost:3000/healthz&quot;]<br>      interval: 5s<br>      timeout: 3s<br>      retries: 3<br>    restart: unless-stopped<br><br>  # Nginx - the traffic cop<br>  nginx:<br>    image: nginx:alpine<br>    container_name: nginx_lb<br>    ports:<br>      - &quot;8080:80&quot;  # Public port<br>    volumes:<br>      - ./nginx/conf.d/nginx.conf:/etc/nginx/nginx.conf:ro<br>      - nginx_logs:/var/log/nginx  # Shared with monitor<br>    networks:<br>      - app_network<br>    depends_on:<br>      - app_blue<br>      - app_green<br>    restart: unless-stopped<br><br>  # The monitoring watchdog<br>  alert_watcher:<br>    build:<br>      context: ./monitoring<br>    container_name: alert_watcher<br>    environment:<br>      - SLACK_WEBHOOK_URL=${SLACK_WEBHOOK_URL}<br>      - ACTIVE_POOL=${ACTIVE_POOL}<br>      - ERROR_RATE_THRESHOLD=${ERROR_RATE_THRESHOLD:-2.0}<br>      - WINDOW_SIZE=${WINDOW_SIZE:-200}<br>      - ALERT_COOLDOWN_SEC=${ALERT_COOLDOWN_SEC:-300}<br>      - MAINTENANCE_MODE=${MAINTENANCE_MODE:-false}<br>      - NGINX_LOG_FILE=/var/log/nginx/access.log<br>    volumes:<br>      - nginx_logs:/var/log/nginx:ro  # Read-only access to logs<br>    networks:<br>      - app_network<br>    depends_on:<br>      - nginx<br>    restart: unless-stopped<br><br>networks:<br>  app_network:<br>    driver: bridge<br><br>volumes:<br>  nginx_logs:  # Shared volume for logs</pre><pre>nginx_logs:  # Shared volume for logs</pre><p><strong>What&#39;s Happening Here?</strong></p><p>Think of this like a restaurant:</p><ul><li>app_blue and app_green are two identical kitchens</li><li>nginx is the host that decides which kitchen gets each order</li><li>alert_watcher is the manager watching everything</li><li>nginx_logs is the shared logbook that they all can read/write from</li></ul><p><strong>Key Points:</strong></p><ol><li>Ports: Blue on 8081, Green on 8082, Nginx (public) on 8080</li><li>Health Checks: Docker checks every 5 seconds if each app is alive</li><li>Shared Volume: Nginx writes logs, watcher reads them</li><li>Network: All containers can talk to each other by name</li></ol><p>Save it: Ctrl+X, Y, Enter</p><p><strong>Step 9: The Helper Scripts</strong></p><p>Let’s create scripts to make our lives easier.</p><p><strong>Script 1: Config Generator</strong></p><pre>nano scripts/generate-config.sh</pre><pre>#!/bin/bash<br># This script turns nginx.conf.template into nginx.conf<br># by replacing ${VARIABLES} with actual values<br><br>set -euo pipefail<br><br># Load .env file<br>source .env<br><br># Determine which pool is primary<br>if [[ &quot;$ACTIVE_POOL&quot; == &quot;blue&quot; ]]; then<br>    BLUE_PRIMARY=&quot;server app_blue:3000 max_fails=2 fail_timeout=5s;&quot;<br>    GREEN_BACKUP=&quot;server app_green:3000 backup max_fails=2 fail_timeout=5s;&quot;<br>    GREEN_PRIMARY=&quot;# Green is backup&quot;<br>    BLUE_BACKUP=&quot;&quot;<br>else<br>    BLUE_PRIMARY=&quot;# Blue is backup&quot;<br>    GREEN_BACKUP=&quot;&quot;<br>    GREEN_PRIMARY=&quot;server app_green:3000 max_fails=2 fail_timeout=5s;&quot;<br>    BLUE_BACKUP=&quot;server app_blue:3000 backup max_fails=2 fail_timeout=5s;&quot;<br>fi<br><br># Replace variables in template<br>export BLUE_PRIMARY GREEN_BACKUP GREEN_PRIMARY BLUE_BACKUP<br><br>envsubst &#39;${BLUE_PRIMARY} ${GREEN_BACKUP} ${GREEN_PRIMARY} ${BLUE_BACKUP}&#39; \<br>    &lt; nginx/templates/nginx.conf.template \<br>    &gt; nginx/conf.d/nginx.conf<br><br>echo &quot;✓ Nginx c</pre><p><strong>What This Does:</strong> Takes your template and fills in the blanks based on which pool is active.</p><p><strong>Save and make executable:</strong></p><pre>chmod +x scripts/generate-config.sh</pre><p><strong>Script 2: Test the System</strong></p><pre>nano scripts/test-alerts.sh</pre><pre>#!/bin/bash<br># Test the monitoring and alerting system<br><br>set -euo pipefail<br><br>echo &quot;Testing Blue/Green Deployment...&quot;<br><br># Test 1: Basic health<br>echo &quot;Test 1: Checking baseline health...&quot;<br>for i in {1..10}; do<br>    curl -s http://localhost:8080/version | jq -r &#39;.pool&#39;<br>done<br><br># Test 2: Trigger failover<br>echo &quot;&quot;<br>echo &quot;Test 2: Injecting chaos into active pool...&quot;<br>source .env<br><br>if [ &quot;$ACTIVE_POOL&quot; = &quot;blue&quot; ]; then<br>    CHAOS_PORT=8081<br>else<br>    CHAOS_PORT=8082<br>fi<br><br>curl -s -X POST &quot;http://localhost:$CHAOS_PORT/chaos/start?mode=error&quot;<br><br>echo &quot;Watching for failover...&quot;<br>sleep 5<br><br>for i in {1..20}; do<br>    POOL=$(curl -s http://localhost:8080/version | jq -r &#39;.pool&#39;)<br>    echo &quot;Request $i: pool=$POOL&quot;<br>    sleep 1<br>done<br><br># Stop chaos<br>curl -s -X POST &quot;http://localhost:$CHAOS_PORT/chaos/stop&quot;<br><br>echo &quot;&quot;<br>echo &quot;✓ Test complete! Check Slack for alerts.&quot;</pre><p><strong>What This Does:</strong></p><ol><li>Sends 10 normal requests</li><li>Breaks the active pool on purpose</li><li>Watches traffic switch to the backup pool</li><li>You should get a Slack alert!</li></ol><p><strong>Save and make executable:</strong></p><pre>chmod +x scripts/test-alerts.sh</pre><p><strong>Step 10: Deploy Everything!</strong></p><p>Moment of truth. Let’s start everything up.</p><p><strong>Generate the Nginx Config</strong></p><pre>cd ~/blue-green-deployment<br>./scripts/generate-config.sh</pre><p>You should see:</p><pre>✓ Nginx configuration generated for ACTIVE_POOL=blue</pre><p>Start All Services</p><pre>docker compose up -d</pre><p><strong>What’s happening:</strong> Docker is:</p><ol><li>Pulling the application images (might take a minute)</li><li>Building the monitoring container</li><li>Starting all four services</li><li>Connecting them on a network</li></ol><h3>Check Everything Started</h3><pre>docker compose ps</pre><pre>NAME            STATUS          PORTS<br>app_blue        Up (healthy)    0.0.0.0:8081-&gt;3000/tcp<br>app_green       Up (healthy)    0.0.0.0:8082-&gt;3000/tcp<br>nginx_lb        Up (healthy)    0.0.0.0:8080-&gt;80/tcp<br>alert_watcher   Up</pre><p>All showing “Up” or “Up (healthy)”? Perfect! 🎉</p><h3>Step 11: Testing Your System</h3><p>Let’s make sure everything works.</p><p><strong>Test 1: Basic Connectivity</strong></p><pre>curl http://localhost:8080/version</pre><pre>{<br>  &quot;pool&quot;: &quot;blue&quot;,<br>  &quot;release&quot;: &quot;v1.0.0-blue&quot;,<br>  &quot;timestamp&quot;: &quot;2025-10-30T...&quot;<br>}</pre><p><strong>What this means:</strong> Nginx is working, Blue is serving traffic, and everything is healthy!</p><p><strong>Test 2: Check the Logs</strong></p><pre># See what the monitor is doing<br>docker logs alert_watcher<br><br># See Nginx logs (in pretty JSON)<br>docker exec nginx_lb tail -20 /var/log/nginx/access.log | jq .</pre><p>You should see beautifully formatted logs showing each request.</p><p><strong>Test 3: The Failover Test</strong></p><p>This is the exciting one! Let’s break Blue and watch Green take over automatically.</p><pre>./scripts/test-alerts.sh</pre><p><strong>Watch what happens:</strong></p><ol><li>First 10 requests: All go to Blue ✓</li><li>You inject chaos into Blue (it starts returning errors)</li><li>Requests 11–15: Might see some Blue, some Green (transition)</li><li>Requests 16–20: All go to Green ✓</li><li><strong>Check your Slack</strong> — You should have received a “FAILOVER” alert! 🚨</li></ol><p><strong>If you see:</strong></p><ul><li>Traffic switching from Blue to Green: ✅ Success!</li><li>A Slack alert about the failover: ✅ Success!</li><li>Zero failed requests (all returned 200): ✅ Success!</li></ul><p>Congratulations! You just built a zero-downtime deployment system! 🎊</p><p><strong>Step 12: Understanding What Just Happened</strong></p><p>Let’s break down the magic:</p><p><strong>The Failover Flow</strong></p><pre>1. User makes request → Nginx<br>2. Nginx tries Blue (2 second timeout)<br>3. Blue is broken (returns 500 error)<br>4. Nginx immediately retries → Green<br>5. Green is healthy (returns 200)<br>6. User gets successful response<br>   <br>Total time: ~2 seconds, user never saw an error!</pre><p><strong>The Monitoring Flow</strong></p><pre>1. Nginx writes JSON log: {&quot;pool&quot;:&quot;green&quot;,&quot;status&quot;:200}<br>2. Watcher reads the log immediately<br>3. Watcher notices: &quot;Hey, we were on Blue, now we&#39;re on Green!&quot;<br>4. Watcher calculates: &quot;2 out of last 200 re</pre><p><strong>The Error Rate Tracking</strong></p><pre>Window: [Request 1, Request 2, ..., Request 200]<br>        <br>        200 requests, 4 errors = 2% error rate ✓ (OK)<br>        200 requests, 5 errors = 2.5% error rate 🚨 (ALERT!)</pre><p><strong>Step 13: Playing With Your New Toy</strong></p><p><strong>Check Which Pool is Active</strong></p><pre>curl -s http://localhost:8080/version | jq -r &#39;.pool&#39;</pre><p><strong>Manually Switch Pools</strong></p><p>Want to deploy a new version to Green, then switch traffic? Easy!</p><pre># Update .env<br>nano .env<br># Change: ACTIVE_POOL=green<br><br># Regenerate config<br>./scripts/generate-config.sh<br><br># Reload Nginx (no downtime!)<br>docker exec nginx_lb nginx -s reload<br><br># Check it worked<br>curl -s http://localhost:8080/version | jq -r &#39;.pool&#39;<br># Should say &quot;green&quot; now!</pre><p><strong>Watch Live Logs</strong></p><pre># Watch the monitor in real-time<br>docker logs -f alert_watcher<br><br># Watch Nginx logs<br>docker exec nginx_lb tail -f /var/log/nginx/access.log | jq .</pre><p><strong>Test Different Error Rates</strong></p><pre># Create a file: tests/test-error-rates.sh<br>nano tests/test-error-rates.sh</pre><p><strong>Paste the error rate testing script from the earlier artifacts.</strong></p><pre>chmod +x tests/test-error-rates.sh<br><br># Test with 5% errors (above threshold - should alert)<br>./tests/test-error-rates.sh 5<br><br># Test with 1% errors (below threshold - no alert)<br>./tests/test-error-rates.sh 1</pre><p><strong>Step 14: Maintenance Mode</strong></p><p>Sometimes you want to do planned work without getting spammed with alerts.</p><p><strong>Create the Maintenance Mode Script</strong></p><pre>nano scripts/maintenance-mode.sh</pre><p>Paste the maintenance mode script from earlier, then:</p><pre>chmod +x scripts/maintenance-mode.sh<br><br># Enable maintenance mode<br>./scripts/maintenance-mode.sh enable<br><br># Now do your deployment work...<br># (no alerts will be sent)<br><br># When done, disable it<br>./scripts/maintenance-mode.sh disable</pre><p><strong>Real-World Scenarios</strong></p><h3>Scenario 1: Deploying a New Version</h3><p>You have v1.0 running on Blue, and you want to deploy v2.0.</p><p><strong>The Safe Way:</strong></p><pre># 1. Deploy v2.0 to Green (while Blue serves traffic)<br>nano .env<br># Change: GREEN_IMAGE=your-registry/app:v2.0<br><br>docker compose pull app_green<br>docker compose up -d app_green<br><br># 2. Test Green directly<br>curl http://localhost:8082/version<br># Check if v2.0 is working properly<br><br># 3. Run some test traffic through Green<br>for i in {1..50}; do<br>  curl http://localhost:8082/healthz<br>done<br><br># 4. Looks good? Switch traffic to Green<br>nano .env<br># Change: ACTIVE_POOL=green<br><br>./scripts/generate-config.sh<br>docker exec nginx_lb nginx -s reload<br><br># 5. Monitor for issues<br>docker logs -f alert_watcher<br><br># 6. If problems occur, instant rollback:<br>nano .env<br># Change: ACTIVE_POOL=blue<br>./scripts/generate-config.sh<br>docker exec nginx_lb nginx -s reload</pre><p><strong>Benefits:</strong></p><ul><li>Zero downtime</li><li>Test the new version before switching traffic</li><li>Instant rollback if needed</li><li>Users never affected</li></ul><h3>Scenario 2: A Container Crashes</h3><p>Let’s say Blue crashes at 3 am.</p><p><strong>What Happens Automatically:</strong></p><pre>03:00:00 - Blue container crashes<br>03:00:01 - User request arrives at Nginx<br>03:00:01 - Nginx tries Blue (times out after 2s)<br>03:00:03 - Nginx switches to Green automatically<br>03:00:03 - User gets successful response from Green<br>03:00:05 - Monitor detects failover<br>03:00:06 - Slack alert sent: &quot;⚠️ Failover detected&quot;<br>03:00:06 - You wake up, check phone, see alert<br>03:00:10 - You investigate Blue container</pre><p><strong>Total user-facing downtime:</strong> 0 seconds ✓</p><p><strong>Scenario 3: Gradual Performance Degradation</strong></p><p>Sometimes apps don’t crash — they just get slow or start returning occasional errors.</p><p><strong>What The Monitor Detects:</strong></p><pre>Window: Last 200 requests<br>- Request 1-180: All successful (0% errors)<br>- Request 181-200: 5 errors mixed in<br>  <br>Error rate: 5/200 = 2.5% 🚨<br><br>Alert sent: &quot;Error rate is 2.5% (threshold: 2.0%)&quot;</pre><p>Now you know there’s a problem before it becomes critical!</p><p><strong>Troubleshooting Guide</strong></p><p><strong>Problem: “curl: (7) Failed to connect.”</strong></p><p><strong>Diagnosis:</strong></p><pre>docker compose ps</pre><p>If services aren’t “Up”, check logs:</p><pre>docker compose logs app_blue<br>docker compose logs nginx</pre><p><strong>Common causes:</strong></p><ul><li>Forgot to pull images</li><li>Port already in use</li><li>Firewall blocking ports</li></ul><p><strong>Solution:</strong></p><pre># Restart everything<br>docker compose down<br>docker compose up -d</pre><p><strong>Problem: “No Slack alerts.”</strong></p><p><strong>Check the webhook is configured:</strong></p><pre>docker exec alert_watcher printenv | grep SLACK</pre><p><strong>Test Webhook Manually</strong></p><pre>source .env<br>curl -X POST &quot;$SLACK_WEBHOOK_URL&quot; \<br>  -H &#39;Content-Type: application/json&#39; \<br>  -d &#39;{&quot;text&quot;:&quot;Test message&quot;}&#39;</pre><p><strong>Check watcher logs:</strong></p><pre>docker logs alert_watcher | grep -i slack</pre><p><strong>Problem: “Failover not working.”</strong></p><p><strong>Check Nginx config:</strong></p><pre>docker exec nginx_lb cat /etc/nginx/nginx.conf | grep -A 5 upstream</pre><p>Should show one server as primary, one as backup.</p><p><strong>Check app health:</strong></p><pre>curl http://localhost:8081/healthz<br>curl http://localhost:8082/healthz</pre><p>Both should return 200 OK.</p><p><strong>Problem: “Watcher not seeing logs.”</strong></p><p><strong>Check shared volume:</strong></p><pre># Nginx should be writing logs<br>docker exec nginx_lb ls -la /var/log/nginx/<br><br># Watcher should be able to read them<br>docker exec alert_watcher ls -la /var/log/nginx/</pre><p>Both should show access.log</p><p><strong>What You’ve Learned</strong></p><p><strong>Let’s recap this journey:</strong></p><ol><li><strong>Blue/Green Deployment</strong> — Run two identical environments, switch between them instantly</li><li><strong>Zero-Downtime Failover</strong> — Nginx automatically routes around failures</li><li><strong>Real-Time Monitoring</strong> — Python script watches logs and alerts you</li><li><strong>Slack Integration</strong> — Know immediately when things go wrong</li><li><strong>Docker Orchestration</strong> — Manage multiple containers elegantly</li><li><strong>Production Patterns</strong> — Health checks, retries, timeouts, graceful degradation</li></ol><p><strong>Key Takeaways:</strong></p><p><strong>What Makes This Production-Ready?</strong></p><p>✅ <strong>Automatic failover</strong> — No manual intervention needed<br> ✅ <strong>Zero downtime</strong> — Users never see errors<br> ✅ <strong>Instant rollback</strong> — Switch back in seconds<br> ✅ <strong>Smart monitoring</strong> — Alerts on real problems, not noise<br> ✅ <strong>Operational visibility</strong> — Know exactly what’s happening<br> ✅ <strong>Easy to understand</strong> — No magic, just smart configuration</p><p><strong>When to Use Blue/Green</strong></p><p><strong>Perfect for:</strong></p><ul><li>Web applications with traffic</li><li>APIs that need high availability</li><li>Services where downtime = lost money</li><li>Teams that deploy frequently</li></ul><p><strong>Final Thoughts:</strong></p><p>You started this guide knowing nothing about blue/green deployments. Now you have:</p><ul><li>A fully functional zero-downtime deployment system ✓</li><li>Real-time monitoring and alerting ✓</li><li>The knowledge to explain it to others ✓</li><li>A foundation for more advanced DevOps ✓</li></ul><p><strong>Next Steps:</strong></p><ol><li><strong>Practice</strong>: Deploy your own application using this setup</li><li><strong>Experiment</strong>: Try breaking things and see how it recovers</li><li><strong>Extend</strong>: Add more monitoring, dashboards, automation</li><li><strong>Share</strong>: Write about your experience, help others learn</li><li><strong>Interview</strong>: You can now talk intelligently about blue/green deployments in job interviews!</li></ol><p><strong>Resources:</strong></p><ul><li>Nginx Documentation: <a href="https://nginx.org/en/docs/">https://nginx.org/en/docs/</a></li><li>Docker Compose Docs: <a href="https://docs.docker.com/compose/">https://docs.docker.com/compose/</a></li><li>Slack Webhooks: <a href="https://api.slack.com/messaging/webhooks">https://api.slack.com/messaging/webhooks</a></li><li>AWS EC2 Guide: <a href="https://aws.amazon.com/ec2/getting-started/">https://aws.amazon.com/ec2/getting-started/</a></li></ul><p><strong>Questions?</strong></p><p>If you get stuck anywhere or have questions:</p><ol><li>Check the troubleshooting section above</li><li>Look at the logs: docker compose logs</li><li>Review the runbook in docs/runbook.md</li><li>The DevOps community is friendly — ask questions!</li></ol><p>Go forth and deploy with confidence! 🚀</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b675bb9a0985" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building Your Own Virtual Private Cloud on Linux: A Complete Hands-On Guide]]></title>
            <link>https://medium.com/@daniel_macdonald/building-your-own-virtual-private-cloud-on-linux-a-complete-hands-on-guide-7b66f7fda124?source=rss-797248f9bb88------2</link>
            <guid isPermaLink="false">https://medium.com/p/7b66f7fda124</guid>
            <dc:creator><![CDATA[Daniel Macdonald]]></dc:creator>
            <pubDate>Wed, 12 Nov 2025 19:13:22 GMT</pubDate>
            <atom:updated>2025-11-12T19:13:22.430Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Ever wondered how AWS creates those isolated networks? Let’s build one ourselves using nothing but Linux!</em></p><p><strong>By Macdonald Daniel</strong></p><h3>Hey There! 👋</h3><p>So, you’ve probably used AWS or heard about VPCs (Virtual Private Clouds), right? They’re these magical isolated networks where you can run your applications safely. But have you ever thought, “How does this actually work under the hood?”</p><p>Well, I recently went down that rabbit hole, and guess what? We can actually build our own VPC right on a Linux machine! No AWS account needed, no credit card, just your computer and some curiosity.</p><p>In this guide, I’ll walk you through building vpcctl—a command-line tool that recreates AWS VPC functionality using pure Linux networking. We&#39;re talking subnets, NAT gateways, VPC peering, the whole deal. And the best part? You&#39;ll actually understand how cloud networking works by the time we&#39;re done.</p><p><strong>What you’ll learn:</strong></p><ul><li>How cloud providers isolate customer networks</li><li>Linux networking magic (namespaces, bridges, and more)</li><li>How NAT gateways actually work</li><li>Building network infrastructure from scratch</li></ul><p>Don’t worry if you’re new to this stuff. I’ll explain everything as we go!</p><h3>Before We Start</h3><p>Here’s what you’ll need:</p><p><strong>The Essentials:</strong></p><ul><li>A Linux machine (I used Ubuntu 20.04, but most distros work)</li><li>Root access (we’ll be using sudo a lot)</li><li>Python 3.6 or newer (probably already installed)</li><li>Basic familiarity with the command line</li></ul><p><strong>Quick check — do you have the tools?</strong></p><pre>which ip iptables python3</pre><p>If something’s missing, no worries:</p><pre>sudo apt-get update<br>sudo apt-get install iproute2 iptables python3</pre><p>That’s it! We’re keeping this simple — no fancy dependencies or complicated setups.</p><h3>What Are We Actually Building?</h3><p>Let me paint you a picture. When you create a VPC in AWS, you’re essentially getting a private network that’s completely isolated from everyone else’s stuff. Inside this VPC, you can create subnets (like different floors in a building), set up routing (how traffic flows), and control internet access.</p><p><strong>The main components:</strong></p><ul><li><strong>VPC:</strong> Your isolated network (think: 10.0.0.0/16)</li><li><strong>Subnets:</strong> Smaller networks within the VPC (like 10.0.1.0/24)</li><li><strong>Router/Gateway:</strong> Directs traffic between subnets</li><li><strong>NAT Gateway:</strong> Lets your private resources talk to the internet</li><li><strong>Security Groups:</strong> Firewall rules (who can talk to whom)</li></ul><p>Now here’s the cool part — we can simulate all of this using Linux primitives!</p><h3>The Linux Magic</h3><p><strong>Component Mapping:</strong></p><ul><li><strong>VPC → Linux Bridge:</strong> Acts like a virtual switch and router</li><li><strong>Subnet → Network Namespace:</strong> Complete network isolation (separate IP stack!)</li><li><strong>Connection → Veth Pair:</strong> Like a virtual ethernet cable</li><li><strong>NAT Gateway → iptables:</strong> Changes IP addresses on the fly</li><li><strong>Security Groups → iptables Rules:</strong> Filters packets going in and out</li><li><strong>VPC Peering → Veth Pair + Routes:</strong> Connects two isolated networks</li></ul><h3>The Big Picture: Our Architecture</h3><p>Let me show you what we’re building. Imagine this setup:</p><pre>┌─────────────────────────────────────────────────────────────────┐<br>│                    Your Linux Machine                            │<br>│                                                                   │<br>│  ┌───────────────────────────────────────────────────────────┐  │<br>│  │              VPC 1 (10.0.0.0/16)                          │  │<br>│  │                                                            │  │<br>│  │        ┌──────────────────────────┐                       │  │<br>│  │        │   Linux Bridge           │                       │  │<br>│  │        │   br-vpc1                │                       │  │<br>│  │        │   Gateway: 10.0.0.1      │◄──── sends traffic    │  │<br>│  │        └──────┬──────────┬────────┘      to internet      │  │<br>│  │               │          │                                 │  │<br>│  │        veth   │          │   veth                         │  │<br>│  │        pair   │          │   pair                         │  │<br>│  │               ▼          ▼                                 │  │<br>│  │    ┌──────────────┐  ┌──────────────┐                    │  │<br>│  │    │ Public       │  │ Private      │                    │  │<br>│  │    │ Subnet       │  │ Subnet       │                    │  │<br>│  │    │ 10.0.1.2     │  │ 10.0.2.2     │                    │  │<br>│  │    │              │  │              │                    │  │<br>│  │    │ ✓ Can reach  │  │ ✗ Locked     │                    │  │<br>│  │    │   internet   │  │   down       │                    │  │<br>│  │    │ Web Server   │  │ Database     │                    │  │<br>│  │    └──────────────┘  └──────────────┘                    │  │<br>│  └───────────────────────────────────────────────────────────┘  │<br>│                            │                                     │<br>│                     Can connect these                            │<br>│                       if we want!                                │<br>│                            │                                     │<br>│  ┌────────────────────────▼──────────────────────────────────┐  │<br>│  │              VPC 2 (172.16.0.0/16)                        │  │<br>│  │        ┌──────────────────────────┐                       │  │<br>│  │        │   Linux Bridge           │                       │  │<br>│  │        │   br-vpc2                │                       │  │<br>│  │        │   Gateway: 172.16.0.1    │                       │  │<br>│  │        └──────────────────────────┘                       │  │<br>│  └───────────────────────────────────────────────────────────┘  │<br>│                                                                   │<br>│              ┌─────────────────┐                                 │<br>│              │   iptables      │                                 │<br>│              │   (the magic    │                                 │<br>│              │    NAT box)     │                                 │<br>│              └────────┬────────┘                                 │<br>│                       │                                           │<br>│              ┌────────▼────────┐                                 │<br>│              │ Your WiFi/      │                                 │<br>│              │ Ethernet        │                                 │<br>│              └────────┬────────┘                                 │<br>└───────────────────────┼──────────────────────────────────────────┘<br>                        │<br>                        ▼<br>                  The Internet 🌐</pre><p><strong>How traffic actually flows:</strong></p><p>When your public subnet wants to reach the internet:</p><ol><li>Packet starts in the namespace (your subnet)</li><li>Goes through the veth pair to the bridge</li><li>Bridge forwards to iptables</li><li>iptables does NAT magic (changes your private IP to the public IP)</li><li>Heads out through your actual network interface</li><li>Comes back the same way, but reversed!</li></ol><p>Meanwhile, your private subnet? It just sits there, happily isolated, with no route to the internet. Perfect for databases and sensitive stuff!</p><h3>Let’s Get This Thing Running</h3><h3>Grab the Code</h3><p>First, let’s get the project:</p><pre>git clone https://github.com/thelogicguy/linux-vpc-project.git<br>cd linux-vpc-project</pre><pre># Make everything executable<br>chmod +x vpcctl demo.sh cleanup.sh</pre><h3>Quick Sanity Check</h3><pre>sudo ./vpcctl --help</pre><p>If you see a bunch of commands listed, you’re good to go!</p><h3>Building Our First VPC (The Fun Part!)</h3><p>Alright, let’s actually build something. We’ll create a VPC with both public and private subnets, just like you would in AWS.</p><h3>Step 1: Create the VPC</h3><pre>sudo ./vpcctl vpc create --name myvpc --cidr 10.0.0.0/16</pre><p><strong>What just happened?</strong><br>The tool created a Linux bridge called br-myvpc and gave it the IP 10.0.0.1. This bridge is going to be our VPC&#39;s router. Everything in our VPC will use this as their gateway.</p><p>Want to see it? Run this:</p><pre>ip link show br-myvpc</pre><p>Cool, right?</p><h3>Step 2: Add a Public Subnet</h3><p>Now let’s create a subnet that can reach the internet:</p><pre>sudo ./vpcctl subnet add --vpc myvpc --name public --cidr 10.0.1.0/24 --type public</pre><p><strong>Behind the scenes:</strong></p><ul><li>Created a network namespace (think: a completely separate network stack)</li><li>Made a veth pair (virtual ethernet cable)</li><li>Plugged one end into the namespace, other end into our bridge</li><li>Gave the namespace the IP 10.0.1.2</li><li>Set the default route to point to our bridge gateway</li></ul><p>Check it out:</p><pre># See all namespaces<br>sudo ip netns list</pre><pre># Look inside the namespace<br>sudo ip netns exec myvpc-public ip addr</pre><p>Mind-blowing, isn’t it? We just created a completely isolated network!</p><h3>Step 3: Add a Private Subnet</h3><p>Let’s add a subnet that stays locked down:</p><pre>sudo ./vpcctl subnet add --vpc myvpc --name private --cidr 10.0.2.0/24 --type private</pre><p>Same deal, but this one gets IP 10.0.2.2 and won&#39;t have internet access.</p><h3>Step 4: Turn On the Internet</h3><p>Here’s where it gets interesting. First, figure out which network interface connects your computer to the internet:</p><pre>ip route | grep default</pre><p>Look for something like eth0, ens33, or wlan0. Got it? Now:</p><pre># Replace eth0 with whatever you found<br>sudo ./vpcctl nat enable --vpc myvpc --interface eth0</pre><p><strong>What’s NAT doing?</strong><br>It’s basically lying to the internet! When your public subnet sends packets out, NAT changes the source IP from 10.0.1.2 (private) to your computer&#39;s real IP address. When replies come back, it changes them back. The internet never knows about your private network!</p><h3>Does It Actually Work? Let’s Find Out!</h3><p>Time for the moment of truth. Let’s test everything!</p><h3>Test #1: Can Subnets Talk to Each Other?</h3><pre>sudo ip netns exec myvpc-public ping -c 3 10.0.2.2</pre><p>You should see something like:</p><pre>64 bytes from 10.0.2.2: icmp_seq=1 ttl=64 time=0.123 ms</pre><p>Success! The subnets can talk through the bridge. This is intra-VPC routing in action!</p><h3>Test #2: Can We Reach the Internet?</h3><pre>sudo ip netns exec myvpc-public ping -c 3 8.8.8.8</pre><p>If you see replies from Google’s DNS server, congratulations — your NAT gateway works!</p><p>Try something fancier:</p><pre>sudo ip netns exec myvpc-public curl -I https://google.com</pre><p>Getting HTTP headers back? Beautiful!</p><h3>Test #3: Is the Private Subnet Really Private?</h3><pre>sudo ip netns exec myvpc-private ping -c 3 8.8.8.8</pre><p>This should timeout or fail. If it does, perfect! Your private subnet is properly isolated from the internet. This is exactly what we want for security.</p><h3>Test #4: Let’s Run a Web Server</h3><p>Now for something cool. Let’s deploy a web server in our public subnet:</p><pre>sudo ./vpcctl deploy webserver --vpc myvpc --subnet public --port 8080</pre><p>Now test it from your host machine:</p><pre>curl http://10.0.1.2:8080</pre><p>You should see a simple HTML page!</p><p>Can the private subnet reach it?</p><pre>sudo ip netns exec myvpc-private curl http://10.0.1.2:8080</pre><p>Yep! Internal traffic works fine.</p><h3>Test #5: Are VPCs Really Isolated?</h3><p>Let’s create another VPC and see if they can talk:</p><pre># Create VPC 2<br>sudo ./vpcctl vpc create --name vpc2 --cidr 172.16.0.0/16<br>sudo ./vpcctl subnet add --vpc vpc2 --name subnet1 --cidr 172.16.1.0/24 --type public</pre><p>Now try to ping from VPC1 to VPC2:</p><pre>sudo ip netns exec myvpc-public ping -c 3 172.16.1.2</pre><p>Fails, right? That’s VPC isolation at work! Just like in AWS, VPCs can’t talk to each other by default.</p><h3>Test #6: VPC Peering Time!</h3><p>But what if we want them to communicate? Let’s create a peering connection:</p><pre>sudo ./vpcctl peer create --vpc1 myvpc --vpc2 vpc2</pre><p>Now try that ping again:</p><pre>sudo ip netns exec myvpc-public ping -c 3 172.16.1.2</pre><p>Magic! It works now. The peering created a direct connection between the two VPC bridges.</p><h3>Test #7: Security Groups (Firewall Rules)</h3><p>Let’s lock down our public subnet. First, we’ll create a security policy file:</p><pre>cat &gt; my-firewall.json &lt;&lt;EOF<br>{<br>  &quot;subnet&quot;: &quot;10.0.1.0/24&quot;,<br>  &quot;ingress&quot;: [<br>    {&quot;port&quot;: 8080, &quot;protocol&quot;: &quot;tcp&quot;, &quot;action&quot;: &quot;allow&quot;},<br>    {&quot;port&quot;: 22, &quot;protocol&quot;: &quot;tcp&quot;, &quot;action&quot;: &quot;deny&quot;}<br>  ]<br>}<br>EOF</pre><p>Now apply the firewall rules:</p><pre>sudo ./vpcctl firewall apply --vpc myvpc --subnet public --policy my-firewall.json</pre><p>Perfect! Now port 8080 is open (for our web server) but port 22 is blocked (no SSH access). You can test both ports to verify the rules are working!</p><h3>Real-World Example: A Web App Setup</h3><p>Let me show you how you’d actually use this for a real application. Imagine you’re deploying a web app with a database backend:</p><pre># Step 1: Create a VPC for your web app<br>sudo ./vpcctl vpc create --name webapp --cidr 10.0.0.0/16</pre><pre># Step 2: Public subnet for web servers (internet-facing)<br>sudo ./vpcctl subnet add --vpc webapp --name web --cidr 10.0.1.0/24 --type public</pre><pre># Step 3: Private subnet for database (locked down)<br>sudo ./vpcctl subnet add --vpc webapp --name db --cidr 10.0.2.0/24 --type private</pre><pre># Step 4: Enable internet access for the web tier<br>sudo ./vpcctl nat enable --vpc webapp --interface eth0</pre><pre># Step 5: Deploy the web server<br>sudo ./vpcctl deploy webserver --vpc webapp --subnet web --port 8080</pre><p>Now let’s secure it with firewall rules:</p><pre># Step 6: Create security policy for web tier<br>cat &gt; web-rules.json &lt;&lt;EOF<br>{<br>  &quot;subnet&quot;: &quot;10.0.1.0/24&quot;,<br>  &quot;ingress&quot;: [<br>    {&quot;port&quot;: 80, &quot;protocol&quot;: &quot;tcp&quot;, &quot;action&quot;: &quot;allow&quot;},<br>    {&quot;port&quot;: 443, &quot;protocol&quot;: &quot;tcp&quot;, &quot;action&quot;: &quot;allow&quot;},<br>    {&quot;port&quot;: 8080, &quot;protocol&quot;: &quot;tcp&quot;, &quot;action&quot;: &quot;allow&quot;}<br>  ]<br>}<br>EOF</pre><pre># Step 7: Apply the firewall rules<br>sudo ./vpcctl firewall apply --vpc webapp --subnet web --policy web-rules.json</pre><p><strong>What you’ve built:</strong></p><ul><li>✓ Web servers that can reach the internet</li><li>✓ A database subnet that’s completely isolated</li><li>✓ Firewall rules protecting your web tier</li><li>✓ Internal communication between web and database</li></ul><p>This is exactly like a real production setup you’d see in AWS or any other cloud provider!</p><h3>When Things Go Wrong (And They Will!)</h3><p>Here are the issues I ran into and how I fixed them:</p><h3>“Permission denied”</h3><p>Forgot to use sudo? Yeah, me too. Always use:</p><pre>sudo ./vpcctl [your command]</pre><h3>Subnets Can’t Talk to Each Other</h3><p>Check if your bridge is actually up:</p><pre>ip link show br-myvpc</pre><p>If it says “DOWN”, fix it:</p><pre>sudo ip link set br-myvpc up</pre><h3>No Internet Access</h3><p>First, check if NAT rules exist:</p><pre>sudo iptables -t nat -L -n | grep 10.0</pre><p>Nothing there? You might need to re-enable NAT. Also check:</p><pre>cat /proc/sys/net/ipv4/ip_forward</pre><p>If that’s 0, turn it on:</p><pre>sudo sysctl -w net.ipv4.ip_forward=1</pre><h3>Something’s Completely Broken</h3><p>When in doubt, nuke everything and start fresh:</p><pre>sudo ./cleanup.sh</pre><p>This deletes all VPC resources. It’s like hitting the reset button.</p><h3>Checking the Logs</h3><p>Everything gets logged to /var/lib/vpcctl/vpcctl.log. Super useful when debugging:</p><pre># Watch logs in real-time<br>sudo tail -f /var/lib/vpcctl/vpcctl.log</pre><pre># Find errors<br>sudo grep ERROR /var/lib/vpcctl/vpcctl.log</pre><pre># See all commands that ran<br>sudo grep &quot;Executing&quot; /var/lib/vpcctl/vpcctl.log</pre><h3>Cleaning Up Your Mess</h3><h3>Delete One VPC</h3><pre>sudo ./vpcctl vpc delete --name myvpc</pre><p>This removes the VPC and all its subnets, routes, everything.</p><h3>Nuclear Option: Delete Everything</h3><pre>sudo ./cleanup.sh</pre><p>This removes ALL VPC resources from your system. You’ll get a confirmation prompt, so don’t worry about accidentally nuking everything.</p><p>After cleanup, verify nothing’s left:</p><pre>sudo ip netns list | grep vpc<br>sudo ip link show | grep br-vpc</pre><p>Should be empty!</p><h3>What You Just Learned</h3><p>Okay, so we’ve built a lot here. Let’s recap:</p><p><strong>Linux Networking:</strong></p><ul><li>Network namespaces give you complete network isolation</li><li>Bridges work like virtual switches and routers</li><li>Veth pairs are virtual ethernet cables</li><li>iptables can do NAT and firewalling</li><li>Static routes control how packets flow</li></ul><p><strong>Cloud Concepts:</strong></p><ul><li>VPCs create isolated virtual networks</li><li>Subnets segment your network for organization and security</li><li>NAT gateways give private resources controlled internet access</li><li>Security groups act as virtual firewalls</li><li>VPC peering connects isolated networks</li></ul><p><strong>Practical Skills:</strong></p><ul><li>You can build infrastructure from scratch</li><li>You understand how cloud providers work under the hood</li><li>You can debug network issues</li><li>You’ve created actual infrastructure code</li></ul><p>Pretty cool, right?</p><h3>What’s Next?</h3><p>Want to take this further? Here are some ideas:</p><ol><li>Add IPv6 support (because the future is now)</li><li>Build a web UI to manage everything visually</li><li>Add bandwidth monitoring per subnet</li><li>Implement route tables for more complex routing</li><li>Create Network ACLs for subnet-level security</li><li>Build a “multi-region” setup with multiple host machines</li></ol><h3>Wrapping Up</h3><p>So there you have it — you just built a VPC from scratch using nothing but Linux! You now understand how cloud providers create isolated networks, how NAT works, how routing decisions are made, and how to secure everything with firewalls.</p><p>The cool thing is, these concepts apply everywhere. Whether you’re working with AWS, Azure, GCP, or even just setting up networks in your homelab, you now understand the fundamentals.</p><p>Hope you had as much fun building this as I did!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7b66f7fda124" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>