The Simplest Way to Make Google Pub/Sub and Longhorn Work Like They Should

Published
Length
3 min read

Luis Felipe Dias

All posts

Teams pair Google Pub/Sub and Longhorn to run event-driven storage tasks in Kubernetes. A cloud message triggers a worker pod to snapshot, back up, or resize a block volume. This workflow keeps cluster storage in sync with outside cloud events. App teams can back up data right before a big job runs. They can also clone disks when a new test message arrives. How data flows between Google Pub/Sub and Longhorn Google Pub/Sub acts as the message bus in Google Cloud. Longhorn runs inside your Kub

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

Teams pair Google Pub/Sub and Longhorn to run event-driven storage tasks in Kubernetes. A cloud message triggers a worker pod to snapshot, back up, or resize a block volume.

This workflow keeps cluster storage in sync with outside cloud events. App teams can back up data right before a big job runs. They can also clone disks when a new test message arrives.

How data flows between Google Pub/Sub and Longhorn

Google Pub/Sub acts as the message bus in Google Cloud. Longhorn runs inside your Kubernetes cluster and manages block storage volumes for pods. These two tools do not talk to each other on their own. You need a small worker app inside the cluster to bridge them.

First, an outside service sends an event message to a Pub/Sub topic. The worker pod in your cluster reads that message from a subscription. To do this, the worker pod logs in to Google Cloud. Workload identity lets the pod trade its Kubernetes token for a cloud token. This step avoids putting static key files inside your container images.

Next, the worker pod reads the message body and checks the task type. It then calls the Kubernetes API to update a Longhorn custom resource. Kubernetes checks the service account role attached to that worker pod. If the role allows the change, the storage controller starts the volume task. Once the disk snapshot or backup finishes, the worker acks the cloud message.

Common failure modes and how to scope roles

Many teams start by sharing one broad service account across many worker pods. This shortcut creates huge security risks in a live cluster. If one pod gets hacked, the attacker can delete every storage volume. Broad cloud keys also let attackers read topics meant for other teams.

Unlogged storage changes cause another major headache during an outage. When five pods use the same account, audit logs show one generic name. You cannot tell which message or user triggered a bad volume wipe. Also, message payloads sometimes hold sensitive fields like emails or phone numbers. If a worker dumps raw payloads to standard output, those fields leak into logs. Data masking rewrites matched fields in results such as card numbers or national IDs.

End-to-End Encryption: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

To fix these issues, scope every role to the smallest possible task. Give your worker pod a cloud role that reads from only one subscription. In Kubernetes, bind the pod to a role that edits only snapshot objects. Never give a message worker full admin rights over the storage namespace.

Network paths and retry safety for storage tasks

Network drops between the cluster and Google Cloud can break storage jobs. Your worker pod needs outbound HTTPS traffic to reach the cloud message API. Inside the cluster, use network rules to block pods that do not need storage APIs. Only your specific worker pod and the storage engine should talk to the API server.

You must also plan for duplicate messages from the cloud topic. Pub/Sub sends a message at least once, so duplicates happen often. If a snapshot takes ten minutes, the ack timer might expire early. The cloud topic then sends the same snapshot message a second time. Give each storage request a unique ID based on the message ID. Before creating a new volume or backup, the worker checks if that ID exists. This simple check stops duplicate disk clones from filling up your cluster nodes.

Check database and API queries on the wire

Identity tools, roles, and network rules decide who opens a session. Setup alone cannot check what each query does or mask sensitive values in a response once a session starts. When workflows touch Postgres, MySQL, SQL Server, MongoDB, ClickHouse, Kubernetes APIs, SSH, or internal HTTP APIs, hoop.dev runs beside those services as an open-source proxy, a sidecar for your Postgres or API. It inspects requests coming in and results going back on the wire.

Your login details and client tools stay as they are while hoop.dev guardrails refuse bad deletes or drops before they run. On responses, hoop.dev data masking rewrites matched sensitive values in memory. Furthermore, hoop.dev holds SOC 2 Type II, is MIT licensed, and writes every query and decision to an audit trail that generates evidence for HIPAA, GDPR, and PCI DSS. See the source on GitHub.

Frequently asked questions

Can Longhorn read messages from Google Pub/Sub directly?

No, Longhorn does not have a built-in cloud message reader. You must run a lightweight worker pod in Kubernetes to read messages and call the storage API.

How should the worker pod log in to Google Cloud?

Use workload identity federation instead of downloaded JSON key files. This setup links a Kubernetes service account to a Google Cloud role with short-lived tokens.

How do I stop duplicate messages from creating extra volume snapshots?

Use the unique Pub/Sub message ID as part of the snapshot name. Have your worker check for an existing snapshot with that name before starting a new one.

The query runs. The emails stay hidden.