Replicate between clusters¶
GarageReplication copies objects between two independent Garage clusters
through their S3 APIs. It has been available since v0.9.2 and is off by default
(chart value replication.enabled). Use it to:
- Replicate: keep a warm copy of production buckets on another cluster.
- Migrate: move buckets to a new cluster with a short write freeze.
It is not a backup. There are no point-in-time snapshots and no restore,
and in Mirror mode an accidental deletion or overwrite on the source reaches
the destination on the next run. Bucket backups need a snapshot tool.
It is also different from Garage's own replication (replication.factor),
which spreads data across the nodes of one cluster, and from
federation, which joins several Kubernetes clusters into one
Garage cluster. Two clusters linked by GarageReplication share nothing: no
RPC secret, no layout, no keys.
How it works¶
Each run starts one Kubernetes Job per bucket, in the GarageReplication's
namespace. The Job runs rclone (image
docker.io/rclone/rclone, pinned by digest) to compare the source and
destination buckets and copy what differs. The operator:
- schedules runs (cron), never runs two at once, and runs at most
transfer.maxConcurrentBucketsbuckets in parallel; - passes credentials to rclone only through
secretKeyRefenvironment variables. They never appear in the Job's arguments, in status or in the operator's logs, and the operator never reads their values; - reads each Job's result from its termination message and records rclone's
counters per bucket in
status.buckets; - deletes old Jobs (
successfulJobsHistoryLimit: 1,failedJobsHistoryLimit: 3).
The Jobs need network access to both S3 endpoints. Nothing else talks to
Garage: the operator does not use either cluster's Admin API for replication.
The Pods run as a non-root user with a read-only root filesystem, no
capabilities and no service account token, so they pass the restricted Pod
Security Standard.
Enable the controller¶
Replication is off by default. Enable it in the chart:
This adds --enable-replication to the operator and grants it
create/delete/get/list/watch on batch/jobs and write access to
GarageReplication status. Without it, GarageReplication objects are
validated but not reconciled, and kubectl apply prints a warning saying so.
Credentials (least privilege)¶
Each side reads an S3 access key from a Secret in the replication's
namespace. The Job sends that key to the endpoint named in the spec, so the
Secret must opt in with the label
garage.rajsingh.info/replication-credentials: "true". Without the label the
replication reports Ready=False with reason CredentialsNotAllowed and runs
nothing. Only someone who can write the Secret can add the label, so being
allowed to create a GarageReplication does not let anyone copy another
application's buckets with its key.
The default data keys, access-key-id and secret-access-key, are the ones a
GarageKey writes. Set the label through the key's Secret template:
# Read-only key on the source cluster
apiVersion: garage.rajsingh.info/v1beta1
kind: GarageKey
metadata:
name: replication-reader
namespace: replication
spec:
clusterRef:
name: garage
namespace: storage # needs a GarageReferenceGrant for kind GarageKey
secretTemplate:
labels:
garage.rajsingh.info/replication-credentials: "true"
bucketPermissions:
- globalAlias: medias
read: true
| Side | Permissions |
|---|---|
| Source | read on every source bucket |
| Destination | read + write on every destination bucket (Garage's write includes deletes, which Mirror needs) |
No owner permission and no bucket creation are needed. Replication never
creates buckets: a missing bucket fails with BucketNotFound. Create
destination buckets beforehand, for example with GarageBucket.
For a destination outside the operator's reach, create and label the Secret yourself:
kubectl -n replication create secret generic dr-replication-writer \
--from-literal=access-key-id=GK... --from-literal=secret-access-key=...
kubectl -n replication label secret dr-replication-writer \
garage.rajsingh.info/replication-credentials=true
A private CA for an endpoint comes from a ConfigMap (caBundleRef,
key ca.crt by default). A CA certificate is public; a Secret reference would
reopen the path the opt-in closes.
Examples¶
Replicate to another site¶
apiVersion: garage.rajsingh.info/v1beta1
kind: GarageReplication
metadata:
name: production-to-dr
namespace: replication
spec:
source:
clusterRef: {name: garage, namespace: storage} # needs a GarageReferenceGrant
credentialsSecretRef: {name: replication-reader}
destination:
endpoint:
url: https://s3.dr.example.com
region: garage
caBundleRef: {name: dr-ca} # only for a private CA
credentialsSecretRef: {name: dr-replication-writer}
buckets:
- source: medias
destination: medias
- source: pg-dumps
destination: pg-dumps-dr
schedule: "*/15 * * * *"
buckets is keyed by destination: a destination bucket appears once, and the
same source may feed several destinations.
A cross-namespace clusterRef needs a grant in the cluster's namespace:
apiVersion: garage.rajsingh.info/v1beta1
kind: GarageReferenceGrant
metadata:
name: allow-replication
namespace: storage
spec:
from:
- kind: GarageReplication
namespace: replication
to:
- kind: GarageCluster
name: garage
One-shot copy¶
Without schedule, a Copy replication runs once when it is created, then
only when triggered:
kubectl -n replication annotate garagereplication production-to-dr \
garage.rajsingh.info/trigger-sync="$(date +%s)" --overwrite
Deletion semantics¶
Nothing destructive is enabled by default.
mode |
Source object deleted | Source object changed | Object only on the destination |
|---|---|---|---|
Copy (default) |
kept on the destination | overwritten on the destination | kept |
Mirror |
deleted on the destination | overwritten | deleted |
Copy does not make the destination immutable: changed objects are
overwritten.
Safety guards for Mirror:
- First run on a trigger. A bucket that has not run in
Mirrormode yet (a new replication, or one switched fromCopy) is held withhold: AwaitingTriggerandSynced=False/AwaitingTrigger. Check both sides, then set thetrigger-syncannotation to start it. Later scheduled runs need no trigger. - Delete caps.
mirror.maxDeletesis required;mirror.maxDeleteSize(for example10G) optionally caps the deleted bytes too. A run that reaches either cap stops withMaxDeletesExceeded. Deletions made up to that point are kept. - Empty source. If the source bucket is empty while the destination is
not, the run refuses to start (
SourceEmpty). This protects against a wrong bucket name, a key that sees nothing, or a wiped source. - Hold. After a cap or the empty-source guard trips, the bucket is
held:
status.buckets[].holdis set,Synced=False/SafetyHold, scheduled runs skip it, and the Job is not retried, so the next run does not delete another batch. Only thetrigger-syncannotation releases a hold. Editing the spec (for example raisingmaxDeletes) does not: investigate, adjust the spec if needed, then trigger.
Changing mode from Mirror to Copy cancels running Mirror Jobs and drops
AwaitingTrigger holds (Copy never deletes). Removing a bucket from
spec.buckets, or giving its destination another source, cancels its running
Job. Other changes apply from the next run; the running Job finishes with the
configuration it started with.
A destination bucket can be written by only one GarageReplication, across
all namespaces: two Mirrors on one bucket would delete each other's objects.
The first one keeps it; admission rejects the second, and if one slips through
(created while the webhook was down, or naming the same cluster through a
clusterRef and a URL) it reports Ready=False/DestinationConflict and runs
nothing. A conflicting object in another namespace is not named.
Deleting a GarageReplication deletes its Jobs (owner references) and never
touches objects on either side.
Operate¶
| Action | How |
|---|---|
| Pause | spec.suspend: true. No scheduled run starts; a running run finishes. |
| Stop now | kubectl delete job -l garage.rajsingh.info/replication-uid=<uid>. The bucket is recorded as Cancelled. |
| Resume | spec.suspend: false. |
| Run now, release holds | garage.rajsingh.info/trigger-sync=<unique value>. Works while suspended. If a run is in progress, the triggered run starts right after it. The annotation is removed once the run starts, and status.lastOperation records it. |
$ kubectl get garagereplications -n replication
NAME MODE SCHEDULE PHASE LAST SUCCESS AGE
production-to-dr Copy */15 * * * * Syncing 14m 3d
status reports:
phase:Pending(configuration not usable, seeReady),Idle,Syncing,SuspendedorFailed(the last run had a failed bucket);currentRun/lastRun: ID, reason (Initial,Scheduled,Triggered), start and completion time, result;lastSyncTime,lastSuccessfulSyncTime,nextSyncTime;buckets[], keyed bydestination:activeJob,lastResult,lastMode,lastSuccessfulTime,hold,lastError,lastVerification, andlastStatistics(transfers,bytes,checks,deletes,errors,elapsedSeconds,differences). The statistics are rclone's own counters for the last Job, and they are absent when the Job reported none (for example a Pod that was killed).
Conditions: Ready (endpoints, Secrets, opt-in, grants and destination
exclusivity resolve; reasons include CredentialsNotAllowed,
DestinationConflict, SecretNotFound, ReferenceNotGranted), Synced
(every bucket succeeded in the last run on the current configuration; reasons
NeverSynced, SyncFailed, SafetyHold, AwaitingTrigger, ConfigChanged),
Suspended. Events: SyncStarted, SyncSucceeded, SyncFailed,
SyncCancelled, SyncSkipped.
lastError is one of these fixed reasons, classified from rclone's exit code
first and from its log only where the exit code is ambiguous. rclone's own
output, which can name objects, stays in the Job's Pod logs.
| Reason | Meaning |
|---|---|
AuthFailed |
the S3 key was rejected (403) |
BucketNotFound |
a source or destination bucket does not exist |
EndpointUnreachable |
connection, timeout, server or TLS errors left after rclone's retries |
SourceEmpty |
empty-source guard (see above) |
MaxDeletesExceeded |
maxDeletes or maxDeleteSize reached (see above) |
VerificationFailed |
verification: Check found differences |
DeadlineExceeded |
the Job ran longer than retry.activeDeadlineSeconds |
TransferErrors |
other rclone errors; see the Pod log |
Unknown |
the Pod ended without a result (for example it was OOM-killed) |
Prometheus metrics (operator registry), with labels namespace,
resource_namespace, replication, source_bucket and destination_bucket.
resource_namespace is the GarageReplication's Kubernetes namespace and is
not rewritten by Prometheus when honorLabels is false; namespace is kept
for compatibility:
garage_operator_replication_last_success_timestamp_secondsgarage_operator_replication_last_failedgarage_operator_replication_last_transferred_bytesgarage_operator_replication_held
Example alert for a 15-minute schedule:
- alert: GarageReplicationStale
expr: time() - garage_operator_replication_last_success_timestamp_seconds > 3600
for: 10m
Tuning¶
| Field | Default | Effect |
|---|---|---|
transfer.concurrency |
4 | objects copied in parallel per bucket (rclone --transfers) |
transfer.checkers |
8 | objects compared in parallel per bucket (--checkers) |
transfer.bandwidthLimit |
unlimited | bytes/s per Job; K, M, G are KiB, MiB, GiB, e.g. 50M (--bwlimit) |
transfer.maxConcurrentBuckets |
1 | buckets replicated at the same time |
retry.backoffLimit |
2 | Pod retries per bucket and run; already-copied objects are skipped |
retry.activeDeadlineSeconds |
21600 | maximum duration of a bucket's Job |
verification |
None |
Check re-compares source and destination after the copy |
resources, podTemplate |
resources, node selector, tolerations, affinity, priority class, Pod labels/annotations |
Inside a run, rclone retries failed operations itself (3 attempts of the
whole bucket, 10 low-level retries). Kubernetes then retries the Pod
backoffLimit times, and the next scheduled run tries again.
Objects are compared by size and MD5. For objects uploaded in one part, Garage's ETag is the MD5 of the content, so the comparison is exact. For multipart objects that rclone did not upload itself there is no MD5, so the comparison falls back to size only. A changed object of the same size, uploaded in several parts by another client, is then not detected.
Migrate to a new cluster¶
- Recreate the configuration on the new cluster declaratively (see
What is not replicated):
GarageBuckets, andGarageKeys withimportKeyreusing the same access key IDs and secrets, so applications only need a new endpoint. - Create a
GarageReplicationfrom the old cluster to the new one withmode: Copyand a schedule. The first run copies everything. - Let scheduled runs keep the new cluster close while applications write to
the old one. Watch
lastStatistics.transfersdrop to the write rate. - Freeze writes: scale the writers down, or remove
writefrom their keys on the old cluster. - Final sync: remove the schedule, set
mode: Mirror(withmirror.maxDeletes) if deletions made since step 2 must reach the new cluster, andverification: Check. Then trigger a run; the trigger is also what lets the firstMirrorrun start:
kubectl annotate garagereplication migrate garage.rajsingh.info/trigger-sync="final-$(date +%s)" --overwrite
- Wait for
lastRun.reason: Triggered,lastRun.result: Succeeded,Synced=True, andlastVerification: Passedwithdifferences: 0on every bucket. - Point applications at the new cluster.
- Set
suspend: trueor delete theGarageReplication(no data is deleted).
The Kind end-to-end test (Label("replication")) runs this sequence.
Limitations of this procedure:
- A run is not a point-in-time snapshot. It lists while writes continue; objects written after the listing are picked up by the next run. Only the write freeze in step 4 makes the final sync complete.
Copynever propagates deletions. That is why step 5 switches toMirror. Anything written directly to the new cluster before cutover is deleted by that run, up tomaxDeletes.- No two-way replication. Two
Mirrorreplications in opposite directions delete each other's writes. - Object metadata.
Content-Type,Cache-Controland user metadata are copied.Last-Modifiedbecomes the time of the copy (rclone keeps the original inx-amz-meta-mtime). Multipart objects get new ETags. In-progress multipart uploads are not copied.
Switch to the replica after losing a site¶
- Set
suspend: trueon the replication. If the source is unreachable, runs fail withEndpointUnreachableand change nothing on the destination. - Check
status.buckets[].lastSuccessfulTime: objects written to the source after that time are not on the replica. - Recreate keys and permissions on the replica (see below) and point applications at it.
- Once the original site is back, replicate in the other direction with
mode: Copyfirst (neverMirroruntil you have checked which side holds which data), then cut back with the migration procedure.
What is not replicated¶
Replication copies objects and their metadata only. Garage's internal metadata is cluster-specific and must never be copied between clusters.
| Layer | Replicated? | How to carry it over |
|---|---|---|
| Objects and user metadata | yes | GarageReplication |
| Buckets, aliases, quotas, website, lifecycle rules | no | GarageBucket on the destination cluster (copy the manifests if the source is declarative) |
| Access keys | no | GarageKey with importKey (same ID and secret; IDs must be GK + 24 hex characters before Garage v2.3) |
| Bucket ↔ key permissions | no | GarageKey.spec.bucketPermissions / GarageBucket.spec.keyPermissions |
| CORS rules, website redirects and routing rules | no | S3 API |
| Cluster configuration, layout, node IDs, admin tokens | no, never | the destination's own GarageCluster |
Known limitations¶
- Each run lists every object on both sides. Transfers are incremental, but the listing is not, because Garage has no change feed. For buckets with tens of millions of objects, use a schedule interval longer than one full listing.
- The copy runs in the operator's Kubernetes cluster. It streams through the
Job Pod (no server-side copy between clusters), so plan network bandwidth
and use
bandwidthLimit. - A
SIGTERM(Job deleted, deadline, node drain) stops rclone cleanly. Objects are uploaded atomically, but an interrupted multipart upload leaves an incomplete upload on the destination. Add anabortIncompleteMultipartUploadDayslifecycle rule to destination buckets: