Skip to main content
This page documents all Deployment manifests used in the exchange platform. Deployments manage the desired state of Pods and provide declarative updates for applications.

Backend Router Deployment

The backend router handles HTTP API requests and routes them to appropriate services. Location: backend/deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: exchange-router-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: exchange-router
  template:
    metadata:
      labels:
        app: exchange-router
    spec:
      containers:
        - name: exchange-router
          image: jogeshwar01/exchange-router:ed9f044dc79ee713da9518648524e0c68a70ddf7
          ports:
            - containerPort: 8080
          resources:
            requests:
              cpu: "300m"
            limits:
              cpu: "2000m"
          env:
            - name: SERVER_ADDR
              valueFrom:
                secretKeyRef:
                  name: exchange-router-secret
                  key: server_addr
            - name: DATABASE_URL
              valueFrom:
                secretKeyRef:
                  name: exchange-router-secret
                  key: database_url
            - name: REDIS_URL
              valueFrom:
                secretKeyRef:
                  name: exchange-router-secret
                  key: redis_url
Key Features:
  • Resource Management: CPU requests of 300m with limits up to 2000m for handling variable load
  • Secret Management: All sensitive configuration loaded from exchange-router-secret
  • Single Replica: Runs one instance (scaled via HPA when needed)
  • Port Configuration: Exposes container port 8080

WebSocket Stream Deployment

Handles real-time WebSocket connections for live market data streaming. Location: websocket/deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: exchange-ws-stream-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: exchange-ws-stream
  template:
    metadata:
      labels:
        app: exchange-ws-stream
    spec:
      containers:
        - name: exchange-ws-stream
          image: jogeshwar01/exchange-ws-stream:ed9f044dc79ee713da9518648524e0c68a70ddf7
          ports:
            - containerPort: 4000
          env:
            - name: WS_STREAM_URL
              valueFrom:
                configMapKeyRef:
                  name: exchange-ws-stream-config
                  key: ws_stream_url
            - name: REDIS_URL
              valueFrom:
                configMapKeyRef:
                  name: exchange-ws-stream-config
                  key: redis_url
Key Features:
  • ConfigMap Configuration: Non-sensitive config from exchange-ws-stream-config
  • WebSocket Port: Listens on port 4000 for WebSocket connections
  • Redis Integration: Connects to Redis for pub/sub messaging
  • Stateless Design: Single replica for WebSocket handling

PostgreSQL Database Deployment

Stateful deployment for the PostgreSQL database with persistent storage. Location: postgres-db/deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: exchange-postgres-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: exchange-postgres
  template:
    metadata:
      labels:
        app: exchange-postgres
    spec:
      containers:
        - name: exchange-postgres
          image: postgres:12.2
          ports:
            - containerPort: 5432
          env:
            - name: POSTGRES_USER
              valueFrom:
                configMapKeyRef:
                  name: exchange-postgres-config
                  key: POSTGRES_USER
            - name: POSTGRES_PASSWORD
              valueFrom:
                configMapKeyRef:
                  name: exchange-postgres-config
                  key: POSTGRES_PASSWORD
            - name: POSTGRES_DB
              valueFrom:
                configMapKeyRef:
                  name: exchange-postgres-config
                  key: POSTGRES_DB
          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              subPath: postgres-data
              name: postgres-storage
      volumes:
        - name: postgres-storage
          persistentVolumeClaim:
            claimName: postgres-pvc
Key Features:
  • Persistent Storage: Mounts PVC for data persistence across pod restarts
  • SubPath Mount: Uses postgres-data subPath to isolate database files
  • Standard Image: Uses official PostgreSQL 12.2 image
  • Configuration: Database credentials loaded from ConfigMap
  • Single Instance: One replica for database consistency

Redis Deployment

In-memory data store for caching and pub/sub messaging. Location: redis/deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: exchange-redis-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: exchange-redis
  template:
    metadata:
      labels:
        app: exchange-redis
    spec:
      containers:
        - name: exchange-redis
          image: redis:6.2-alpine
          ports:
            - containerPort: 6379
          volumeMounts:
            - mountPath: /data
              subPath: redis-data
              name: redis-storage
      volumes:
        - name: redis-storage
          persistentVolumeClaim:
            claimName: redis-pvc
