|
| 1 | +# Kubernetes Deployment Configuration |
| 2 | + |
| 3 | +This directory contains Kubernetes configuration files for deploying the KidsMedia Phoenix LiveView application. |
| 4 | + |
| 5 | +## Files |
| 6 | + |
| 7 | +- `deployment.yaml` - Main application deployment with secrets |
| 8 | +- `service.yaml` - ClusterIP service to expose the application internally |
| 9 | +- `ingress.yaml` - Ingress configuration with NGINX and SSL/TLS |
| 10 | + |
| 11 | +## Prerequisites |
| 12 | + |
| 13 | +1. **Kubernetes cluster** with NGINX Ingress Controller installed |
| 14 | +2. **cert-manager** (optional, for automatic SSL certificates) |
| 15 | +3. **Container registry access** to GitHub Container Registry (GHCR) |
| 16 | + |
| 17 | +## Configuration Steps |
| 18 | + |
| 19 | +### 1. Update Secrets |
| 20 | + |
| 21 | +Edit `deployment.yaml` and replace the placeholder values: |
| 22 | + |
| 23 | +```yaml |
| 24 | +stringData: |
| 25 | + secret-key-base: "CHANGE_ME_64_CHAR_SECRET_KEY_BASE_FOR_PHOENIX_APP_SECURITY" |
| 26 | + unsplash-access-key: "YOUR_UNSPLASH_ACCESS_KEY_HERE" |
| 27 | +``` |
| 28 | +
|
| 29 | +Generate a secret key base: |
| 30 | +```bash |
| 31 | +mix phx.gen.secret |
| 32 | +``` |
| 33 | + |
| 34 | +### 2. Configure Container Registry Access |
| 35 | + |
| 36 | +If using a private registry, create the Docker config secret: |
| 37 | + |
| 38 | +```bash |
| 39 | +kubectl create secret docker-registry ghcr-secret \ |
| 40 | + --docker-server=ghcr.io \ |
| 41 | + --docker-username=YOUR_GITHUB_USERNAME \ |
| 42 | + --docker-password=YOUR_GITHUB_TOKEN \ |
| 43 | + --docker-email=YOUR_EMAIL |
| 44 | +``` |
| 45 | + |
| 46 | +For public images, remove the `imagePullSecrets` section from `deployment.yaml`. |
| 47 | + |
| 48 | +### 3. Update Domain |
| 49 | + |
| 50 | +Replace `kids-media.example.com` in both `deployment.yaml` (PHX_HOST) and `ingress.yaml` with your actual domain. |
| 51 | + |
| 52 | +### 4. SSL/TLS Configuration |
| 53 | + |
| 54 | +The ingress is configured for automatic SSL using cert-manager. If you don't have cert-manager: |
| 55 | + |
| 56 | +- Remove the `cert-manager.io/cluster-issuer` annotation |
| 57 | +- Remove the `tls` section from ingress |
| 58 | +- Set `nginx.ingress.kubernetes.io/ssl-redirect: "false"` |
| 59 | + |
| 60 | +## Deployment |
| 61 | + |
| 62 | +Apply the configurations in order: |
| 63 | + |
| 64 | +```bash |
| 65 | +# Apply deployment and service |
| 66 | +kubectl apply -f k8s/deployment.yaml |
| 67 | +kubectl apply -f k8s/service.yaml |
| 68 | + |
| 69 | +# Apply ingress |
| 70 | +kubectl apply -f k8s/ingress.yaml |
| 71 | +``` |
| 72 | + |
| 73 | +Or apply all at once: |
| 74 | +```bash |
| 75 | +kubectl apply -f k8s/ |
| 76 | +``` |
| 77 | + |
| 78 | +## Verification |
| 79 | + |
| 80 | +Check deployment status: |
| 81 | +```bash |
| 82 | +# Check pods |
| 83 | +kubectl get pods -l app=kids-media |
| 84 | + |
| 85 | +# Check service |
| 86 | +kubectl get service kids-media-service |
| 87 | + |
| 88 | +# Check ingress |
| 89 | +kubectl get ingress kids-media-ingress |
| 90 | + |
| 91 | +# Check logs |
| 92 | +kubectl logs -l app=kids-media |
| 93 | +``` |
| 94 | + |
| 95 | +## Scaling |
| 96 | + |
| 97 | +Scale the application: |
| 98 | +```bash |
| 99 | +kubectl scale deployment kids-media --replicas=5 |
| 100 | +``` |
| 101 | + |
| 102 | +## Environment Variables |
| 103 | + |
| 104 | +The application uses these environment variables: |
| 105 | + |
| 106 | +- `PORT`: Application port (4000) |
| 107 | +- `SECRET_KEY_BASE`: Phoenix secret key base (from secret) |
| 108 | +- `UNSPLASH_ACCESS_KEY`: Unsplash API key (from secret) |
| 109 | +- `PHX_HOST`: External hostname for Phoenix |
| 110 | +- `MIX_ENV`: Elixir environment (prod) |
| 111 | + |
| 112 | +## Resource Limits |
| 113 | + |
| 114 | +Current resource configuration: |
| 115 | +- **Requests**: 256Mi memory, 250m CPU |
| 116 | +- **Limits**: 512Mi memory, 500m CPU |
| 117 | + |
| 118 | +Adjust based on your cluster capacity and application needs. |
| 119 | + |
| 120 | +## Health Checks |
| 121 | + |
| 122 | +The deployment includes: |
| 123 | +- **Liveness probe**: Checks if app is running (restarts if failing) |
| 124 | +- **Readiness probe**: Checks if app can receive traffic |
| 125 | +- **Startup probe**: Gives extra time during initial startup |
| 126 | + |
| 127 | +## Troubleshooting |
| 128 | + |
| 129 | +Common issues: |
| 130 | + |
| 131 | +1. **ImagePullBackOff**: Check registry credentials and image name |
| 132 | +2. **CrashLoopBackOff**: Check logs for application errors |
| 133 | +3. **503 errors**: Check service selector and pod labels match |
| 134 | +4. **SSL issues**: Verify cert-manager is working and domain is correct |
| 135 | + |
| 136 | +Check application logs: |
| 137 | +```bash |
| 138 | +kubectl logs -l app=kids-media --tail=100 |
| 139 | +``` |
| 140 | + |
| 141 | +## Production Considerations |
| 142 | + |
| 143 | +- Use a proper secret management solution (e.g., HashiCorp Vault, AWS Secrets Manager) |
| 144 | +- Set up monitoring and alerting |
| 145 | +- Configure resource limits based on load testing |
| 146 | +- Use horizontal pod autoscaling (HPA) for automatic scaling |
| 147 | +- Consider using a service mesh for advanced networking features |
0 commit comments