|
| 1 | +# Kubernetes Deployment Guide |
| 2 | + |
| 3 | +This directory contains Kubernetes manifests for deploying the eShopOnWeb application to a Kubernetes cluster. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +- Kubernetes cluster (AKS, EKS, GKE, or local cluster like minikube/kind) |
| 8 | +- `kubectl` CLI installed and configured |
| 9 | +- Container images built and pushed to a container registry (ACR, Docker Hub, etc.) |
| 10 | +- (Optional) `kustomize` CLI for environment-specific deployments |
| 11 | + |
| 12 | +## Architecture |
| 13 | + |
| 14 | +The deployment includes: |
| 15 | +- **Web Application** (eshop-web): Frontend MVC application |
| 16 | +- **Public API** (eshop-publicapi): Backend REST API |
| 17 | +- **ConfigMap**: Application configuration |
| 18 | +- **Secret**: Sensitive configuration data |
| 19 | +- **Services**: ClusterIP for API, LoadBalancer for Web |
| 20 | +- **Ingress**: Optional ingress for external access |
| 21 | + |
| 22 | +## Quick Start |
| 23 | + |
| 24 | +### 1. Build and Push Container Images |
| 25 | + |
| 26 | +First, build the container images and push them to your registry: |
| 27 | + |
| 28 | +```bash |
| 29 | +# From the repository root |
| 30 | +docker-compose build |
| 31 | + |
| 32 | +# Tag and push to your registry (e.g., Azure Container Registry) |
| 33 | +docker tag eshopwebmvc:latest <your-registry>.azurecr.io/eshopwebmvc:latest |
| 34 | +docker tag eshoppublicapi:latest <your-registry>.azurecr.io/eshoppublicapi:latest |
| 35 | + |
| 36 | +docker push <your-registry>.azurecr.io/eshopwebmvc:latest |
| 37 | +docker push <your-registry>.azurecr.io/eshoppublicapi:latest |
| 38 | +``` |
| 39 | + |
| 40 | +### 2. Update Kustomization |
| 41 | + |
| 42 | +Edit `kustomization.yaml` to point to your container registry: |
| 43 | + |
| 44 | +```yaml |
| 45 | +images: |
| 46 | + - name: eshopwebmvc |
| 47 | + newName: <your-registry>.azurecr.io/eshopwebmvc |
| 48 | + newTag: latest |
| 49 | + - name: eshoppublicapi |
| 50 | + newName: <your-registry>.azurecr.io/eshoppublicapi |
| 51 | + newTag: latest |
| 52 | +``` |
| 53 | +
|
| 54 | +### 3. Deploy Using kubectl |
| 55 | +
|
| 56 | +```bash |
| 57 | +# Deploy all resources |
| 58 | +kubectl apply -k . |
| 59 | + |
| 60 | +# Or deploy individual manifests |
| 61 | +kubectl apply -f namespace.yaml |
| 62 | +kubectl apply -f configmap.yaml |
| 63 | +kubectl apply -f secret.yaml |
| 64 | +kubectl apply -f web-deployment.yaml |
| 65 | +kubectl apply -f web-service.yaml |
| 66 | +kubectl apply -f publicapi-deployment.yaml |
| 67 | +kubectl apply -f publicapi-service.yaml |
| 68 | +kubectl apply -f ingress.yaml |
| 69 | +``` |
| 70 | + |
| 71 | +### 4. Verify Deployment |
| 72 | + |
| 73 | +```bash |
| 74 | +# Check namespace |
| 75 | +kubectl get namespaces | grep eshoponweb |
| 76 | + |
| 77 | +# Check pods |
| 78 | +kubectl get pods -n eshoponweb |
| 79 | + |
| 80 | +# Check services |
| 81 | +kubectl get services -n eshoponweb |
| 82 | + |
| 83 | +# Get Web service external IP (if LoadBalancer) |
| 84 | +kubectl get service eshop-web-service -n eshoponweb |
| 85 | +``` |
| 86 | + |
| 87 | +## Environment-Specific Deployments |
| 88 | + |
| 89 | +Use Kustomize overlays for different environments: |
| 90 | + |
| 91 | +### Development |
| 92 | +```bash |
| 93 | +kubectl apply -k overlays/dev/ |
| 94 | +``` |
| 95 | + |
| 96 | +### Staging |
| 97 | +```bash |
| 98 | +kubectl apply -k overlays/staging/ |
| 99 | +``` |
| 100 | + |
| 101 | +### Production |
| 102 | +```bash |
| 103 | +kubectl apply -k overlays/production/ |
| 104 | +``` |
| 105 | + |
| 106 | +## Configuration |
| 107 | + |
| 108 | +### ConfigMap |
| 109 | +Edit `configmap.yaml` to change application settings: |
| 110 | +- `ASPNETCORE_ENVIRONMENT`: Development, Staging, Production |
| 111 | +- `UseOnlyInMemoryDatabase`: true/false |
| 112 | +- `ASPNETCORE_HTTP_PORTS`: HTTP port |
| 113 | + |
| 114 | +### Secrets |
| 115 | +Update `secret.yaml` with your database credentials: |
| 116 | +- `CATALOG_DB_CONNECTION`: Catalog database connection string |
| 117 | +- `IDENTITY_DB_CONNECTION`: Identity database connection string |
| 118 | + |
| 119 | +⚠️ **Important**: In production, use proper secret management (Azure Key Vault, Kubernetes Secrets, Sealed Secrets, etc.) |
| 120 | + |
| 121 | +## Private Container Registry |
| 122 | + |
| 123 | +If using a private container registry, create a registry secret: |
| 124 | + |
| 125 | +```bash |
| 126 | +kubectl create secret docker-registry registry-credentials \ |
| 127 | + --docker-server=<your-registry>.azurecr.io \ |
| 128 | + --docker-username=<username> \ |
| 129 | + --docker-password=<password> \ |
| 130 | + --docker-email=<email> \ |
| 131 | + -n eshoponweb |
| 132 | +``` |
| 133 | + |
| 134 | +Then uncomment the `imagePullSecrets` section in deployment files. |
| 135 | + |
| 136 | +## Ingress Configuration |
| 137 | + |
| 138 | +The included `ingress.yaml` uses NGINX Ingress Controller. Update annotations for your ingress controller: |
| 139 | + |
| 140 | +### For Azure Application Gateway Ingress Controller: |
| 141 | +```yaml |
| 142 | +annotations: |
| 143 | + kubernetes.io/ingress.class: azure/application-gateway |
| 144 | + appgw.ingress.kubernetes.io/ssl-redirect: "true" |
| 145 | +``` |
| 146 | +
|
| 147 | +### Update the host: |
| 148 | +```yaml |
| 149 | +spec: |
| 150 | + rules: |
| 151 | + - host: your-domain.com |
| 152 | +``` |
| 153 | +
|
| 154 | +## Monitoring and Scaling |
| 155 | +
|
| 156 | +### View logs |
| 157 | +```bash |
| 158 | +kubectl logs -f deployment/eshop-web -n eshoponweb |
| 159 | +kubectl logs -f deployment/eshop-publicapi -n eshoponweb |
| 160 | +``` |
| 161 | + |
| 162 | +### Scale deployments |
| 163 | +```bash |
| 164 | +kubectl scale deployment eshop-web --replicas=3 -n eshoponweb |
| 165 | +kubectl scale deployment eshop-publicapi --replicas=3 -n eshoponweb |
| 166 | +``` |
| 167 | + |
| 168 | +### Horizontal Pod Autoscaling |
| 169 | +```bash |
| 170 | +kubectl autoscale deployment eshop-web --cpu-percent=70 --min=2 --max=10 -n eshoponweb |
| 171 | +``` |
| 172 | + |
| 173 | +## Validation |
| 174 | + |
| 175 | +Validate manifests before applying: |
| 176 | + |
| 177 | +```bash |
| 178 | +# Dry-run validation |
| 179 | +kubectl apply -k . --dry-run=client |
| 180 | + |
| 181 | +# Server-side dry-run |
| 182 | +kubectl apply -k . --dry-run=server |
| 183 | + |
| 184 | +# Validate individual files |
| 185 | +kubectl apply -f web-deployment.yaml --dry-run=client |
| 186 | +``` |
| 187 | + |
| 188 | +## Cleanup |
| 189 | + |
| 190 | +To remove all resources: |
| 191 | + |
| 192 | +```bash |
| 193 | +# Using kustomize |
| 194 | +kubectl delete -k . |
| 195 | + |
| 196 | +# Or delete namespace (removes all resources) |
| 197 | +kubectl delete namespace eshoponweb |
| 198 | +``` |
| 199 | + |
| 200 | +## Troubleshooting |
| 201 | + |
| 202 | +### Pods not starting |
| 203 | +```bash |
| 204 | +kubectl describe pod <pod-name> -n eshoponweb |
| 205 | +kubectl logs <pod-name> -n eshoponweb |
| 206 | +``` |
| 207 | + |
| 208 | +### Service not accessible |
| 209 | +```bash |
| 210 | +kubectl get endpoints -n eshoponweb |
| 211 | +kubectl describe service eshop-web-service -n eshoponweb |
| 212 | +``` |
| 213 | + |
| 214 | +### Image pull errors |
| 215 | +- Verify registry credentials |
| 216 | +- Check image name and tag |
| 217 | +- Ensure imagePullSecrets is configured |
| 218 | + |
| 219 | +## Compatibility |
| 220 | + |
| 221 | +These manifests are compatible with: |
| 222 | +- Azure Kubernetes Service (AKS) |
| 223 | +- Amazon Elastic Kubernetes Service (EKS) |
| 224 | +- Google Kubernetes Engine (GKE) |
| 225 | +- On-premises Kubernetes clusters |
| 226 | +- Local development clusters (minikube, kind, k3s) |
| 227 | + |
| 228 | +## Additional Resources |
| 229 | + |
| 230 | +- [Kubernetes Documentation](https://kubernetes.io/docs/) |
| 231 | +- [Kustomize Documentation](https://kustomize.io/) |
| 232 | +- [Azure Kubernetes Service](https://docs.microsoft.com/azure/aks/) |
| 233 | +- [Kubectl Cheat Sheet](https://kubernetes.io/docs/reference/kubectl/cheatsheet/) |
0 commit comments