need to verify we are not doing bots
1.1K
A Kubernetes operator that automates database snapshot management with intelligent retention policies and cross-cluster replication.
The Database Snapshot Operator watches for DatabaseSnapshot custom resources and automatically creates, manages, and cleans up database snapshots according to your specified policies. It supports multiple database engines and can replicate snapshots across different storage backends for disaster recovery.
apiVersion: snapshot.k8s.io/v1alpha1
kind: DatabaseSnapshot
metadata:
name: prod-postgres-daily
namespace: databases
spec:
databaseRef:
name: postgres-primary
namespace: databases
schedule: "0 2 * * *" # Daily at 2 AM
retention:
hourly: 24
daily: 7
weekly: 4
monthly: 12
compression: gzip
encryption:
enabled: true
secretRef: snapshot-encryption-key
replication:
- provider: s3
bucket: my-backup-bucket
region: us-west-2
storageClass: GLACIER_IR
notifications:
- type: slack
webhook: https://hooks.slack.com/services/YOUR/WEBHOOK/URL
events: [success, failure]
kubectl configured to access your clusterdocker pull ghcr.io/your-org/database-snapshot-operator:v1.2.3
helm repo add snapshot-operator https://charts.snapshot-operator.io
helm repo update
helm install snapshot-operator snapshot-operator/database-snapshot-operator \
--namespace snapshot-operator-system \
--create-namespace \
--set image.repository=ghcr.io/your-org/database-snapshot-operator \
--set image.tag=v1.2.3
# Install CRDs
kubectl apply -f https://raw.githubusercontent.com/your-org/database-snapshot-operator/main/config/crd/bases/snapshot.k8s.io_databasesnapshots.yaml
# Create namespace
kubectl create namespace snapshot-operator-system
# Deploy the operator
kubectl apply -f - <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
name: snapshot-operator-controller
namespace: snapshot-operator-system
spec:
replicas: 1
selector:
matchLabels:
app: snapshot-operator
template:
metadata:
labels:
app: snapshot-operator
spec:
serviceAccountName: snapshot-operator-controller
containers:
- name: manager
image: ghcr.io/your-org/database-snapshot-operator:v1.2.3
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080
name: metrics
- containerPort: 8081
name: health
env:
- name: ENABLE_WEBHOOKS
value: "true"
- name: LOG_LEVEL
value: "info"
resources:
limits:
cpu: 500m
memory: 512Mi
requests:
cpu: 100m
memory: 128Mi
livenessProbe:
httpGet:
path: /healthz
port: 8081
initialDelaySeconds: 15
periodSeconds: 20
readinessProbe:
httpGet:
path: /readyz
port: 8081
initialDelaySeconds: 5
periodSeconds: 10
EOF
# Create RBAC resources
kubectl apply -f https://raw.githubusercontent.com/your-org/database-snapshot-operator/main/config/rbac/
For development or testing:
# Run the operator container with kubeconfig mounted
docker run -d \
--name snapshot-operator \
-v ~/.kube/config:/root/.kube/config:ro \
-e KUBECONFIG=/root/.kube/config \
-p 8080:8080 \
-p 8081:8081 \
ghcr.io/your-org/database-snapshot-operator:v1.2.3
# View logs
docker logs -f snapshot-operator
apiVersion: v1
kind: Secret
metadata:
name: postgres-credentials
namespace: databases
type: Opaque
stringData:
username: admin
password: supersecret
host: postgres-primary.databases.svc.cluster.local
port: "5432"
apiVersion: snapshot.k8s.io/v1alpha1
kind: DatabaseSnapshot
metadata:
name: hourly-snapshots
namespace: databases
spec:
databaseRef:
name: postgres-primary
namespace: databases
schedule: "0 * * * *" # Every hour
retention:
hourly: 48
compression: zstd
encryption:
enabled: true
secretRef: snapshot-encryption-key
Apply the configuration:
kubectl apply -f snapshot-policy.yaml
# List all snapshots
kubectl get databasesnapshots -A
# Get detailed snapshot information
kubectl describe databasesnapshot hourly-snapshots -n databases
# View snapshot events
kubectl get events -n databases --field-selector involvedObject.name=hourly-snapshots
| Variable | Description | Default |
|---|---|---|
ENABLE_WEBHOOKS | Enable admission webhooks | false |
LOG_LEVEL | Logging level (debug, info, warn, error) | info |
METRICS_ADDR | Metrics endpoint address | :8080 |
HEALTH_PROBE_ADDR | Health probe address | :8081 |
MAX_CONCURRENT_RECONCILES | Max concurrent snapshot operations | 3 |
SNAPSHOT_TIMEOUT | Snapshot operation timeout | 30m |
replicaCount: 1
image:
repository: ghcr.io/your-org/database-snapshot-operator
tag: v1.2.3
pullPolicy: IfNotPresent
resources:
limits:
cpu: 500m
memory: 512Mi
requests:
cpu: 100m
memory: 128Mi
metrics:
enabled: true
serviceMonitor:
enabled: true
webhooks:
enabled: true
certManager: true
The operator exposes Prometheus metrics on port 8080:
# Port-forward to access metrics
kubectl port-forward -n snapshot-operator-system \
deployment/snapshot-operator-controller 8080:8080
# View metrics
curl http://localhost:8080/metrics
Key metrics:
snapshot_operations_total - Total snapshot operationssnapshot_duration_seconds - Snapshot operation durationsnapshot_size_bytes - Snapshot size in bytessnapshot_failures_total - Failed snapshot operationskubectl logs -n snapshot-operator-system \
deployment/snapshot-operator-controller -f
kubectl get crd databasesnapshots.snapshot.k8s.io
apiVersion: snapshot.k8s.io/v1alpha1
kind: DatabaseSnapshot
metadata:
name: manual-test
namespace: databases
spec:
databaseRef:
name: postgres-primary
namespace: databases
schedule: "@once" # Run immediately
retention:
hourly: 1
Issue: Snapshots not being created
Solution: Check database credentials and network connectivity
kubectl get secret postgres-credentials -n databases -o yaml
kubectl exec -it postgres-primary-0 -n databases -- pg_isready
Issue: Permission denied errors
Solution: Verify RBAC roles are correctly configured
kubectl get clusterrole snapshot-operator-role -o yaml
See the examples directory for more configurations:
Contributions are welcome! Please read our Contributing Guide for details.
Apache License 2.0 - see LICENSE file for details.
Content type
Image
Digest
sha256:4b65cf5f4…
Size
3.8 MB
Last updated
12 months ago
docker pull cohandv/kubernetes-operator