Skip to main content

Kubernetes Best Practices Implementation Guide

Status: Implementation in progress Last Updated: 2025-11-02 Priority: HIGH - GCP GKE, AWS EKS, Azure AKS deployment best practices

Executive Summary

This document tracks the implementation of 11 high-priority Kubernetes best practices improvements across GCP GKE, AWS EKS, and Azure AKS deployments. All implementations follow TDD principles (tests first, then implementation).

✅ COMPLETED IMPLEMENTATIONS

1. Cloud-Managed PostgreSQL (HIGH AVAILABILITY)

Status: ✅ COMPLETE What was implemented:
  • Azure Database for PostgreSQL Terraform module with zone-redundant HA
  • Helm chart support for external databases (CloudSQL, RDS, Azure Database)
  • CloudSQL Proxy sidecar integration for GKE
  • Comprehensive monitoring and alerting for all cloud providers
Files created:
  • terraform/modules/azure-database/main.tf (354 lines)
  • terraform/modules/azure-database/variables.tf (309 lines)
  • terraform/modules/azure-database/outputs.tf (98 lines)
  • terraform/modules/azure-database/versions.tf
Files modified:
  • deployments/helm/mcp-server-langgraph/values.yaml (added external DB config)
  • deployments/helm/mcp-server-langgraph/templates/deployment.yaml (added CloudSQL proxy sidecar)
Usage Examples:
Test coverage: 12 tests in tests/infrastructure/test_database_ha.py

2. Topology Spread Constraints (ZONE-BASED HA)

Status: ✅ COMPLETE What was implemented:
  • TopologySpreadConstraints for zone distribution
  • Upgraded podAntiAffinity from preferred to required for production
  • Zone-level anti-affinity to prevent single-zone failures
  • Node-level spreading for better resource utilization
Files modified:
  • deployments/base/deployment.yaml
  • deployments/helm/mcp-server-langgraph/values.yaml
  • deployments/helm/mcp-server-langgraph/templates/deployment.yaml
Configuration:
Impact:
  • Ensures 99.99% availability (multi-zone)
  • Prevents cascading failures from zone outages
  • Meets production SLA requirements
Test coverage: 8 tests in tests/infrastructure/test_topology_spread.py

🚧 IN PROGRESS

3. Velero Backup/DR

Status: 🚧 IN PROGRESS Implementation Path:
  1. Create Velero Helm configuration:
  1. Create backup schedules:
  1. Installation:
Files to create:
  • deployments/backup/velero-values-aws.yaml
  • deployments/backup/velero-values-gcp.yaml
  • deployments/backup/velero-values-azure.yaml
  • deployments/backup/backup-schedule.yaml
  • deployments/backup/restore-procedure.md

📋 PENDING IMPLEMENTATIONS

4. Istio Service Mesh with mTLS STRICT

Priority: HIGH (Security) Implementation Path:
  1. Update Helm values to enable Istio:
  1. Add Istio resources (already exist at deployments/service-mesh/istio/):
    • istio-config.yaml (Gateway, VirtualService, DestinationRule)
    • ✅ AuthorizationPolicy for RBAC
    • ✅ PeerAuthentication for mTLS
  2. Update namespace labels:
Estimated time: 2 hours

5. Pod Security Standards

Priority: HIGH (Security) Implementation Path:
Validation: Ensure all pods comply with restricted PSS (already compliant based on current pod security contexts). Estimated time: 30 minutes

6. Network Policies for All Services

Priority: HIGH (Security) Files to create:
Similar policies needed for:
  • redis-networkpolicy.yaml
  • keycloak-networkpolicy.yaml
  • openfga-networkpolicy.yaml
Estimated time: 1 hour

7. Loki Log Aggregation

Priority: MEDIUM (Observability) Implementation:
Estimated time: 1.5 hours

8. ResourceQuota and LimitRange

Priority: MEDIUM (Cost & Stability) Files to create:
Estimated time: 30 minutes

9. Kubecost for FinOps

Priority: MEDIUM (Cost Optimization) Implementation:
Cloud billing integration:
  • AWS: Configure CUR (Cost and Usage Report)
  • GCP: Enable BigQuery billing export
  • Azure: Configure Cost Management API
Estimated time: 2 hours

10. Karpenter for EKS

Priority: MEDIUM (Cost Optimization - AWS only) Implementation:
Estimated time: 3 hours

11. VPA for Stateful Services

Priority: LOW (Cost Optimization) Implementation:
Similar VPAs for:
  • Redis
  • Keycloak
Estimated time: 1 hour

TESTING STRATEGY

All implementations follow TDD:
  1. Write tests first (RED phase)
  2. Implement minimal solution (GREEN phase)
  3. Refactor and optimize (REFACTOR phase)
Test files created:
  • tests/infrastructure/test_database_ha.py - Database HA tests
  • tests/infrastructure/test_topology_spread.py - Zone spreading tests
Additional tests needed:
  • tests/infrastructure/test_backup_restore.py - Velero backup/restore
  • tests/infrastructure/test_service_mesh.py - Istio mTLS validation
  • tests/infrastructure/test_network_policies.py - Network isolation
  • tests/infrastructure/test_observability.py - Loki, Kubecost integration
  • tests/infrastructure/test_autoscaling.py - Karpenter, VPA validation

DEPLOYMENT CHECKLIST

Before deploying to production:
  • Cloud-managed databases configured
  • Topology spread constraints enabled
  • Velero backups tested and validated
  • Istio mTLS STRICT mode enabled
  • Pod Security Standards enforced
  • Network policies applied to all services
  • Loki log aggregation operational
  • Resource quotas configured
  • Kubecost monitoring enabled
  • Karpenter autoscaling tested (EKS)
  • VPA recommendations validated
  • All tests passing
  • Documentation updated

ESTIMATED TIMELINE


ROLLBACK PROCEDURES

Cloud-Managed Databases

  1. Keep in-cluster PostgreSQL running during migration
  2. Test external database connectivity before switching
  3. Update Helm values: postgresql.enabled=false
  4. Monitor application metrics post-migration
  5. Rollback: postgresql.enabled=true

Topology Spread Constraints

  1. Test in dev/staging first
  2. Ensure cluster has 3+ zones
  3. Monitor pod scheduling (watch for Pending pods)
  4. Rollback: Remove topologySpreadConstraints, revert to preferred anti-affinity

Istio Service Mesh

  1. Enable incrementally (namespace by namespace)
  2. Start with PERMISSIVE mTLS, then upgrade to STRICT
  3. Monitor latency and error rates
  4. Rollback: kubectl label namespace mcp-server-langgraph istio-injection-

SUPPORT & TROUBLESHOOTING

Common Issues

Issue: Pods stuck in Pending due to topology constraints Solution: Verify cluster has 3+ zones, reduce minReplicas temporarily Issue: CloudSQL proxy authentication failing Solution: Verify Workload Identity binding, check service account permissions Issue: Istio mTLS connection refused Solution: Check PeerAuthentication mode, verify certificates with istioctl

Useful Commands


REFERENCES