CoreAPI staging deployment notes
Overview¶
This document covers the troubleshooting and resolution of the CoreAPI staging deployment on a Kubernetes (k3s) cluster.
1. Gateway 502 / No Backend¶
Symptom:
Cause: APISIX could not find a matching backendRef for the route.
Fix: Ensure the HTTPRoute has a valid backendRef pointing to the correct service and port.
2. Database Connection Failure¶
Symptom:
Cause: The app uses individual env vars (DB_HOST, DB_PORT, etc.) to build the DSN. A missing env var (e.g. TICKETING_DB_NAME) caused the DSN parser to shift tokens, treating sslmode=disable as the database name.
DSN format in code (db.go):
dsn := fmt.Sprintf(
"host=%s port=%s user=%s password=%s dbname=%s sslmode=disable TimeZone=UTC",
os.Getenv("DB_HOST"),
os.Getenv("DB_PORT"),
os.Getenv("DB_USER"),
os.Getenv("DB_PASS"),
os.Getenv("DB_NAME"),
)
Fix: Ensure all required env vars are present in the ConfigMap, especially TICKETING_DB_NAME.
3. ConfigMap Setup¶
ConfigMap: coreapi-configmap (namespace: staging)
apiVersion: v1
kind: ConfigMap
metadata:
name: coreapi-configmap
namespace: staging
data:
DB_HOST: postgres-internal.database.svc.cluster.local
DB_PORT: "5432"
DB_USER: postgres
DB_PASS: <DB_PASSWORD>
DB_NAME: coreapi
MINIO_ENDPOINT: stg.s3.castis.io # No https:// prefix
MINIO_ACCESS_KEY: <MINIO_ACCESS_KEY>
MINIO_SECRET_KEY: <MINIO_SECRET_KEY>
MINIO_USE_SSL: "true"
MINIO_BUCKET: cms
ASSET_BASE_URL: http://stg.playout.castis.io
SEAWEEDFS_FILER: http://stg.filer.castis.io
CORS_ORIGIN: "https://stag.tellyid.castis.io,..."
ALLOWED_REDIRECT_URIS: "https://shaka.castis.io"
ConfigMap: coreapi-ticketing (namespace: staging)
apiVersion: v1
kind: ConfigMap
metadata:
name: coreapi-ticketing
namespace: staging
data:
TICKETING_DB_NAME: johorZoo
TICKETING_DB_SCHEMA: johorzoo
TICKETING_BANNER_STORAGE_PATH: /app/storage/Banners
TICKETING_TICKET_GROUP_STORAGE_PATH: /app/storage/Ticket_Group
TICKETING_GROUP_GALLERY_STORAGE_PATH: /app/storage/Group_Gallery
TICKETING_GENERAL_STORAGE_PATH: /app/storage/General
TICKETING_REPORT_STORAGE_PATH: /app/storage/Report
TICKETING_AUTO_MIGRATE: "false"
# ... other ticketing env vars
Deployment envFrom:
4. MinIO Endpoint Format¶
Symptom:
Cause: MINIO_ENDPOINT was set to https://stg.s3.castis.io. The S3/MinIO client does not accept a URL with the https:// prefix — it expects a bare hostname.
Fix:
5. App Listening on Port 8080, Not 3000¶
Symptom: curl stag.api.castis.io returns 502. Port-forward to 3000 fails inside the pod.
Diagnosis: Checked /proc/net/tcp inside the container:
Fix: Update the Deployment and Service to use port 8080, and update the APISIX upstream accordingly.
6. JohorZoo Database Migration¶
6.1 Create the Database¶
kubectl exec -it -n database postgres-0 -- psql -U postgres -c "CREATE ROLE \"johorZoo_admin\";"
kubectl exec -it -n database postgres-0 -- psql -U postgres -c "CREATE DATABASE \"johorZoo\";"
Note: Always quote
johorZoo— Postgres is case-sensitive for database names.
6.2 Run Schema Migration (Ticketingprod.sql)¶
Known warnings (safe to ignore):
- invalid command \restrict / \unrestrict — pg_dump artifacts
- role "johorZoo_admin" does not exist — harmless if app connects as postgres
6.3 Run Data Migration (migration.sql)¶
The migration.sql uses bare table names without schema prefix. Set search_path before running:
(echo "SET search_path TO johorzoo;"; cat ./migration.sql) | \
kubectl exec -i -n database postgres-0 -- psql -U postgres -d "johorZoo"
Why: The schema is johorzoo (lowercase), so UPDATE general fails without the search path set. Setting it tells Postgres to resolve general as johorzoo.general.
For full details on all migration files and run options, see Section 7.
6.4 Make search_path Permanent (Optional)¶
kubectl exec -it -n database postgres-0 -- psql -U postgres -c \
"ALTER DATABASE \"johorZoo\" SET search_path TO johorzoo;"
7. SQL Migration Files¶
The migration consists of three files. Two target PostgreSQL (johorZoo) and must be run in order. One targets ClickHouse and is run independently.
7.1 File Overview & Run Order¶
PostgreSQL — run in this exact order:
| Order | File | Purpose |
|---|---|---|
| 1st | Ticketingprod.sql |
Full schema bootstrap — creates all base tables, roles, indexes |
| 2nd | migration.sql |
App data changes — new tables, column additions, data updates |
migration.sqldepends on tables created byTicketingprod.sql. Always runTicketingprod.sqlfirst on a fresh database.
ClickHouse — run independently:
| File | Purpose |
|---|---|
migration_analytics_events.sql |
Creates analytics.events table (ClickHouse only) |
Do not run
migration_analytics_events.sqlagainst PostgreSQL — it uses ClickHouse-specific syntax (MergeTree,toYYYYMM) that is incompatible.
7.2 Ticketingprod.sql — PostgreSQL (Run First)¶
Full schema bootstrap dump. Run this against a freshly created johorZoo database before anything else.
How to run (via kubectl):
Known warnings (safe to ignore):
- invalid command \restrict / \unrestrict — pg_dump artifacts
- role "johorZoo_admin" does not exist — harmless if app connects as postgres
7.3 migration.sql — PostgreSQL (Run Second)¶
Targets the johorZoo database under the johorzoo schema. Runs inside a single transaction so it rolls back cleanly on any error.
Contents (in execution order):
| # | Statement | Description |
|---|---|---|
| 1 | UPDATE general |
Zoo API credentials and endpoint config |
| 2 | CREATE TABLE advertising_ticket |
New advertising ticket table with type check constraint |
| 3 | CREATE TABLE rail_menu_advertising_ticket |
Junction table linking rail menus to tickets |
| 4 | UPDATE ticket_group |
Sets is_ticket_internal = false for group ID 5 |
| 5 | ALTER TABLE ticket_group + UPDATE |
Adds group_subtext_bm/en/cn columns and populates them |
How to run (via kubectl):
# Recommended: prepend search_path so bare table names resolve to johorzoo schema
(echo "SET search_path TO johorzoo;"; cat ./migration.sql) | \
kubectl exec -i -n database postgres-0 -- psql -U postgres -d "johorZoo"
How to run (direct psql):
Dry-run (verify without committing):
# Wrap in a transaction and ROLLBACK to test safely
(echo "BEGIN;"; echo "SET search_path TO johorzoo;"; cat ./migration.sql; echo "ROLLBACK;") | \
kubectl exec -i -n database postgres-0 -- psql -U postgres -d "johorZoo"
Change
ROLLBACKtoCOMMITonce you are satisfied there are no errors.
7.4 migration_analytics_events.sql — ClickHouse¶
Targets ClickHouse only. Creates the analytics.events table with MergeTree engine, partitioned by month.
How to run:
# Via clickhouse-client file input
clickhouse-client -h <host> --user <user> --password <password> \
--multiquery < migration_analytics_events.sql
# Or inline query
clickhouse-client -h <host> --user <user> --password <password> \
-d analytics \
--query "$(cat migration_analytics_events.sql)"
The
analyticsdatabase must already exist before running. Create it if needed:
7.5 Pre-Migration Checklist¶
Before running any file in production:
-
Back up the database
-
Verify the target database exists
-
Run
Ticketingprod.sqlfirst, then dry-runmigration.sqlbefore committing (see section 7.3 above) -
Check tables after migration
8. Zitadel Migration Job¶
8.1 Architecture Mismatch¶
Symptom:
Cause: Image was built for amd64 only, but mac-mini node runs arm64 (Apple Silicon running Debian).
Fix: Rebuild image for both platforms:
docker buildx build --platform linux/amd64,linux/arm64 \
-f Dockerfile.zitadel \
-t registry.nexus.castis.io/malaysia/ticketing/zitadel-migrate:v1 \
--push .
8.2 Wrong Environment Variable Name¶
Symptom:
Cause: The app reads ZITADEL_MIGRATION_ORG_ID but the secret had ZITADEL_ORG_ID.
Fix: Update the secret to use the correct key:
8.3 Zitadel Migration Secret¶
apiVersion: v1
kind: Secret
metadata:
name: zitadel-migration-secret
namespace: staging
type: Opaque
stringData:
DB_HOST: "postgres-internal.database.svc.cluster.local"
DB_PORT: "5432"
DB_USER: "postgres"
DB_PASSWORD: "<DB_PASSWORD>"
DB_NAME: "johorZoo"
ZITADEL_BASE_URL: "https://stg.zitadel.castis.io"
ZITADEL_MIGRATION_ORG_ID: "377501710811136482" # Note: MIGRATION_ORG_ID not ORG_ID
ZITADEL_PAT: "<your-pat>"
AUTHAPI_BASE_URL: "http://authapi.staging.svc.cluster.local:3002"
MIGRATE_ADMINS: "true"
MIGRATE_CUSTOMERS: "false"
BATCH_DELAY_MS: "10"
9. Cluster Node Architecture¶
| Node | Role | OS | Architecture |
|---|---|---|---|
| castis-desktop | control-plane | Ubuntu 22.04 | amd64 |
| nuc-a | control-plane | Ubuntu 22.04 | amd64 |
| nuc-b | control-plane | Ubuntu 22.04 | amd64 |
| mac-mini | worker | Debian 12 | arm64 |
Images must support
linux/arm64to run onmac-mini.
10. Useful Commands¶
# Check pod env vars
kubectl exec -it deploy/<name> -n <namespace> -- env | grep DB_
# Test DB connectivity from staging namespace
kubectl run pgtest --rm -it --restart=Never -n staging \
--image=postgres:15-alpine \
-- psql "host=postgres-internal.database.svc.cluster.local port=5432 user=postgres password=<DB_PASSWORD> dbname=coreapi sslmode=disable"
# Check what port app is listening on
kubectl exec -it deploy/<name> -n <namespace> -- cat /proc/net/tcp
# List databases in Postgres
kubectl exec -it -n database postgres-0 -- psql -U postgres -c "\l"
# List tables in a schema
kubectl exec -it -n database postgres-0 -- psql -U postgres -d "johorZoo" -c "\dt johorzoo.*"
# Restart a deployment
kubectl rollout restart deploy/<name> -n <namespace>
kubectl rollout status deploy/<name> -n <namespace>
# Verify secret values
kubectl get secret <secret-name> -n <namespace> -o jsonpath='{.data.<KEY>}' | base64 -d
11. Known Non-Critical Issues¶
| Issue | Details |
|---|---|
REPORT_STORAGE_PATH not set |
Background report job fails to save Excel files. Non-blocking. Add REPORT_STORAGE_PATH env var to fix. |
operator does not exist: boolean = integer |
SQL type mismatch in order processing query. Needs code fix. |
Intermittent no route to host to Nexus registry |
Network instability to <DO_PUBLIC_IP>. Transient, retries resolve it. |