A Django REST API and React UI for registering Kubernetes clusters, creating namespaces, and managing application Deployments. Cluster credentials are encrypted at rest with Fernet. PostgreSQL stores control-plane records and application status is read live from Kubernetes.
The API does not provide user authentication. Do not expose it directly to the public internet. Put it behind a private network, VPN, SSO proxy, or add application authentication before public exposure. Configure TLS and authentication for the Ingress controller used by your cluster.
config/ Django project settings, root urls, wsgi/asgi
core/ Shared infrastructure: encryption, dynamic k8s client,
domain exceptions -> HTTP status mapping, log redaction
clusters/ Cluster model + API (pure DB, POST/GET)
namespaces/ Namespace model + API + Kubernetes service layer
(POST/GET/DELETE, concurrency-safe delete)
workloads/ Application (Deployment) model + API + service layer
(POST/GET/PATCH/DELETE, live pod status)Each domain app follows the same shape: models.py (desired state +
status), services.py (Kubernetes I/O, framework-agnostic),
views.py (thin HTTP layer), serializers.py, urls.py, admin.py.
This separation is what makes it easy to later reuse the service layer
from a Celery task, a management command, or a different transport.
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
cp .env.example .env
# Generate and paste a key into .env:
python -c "from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())"
python manage.py migrate
python manage.py createsuperuser # optional, for /admin/
python manage.py runserverRun the UI in a second terminal:
cd frontend
npm ci
npm run devThe Vite server proxies /api to http://127.0.0.1:8000.
cp .env.example .env
# Set DJANGO_SECRET_KEY, FIELD_ENCRYPTION_KEY, and POSTGRES_PASSWORD in .env.
docker compose up --buildOpen http://localhost:8080. Compose runs PostgreSQL, Redis, the backend,
Celery worker, and the frontend. Database and Redis are internal services.
POST /api/clusters/ — persists a cluster row only, no Kubernetes contact.
{"name": "cluster-1", "address": "95.43.54.43:6443", "token": "<bearer token>"}Response (201) never includes the token.
GET /api/clusters/ — list clusters (token never included).
POST /api/namespaces/ — creates the namespace in Kubernetes, then in DB.
{"cluster_id": 1, "name": "new-ns"}404 cluster_not_foundif the cluster id doesn't exist409 namespace_already_existsif it's already there (in k8s or in our DB)502 cluster_unreachableif the cluster's API server can't be reached400for an invalid (non RFC-1123) namespace name
GET /api/namespaces/?cluster_id=1 — namespaces this backend created,
read from the database (source of truth), not live from Kubernetes.
DELETE /api/namespaces/<id>/ — concurrency-safe delete. A namespace
row is locked and flipped to a deleting status before any network
call; a second concurrent DELETE gets 409 namespace_operation_in_progress immediately. Deleting an
already-gone-from-Kubernetes namespace still succeeds (idempotent).
POST /api/apps/ — creates a Deployment in the given namespace.
{
"namespace_id": 3,
"name": "my-app",
"image": "nginx:1.27",
"replicas": 2,
"cpu_request": "100m",
"cpu_limit": "500m",
"memory_request": "128Mi",
"memory_limit": "512Mi"
}GET /api/apps/?namespace_id=3 — desired-state records plus a live
block per app, populated from Kubernetes at request time:
{
"id": 1, "name": "my-app", "namespace": 3, "replicas": 2,
"status": "active",
"live": {
"ready": false,
"ready_replicas": 1,
"desired_replicas": 2,
"pods": [
{"name": "my-app-abc123", "phase": "Running", "ready": true},
{"name": "my-app-def456", "phase": "Pending", "ready": false}
]
}
}PATCH /api/apps/<id>/ — update replicas, image, or resource
requests/limits; patches the Deployment in Kubernetes.
DELETE /api/apps/<id>/ — same concurrency-safe pattern as namespaces.
- Token security: encrypted at rest (Fernet), write-only in the create serializer, excluded from every list/detail serializer and from the Django admin, and redacted from logs.
- Namespace/App create: Kubernetes is called first; if the subsequent DB write fails, a compensating delete is attempted against Kubernetes so nothing is orphaned. If the compensating delete also fails, this is logged loudly for manual/automated reconciliation.
- Concurrent deletes:
select_for_update()inside a short transaction transitionsactive -> deletingbefore any network call, so a second simultaneous DELETE seesdeletingand returns 409 without ever reaching Kubernetes. - Crash between Kubernetes delete and DB delete: the row is left in
deleting. This repo doesn't ship a reconciliation job (out of scope per the assignment), but the service layer (namespace_exists_in_k8s) provides exactly what such a job would need — see the docstring innamespaces/services.py. - Idempotent deletes: deleting something already gone from Kubernetes (404) is treated as success.
- Live status:
workloads/services.get_live_statusalways queries Pods by label selector at request time; nothing about Pod/Deployment readiness is ever read from or written to the database. - Throttling: DRF
ScopedRateThrottleon all write endpoints (cluster-write,namespace-write,app-write), configurable viaREST_FRAMEWORK["DEFAULT_THROTTLE_RATES"]inconfig/settings.py.
deploy/k8s/ is a Kustomize baseline for k3s or Kubernetes on one Linux VM.
It includes PostgreSQL, Redis, two backend replicas, one Celery worker, two
frontend replicas, probes, a PVC, and an Ingress. For critical workloads,
prefer managed PostgreSQL/Redis and an external secret manager.
export REGISTRY=registry.example.com/team
export TAG=$(git rev-parse --short HEAD)
docker build -t "$REGISTRY/k8s-manager-backend:$TAG" .
docker build -t "$REGISTRY/k8s-manager-frontend:$TAG" ./frontend
docker push "$REGISTRY/k8s-manager-backend:$TAG"
docker push "$REGISTRY/k8s-manager-frontend:$TAG"For a single-node k3s VM without a registry, import both images into the node's container runtime and use the same image names in the manifests.
Do not apply deploy/k8s/secret.example.yaml unchanged. Create the Secret
directly so credentials are not committed:
kubectl create namespace k8s-manager
kubectl -n k8s-manager create secret generic k8s-manager-secrets \
--from-literal=DJANGO_SECRET_KEY='<long-random-value>' \
--from-literal=FIELD_ENCRYPTION_KEY='<Fernet-key>' \
--from-literal=POSTGRES_PASSWORD='<long-random-password>'Edit deploy/k8s/app.yaml and set the hostname in its ConfigMap and Ingress.
Use the same HTTPS origin for CORS_ALLOWED_ORIGINS and
CSRF_TRUSTED_ORIGINS.
kubectl -n k8s-manager apply -k deploy/k8s
kubectl -n k8s-manager set image deployment/backend \
backend="$REGISTRY/k8s-manager-backend:$TAG"
kubectl -n k8s-manager set image deployment/worker \
worker="$REGISTRY/k8s-manager-backend:$TAG"
kubectl -n k8s-manager set image deployment/frontend \
frontend="$REGISTRY/k8s-manager-frontend:$TAG"
kubectl -n k8s-manager rollout status deployment/backend
kubectl -n k8s-manager rollout status deployment/frontend
kubectl -n k8s-manager get pods,svc,ingress,pvcEach backend pod runs migrations in an init container before starting Gunicorn.
The frontend proxies /api to the internal backend Service, so the browser
uses one origin. Configure DNS and TLS for the Ingress controller.
The repository includes the VictoriaMetrics/Grafana stack values, a
VMServiceScrape for the deployed backend, and a Grafana operations
dashboard. Install the monitoring stack separately, then apply the two
application monitoring resources:
helm repo add vm https://victoriametrics.github.io/helm-charts
helm repo update
helm upgrade --install monitoring vm/victoria-metrics-k8s-stack \
--namespace monitoring-system --create-namespace \
-f stack-values.yaml
kubectl apply -f django-metrics.yaml
kubectl apply -f grafana-dashboard.yamlReplace grafana.example.com in stack-values.yaml with the real hostname.
Create the grafana-tls Secret through cert-manager or your Ingress
controller, point DNS to the Ingress address, and access Grafana at
https://grafana.example.com. Grafana authentication remains enabled by
default. VictoriaMetrics is intentionally kept internal; use
kubectl port-forward for temporary direct access.
DJANGO_DEBUG=False, explicit DJANGO_ALLOWED_HOSTS, PostgreSQL via
DATABASE_URL, and FIELD_ENCRYPTION_KEY are required in production.
K8S_VERIFY_SSL=True should be used with a trusted CA. Changing the Fernet
key makes existing encrypted cluster tokens unreadable, so back it up with
the database credentials.
Health endpoints are /api/health/ for liveness and
/api/health/ready/ for database readiness. /metrics exposes Prometheus
metrics and should remain internal or be protected at the ingress.
This project includes a simple backups API that enqueues backup tasks
to a Celery worker using Redis as the broker/result backend. SQLite backups
are copied directly; PostgreSQL backups are created with pg_dump.
Run Redis (e.g. via Docker) and start a worker/server locally:
# start redis
docker run -p 6379:6379 --name redis -d redis:7
# install deps
pip install -r requirements.txt
# run Django dev server
python manage.py runserver
# start celery worker (from project root)
celery -A config worker -l infoTrigger a backup via HTTP:
curl -X POST http://localhost:8000/api/backups/The endpoint returns a Celery task id. In local development, the worker will
create a file under backups/. In production, configure S3 because worker
container filesystems are disposable; the task fails instead of reporting a
successful backup when durable remote storage is not configured or an upload
fails.
The included docker-compose.yml now provides redis and a worker
service (Celery) alongside the backend. To run everything together:
export DJANGO_SECRET_KEY=$(python -c "import secrets; print(secrets.token_urlsafe(50))")
export FIELD_ENCRYPTION_KEY=$(python -c "from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())")
# production backups require these S3 settings
export AWS_S3_BUCKET=your-bucket
export AWS_S3_REGION=us-east-1
export AWS_S3_KEY_PREFIX=backups/
docker compose up --buildBackups are uploaded to S3 and the task result includes an s3_url like
s3://your-bucket/backups/backup-.... Configure AWS credentials through the
runtime environment or the workload's IAM role; do not commit credentials.