Key Features:
  • Alpine Image: Lightweight Redis 6.2 Alpine variant
  • Persistent Storage: Data persists across restarts via PVC
  • Standard Port: Exposes Redis on default port 6379
  • SubPath Mount: Isolates Redis data in redis-data subdirectory

Engine Deployment

Core matching engine that processes order book operations. Location: engine/deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: exchange-engine-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: exchange-engine
  template:
    metadata:
      labels:
        app: exchange-engine
    spec:
      containers:
        - name: exchange-engine
          image: jogeshwar01/exchange-engine:ed9f044dc79ee713da9518648524e0c68a70ddf7
          env:
            - name: DATABASE_URL
              valueFrom:
                configMapKeyRef:
                  name: exchange-engine-config
                  key: database_url
            - name: REDIS_URL
              valueFrom:
                configMapKeyRef:
                  name: exchange-engine-config
                  key: redis_url
Key Features:
  • In-Memory Processing: No persistent storage (stateless)
  • Database Connection: Connects to PostgreSQL for order persistence
  • Redis Integration: Uses Redis for pub/sub and caching
  • ConfigMap Configuration: Database and Redis URLs from ConfigMap

Database Processor Deployment

Asynchronous worker that processes database operations from Redis queue. Location: db-processor/deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: exchange-db-processor-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: exchange-db-processor
  template:
    metadata:
      labels:
        app: exchange-db-processor
    spec:
      containers:
        - name: exchange-db-processor
          image: jogeshwar01/exchange-db-processor:ed9f044dc79ee713da9518648524e0c68a70ddf7
          env:
            - name: DATABASE_URL
              valueFrom:
                configMapKeyRef:
                  name: exchange-db-processor-config
                  key: database_url
            - name: REDIS_URL
              valueFrom:
                configMapKeyRef:
                  name: exchange-db-processor-config
                  key: redis_url
Key Features:
  • Queue Worker: Processes messages from Redis queue
  • Database Writer: Writes processed data to PostgreSQL
  • Stateless Design: No local storage requirements
  • ConfigMap Configuration: Connection strings from ConfigMap

Common Deployment Patterns

Labels and Selectors

All deployments use consistent labeling:
  • app: <service-name> - Used for pod selection and service routing
  • Labels match between selector.matchLabels and template.metadata.labels

Environment Configuration

Two approaches used across deployments:
  1. Secrets (backend router):
    valueFrom:
      secretKeyRef:
        name: exchange-router-secret
        key: server_addr
    
  2. ConfigMaps (other services):
    valueFrom:
      configMapKeyRef:
        name: exchange-engine-config
        key: database_url
    

Replica Strategy

All deployments currently run with replicas: 1:
  • Backend router uses HPA for auto-scaling
  • Stateful services (database, Redis) require single instance
  • Other services can be manually scaled if needed

Deployment Operations

Apply a Deployment

kubectl apply -f deployment.yml

Check Deployment Status

kubectl get deployments
kubectl describe deployment exchange-router-deployment

Scale a Deployment

kubectl scale deployment exchange-router-deployment --replicas=3

Update Container Image

kubectl set image deployment/exchange-router-deployment \
  exchange-router=jogeshwar01/exchange-router:new-tag

View Deployment Logs

kubectl logs -l app=exchange-router --tail=100 -f

Best Practices

  1. Resource Limits: Define CPU/memory requests and limits for predictable scheduling
  2. Health Checks: Add readiness and liveness probes for production deployments
  3. Image Tags: Use specific commit SHAs instead of latest for reproducibility
  4. Configuration Management: Use ConfigMaps for non-sensitive data, Secrets for sensitive data
  5. Rolling Updates: Default strategy ensures zero-downtime deployments
  6. Persistent Data: Use PVCs for stateful services (database, cache)
  • Services - Service definitions for these deployments
  • HPA - Auto-scaling configuration
  • PVC - Persistent storage configuration

Build docs developers (and LLMs) love