# Advanced Networking Guide

Implement complex networking topologies including multi-region, hybrid cloud, and service mesh.

## Network Architecture Patterns

### Hub-and-Spoke Model

Central hub connects to multiple spokes (regions/environments):

```nickel
{
  network = {
    model = "hub-and-spoke",

    hub = {
      name = "central-hub",
      vpc_cidr = "10.0.0.0/16",
      region = "us-east-1",
      role = "transit-hub"
    },

    spokes = [
      {
        name = "production-spoke",
        vpc_cidr = "10.1.0.0/16",
        region = "us-east-1"
      },
      {
        name = "staging-spoke",
        vpc_cidr = "10.2.0.0/16",
        region = "us-west-2"
      },
      {
        name = "onprem-spoke",
        vpc_cidr = "172.16.0.0/16",
        connection_type = "vpn"
      }
    ],

    transit_gateway = {
      enabled = true,
      asn = 64512,
      route_tables = [
        {
          name = "hub-routes",
          routes = [
            { destination = "10.0.0.0/8", target = "hub" }
          ]
        },
        {
          name = "spoke-routes",
          routes = [
            { destination = "10.0.0.0/16", target = "hub" }
          ]
        }
      ]
    }
  }
}
```

### Mesh Network Model

Every node connects to multiple others for resilience:

```nickel
{
  network = {
    model = "mesh",

    mesh_nodes = [
      {
        name = "us-east-1",
        vpc_cidr = "10.1.0.0/16",
        peers = ["us-west-2", "eu-west-1", "ap-southeast-1"]
      },
      {
        name = "us-west-2",
        vpc_cidr = "10.2.0.0/16",
        peers = ["us-east-1", "eu-west-1", "ap-southeast-1"]
      },
      {
        name = "eu-west-1",
        vpc_cidr = "10.3.0.0/16",
        peers = ["us-east-1", "us-west-2", "ap-southeast-1"]
      },
      {
        name = "ap-southeast-1",
        vpc_cidr = "10.4.0.0/16",
        peers = ["us-east-1", "us-west-2", "eu-west-1"]
      }
    ],

    peering = {
      encryption = "ipsec",
      bandwidth_limit = "10Gbps",
      failover_enabled = true
    }
  }
}
```

## Load Balancing Strategies

### Global Load Balancing

```nushell
def configure-global-load-balancer [] {
    print "Configuring global load balancer..."

    provisioning lb create \
        --name global-lb \
        --type global \
        --algorithm latency-based

    # Add endpoints in multiple regions
    provisioning lb add-endpoint \
        --lb global-lb \
        --region us-east-1 \
        --target us-east-1-alb.elb.amazonaws.com \
        --weight 40

    provisioning lb add-endpoint \
        --lb global-lb \
        --region eu-west-1 \
        --target eu-west-1-alb.elb.eu-west-1.amazonaws.com \
        --weight 35

    provisioning lb add-endpoint \
        --lb global-lb \
        --region ap-southeast-1 \
        --target ap-southeast-1-alb.elb.ap-southeast-1.amazonaws.com \
        --weight 25

    # Health checks
    provisioning lb health-check configure \
        --lb global-lb \
        --path "/health" \
        --interval 10 \
        --timeout 5 \
        --healthy-threshold 2 \
        --unhealthy-threshold 3
}
```

### Rate Limiting and DDoS Protection

```nickel
{
  load_balancer = {
    advanced_features = {
      rate_limiting = {
        enabled = true,
        rules = [
          {
            name = "api_rate_limit",
            path = "/api/*",
            requests_per_second = 1000,
            burst_size = 2000
          },
          {
            name = "login_rate_limit",
            path = "/login",
            requests_per_second = 10,
            burst_size = 20,
            by_ip = true
          }
        ]
      },

      ddos_protection = {
        enabled = true,
        level = "high",
        auto_mitigation = true,
        managed_rules = true
      },

      waf = {
        enabled = true,
        rules = [
          {
            name = "sql_injection_protection",
            enabled = true
          },
          {
            name = "xss_protection",
            enabled = true
          },
          {
            name = "cors_enforcement",
            enabled = true,
            allowed_origins = [" [https://example.com"]](https://example.com"])
          }
        ]
      }
    }
  }
}
```

## Service Mesh Integration

### Istio Service Mesh

```nushell
def deploy-service-mesh [] {
    print "Deploying Istio service mesh..."

    # Install Istio
    provisioning istio install \
        --namespace istio-system \
        --profile production \
        --enable-sidecar-injection

    # Create virtual services for inter-service communication
    provisioning virtualservice create \
        --name api-service \
        --namespace production \
        --hosts ["api.internal"] \
        --destinations [
            { host = "api-v1", subset = "v1", weight = 80 },
            { host = "api-v2", subset = "v2", weight = 20 }
        ]

    # Define destination rules for load balancing
    provisioning destinationrule create \
        --name api-service \
        --namespace production \
        --host "api-service" \
        --traffic_policy {
            connection_pool: {
                tcp: { max_connections: 100 },
                http: { http1_max_pending_requests: 100 }
            },
            load_balancer: { simple: "LEAST_CONN" }
        }

    # Configure circuit breaker
    provisioning circuit-breaker create \
        --service api-service \
        --consecutive_errors 5 \
        --interval 30s \
        --max_requests 100
}
```

