← All notes

DevOps

Monitoring Celery with Flower — Local Setup and SSH Tunnel for Remote Servers

Flower exposes your Celery queue to the public internet if configured carelessly. This post covers local development setup and a secure SSH tunnel approach for remote production monitoring.

Published
Reading
6 min
Author
Fidelis Uwem Joseph
Series · Part 7 of 9FastAPI in Production
  1. FastAPI Layered Architecture — Routes, Services, and Repositories
  2. Alembic with SQLAlchemy 2.0 Async — Migrations That Hold in Production
  3. Alembic Async Migrations and Idempotent Database Seeding in FastAPI
  4. Stateless OTP Login in FastAPI with JWT and Redis
  5. Async Email Delivery in FastAPI — Two SMTP Paths, One Jinja2 Template System
  6. Celery + Redis in Production — Task Reliability with acks_late and Retry Backoff
  7. Monitoring Celery with Flower — Local Setup and SSH Tunnel for Remote Servers
  8. Mailpit as a Dev SMTP Catch-All Behind Traefik
  9. MinIO as a Self-Hosted S3 Backend in Docker

← Part 6Series overviewPart 8 →

Flower is the standard Celery monitoring dashboard. It shows active workers, task throughput, retry history, and the current queue depth — everything you need to diagnose a stuck worker or a failing task in production. The problem is that every Flower tutorial binds to 0.0.0.0:5555, which means the dashboard is publicly accessible on any server that does not sit behind a firewall.

This post covers two setups: a local development configuration where Flower runs alongside Mailpit and MinIO in docker-compose, and a production configuration where Flower is locked to localhost and accessed through an SSH tunnel from your machine.

Prerequisites

Docker and Docker Compose v2 (docker compose, not docker-compose). For the remote setup, SSH access to the production server with key-based authentication. A running Celery worker is required — Flower connects to the broker, not to a worker process directly.

Background

Flower connects to the Redis broker, listens to the stream of task events that workers publish, and queries workers through Celery’s remote-control (inspect) API. It does not need to run on the same machine as the workers — it only needs network access to the broker. This makes it straightforward to run locally against a remote broker, or to run it on the server but never expose it to the internet.

Implementation — Local Development

1. docker-compose-dev.yml configuration

In development, the goal is convenience: Flower should start automatically, be accessible on a known port, and connect to the same Redis instance as the worker.

docker-compose-dev.yml
services:
redis:
image: redis:7-alpine
container_name: redis
command: redis-server --requirepass ${REDIS_PASSWORD:-password}
ports:
- "6379:6379"
healthcheck:
test: ["CMD-SHELL", "redis-cli -a \"$${REDIS_PASSWORD:-password}\" --no-auth-warning ping | grep -q PONG"]
interval: 10s
timeout: 5s
retries: 5
worker:
build: .
command: celery -A app.workers.celery_app worker --loglevel=info --concurrency=4 -E
env_file: .env.docker
depends_on:
redis:
condition: service_healthy
flower:
image: mher/flower:2.0
ports:
- "127.0.0.1:5555:5555"
environment:
- CELERY_BROKER_URL=redis://:${REDIS_PASSWORD:-password}@redis:6379/0
- FLOWER_BASIC_AUTH=${FLOWER_USER:-admin}:${FLOWER_PASSWORD:-admin}
depends_on:
- redis
- worker

Two details matter even in development. The worker runs with -E, which turns on task events; without it Flower lists workers but its Tasks tab stays empty (set worker_send_task_events=True in the Celery config to make it permanent). And the port is bound to 127.0.0.1: a bare "5555:5555" publishes Flower on every interface, so anyone on the same café Wi-Fi can open it, and Docker’s published ports bypass UFW (see Docker, UFW and SSH tunnels). Open http://localhost:5555 and authenticate with the credentials from your .env.docker file.

2. Running a worker locally alongside Flower

If you are developing without Docker, run the worker and Flower in separate terminals:

Terminal 1 — Celery worker
# Start Redis first (or use the dev compose for infrastructure only)
$ docker compose -f docker-compose-dev.yml up redis mailpit -d
# Then start the worker directly
$ celery -A app.workers.celery_app worker --loglevel=info --concurrency=2
Terminal 2 — Flower
$ celery -A app.workers.celery_app flower --port=5555

Flower connects to the broker URL from your .env file via the Celery app configuration. No separate broker URL argument is needed when running it through celery flower rather than the mher/flower image.

Implementation — Remote Production with SSH Tunnel

1. Production compose.yml — bind to localhost only

