Aller au contenu

Digital Ocean Kubernetes Deployment Guide

Brouillon : cette page n'a pas encore été relue et peut être incomplète ou inexacte.

This guide walks you through deploying the Kili Shops microservices (backend) and Kilimmanjaro frontend to a Digital Ocean Kubernetes cluster for testing.

  1. Prerequisites
  2. Step 1: Create Digital Ocean Kubernetes Cluster
  3. Step 2: Configure kubectl
  4. Step 3: Setup Container Registry
  5. Step 4: Build and Push Docker Images
  6. Step 5: Deploy Backend Services
  7. Step 6: Deploy Frontend
  8. Step 7: Configure DNS and Access
  9. Step 8: Testing and Verification
  10. Troubleshooting
  11. Cleanup

  1. Digital Ocean Account

  2. doctl CLI (Digital Ocean CLI)

    Fenêtre de terminal
    # macOS
    brew install doctl
    # Linux
    cd ~
    wget https://github.com/digitalocean/doctl/releases/download/v1.94.0/doctl-1.94.0-linux-amd64.tar.gz
    tar xf ~/doctl-1.94.0-linux-amd64.tar.gz
    sudo mv ~/doctl /usr/local/bin
    # Verify
    doctl version
  3. kubectl

    Fenêtre de terminal
    # Already installed from previous setup
    kubectl version --client
  4. Helm

    Fenêtre de terminal
    # Already installed from previous setup
    helm version
  5. Docker

    Fenêtre de terminal
    # Verify Docker is running
    docker --version
Fenêtre de terminal
# Get API token from Digital Ocean
# Go to: https://cloud.digitalocean.com/account/api/tokens
# Create a new token with read/write access
# Authenticate
doctl auth init
# Paste your API token when prompted
# Verify authentication
doctl account get

  1. Go to Digital Ocean Dashboard

  2. Create Cluster

    • Click “Create Kubernetes Cluster”
    • Name: kili-shops-test
    • Region: Choose closest to you (e.g., nyc1, sfo3, sgp1)
    • Kubernetes Version: Latest stable version (e.g., 1.28.x)
    • Node Pool:
      • Plan: Basic nodes
      • Machine Type: 2 vCPU / 4 GB RAM ($24/month per node)
      • Node Count: 3 nodes (for high availability)
    • Total Monthly Cost: ~$72
  3. Create Cluster

    • Click “Create Cluster”
    • Wait 3-5 minutes for cluster to provision
Fenêtre de terminal
# Create cluster with 3 nodes
doctl kubernetes cluster create kili-shops-test \
--region nyc1 \
--version 1.28.2-do.0 \
--node-pool "name=worker-pool;size=s-2vcpu-4gb;count=3" \
--wait
# This will take 3-5 minutes
Fenêtre de terminal
# List clusters
doctl kubernetes cluster list
# Output example:
# ID Name Region Version Auto Upgrade Status Node Pools
# abc123 kili-shops-test nyc1 1.28.2-do.0 false running worker-pool

Fenêtre de terminal
# Download kubeconfig
doctl kubernetes cluster kubeconfig save kili-shops-test
# This adds the cluster to your ~/.kube/config
# Verify connection
kubectl cluster-info
# Check nodes
kubectl get nodes
# Expected output: 3 nodes in Ready state
Fenêtre de terminal
# List contexts
kubectl config get-contexts
# Switch to Digital Ocean cluster
kubectl config use-context do-nyc1-kili-shops-test
# Verify
kubectl config current-context

You have two options for storing Docker images:

Section intitulée « Option A: Digital Ocean Container Registry (Recommended) »
Fenêtre de terminal
# Create registry
doctl registry create kili-shops
# Login to registry
doctl registry login
# Get registry URL
doctl registry get
# Example: registry.digitalocean.com/kili-shops
Fenêtre de terminal
# Login to Docker Hub
docker login
# You'll use docker.io/YOUR_USERNAME/image-name

For this guide, we’ll use Digital Ocean Container Registry.

Digital Ocean automatically creates an image pull secret, but verify:

Fenêtre de terminal
# Check for registry secret
kubectl get secret -n kube-system | grep registry
# If using DO registry, it should be automatically configured