## DNS and Traffic Management

### Multi-Region DNS

```nushell
def setup-multiregion-dns [] {
    print "Setting up multi-region DNS..."

    # Primary region
    provisioning dns record create \
        --zone example.com \
        --name api \
        --type A \
        --value "10.0.1.10" \
        --region us-east-1 \
        --set-id "primary" \
        --failover-type PRIMARY

    # Secondary region (failover)
    provisioning dns record create \
        --zone example.com \
        --name api \
        --type A \
        --value "10.2.1.10" \
        --region eu-west-1 \
        --set-id "secondary" \
        --failover-type SECONDARY

    # Health check for failover
    provisioning dns health-check create \
        --name api-health \
        --type HTTP \
        --ip-address "10.0.1.10" \
        --port 80 \
        --path "/health" \
        --interval 30 \
        --failure-threshold 3
}
```

## Network Security

### Zero-Trust Network Access

```nickel
{
  network_security = {
    zero_trust = {
      enabled = true,
      principles = [
        "verify-every-access",
        "encrypt-all-traffic",
        "minimize-exposure",
        "assume-breach"
      ]
    },

    network_zones = [
      {
        name = "public",
        cidr = "10.0.1.0/24",
        security_level = "high",
        allowed_inbound = [
          { source = "0.0.0.0/0", port = 443, protocol = "tcp" }
        ]
      },
      {
        name = "app",
        cidr = "10.0.2.0/24",
        security_level = "very-high",
        allowed_inbound = [
          { source = "10.0.1.0/24", port = 8080, protocol = "tcp" }
        ]
      },
      {
        name = "database",
        cidr = "10.0.3.0/24",
        security_level = "critical",
        allowed_inbound = [
          { source = "10.0.2.0/24", port = 5432, protocol = "tcp" }
        ]
      }
    ]
  }
}
```

### Network Policies

```yaml
# Kubernetes network policies for zero-trust
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: deny-all
spec:
  podSelector: {}
  policyTypes:
  - Ingress
  - Egress

---

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-api-to-database
spec:
  podSelector:
    matchLabels:
      app: database
  policyTypes:
  - Ingress
  ingress:
  - from:
    - podSelector:
        matchLabels:
          app: api
    ports:
    - protocol: TCP
      port: 5432
```

## Performance Optimization

### Edge Caching and CDN

```nickel
{
  cdn = {
    enabled = true,
    provider = "cloudfront",

    distributions = [
      {
        name = "api-cdn",
        origins = [
          {
            name = "primary",
            domain = "api-us-east-1.example.com"
          }
        ],

        caching = {
          default_ttl = 300,
          max_ttl = 3600,
          compress = true,
          cache_policy = "caching-optimized"
        },

        edge_locations = "all",
        enable_http2 = true
      }
    ]
  }
}
```

### Bandwidth Optimization

```nushell
def optimize-bandwidth [] {
    print "Optimizing bandwidth usage..."

    # Enable compression
    provisioning cdn compression enable \
        --distribution api-cdn \
        --types "text/*", "application/json", "application/javascript"

    # Set up adaptive bitrate streaming
    provisioning cdn adaptive-bitrate enable \
        --distribution media-cdn

    # Monitor bandwidth usage
    provisioning cdn bandwidth monitor \
        --distribution api-cdn \
        --alert-threshold "80%" \
        --duration "24h"
}
```

## Troubleshooting Network Issues

```nushell
def diagnose-network-connectivity [--target: string] {
    print $"Diagnosing connectivity to ($target)..."

    # DNS resolution
    let dns_test = (
        provisioning network test dns \
            --hostname $target
    )
    print $"DNS: ($dns_test.status) - ($dns_test.latency)ms"

    # TCP connectivity
    let tcp_test = (
        provisioning network test tcp \
            --host $target \
            --port 443
    )
    print $"TCP: ($tcp_test.status)"

    # TLS/SSL
    let ssl_test = (
        provisioning network test ssl \
            --host $target \
            --port 443
    )
    print $"TLS: ($ssl_test.status)"

    # HTTP
    let http_test = (
        provisioning network test http \
            --url $" [https://($targe](https://($targe)t)" \
            --timeout 10
    )
    print $"HTTP: ($http_test.status_code) - ($http_test.latency)ms"

    # Traceroute
    let trace = (
        provisioning network trace \
            --host $target \
            --max-hops 20
    )
    print "Trace path:"
    $trace.hops | each { | hop |
        print $"  ($hop.number): ($hop.host) ($hop.latency)ms"
    }
}
```

## See Also

- [Networking Infrastructure](./networking.md)
- [Disaster Recovery Guide](../guides/disaster-recovery.md)
- [Hybrid Cloud Deployment](../guides/hybrid-cloud-deployment.md)
