feat: Update Docker and Kubernetes for database infrastructure

- Update backend Dockerfile with PostgreSQL deps and entrypoint
- Add entrypoint.sh with db/redis wait and auto-migration
- Add /ready endpoint for Kubernetes readiness probe
- Enhance /health endpoint with database and Redis status
- Update k8s deployment with PostgreSQL and Redis services
- Add proper secrets management for database credentials
- Update k8s readiness probe to use /ready endpoint
This commit is contained in:
2025-12-31 10:58:41 +01:00
parent 550f3516db
commit 3d37ce4582
4 changed files with 335 additions and 6 deletions

61
main.py
View File

@@ -305,11 +305,36 @@ async def health_check():
health_status = await health_checker.check_health()
status_code = 200 if health_status.get("status") == "healthy" else 503
# Check database connection
db_status = {"status": "not_configured"}
try:
from database.connection import check_db_connection
if check_db_connection():
db_status = {"status": "healthy"}
else:
db_status = {"status": "unhealthy"}
except Exception as e:
db_status = {"status": "error", "error": str(e)}
# Check Redis connection
redis_status = {"status": "not_configured"}
redis_client = get_redis_client()
if redis_client:
try:
redis_client.ping()
redis_status = {"status": "healthy"}
except Exception as e:
redis_status = {"status": "unhealthy", "error": str(e)}
elif redis_client is False:
redis_status = {"status": "connection_failed"}
return JSONResponse(
status_code=status_code,
content={
"status": health_status.get("status", "unknown"),
"translation_service": config.TRANSLATION_SERVICE,
"database": db_status,
"redis": redis_status,
"memory": health_status.get("memory", {}),
"disk": health_status.get("disk", {}),
"cleanup_service": health_status.get("cleanup_service", {}),
@@ -322,6 +347,42 @@ async def health_check():
)
@app.get("/ready")
async def readiness_check():
"""Kubernetes readiness probe - check if app can serve traffic"""
issues = []
# Check database
try:
from database.connection import check_db_connection, DATABASE_URL
if DATABASE_URL: # Only check if configured
if not check_db_connection():
issues.append("database_unavailable")
except ImportError:
pass # Database module not available - OK for development
except Exception as e:
issues.append(f"database_error: {str(e)}")
# Check Redis (optional but log if configured and unavailable)
if REDIS_URL:
redis_client = get_redis_client()
if redis_client:
try:
redis_client.ping()
except Exception:
issues.append("redis_unavailable")
elif redis_client is False:
issues.append("redis_connection_failed")
if issues:
return JSONResponse(
status_code=503,
content={"ready": False, "issues": issues}
)
return {"ready": True}
@app.get("/languages")
async def get_supported_languages():
"""Get list of supported language codes"""