Aller au contenu

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.

  1. Kubernetes Cluster

    • Docker Desktop with Kubernetes enabled, or
    • Minikube, or
    • Any cloud Kubernetes service (GKE, EKS, AKS)
  2. 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 installation
    helm version
  3. 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 installation
    kubectl version --client

The fastest way to deploy to a local Kubernetes cluster:

Fenêtre de terminal
# Make scripts executable
chmod +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 --url

To clean up:

Fenêtre de terminal
./scripts/k8s-cleanup.sh
  1. 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)
  2. Verify Cluster

    Fenêtre de terminal
    kubectl cluster-info
    kubectl get nodes
  3. 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 Helm
    helm install kili-shops ./helm/kili-shops -f ./helm/kili-shops/values-local.yaml
  4. Access Application

    Fenêtre de terminal
    # Check services
    kubectl get services
    # API Gateway is available at http://localhost:3000
    curl http://localhost:3000/health
  1. 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-amd64
    sudo install minikube-linux-amd64 /usr/local/bin/minikube
    # Start cluster (with sufficient resources)
    minikube start --cpus=4 --memory=8192 --disk-size=20g
  2. Configure Docker to Use Minikube

    Fenêtre de terminal
    # Use Minikube's Docker daemon
    eval $(minikube docker-env)
    # Verify
    docker ps
  3. 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 Docker
    eval $(minikube docker-env)
    ./scripts/k8s-build-images.sh
    # Deploy with Helm
    helm install kili-shops ./helm/kili-shops -f ./helm/kili-shops/values-local.yaml
  4. Access Application

    Fenêtre de terminal
    # Get service URL
    minikube service api-gateway --url
    # Or use port forwarding
    kubectl port-forward service/api-gateway 3000:3000
    # Test
    curl http://localhost:3000/health

If you prefer to deploy step-by-step:

Fenêtre de terminal
# From project root
docker 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 images
docker images | grep kili-shops
Fenêtre de terminal
# Install new release
helm install kili-shops ./helm/kili-shops
# Or install with custom values
helm install kili-shops ./helm/kili-shops -f ./helm/kili-shops/values-local.yaml
# Or upgrade existing release
helm upgrade kili-shops ./helm/kili-shops
Fenêtre de terminal
# Check all resources
kubectl get all
# Check pods are running
kubectl get pods
# Check services
kubectl get services
# Check persistent volumes
kubectl get pvc

Create your own values file:

custom-values.yaml
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:

Fenêtre de terminal
helm install kili-shops ./helm/kili-shops -f custom-values.yaml

Development:

Fenêtre de terminal
helm install kili-shops ./helm/kili-shops -f helm/kili-shops/values-local.yaml

Production:

