Skip to content

Migrate the CDN stack from Docker Compose to k3s

Overview

This wiki documents the migration of the CDN stack (streamer, cproxy_shield, cproxy_nuc, elb, gslb) from Docker Compose to k3s, including routing, TLS, WebSocket, and troubleshooting notes.


Architecture

┌─────────────────────────────────────────────┐
│           Digital Ocean Cluster             │
│                                             │
│  Internet                                   │
│     │                                       │
│     ▼                                       │
│  APISIX Gateway                             │
│     │  gslb-do.cdn.castis.io                │
│     ▼                                       │
│  GSLB :8080                                 │
│     │  redirect to elb-do.cdn.castis.io     │
│     ▼                                       │
│  APISIX Gateway                             │
│     │  elb-do.cdn.castis.io                 │
│     ▼                                       │
│  ELB :8080                                  │
│     │  redirect to cproxy-nuc-01            │
└─────────────────────────────────────────────┘
                     │
                     │ (internet)
                     ▼
┌─────────────────────────────────────────────┐
│              NUC Cluster                    │
│                                             │
│  APISIX Gateway                             │
│     │  cproxy-nuc-01.cdn.castis.io          │
│     ▼                                       │
│  cproxy-nuc :8080                           │
│     │  origin: shield-ct1-01                │
│     ▼                                       │
│  APISIX Gateway                             │
│     │  shield-ct1-01.cdn.castis.io          │
│     ▼                                       │
│  cproxy-shield :8080                        │
│     │  origin: streamer:18080               │
│     ▼                                       │
│  streamer :18080                            │
│     │                                       │
│     ▼                                       │
│  SeaweedFS :8333                            │
└─────────────────────────────────────────────┘

Namespaces

Namespace Services
cdn streamer, cproxy-shield, cproxy-nuc, elb, gslb
seaweedfs-staging seaweedfs
apisix apisix-gateway

Services

streamer

Item Value
Image registry.nexus.castis.io/cdn/streamer:v1.2.23
Platform linux/amd64
Ports 18080 (http), 18081 (management)
Config Mounted via ConfigMap at /castis/bin/streamer/streamer.yml

Files: configmap.yaml, deployment.yaml, service.yaml

Internal DNS:

streamer.cdn.svc.cluster.local:18080


cproxy-shield (shield)

Item Value
Image registry.nexus.castis.io/cdn/cproxy:v1.1.5
Platform linux/amd64
Ports 8080 (http), 8081 (management)
External URL shield-ct1-01.cdn.castis.io
Origin streamer.cdn.svc.cluster.local:18080

Startup command:

cp /tmp/cproxy-init.yml /castis/bin/cproxy/cproxy.yml && ./cproxy

Key config:

origins:
  - key: placeholder
    url-pattern: ^/.*$
    origin-urls:
      - url: http://streamer.cdn.svc.cluster.local:18080
        host: streamer.cdn.svc.cluster.local
    memcache-config-id: mem-cache
    ttl: 1s
    custom-headers:
      - key: Access-Control-Allow-Origin
        value: "*"
        override: true
      - key: Access-Control-Allow-Headers
        value: "*"
        override: true
      - key: Access-Control-Allow-Methods
        value: GET,HEAD,OPTIONS
        override: true

Internal DNS:

cproxy-shield.cdn.svc.cluster.local:8080


cproxy-nuc

Item Value
Image registry.nexus.castis.io/cdn/cproxy:v1.1.5
Platform linux/amd64
Ports 8080 (http), 8081 (management)
External URL cproxy-nuc-01.cdn.castis.io
Origin shield-ct1-01.cdn.castis.io

Key config:

origins:
  - key: origin_shield
    url-pattern: ^/.*$
    origin-urls:
      - url: https://shield-ct1-01.cdn.castis.io
        host: shield-ct1-01.cdn.castis.io
    redirect-map:
      - url-pattern: ^(https?://[^/]+)(/.*)?$
        mapping-key: $2
    tls-insecure-skip-verify: true
    memcache-config-id: mem-cache
    ttl: 1s

Internal DNS:

cproxy-nuc.cdn.svc.cluster.local:8080


ELB

Item Value
Image registry.nexus.castis.io/cdn/elb:v1.0.3
Platform linux/amd64
Ports 8080 (http), 8081 (management)
External URL elb-do.cdn.castis.io
Balance round-robin

Startup command:

cp /tmp/elb-init.yml /castis/bin/elb/elb.yml && ./elb

Key config:

balance: round-robin
nodes:
  - id: cproxy-nuc-01
    websocket-url: "ws://cproxy-nuc-01.cdn.castis.io/api/traffic"
    bandwidth-limit-bps: 16000000
    bandwidth-limit-percent: 90
    redirect-map:
      - listen-addresses: [:8080]
        hosts: [elb-do.cdn.castis.io]
        redirect-https: false
        redirect-address: "cproxy-nuc-01.cdn.castis.io"

Internal DNS:

elb.cdn.svc.cluster.local:8080


GSLB

Item Value
Image registry.nexus.castis.io/cdn/elb:v1.0.3
Platform linux/amd64
Ports 8080 (http), 8081 (management)
External URL gslb-do.cdn.castis.io
Balance round-robin

GSLB is the first hop from the internet — same config as ELB but points to ELB instead of cproxy.

Startup command:

cp /tmp/gslb-init.yml /castis/bin/elb/elb.yml && ./elb

Key config:

balance: round-robin
nodes:
  - id: elb-do
    websocket-url: "ws://elb-do.cdn.castis.io/api/traffic"
    redirect-map:
      - listen-addresses: [:8080]
        hosts: [gslb-do.cdn.castis.io]
        redirect-https: false
        redirect-address: "elb-do.cdn.castis.io"

Internal DNS:

gslb.cdn.svc.cluster.local:8080

WebSocket — same as ELB, register via APISIX Admin API:

curl -X PUT http://127.0.0.1:9180/apisix/admin/routes/gslb-management \
  -H "X-API-KEY: <APISIX_ADMIN_KEY>" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "gslb-management",
    "uri": "/api/*",
    "host": "gslb-do.cdn.castis.io",
    "enable_websocket": true,
    "status": 1,
    "upstream": {
      "type": "roundrobin",
      "scheme": "http",
      "pass_host": "pass",
      "nodes": {
        "gslb.cdn.svc.cluster.local:8081": 1
      }
    }
  }'


