# Microservices Deployment Example

Deploy a complete microservices architecture.

## Architecture

```text
┌────────────────────────────────────┐
│      API Gateway / Load Balancer   │
└──────┬───────────────┬─────────────┘
       │               │
   ┌───▼─┐         ┌──▼───┐
   │Auth │         │Order │
   │Svc  │         │Svc   │
   └─────┘         └──────┘
       │               │
   ┌───▼───────────────▼────┐
   │    Shared Services     │
   ├────────────────────────┤
   │ - PostgreSQL (Primary) │
   │ - Redis (Cache)        │
   │ - Message Queue        │
   └────────────────────────┘
```

## Kubernetes Configuration

```nickel
{
  kubernetes = {
    namespace = "production",

    services = [
      {
        name = "auth-service",
        image = "myregistry/auth:1.0.0",
        replicas = 3,
        port = 8001,
        health_check = { path = "/health", interval = 10 }
      },
      {
        name = "order-service",
        image = "myregistry/order:1.0.0",
        replicas = 5,
        port = 8002,
        health_check = { path = "/health", interval = 10 }
      },
      {
        name = "user-service",
        image = "myregistry/user:1.0.0",
        replicas = 3,
        port = 8003,
        health_check = { path = "/health", interval = 10 }
      }
    ],

    shared_dependencies = {
      database = {
        engine = "postgres",
        replicas = 3
      },
      cache = {
        engine = "redis",
        cluster_mode = true
      },
      message_queue = {
        engine = "kafka",
        partitions = 10
      }
    },

    ingress = {
      host = "api.example.com",
      tls = true
    }
  }
}
```

## Deployment Process

```bash
#!/usr/bin/env nu

def deploy-microservices [] {
  print "Deploying microservices architecture..."

  # Create namespace
  provisioning k8s create-namespace production

  # Deploy database first
  provisioning infra apply --config db-config.ncl

  # Deploy cache
  provisioning infra apply --config cache-config.ncl

  # Deploy services (order matters due to dependencies)
  provisioning infra apply --config auth-service.ncl
  provisioning infra apply --config order-service.ncl
  provisioning infra apply --config user-service.ncl

  # Deploy API gateway
  provisioning infra apply --config api-gateway.ncl

  # Verify all services
  sleep 30sec
  provisioning test smoke-tests --namespace production

  print "✓ Microservices deployed successfully"
}

deploy-microservices
```

## Service Communication

```nickel
{
  service_mesh = {
    enabled = true,
    type = "istio",

    policies = [
      {
        source = "auth-service",
        destination = "order-service",
        allowed = true,
        retry = { max_attempts = 3 }
      },
      {
        source = "order-service",
        destination = "user-service",
        allowed = true,
        retry = { max_attempts = 3 }
      }
    ]
  }
}
```

## Related Documentation

- [High Availability](./high-availability-examples.md)
- [Kubernetes Guide](../infrastructure/clusters.md)
- [Monitoring](./monitoring-examples.md)
