EN · $

Nextcloud Scaling on VPS with Docker Swarm and Load Balancing

Learn how to automatically scale Nextcloud on a VPS with Docker Swarm and distribute the load to handle growing user numbers.

Why Scale Nextcloud?

When your Nextcloud instance grows, demands on storage, computing power, and network increase. With Docker Swarm, you can horizontally scale your Nextcloud containers and distribute the load across multiple nodes. This keeps you performant even with many users.

Prerequisites

  • A VPS with Docker and Docker Swarm (multiple nodes recommended)
  • Basic knowledge of Docker and Nextcloud
  • A network load balancer (e.g., Traefik or Nginx)

Step 1: Initialize Docker Swarm

Initialize your swarm on the manager node:

docker swarm init --advertise-addr <MANAGER-IP>

Add worker nodes by running the command from the output on the workers.

Step 2: Define Nextcloud as a Stack

Create a docker-compose.yml for Nextcloud including database and Redis. Define services with deploy policies for scaling:

version: '3.8'
services:
  nextcloud:
    image: nextcloud:latest
    ports:
      - "8080:80"
    deploy:
      replicas: 3
      resources:
        limits:
          cpus: '0.5'
          memory: 512M
    volumes:
      - nextcloud_data:/var/www/html
    networks:
      - nextcloud_net
  db:
    image: mariadb:10.5
    ...
  redis:
    image: redis:alpine
    ...
volumes:
  nextcloud_data:
    driver: local
networks:
  nextcloud_net:
    driver: overlay

Step 3: Load Balancing with Traefik

Traefik as a reverse proxy automatically detects Docker Swarm. Configure Traefik with a traefik.yml and add labels in the compose file:

labels:
  - "traefik.enable=true"
  - "traefik.http.routers.nextcloud.rule=Host(\"nextcloud.your-domain.de\")"
  - "traefik.http.services.nextcloud.loadbalancer.server.port=80"

Deploy the stack with docker stack deploy -c docker-compose.yml nextcloud.

Step 4: Set Up Automatic Scaling

Use Docker Swarm's built-in service rollback and update. For automatic scaling based on CPU/memory, you can use tools like Docker Swarm Autoscaler or Kubernetes Metrics Server (if you migrate later). A simple script with docker service scale often suffices:

#!/bin/bash
if [ $(docker stats --no-stream --format "{{.CPUPerc}}" $(docker service ps nextcloud_nextcloud -q) | awk -F'%' '{print $1}' | sort -rn | head -1) > 80 ]; then
  docker service scale nextcloud_nextcloud=5
fi

Step 5: Persistent Data and Storage

Use a distributed file system like NFS or GlusterFS so that all containers can access the same data. Alternatively, you can use Nextcloud's Object Storage (e.g., S3-compatible).

Further Optimizations

  • Use Redis for caching and locking (as shown above).
  • Enable PHP Opcache and APCu in Nextcloud.
  • Use a CDN for static assets.

With Docker Swarm and load balancing, your Nextcloud instance scales effortlessly. For powerful VPS solutions, check out our VPS Server – ideal for Docker Swarm and container orchestration.