The production Flower service binds to 127.0.0.1:5556 on the host, not 0.0.0.0. This means no inbound traffic from outside the server can reach the port. The only way to access Flower is through the server’s loopback interface — which requires either local access or an SSH tunnel.

compose.yml
flower:
image: mher/flower:2.0
container_name: hms-flower
env_file: .env
command: >
celery
--broker=${CELERY_BROKER_URL}
flower
--port=5555
--basic_auth=${FLOWER_USER}:${FLOWER_PASSWORD}
networks:
- infra_net
ports:
- "127.0.0.1:5556:5555"
restart: unless-stopped
depends_on:
worker:
condition: service_healthy

The port mapping 127.0.0.1:5556:5555 is the key detail. Docker maps container port 5555 to host port 5556, but only on the loopback interface. Without the 127.0.0.1: prefix, Docker maps to 0.0.0.0:5556 by default, which makes the port reachable on the server’s public IP.

Using port 5556 on the host instead of 5555 is a minor defence-in-depth measure — automated scanners that probe for Flower specifically look for 5555.

2. Required environment variables

.env
CELERY_BROKER_URL=redis://:yourredispassword@redis:6379/0
FLOWER_USER=admin
FLOWER_PASSWORD=a-strong-random-password-here

The --basic_auth flag in the command sets HTTP Basic Auth. The tunnel already encrypts the connection; basic auth is a second layer against other users and processes on the server, and against the day someone removes the 127.0.0.1: prefix by accident.

3. Opening the SSH tunnel

From your local machine, open a tunnel that forwards your local port 5556 to the server’s loopback port 5556:

Local machine
$ ssh -L 5556:127.0.0.1:5556 user@your-server-ip -N
# -L localport:remote-host:remote-port
# -N do not execute a remote command, tunnel only

After running this, open http://localhost:5556 in your browser. Traffic flows:

Browser → localhost:5556 → SSH tunnel → server:5556 → Docker → Flower container:5555

The connection is encrypted by SSH. On the server, the tunnel’s traffic arrives on the loopback interface, which is the only place the published port listens.

4. Verifying Flower can see the workers

When Flower loads, the Workers tab should list each Celery worker by hostname with its current status (Online/Offline), active task count, and processed task count.

Server — check worker is registered
$ docker exec hms-worker celery -A app.workers.celery_app inspect ping
hms-worker@abc123: {'ok': 'pong'}

If the Workers tab is empty but the inspect command succeeds, the broker URL in the Flower command does not match the broker URL the worker is using. Both must point to the same Redis database index.

5. Checking the Flower UI — key panels

Workers tab: Active task count, task throughput rate, and worker uptime. A worker that shows 0 processed tasks after several minutes of expected activity has not connected to the broker.

Tasks tab: Every task Flower has seen an event for, with its state (SUCCESS, FAILURE, RETRY), runtime and the exception traceback for failures. Filter by state to find all tasks currently retrying. If it stays empty while the Workers tab looks healthy, task events are off: start workers with -E.

Metrics: Flower 2.0 dropped the old in-browser Monitor graphs and exposes Prometheus metrics at /metrics instead (task counts by state, runtimes, queue-to-start latency, worker online status). Scrape it and graph it in Grafana; a flat task rate during expected load means tasks are not being enqueued — check the application side, not the worker.

Pitfalls

Wrapping up

Flower bound to localhost and accessed via SSH tunnel gives you full observability without a single open port. The tunnel uses your existing SSH keys, encrypts all traffic, and adds no new credentials to manage beyond the basic auth password stored in .env.

The next operational problem is history: by default Flower keeps task state in memory, so a restart wipes it. --persistent=True --db=/data/flower.db on a mounted volume keeps it across restarts, but for real retention ship worker logs to an aggregator (Loki, Datadog, CloudWatch) and keep the metrics in Prometheus.

Going deeper

Alert on symptoms, not dashboards. Nobody watches Flower at 3am. The three alerts worth having: queue depth growing for more than N minutes (Redis LLEN celery), failure rate above a threshold per task name, and no worker online. Queue depth is the earliest signal: it rises before users notice anything.

Queue latency beats runtime. A task that runs in 200 ms but waited 4 minutes in the queue is slow from the user’s point of view. The gap between a task’s task-sent and task-started events is its waiting time; graph it per queue to know when to add workers. (task-sent events need task_send_sent_event=True on the producer side.)

A bastion instead of a key per server. With several servers, ProxyJump in ~/.ssh/config routes tunnels through one hardened bastion host, so production servers accept SSH only from the bastion’s address. The Flower setup doesn’t change; only the Host entry does.