Fenêtre de terminal
# Navigate to backend project
cd /Users/moise/projects/af-corp/kili-shops-microservices
# Set registry URL (replace with your registry)
REGISTRY="registry.digitalocean.com/kili-shops"
# Build and tag images
echo "Building API Gateway..."
docker build -f Dockerfile.gateway -t ${REGISTRY}/api-gateway:v1.0.0 .
docker tag ${REGISTRY}/api-gateway:v1.0.0 ${REGISTRY}/api-gateway:latest
echo "Building Shops service..."
docker build -f Dockerfile.shops -t ${REGISTRY}/shops:v1.0.0 .
docker tag ${REGISTRY}/shops:v1.0.0 ${REGISTRY}/shops:latest
echo "Building Users service..."
docker build -f Dockerfile.users -t ${REGISTRY}/users:v1.0.0 .
docker tag ${REGISTRY}/users:v1.0.0 ${REGISTRY}/users:latest
# Push images to registry
echo "Pushing images..."
docker push ${REGISTRY}/api-gateway:v1.0.0
docker push ${REGISTRY}/api-gateway:latest
docker push ${REGISTRY}/shops:v1.0.0
docker push ${REGISTRY}/shops:latest
docker push ${REGISTRY}/users:v1.0.0
docker push ${REGISTRY}/users:latest
# Verify images
doctl registry repository list-v2
Fenêtre de terminal
# Navigate to frontend project
cd /Users/moise/projects/af-corp/kilimmanjaro
# Get the LoadBalancer IP or domain for backend API
# (We'll come back to this after deploying backend)
# For now, use a placeholder - we'll rebuild after backend is deployed
REGISTRY="registry.digitalocean.com/kili-shops"
# Build frontend (placeholder API URL)
docker build \
--build-arg REACT_APP_API_URL="http://BACKEND_URL_HERE/api" \
-t ${REGISTRY}/frontend:v1.0.0 \
-f Dockerfile .
docker tag ${REGISTRY}/frontend:v1.0.0 ${REGISTRY}/frontend:latest
# Push to registry
docker push ${REGISTRY}/frontend:v1.0.0
docker push ${REGISTRY}/frontend:latest

