Stash Documentation

repository·master·Indexed 23 days ago

https://github.com/stashed/stash

Stash is a Kubernetes operator for cloud-native data backup and recovery. It leverages restic or CSI VolumeSnapshotter to manage backups for volumes, workloads (Deployments, DaemonSets, StatefulSets), and databases. It is available in a Community Edition for open-source needs and an Enterprise Edition that adds support for on-prem storage, production-grade database management (PostgreSQL, MySQL, MongoDB, etc.), and automation features like batch operations and templates.

Tokens
2.5K
Snippets
5
Records
16
Agent score
81%

What's inside Stash

  1. What is Stash?

    master

    Stash is a cloud-native data backup and recovery solution for Kubernetes workloads. It operates as a Kubernetes operator that leverages restic or Kubernetes CSI Driver VolumeSnapshotter functionality.

    Stash allows you to:

    • Backup Kubernetes volumes mounted in workloads (Deployments, DaemonSets, StatefulSets, etc.).
    • Backup stand-alone volumes (PersistentVolumeClaims and PersistentVolumes).
    • Backup and restore databases (available in Enterprise Edition).
    • Extend functionality for custom workloads using addons.
  2. Compare Stash Community and Enterprise Editions

    master

    Stash is available in two editions. Use the Community Edition for open-source backup needs and the Enterprise Edition for production-grade database management and automation.

    Community Edition (Open Source)

    • Workload Backup: Deployment, DaemonSet, StatefulSet, ReplicaSet, ReplicationController, OpenShift DeploymentConfig.
    • Volume Backup: PersistentVolumeClaim, PersistentVolume.
    • Scheduling: Cron expressions or instant backup via kubectl plugin.
    • Features: Pause backup, subset file backup (pattern matching), automatic snapshot cleanup (retention policies), AES-256 encryption, and deduplication.
    • Storage Backends: AWS S3, Minio, Rook, GCS, Azure, OpenStack Swift, Backblaze B2, and Rest Server.
    • Integration: CSI Driver integration, Prometheus metrics, RBAC/PSP/Network Policy support, and Hooks (httpGet, httpPost, tcpSocket, exec).

    Enterprise Edition (Open Core)

    Includes all Community features plus:

    • On-prem Storage: Local Kubernetes Volumes (e.g., NFS).
    • Database Support: PostgreSQL, MySQL, MongoDB, Elasticsearch, Redis, MariaDB, Percona XtraDB.
    • Automation: Auto Backup via templates and annotations.
    • Batch Operations: Batch Backup & Batch Restore for co-related applications (e.g., WordPress and its database).
    • Planned: Point-In-Time Recovery (PITR).
  3. CompletedConfig

    master
    CompletedConfig is the result of calling Complete() on a StashConfig. It contains the validated and fully populated configuration required to instantiate a StashServer. It wraps a private completedConfig to ensure it can only be created through the proper configuration lifecycle.
  4. Configure and initialize the StashServer

    master

    To run the Stash service, you must use the StashConfig type to provide both a GenericConfig (for the Kubernetes API server) and an ExtraConfig (for the Stash controller).

    Follow this pattern to initialize the server:

    1. Populate a StashConfig object.
    2. Call Complete() on the config to get a CompletedConfig.
    3. Call New() on the CompletedConfig to instantiate the StashServer.
    4. Call Run(ctx) on the resulting StashServer to start the controller and the API server.
  5. Configure cloud storage credentials via environment variables

    master

    Stash uses environment variables to authenticate with various cloud storage providers. You can configure credentials for AWS, DigitalOcean Spaces, Google Cloud, Azure, OpenStack Swift, and Backblaze B2 by setting the corresponding keys in your environment or a .env file.

    # AWS
    AWS_ACCESS_KEY_ID=<your-aws-key-id>
    AWS_SECRET_ACCESS_KEY=<your-aws-secret-access-key>
    
    # DigitalOcean Spaces
    DO_ACCESS_KEY_ID=<your-do-spaces-key-id>
    DO_SECRET_ACCESS_KEY=<your-do-spaces-secret-access-key>
    
    # Google Cloud
    GOOGLE_PROJECT_ID=<your-google-project-id>
    GOOGLE_APPLICATION_CREDENTIALS=<path-to-sa-json-key-file>
    
    # Azure
    AZURE_ACCOUNT_NAME=<your-azure-storage-account-name>
    AZURE_ACCOUNT_KEY=<your-azure-storage-account-key>
    
    # OpenStack Swift
    OS_AUTH_URL=<your-openstack-swift-auth-url>
    OS_TENANT_ID=<your-openstack-tenant-id>
    OS_TENANT_NAME=<your-openstack-tenant-name>
    OS_USERNAME=<your-openstack-username>
    OS_PASSWORD=<your-openstack-password>
    OS_REGION_NAME=<your-openstack-region-if-any>
    
    # Backblaze B2
    B2_ACCOUNT_ID=<your-b2-account-id>
    B2_ACCOUNT_KEY=<your-b2-account-key>
  6. StashConfig

    master

    StashConfig is the initial configuration structure used to define the parameters for the Stash service. It requires two main components:

    • GenericConfig: A *genericapiserver.RecommendedConfig used to configure the underlying Kubernetes generic API server.
    • ExtraConfig: A *controller.Config used to configure the Stash-specific controller logic, including webhooks and client configurations.
  7. Run the Stash CLI via the main entrypoint

    master

    The main package provides the entrypoint for the Stash application. It initializes the root command using cmds.NewRootCmd(), configures logging via logs.Init, and executes the command-line interface. If execution fails, it logs the error using klog.Fatalln.

    package main
    
    import (
    	"os"
    	"runtime"
    
    	"stash.appscode.dev/stash/pkg/cmds"
    	"gomodules.xyz/logs"
    	"k8s.io/klog/v2"
    )
    
    func main() {
    	rootCmd := cmds.NewRootCmd()
    	logs.Init(rootCmd, true)
    	defer logs.FlushLogs()
    
    	if len(os.Getenv("GOMAXPROCS")) == 0 {
    		runtime.GOMAXPROCS(runtime.NumCPU())
    	}
    
    	if err := rootCmd.Execute(); err != nil {
    		klog.Fatalln("Error in Stash Main:", err)
    	}
    	klog.Infoln("Exiting Stash Main")
    }
  8. Configure and run the Stash server with StashOptions

    master

    The StashOptions struct is the primary configuration object used to initialize, validate, and run the Stash server. It combines standard Kubernetes RecommendedOptions with ExtraOptions specific to Stash.

    To use it, follow this lifecycle:

    1. Initialize with NewStashOptions(out, errOut).
    2. Register flags using AddFlags(fs).
    3. Validate the configuration using Validate(args).
    4. Run the server using Run(ctx) (which internally calls Config() and Complete()).
  9. Initialize StashOptions with NewStashOptions

    master
    Use NewStashOptions(out, errOut io.Writer) to create a new instance of StashOptions. This function initializes the RecommendedOptions with the default etcd path prefix /registry/stash.appscode.com and the legacy codec for admissionv1beta1.SchemeGroupVersion. Note that Etcd and Admission options are explicitly set to nil in this constructor.
  10. Generate StashConfig with Config()

    master

    The Config() method transforms StashOptions into a server.StashConfig. This process:

    • Sets up secure serving with self-signed certificates on localhost (127.0.0.1) if not otherwise configured.
    • Applies options to a new genericapiserver.RecommendedConfig.
    • Configures OpenAPI (v2 and v3) definitions using v1alpha1.GetOpenAPIDefinitions.
    • Sets up IgnorePrefixes for various admission webhooks and validators to prevent OpenAPI discovery issues.
    • Initializes ExtraConfig using controller.NewConfig and applies ExtraOptions to it.