Fenêtre de terminal
helm install kili-shops ./helm/kili-shops -f production-values.yaml
Fenêtre de terminal
# Modify values.yaml or create custom values file
# Then upgrade the release
helm upgrade kili-shops ./helm/kili-shops -f custom-values.yaml
Fenêtre de terminal
# Get service details
kubectl get service api-gateway
# Access directly
curl http://localhost:3000/health
curl http://localhost:3000/api/shops
curl http://localhost:3000/api/users
Fenêtre de terminal
# Get service URL
minikube service api-gateway --url
# Access using the provided URL
curl $(minikube service api-gateway --url)/health
Fenêtre de terminal
# Forward API Gateway port
kubectl port-forward service/api-gateway 3000:3000
# In another terminal
curl http://localhost:3000/health

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
Fenêtre de terminal
# List all pods
kubectl get pods
# Describe a specific pod
kubectl describe pod <pod-name>
# Watch pods
kubectl get pods -w
Fenêtre de terminal
# API Gateway logs
kubectl logs -f deployment/api-gateway
# Shops microservice logs
kubectl logs -f deployment/shops
# Users microservice logs
kubectl logs -f deployment/users
# MongoDB logs
kubectl logs -f statefulset/mongodb
# PostgreSQL logs
kubectl logs -f statefulset/postgres
# Previous logs (if pod crashed)
kubectl logs --previous <pod-name>
Fenêtre de terminal
# Open shell in API Gateway pod
kubectl exec -it deployment/api-gateway -- /bin/sh
# Test database connectivity from Shops pod
kubectl exec -it deployment/shops -- nc -zv mongodb 27017
# Test database connectivity from Users pod
kubectl exec -it deployment/users -- nc -zv postgres 5432
Fenêtre de terminal
# All events
kubectl get events --sort-by=.metadata.creationTimestamp
# Events for specific pod
kubectl describe pod <pod-name>
Fenêtre de terminal
# CPU and memory usage
kubectl top nodes
kubectl top pods
Fenêtre de terminal
# Check pod description for issues
kubectl describe pod <pod-name>
# Common causes:
# - Insufficient resources
# - PVC not bound
# - Image pull errors
Fenêtre de terminal
# Ensure images are built
docker images | grep kili-shops
# For Minikube, ensure using Minikube's Docker
eval $(minikube docker-env)
./scripts/k8s-build-images.sh
Fenêtre de terminal
# View logs to identify crash reason
kubectl logs <pod-name>
kubectl logs --previous <pod-name>
# Common causes:
# - Database not ready (wait for init containers)
# - Configuration errors
# - Application errors
Fenêtre de terminal
# Check database pods
kubectl get pods -l app.kubernetes.io/component=database
# Check database logs
kubectl logs statefulset/mongodb
kubectl logs statefulset/postgres
# Test connectivity
kubectl exec -it deployment/shops -- nc -zv mongodb 27017
kubectl exec -it deployment/users -- nc -zv postgres 5432
  1. Use Secrets for Sensitive Data

    Fenêtre de terminal
    # Create secret for PostgreSQL password
    kubectl create secret generic postgresql-secret \
    --from-literal=postgres-password='your-secure-password'
  2. Update values for production:

    production-values.yaml
    postgresql:
    env:
    POSTGRES_PASSWORD: "" # Use from secret
  3. Enable Network Policies

  4. Use RBAC for access control

  5. Enable pod security policies

production-values.yaml
apiGateway:
replicaCount: 3
autoscaling:
enabled: true
minReplicas: 2
maxReplicas: 10
targetCPUUtilizationPercentage: 70
shops:
replicaCount: 2
users:
replicaCount: 2
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: 4Gi
mongodb:
persistence:
enabled: true
storageClass: "fast-ssd" # Use appropriate storage class
size: 50Gi
postgresql:
persistence:
enabled: true
storageClass: "fast-ssd"
size: 50Gi
ingress:
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.com

Deploy ingress controller:

Fenêtre de terminal
# Install NGINX Ingress Controller
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
helm install nginx-ingress ingress-nginx/ingress-nginx

Consider adding:

  1. Prometheus + Grafana

    Fenêtre de terminal
    helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
    helm install prometheus prometheus-community/kube-prometheus-stack
  2. ELK/EFK Stack for centralized logging

  3. Jaeger for distributed tracing

Fenêtre de terminal
# Backup MongoDB
kubectl exec statefulset/mongodb -- mongodump --archive=/tmp/backup.archive
# Backup PostgreSQL
kubectl exec statefulset/postgres -- pg_dump -U postgres killi_users > backup.sql
Fenêtre de terminal
# Install
helm install kili-shops ./helm/kili-shops
# Upgrade
helm upgrade kili-shops ./helm/kili-shops
# Install or upgrade
helm upgrade --install kili-shops ./helm/kili-shops
# Rollback
helm rollback kili-shops
# Uninstall
helm uninstall kili-shops
# List releases
helm list
# Get values
helm get values kili-shops
# Get all manifest
helm get manifest kili-shops
# Dry run
helm install kili-shops ./helm/kili-shops --dry-run --debug
# Template rendering
helm template kili-shops ./helm/kili-shops

For issues:

  1. Check logs: kubectl logs <pod-name>
  2. Check events: kubectl get events --sort-by=.metadata.creationTimestamp
  3. Check pod description: kubectl describe pod <pod-name>
  4. Review Helm chart README