ConfigMap & Volume Mount

ConfigMaps replace bind mounts from Docker Compose.

# ConfigMap key name
data:
  cproxy.yml: |       # ← this is the "subPath"
    ...

# In deployment
volumeMounts:
  - name: config
    mountPath: /tmp/cproxy-init.yml   # path inside container (can differ)
    subPath: cproxy.yml               # must match the key name in ConfigMap
  • mountPath — where the file appears inside the container
  • subPath — which key to pick from the ConfigMap (mounts single file, not whole directory)

Update config and restart:

kubectl apply -f configmap.yaml
kubectl rollout restart deployment <name> -n cdn


Node Scheduling

Nodes in the cluster:

Node Arch CPU% Memory%
mac-mini ARM64 2% 28%
nuc-a amd64 8% 88%
nuc-b amd64 10% 73%

Images are built for linux/amd64 only — mac-mini (ARM64) will fail with:

no match for platform in manifest: not found

Always use:

nodeSelector:
  kubernetes.io/arch: amd64

To pin to a specific node:

nodeSelector:
  kubernetes.io/hostname: nuc-b


SeaweedFS

Item Value
Namespace seaweedfs-staging
S3 Port 8333
Master Port 9333

Internal DNS from cdn namespace:

seaweedfs.seaweedfs-staging.svc.cluster.local:8333

Common Issue — s3.json syntax error

SeaweedFS crashes with:

