Sign inSign up

cohandv/kubernetes-operator

By cohandv

Updated 12 months ago

need to verify we are not doing bots

Image
0

1.1K

cohandv/kubernetes-operator repository overview

Database Snapshot Operator

A Kubernetes operator that automates database snapshot management with intelligent retention policies and cross-cluster replication.

Overview

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.

Features

  • 🗄️ Multi-Engine Support: PostgreSQL, MySQL, MongoDB, and Redis
  • 📅 Flexible Scheduling: Cron-based snapshot schedules
  • 🔄 Automatic Retention: Configurable retention policies (hourly, daily, weekly, monthly)
  • 🌍 Cross-Region Replication: Replicate snapshots to S3, GCS, or Azure Blob
  • 🔔 Webhook Notifications: Send alerts to Slack, Discord, or custom webhooks
  • 🏷️ Smart Labeling: Automatic tagging and metadata management
  • 📊 Metrics Export: Prometheus-compatible metrics endpoint

Custom Resource Definition

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]

Installation

Prerequisites
  • Kubernetes 1.21+
  • kubectl configured to access your cluster
  • Cluster admin permissions
Using Docker Image
1. Pull the Operator Image
docker pull ghcr.io/your-org/database-snapshot-operator:v1.2.3
2. Deploy Using Helm
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
3. Deploy Using kubectl
# 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/
4. Running Locally with Docker

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

Quick Start

1. Create a Database Reference
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"
2. Create a Snapshot Policy
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
3. Verify Snapshots
# 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

Configuration Options

Environment Variables
VariableDescriptionDefault
ENABLE_WEBHOOKSEnable admission webhooksfalse
LOG_LEVELLogging level (debug, info, warn, error)info
METRICS_ADDRMetrics endpoint address:8080
HEALTH_PROBE_ADDRHealth probe address:8081
MAX_CONCURRENT_RECONCILESMax concurrent snapshot operations3
SNAPSHOT_TIMEOUTSnapshot operation timeout30m
Helm Values
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

Monitoring

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 operations
  • snapshot_duration_seconds - Snapshot operation duration
  • snapshot_size_bytes - Snapshot size in bytes
  • snapshot_failures_total - Failed snapshot operations

Troubleshooting

Check Operator Logs
kubectl logs -n snapshot-operator-system \
  deployment/snapshot-operator-controller -f
Verify CRD Installation
kubectl get crd databasesnapshots.snapshot.k8s.io
Test with Manual Snapshot
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
Common Issues

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

Examples

See the examples directory for more configurations:

Contributing

Contributions are welcome! Please read our Contributing Guide for details.

License

Apache License 2.0 - see LICENSE file for details.

Support

Tag summary

Content type

Image

Digest

sha256:4b65cf5f4

Size

3.8 MB

Last updated

12 months ago

docker pull cohandv/kubernetes-operator