Kubernetes Deployment Guide
Brouillon : cette page n'a pas encore été relue et peut être incomplète ou inexacte.
This guide explains how to deploy Kili Shops microservices to Kubernetes using Helm charts.
Table of Contents
Section intitulée « Table of Contents »- Prerequisites
- Quick Start
- Local Development
- Manual Deployment
- Configuration
- Accessing the Application
- Monitoring and Troubleshooting
- Production Deployment
Prerequisites
Section intitulée « Prerequisites »Required Tools
Section intitulée « Required Tools »-
Kubernetes Cluster
- Docker Desktop with Kubernetes enabled, or
- Minikube, or
- Any cloud Kubernetes service (GKE, EKS, AKS)
-
Helm 3.0+
Fenêtre de terminal # Install Helm (macOS)brew install helm# Install Helm (Linux)curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash# Verify installationhelm version -
kubectl
Fenêtre de terminal # Install kubectl (macOS)brew install kubectl# Install kubectl (Linux)curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl# Verify installationkubectl version --client
Quick Start
Section intitulée « Quick Start »The fastest way to deploy to a local Kubernetes cluster:
# Make scripts executablechmod +x scripts/k8s-*.sh
# Deploy everything (builds images and deploys)./scripts/k8s-deploy.sh
# Access the API# - Docker Desktop: http://localhost:3000# - Minikube: Use the URL from minikube service api-gateway --urlTo clean up:
./scripts/k8s-cleanup.shLocal Development
Section intitulée « Local Development »Docker Desktop
Section intitulée « Docker Desktop »-
Enable Kubernetes
- Open Docker Desktop settings
- Go to “Kubernetes” tab
- Check “Enable Kubernetes”
- Click “Apply & Restart”
- Wait for Kubernetes to start (green icon in Docker Desktop)
-
Verify Cluster
Fenêtre de terminal kubectl cluster-infokubectl get nodes -
Deploy Application
Fenêtre de terminal # Option 1: Use deployment script (recommended)./scripts/k8s-deploy.sh# Option 2: Manual deployment# Build images./scripts/k8s-build-images.sh# Deploy with Helmhelm install kili-shops ./helm/kili-shops -f ./helm/kili-shops/values-local.yaml -
Access Application
Fenêtre de terminal # Check serviceskubectl get services# API Gateway is available at http://localhost:3000curl http://localhost:3000/health
Minikube
Section intitulée « Minikube »-
Start Minikube
Fenêtre de terminal # Install Minikube (macOS)brew install minikube# Install Minikube (Linux)curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64sudo install minikube-linux-amd64 /usr/local/bin/minikube# Start cluster (with sufficient resources)minikube start --cpus=4 --memory=8192 --disk-size=20g -
Configure Docker to Use Minikube
Fenêtre de terminal # Use Minikube's Docker daemoneval $(minikube docker-env)# Verifydocker ps -
Deploy Application
Fenêtre de terminal # Option 1: Use deployment script (recommended)./scripts/k8s-deploy.sh# Option 2: Manual deployment# Build images in Minikube's Dockereval $(minikube docker-env)./scripts/k8s-build-images.sh# Deploy with Helmhelm install kili-shops ./helm/kili-shops -f ./helm/kili-shops/values-local.yaml -
Access Application
Fenêtre de terminal # Get service URLminikube service api-gateway --url# Or use port forwardingkubectl port-forward service/api-gateway 3000:3000# Testcurl http://localhost:3000/health
Manual Deployment
Section intitulée « Manual Deployment »If you prefer to deploy step-by-step:
Step 1: Build Docker Images
Section intitulée « Step 1: Build Docker Images »# From project rootdocker build -f Dockerfile.gateway -t kili-shops/api-gateway:latest .docker build -f Dockerfile.shops -t kili-shops/shops:latest .docker build -f Dockerfile.users -t kili-shops/users:latest .
# Verify imagesdocker images | grep kili-shopsStep 2: Deploy with Helm
Section intitulée « Step 2: Deploy with Helm »# Install new releasehelm install kili-shops ./helm/kili-shops
# Or install with custom valueshelm install kili-shops ./helm/kili-shops -f ./helm/kili-shops/values-local.yaml
# Or upgrade existing releasehelm upgrade kili-shops ./helm/kili-shopsStep 3: Verify Deployment
Section intitulée « Step 3: Verify Deployment »# Check all resourceskubectl get all
# Check pods are runningkubectl get pods
# Check serviceskubectl get services
# Check persistent volumeskubectl get pvcConfiguration
Section intitulée « Configuration »Using Custom Values
Section intitulée « Using Custom Values »Create your own values file:
apiGateway: replicaCount: 2 service: type: LoadBalancer
shops: replicaCount: 2 resources: limits: memory: 1Gi
users: replicaCount: 2
mongodb: persistence: size: 20Gi
postgresql: persistence: size: 20Gi env: POSTGRES_PASSWORD: "your-secure-password"Deploy with custom values:
helm install kili-shops ./helm/kili-shops -f custom-values.yamlEnvironment-Specific Configurations
Section intitulée « Environment-Specific Configurations »Development:
helm install kili-shops ./helm/kili-shops -f helm/kili-shops/values-local.yamlProduction:
helm install kili-shops ./helm/kili-shops -f production-values.yamlUpdating Configuration
Section intitulée « Updating Configuration »# Modify values.yaml or create custom values file# Then upgrade the releasehelm upgrade kili-shops ./helm/kili-shops -f custom-values.yamlAccessing the Application
Section intitulée « Accessing the Application »LoadBalancer (Docker Desktop)
Section intitulée « LoadBalancer (Docker Desktop) »# Get service detailskubectl get service api-gateway
# Access directlycurl http://localhost:3000/healthcurl http://localhost:3000/api/shopscurl http://localhost:3000/api/usersNodePort (Minikube)
Section intitulée « NodePort (Minikube) »# Get service URLminikube service api-gateway --url
# Access using the provided URLcurl $(minikube service api-gateway --url)/healthPort Forwarding (Any Cluster)
Section intitulée « Port Forwarding (Any Cluster) »# Forward API Gateway portkubectl port-forward service/api-gateway 3000:3000
# In another terminalcurl http://localhost:3000/healthUsing Postman
Section intitulée « Using Postman »Import the included Postman collection and update the base URL:
- Docker Desktop:
http://localhost:3000 - Minikube: Use URL from
minikube service api-gateway --url - Port Forward:
http://localhost:3000
Monitoring and Troubleshooting
Section intitulée « Monitoring and Troubleshooting »View Pod Status
Section intitulée « View Pod Status »# List all podskubectl get pods
# Describe a specific podkubectl describe pod <pod-name>
# Watch podskubectl get pods -wView Logs
Section intitulée « View Logs »# API Gateway logskubectl logs -f deployment/api-gateway
# Shops microservice logskubectl logs -f deployment/shops
# Users microservice logskubectl logs -f deployment/users
# MongoDB logskubectl logs -f statefulset/mongodb
# PostgreSQL logskubectl logs -f statefulset/postgres
# Previous logs (if pod crashed)kubectl logs --previous <pod-name>Execute Commands in Pods
Section intitulée « Execute Commands in Pods »# Open shell in API Gateway podkubectl exec -it deployment/api-gateway -- /bin/sh
# Test database connectivity from Shops podkubectl exec -it deployment/shops -- nc -zv mongodb 27017
# Test database connectivity from Users podkubectl exec -it deployment/users -- nc -zv postgres 5432View Events
Section intitulée « View Events »# All eventskubectl get events --sort-by=.metadata.creationTimestamp
# Events for specific podkubectl describe pod <pod-name>Check Resource Usage
Section intitulée « Check Resource Usage »# CPU and memory usagekubectl top nodeskubectl top podsCommon Issues
Section intitulée « Common Issues »Pods Stuck in Pending
Section intitulée « Pods Stuck in Pending »# Check pod description for issueskubectl describe pod <pod-name>
# Common causes:# - Insufficient resources# - PVC not bound# - Image pull errorsImagePullBackOff
Section intitulée « ImagePullBackOff »# Ensure images are builtdocker images | grep kili-shops
# For Minikube, ensure using Minikube's Dockereval $(minikube docker-env)./scripts/k8s-build-images.shCrashLoopBackOff
Section intitulée « CrashLoopBackOff »# View logs to identify crash reasonkubectl logs <pod-name>kubectl logs --previous <pod-name>
# Common causes:# - Database not ready (wait for init containers)# - Configuration errors# - Application errorsDatabase Connection Issues
Section intitulée « Database Connection Issues »# Check database podskubectl get pods -l app.kubernetes.io/component=database
# Check database logskubectl logs statefulset/mongodbkubectl logs statefulset/postgres
# Test connectivitykubectl exec -it deployment/shops -- nc -zv mongodb 27017kubectl exec -it deployment/users -- nc -zv postgres 5432Production Deployment
Section intitulée « Production Deployment »Security Considerations
Section intitulée « Security Considerations »-
Use Secrets for Sensitive Data
Fenêtre de terminal # Create secret for PostgreSQL passwordkubectl create secret generic postgresql-secret \--from-literal=postgres-password='your-secure-password' -
Update values for production:
production-values.yaml postgresql:env:POSTGRES_PASSWORD: "" # Use from secret -
Enable Network Policies
-
Use RBAC for access control
-
Enable pod security policies
High Availability
Section intitulée « High Availability »apiGateway: replicaCount: 3 autoscaling: enabled: true minReplicas: 2 maxReplicas: 10 targetCPUUtilizationPercentage: 70
shops: replicaCount: 2
users: replicaCount: 2Resource Management
Section intitulée « Resource Management »apiGateway: resources: requests: cpu: 500m memory: 512Mi limits: cpu: 1000m memory: 1Gi
mongodb: resources: requests: cpu: 1000m memory: 2Gi limits: cpu: 2000m memory: 4Gi
postgresql: resources: requests: cpu: 1000m memory: 2Gi limits: cpu: 2000m memory: 4GiPersistence
Section intitulée « Persistence »mongodb: persistence: enabled: true storageClass: "fast-ssd" # Use appropriate storage class size: 50Gi
postgresql: persistence: enabled: true storageClass: "fast-ssd" size: 50Giingress: enabled: true className: nginx annotations: cert-manager.io/cluster-issuer: letsencrypt-prod hosts: - host: api.kili-shops.com paths: - path: / pathType: Prefix tls: - secretName: kili-shops-tls hosts: - api.kili-shops.comDeploy ingress controller:
# Install NGINX Ingress Controllerhelm repo add ingress-nginx https://kubernetes.github.io/ingress-nginxhelm install nginx-ingress ingress-nginx/ingress-nginxMonitoring
Section intitulée « Monitoring »Consider adding:
-
Prometheus + Grafana
Fenêtre de terminal helm repo add prometheus-community https://prometheus-community.github.io/helm-chartshelm install prometheus prometheus-community/kube-prometheus-stack -
ELK/EFK Stack for centralized logging
-
Jaeger for distributed tracing
Backup Strategy
Section intitulée « Backup Strategy »# Backup MongoDBkubectl exec statefulset/mongodb -- mongodump --archive=/tmp/backup.archive
# Backup PostgreSQLkubectl exec statefulset/postgres -- pg_dump -U postgres killi_users > backup.sqlHelm Commands Reference
Section intitulée « Helm Commands Reference »# Installhelm install kili-shops ./helm/kili-shops
# Upgradehelm upgrade kili-shops ./helm/kili-shops
# Install or upgradehelm upgrade --install kili-shops ./helm/kili-shops
# Rollbackhelm rollback kili-shops
# Uninstallhelm uninstall kili-shops
# List releaseshelm list
# Get valueshelm get values kili-shops
# Get all manifesthelm get manifest kili-shops
# Dry runhelm install kili-shops ./helm/kili-shops --dry-run --debug
# Template renderinghelm template kili-shops ./helm/kili-shopsAdditional Resources
Section intitulée « Additional Resources »For issues:
- Check logs:
kubectl logs <pod-name> - Check events:
kubectl get events --sort-by=.metadata.creationTimestamp - Check pod description:
kubectl describe pod <pod-name> - Review Helm chart README