Fenêtre de terminal
cd /Users/moise/projects/af-corp/kili-shops-microservices
# Create production values
cat > helm/kili-shops/values-do.yaml << 'EOF'
# Digital Ocean production values
# Use registry images
global:
imagePullPolicy: Always
apiGateway:
enabled: true
replicaCount: 2
image:
repository: registry.digitalocean.com/afcorp/api-gateway
tag: latest
service:
type: LoadBalancer # DO will create a Load Balancer
port: 80
resources:
limits:
cpu: 500m
memory: 512Mi
requests:
cpu: 250m
memory: 256Mi
shops:
enabled: true
replicaCount: 2
image:
repository: registry.digitalocean.com/afcorp/shops
tag: latest
resources:
limits:
cpu: 500m
memory: 512Mi
requests:
cpu: 250m
memory: 256Mi
users:
enabled: true
replicaCount: 2
image:
repository: registry.digitalocean.com/afcorp/users
tag: latest
resources:
limits:
cpu: 500m
memory: 512Mi
requests:
cpu: 250m
memory: 256Mi
mongodb:
enabled: true
persistence:
enabled: true
size: 10Gi
storageClass: "do-block-storage"
resources:
limits:
cpu: 1000m
memory: 1Gi
requests:
cpu: 500m
memory: 512Mi
postgresql:
enabled: true
persistence:
enabled: true
size: 10Gi
storageClass: "do-block-storage"
env:
POSTGRES_PASSWORD: "CHANGE_ME_IN_PRODUCTION" # Change this!
resources:
limits:
cpu: 1000m
memory: 1Gi
requests:
cpu: 500m
memory: 512Mi
EOF
echo "✓ Created values-do.yaml"
echo "⚠️ IMPORTANT: Edit values-do.yaml and change the PostgreSQL password!"
Fenêtre de terminal
# Install backend
helm install kili-shops ./helm/kili-shops -f ./helm/kili-shops/values-do.yaml
# Watch deployment
kubectl get pods -w
# Wait for all pods to be Running (this may take 5-10 minutes)
# Press Ctrl+C when done watching
Fenêtre de terminal
# Wait for Load Balancer to be provisioned (2-3 minutes)
kubectl get service api-gateway -w
# When EXTERNAL-IP shows an IP address (not <pending>), press Ctrl+C
# Get the IP
export BACKEND_IP=$(kubectl get service api-gateway -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
echo "Backend API available at: http://${BACKEND_IP}"
# Test the backend
curl http://${BACKEND_IP}/health

Fenêtre de terminal
cd /Users/moise/projects/af-corp/kilimmanjaro
# Use the backend IP from previous step
REGISTRY="registry.digitalocean.com/kili-shops"
BACKEND_IP="YOUR_BACKEND_IP_HERE" # Replace with actual IP
# Rebuild frontend with correct API URL
docker build \
--build-arg REACT_APP_API_URL="http://${BACKEND_IP}/api" \
-t ${REGISTRY}/frontend:v1.0.0 \
-f Dockerfile .
docker tag ${REGISTRY}/frontend:v1.0.0 ${REGISTRY}/frontend:latest
# Push updated image
docker push ${REGISTRY}/frontend:v1.0.0
docker push ${REGISTRY}/frontend:latest
Fenêtre de terminal
cat > helm/kilimmanjaro-frontend/values-do.yaml << EOF
# Digital Ocean production values for frontend
frontend:
enabled: true
replicaCount: 2
image:
repository: registry.digitalocean.com/afcorp/frontend
tag: latest
pullPolicy: Always
service:
type: LoadBalancer
port: 80
env:
REACT_APP_API_URL: "http://${BACKEND_IP}/api"
resources:
limits:
cpu: 200m
memory: 256Mi
requests:
cpu: 100m
memory: 128Mi
EOF
echo "✓ Created frontend values-do.yaml"
Fenêtre de terminal
# Install frontend
helm install kilimmanjaro ./helm/kilimmanjaro-frontend -f ./helm/kilimmanjaro-frontend/values-do.yaml
# Watch deployment
kubectl get pods -l app.kubernetes.io/component=frontend -w
# Get frontend Load Balancer IP (wait 2-3 minutes)
kubectl get service kilimmanjaro-frontend -w
# When EXTERNAL-IP appears, press Ctrl+C
# Get the IP
export FRONTEND_IP=$(kubectl get service kilimmanjaro-frontend -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
echo "Frontend available at: http://${FRONTEND_IP}"

Fenêtre de terminal
echo "Backend API: http://${BACKEND_IP}"
echo "Frontend: http://${FRONTEND_IP}"
# Open in browser
open "http://${FRONTEND_IP}"
  1. Go to your DNS provider (Namecheap, GoDaddy, Cloudflare, etc.)

  2. Add A records:

    Type Name Value
    A api.test.kilimmanjaro [BACKEND_IP]
    A test.kilimmanjaro [FRONTEND_IP]
  3. Wait for DNS propagation (5-30 minutes)

  4. Update frontend to use domain:

    Fenêtre de terminal
    cd /Users/moise/projects/af-corp/kilimmanjaro
    # Rebuild with domain
    docker build \
    --build-arg REACT_APP_API_URL="http://api.dev.kilimmanjaro.com/api" \
    -t registry.digitalocean.com/afcorp/frontend:v1.0.0 \
    -f Dockerfile .
    docker push registry.digitalocean.com/afcorp/frontend:v1.0.0
    # Restart frontend pods to pull new image
    kubectl rollout restart deployment/kilimmanjaro-frontend
  5. Access:

    • Frontend: http://dev.kilimmanjaro.com
    • Backend API: http://api.dev.kilimmanjaro.com

Fenêtre de terminal
# Check all pods
kubectl get pods
# All pods should be in Running state
# Expected pods:
# - api-gateway-xxx (2 replicas)
# - shops-xxx (2 replicas)
# - users-xxx (2 replicas)
# - mongodb-0 (1 StatefulSet)
# - postgres-0 (1 StatefulSet)
# - kilimmanjaro-frontend-xxx (2 replicas)
Fenêtre de terminal
# Check all services
kubectl get services
# Should see:
# - api-gateway (LoadBalancer with EXTERNAL-IP)
# - kilimmanjaro-frontend (LoadBalancer with EXTERNAL-IP)
# - mongodb, postgres, shops, users (ClusterIP)
Fenêtre de terminal
# Health check
curl http://${BACKEND_IP}/health
# Get shops (should return empty array initially)
curl http://${BACKEND_IP}/api/shops
# Get users (should return empty array initially)
curl http://${BACKEND_IP}/api/users
# Create a test shop
curl -X POST http://${BACKEND_IP}/api/shops \
-H "Content-Type: application/json" \
-d '{
"name": "Test Shop",
"description": "A test shop",
"location": "New York"
}'
# Verify shop was created
curl http://${BACKEND_IP}/api/shops
Fenêtre de terminal
# Open in browser
echo "Open: http://${FRONTEND_IP}"
# Or if using DNS
echo "Open: http://dev.kilimmanjaro.com"

In the browser:

  1. Navigate to the frontend URL
  2. Check that pages load correctly
  3. Try creating a shop or user
  4. Verify data persists
Fenêtre de terminal
# API Gateway logs
kubectl logs -f deployment/api-gateway
# Shops service logs
kubectl logs -f deployment/shops
# Users service logs
kubectl logs -f deployment/users
# Frontend logs
kubectl logs -f deployment/kilimmanjaro-frontend
# Database logs
kubectl logs -f statefulset/mongodb
kubectl logs -f statefulset/postgres
Fenêtre de terminal
# Install metrics server (if not already installed)
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
# Wait a minute, then check
kubectl top nodes
kubectl top pods

Fenêtre de terminal
# Describe pod to see events
kubectl describe pod <pod-name>
# Check logs
kubectl logs <pod-name>
# Check if image exists
doctl registry repository list-tags <repository-name>
Fenêtre de terminal
# Verify registry authentication
kubectl get secret -n kube-system | grep registry
# Re-authenticate with registry
doctl registry login
# If using DO registry, ensure cluster has access
doctl registry kubernetes-manifest | kubectl apply -f -
Fenêtre de terminal
# Check service
kubectl describe service api-gateway
# Digital Ocean may have limits - check dashboard
# Ensure you have fewer than the account limit of Load Balancers
Fenêtre de terminal
# Check database pods
kubectl get pods -l app.kubernetes.io/component=database
# Check logs
kubectl logs statefulset/mongodb
kubectl logs statefulset/postgres
# Test connectivity from app pod
kubectl exec -it deployment/shops -- nc -zv mongodb 27017
kubectl exec -it deployment/users -- nc -zv postgres 5432

This is usually because the API URL is wrong:

Fenêtre de terminal
# Check what API URL was baked into frontend
kubectl get configmap kilimmanjaro-frontend-config -o yaml
# If wrong, rebuild frontend with correct URL and redeploy

Fenêtre de terminal
# Digital Ocean dashboard
echo "View cluster at: https://cloud.digitalocean.com/kubernetes/clusters"
# Or via CLI
doctl kubernetes cluster get kili-shops-test
Fenêtre de terminal
# Backend update
cd /Users/moise/projects/af-corp/kili-shops-microservices
# Build and push new version
docker build -f Dockerfile.gateway -t registry.digitalocean.com/afcorp/api-gateway:v1.1.0 .
docker push registry.digitalocean.com/afcorp/api-gateway:v1.1.0
# Update Helm values to use new tag
# Then upgrade
helm upgrade kili-shops ./helm/kili-shops -f ./helm/kili-shops/values-do.yaml
# Frontend update
cd /Users/moise/projects/af-corp/kilimmanjaro
docker build --build-arg REACT_APP_API_URL="http://${BACKEND_IP}/api" \
-t registry.digitalocean.com/afcorp/frontend:v1.1.0 .
docker push registry.digitalocean.com/afcorp/frontend:v1.1.0
helm upgrade kilimmanjaro ./helm/kilimmanjaro-frontend -f ./helm/kilimmanjaro-frontend/values-do.yaml
Fenêtre de terminal
# Scale API Gateway
kubectl scale deployment api-gateway --replicas=3
# Or update Helm values and upgrade

Fenêtre de terminal
# Uninstall frontend
helm uninstall kilimmanjaro
# Uninstall backend
helm uninstall kili-shops
# Delete PVCs (this deletes database data!)
kubectl delete pvc --all
Fenêtre de terminal
# Via CLI
doctl kubernetes cluster delete kili-shops-test
# Or via web interface
# Go to: https://cloud.digitalocean.com/kubernetes/clusters
# Click on cluster → Settings → Destroy
Fenêtre de terminal
# Delete registry
doctl registry delete kili-shops
# This deletes all images

Monthly costs for this setup:

  • Kubernetes Cluster: 3 nodes × $24/month = $72/month
  • Load Balancers: 2 × $12/month = $24/month
  • Block Storage (PVCs): 20GB × $0.10/GB = $2/month
  • Container Registry: Free tier (500MB storage)

Total: ~$98/month

To reduce costs:

  • Use 2 nodes instead of 3: ~$50/month savings
  • Use NodePort instead of LoadBalancer for frontend: $12/month savings
  • Use smaller nodes ($18/month each): ~$18/month savings

  1. Change PostgreSQL Password

    Fenêtre de terminal
    # Generate strong password
    openssl rand -base64 32
    # Update in values-do.yaml
    # Then upgrade: helm upgrade kili-shops ...
  2. Enable HTTPS

    • Use cert-manager for automatic SSL certificates
    • Configure ingress with TLS
  3. Setup Ingress Controller

    • Instead of LoadBalancer per service
    • One LoadBalancer with ingress routing
  4. Enable Network Policies

    • Restrict pod-to-pod communication
  5. Setup Monitoring

    • Prometheus + Grafana
    • Digital Ocean Monitoring
  6. Setup Backups

    • Database backups
    • Automated snapshots

After successful deployment:

  1. Setup CI/CD

    • GitHub Actions to auto-deploy on push
    • Automated testing
  2. Setup Monitoring

    • Application monitoring
    • Error tracking (Sentry)
  3. Setup Logging

    • Centralized logging (ELK stack)
  4. Performance Optimization

    • Enable caching
    • Optimize database queries
    • CDN for frontend assets
  5. Security Hardening

    • Regular security updates
    • Vulnerability scanning
    • Secret management (Sealed Secrets or Vault)