proto: syntax error (line 19:5): unexpected token {

Cause: missing comma between JSON objects in /etc/seaweedfs/s3.json.

// Wrong
{ "name": "admin", ... }
{ "name": "anonymous", ... }

// Correct
{ "name": "admin", ... },   ← comma here
{ "name": "anonymous", ... }

Fix: update the ConfigMap and delete the pod (rollout restart won't work when pod is in CrashLoopBackOff):

kubectl apply -f seaweedfs-s3-configmap.yaml
kubectl delete pod seaweedfs-0 -n seaweedfs-staging


APISIX Routing

External URLs

URL Service Port
elb-do.cdn.castis.io elb 8080
elb-do.cdn.castis.io/api/* elb 8081
cproxy-nuc-01.cdn.castis.io cproxy-nuc 8080
cproxy-nuc-01.cdn.castis.io/api/* cproxy-nuc 8081 (WebSocket)
shield-ct1-01.cdn.castis.io cproxy-shield 8080
shield-ct1-01.cdn.castis.io/api/* cproxy-shield 8081

WebSocket — ELB to cproxy-nuc

ELB requires WebSocket to monitor cproxy traffic stats. Since APISIX >2.0 does not support websocket: true via CRD, it must be registered via Admin API:

# Port forward APISIX admin
kubectl port-forward -n apisix svc/apisix-admin 9180:9180

# Register route with websocket enabled
curl -X PUT http://127.0.0.1:9180/apisix/admin/routes/cproxy-nuc-1-management \
  -H "X-API-KEY: <APISIX_ADMIN_KEY>" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "cproxy-nuc-1-management",
    "uri": "/api/*",
    "host": "cproxy-nuc-01.cdn.castis.io",
    "enable_websocket": true,
    "status": 1,
    "upstream": {
      "type": "roundrobin",
      "scheme": "http",
      "pass_host": "pass",
      "nodes": {
        "cproxy-nuc.cdn.svc.cluster.local:8081": 1
      }
    }
  }'

Route File: cproxy-tls-route.yaml (shield-ct1-01)

apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: shield-ct1-01-cert
  namespace: cdn
spec:
  secretName: shield-ct1-01-tls-secret
  issuerRef:
    name: letsencrypt-prod
    kind: ClusterIssuer
  dnsNames:
    - shield-ct1-01.cdn.castis.io
---
apiVersion: apisix.apache.org/v2
kind: ApisixTls
metadata:
  name: shield-ct1-01-tls
  namespace: cdn
spec:
  hosts:
    - shield-ct1-01.cdn.castis.io
  secret:
    name: shield-ct1-01-tls-secret
    namespace: cdn
---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
  name: shield-ct1-01-route
  namespace: cdn
spec:
  http:
    - name: shield-ct1-01-rule
      match:
        hosts:
          - shield-ct1-01.cdn.castis.io
        paths:
          - "/*"
      backends:
        - serviceName: cproxy
          servicePort: 8080
      plugins:
        - name: redirect
          enable: true
          config:
            http_to_https: true
        - name: proxy-rewrite
          enable: true
          config:
            headers:
              set:
                X-Forwarded-Proto: "https"
                X-Forwarded-Host: "shield-ct1-01.cdn.castis.io"
---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
  name: shield-ct1-01-management-route
  namespace: cdn
spec:
  http:
    - name: shield-ct1-01-management-rule
      match:
        hosts:
          - shield-ct1-01.cdn.castis.io
        paths:
          - "/api/*"
      backends:
        - serviceName: cproxy
          servicePort: 8081
      plugins:
        - name: redirect
          enable: true
          config:
            http_to_https: true
        - name: proxy-rewrite
          enable: true
          config:
            headers:
              set:
                X-Forwarded-Proto: "https"
                X-Forwarded-Host: "shield-ct1-01.cdn.castis.io"

TLS — Wildcard Limitation

Challenge Type Supports Wildcard
HTTP-01 No
DNS-01 Yes

GoDaddy has no official cert-manager DNS-01 webhook, so *.cdn.castis.io won't work. Workaround: list each subdomain explicitly in dnsNames.

Long-term recommendation: move DNS to Cloudflare (keep GoDaddy as registrar, point nameservers to Cloudflare) — native cert-manager DNS-01 support, free tier.


Troubleshooting

cproxy — origin timeout

failed to doHttp, timeout
Check the host field in origin-urls. cproxy passes the client Host header to the origin by default, which causes APISIX to return 404 if the host doesn't match any route. Fix:
origin-urls:
  - url: https://shield-ct1-01.cdn.castis.io
    host: shield-ct1-01.cdn.castis.io   # ← force correct host header

cproxy — redirect-map $1 vs $2

redirect-map:
  - url-pattern: ^(https?://[^/]+)(/.*)?$
    mapping-key: $2   # $1 = host, $2 = path
Always use $2 to extract the path as the cache key. Using $1 extracts the host which breaks caching.

ELB — WebSocket bad handshake

[cproxy-nuc-01] failed to connect, websocket: bad handshake
selected nothing → 503
ELB requires WebSocket to connect to cproxy before marking the node as active. Register the /api/* route with enable_websocket: true via APISIX Admin API (see WebSocket section above).

ELB — invalid redirect-address

invalid redirect-address, id[cproxy-nuc-01], localAddr[...], host[elb-do.cdn.castis.io]
The hosts in redirect-map must match the incoming request host exactly:
redirect-map:
  - listen-addresses: [:8080]
    hosts: [elb-do.cdn.castis.io]   # must match incoming Host header
    redirect-https: false
    redirect-address: "cproxy-nuc-01.cdn.castis.io"

SeaweedFS — CrashLoopBackOff s3.json

proto: syntax error (line 19:5): unexpected token {
Missing comma between identity objects in s3.json. See SeaweedFS section above.

Pod on wrong node / ImagePullBackOff

no match for platform in manifest: not found
mac-mini is ARM64 — all CDN images are amd64 only. Always set nodeSelector: kubernetes.io/arch: amd64.


Apply Order

# SeaweedFS
kubectl apply -f services/storage/seaweedfs/

# Streamer
kubectl apply -f services/cdn/streamer/

# cproxy-shield (shield)
kubectl apply -f services/cdn/cproxy-shield/

# cproxy-nuc
kubectl apply -f services/cdn/cproxy-nuc/

# ELB
kubectl apply -f services/cdn/elb/

# Routes
kubectl apply -f services/cdn/cproxy-shield/cproxy-tls-route.yaml
kubectl apply -f services/cdn/cproxy-nuc/cproxy-nuc-route.yaml
kubectl apply -f services/cdn/elb/elb-route.yaml

# WebSocket route via Admin API (must be done manually)
kubectl port-forward -n apisix svc/apisix-admin 9180:9180
# then run the curl command in WebSocket section

Useful Commands

# Watch pods
kubectl get pods -n cdn -w

# Check logs
kubectl logs -n cdn <pod-name>

# Force restart (when CrashLoopBackOff)
kubectl delete pod <pod-name> -n <namespace>

# Rolling restart
kubectl rollout restart deployment <name> -n <namespace>

# Check node resources
kubectl top node

# Exec into pod
kubectl exec -it <pod-name> -n <namespace> -- bash

# Delete all resources in a folder
kubectl delete -f services/cdn/streamer/

# APISIX Admin API
kubectl port-forward -n apisix svc/apisix-admin 9180:9180
curl http://127.0.0.1:9180/apisix/admin/routes -H "X-API-KEY: <APISIX_ADMIN_KEY>"