Backup using namespace labels
Namespace labels
Namespace labels are key-value pairs attached to namespaces to organize and identify them for various purposes like filtering, categorizing, or applying policies. The following shows the structure of key-value pairs for namespace labels:
metadata:
name: <namespace-name>
labels:
<key1>: <value1>
<key2>: <value2>
...
In Kubernetes labels, the key in a key-value pair follows specific conventions. The following guidelines describe how to define keys:
Key structure
Format: Keys can have up to two segments separated by a /, with the first segment being optional.
Example: prefix/name
Prefix:
Optional, but if used:
- Must start with an alphanumeric character
- Must be a DNS sub-domain (for example:
example.com/) - Must be 253 characters or less. If included, it can end with a
/
Name:
Required
- Must be 63 characters or less
- Must consist of lowercase alphanumeric characters, hyphens (
-), and periods (.) - Must start and end with an alphanumeric character.
Examples of valid keys:
- environment (without prefix)
- app.kubernetes.io/name
- example.com/owner
Examples of common keys:
Without prefix:
- environment: to specify the environment type (for example: production, staging)
- team: to indicate the responsible team (for example: backend, frontend)
With prefix:
- app.kubernetes.io/version: to specify the version of an app (for example: v1.0)
- cloud.example.com/region: to specify a region in multi-cloud setups (for example: us-west)
Using prefixes is helpful to avoid conflicts, especially in shared cluster environments.
For more information on labeling guidelines, refer to Kubernetes labels.
Prerequisites
You can apply labels to namespaces using the CLI, filter the labeled namespaces with the web console, and then back up all the labeled namespaces in a single click. The following prerequisites apply to namespace labels:
-
The Stork version on your application cluster must be 23.9.1 to use the namespace and resource label filters feature.
The Add Namespace label field is disabled if the required Stork version is not deployed on the cluster. The web console displays the minimum required Stork version.
Apply labels for namespaces
To apply labels for a namespace, from your CLI:
-
Run the following command:
kubectl label namespaces <name-of-namespace> <label_name_1> <label_name_2>………...<label_name_n>You can apply multiple unique labels (separated by a space) to a namespace in key-value format.
noteThe key-value pair label format takes equals sign (=) as a separator.
-
Run the following command to list the namespaces with the applied labels:
kubectl get namespaces --show-labelsnoteIf you apply the same labels to two different namespaces and list them with the applied labels, only the first one is listed.
-
To remove the labels from the namespace:
kubectl label namespaces <name-of-namespace> <key-of-the-label>-
Filter and back up labeled namespaces
To filter or view the namespaces with preset labels and then take a backup of the listed namespaces in the web console:
-
From the home page, go to the left navigation pane and click Clusters icon.
-
In the Clusters page, navigate to the required cluster.
-
Go to Applications tab and then click NS tab.
-
In the Search by backup label field, provide the required namespace label(s) that you applied through the CLI and then press Enter.
Portworx Backup lists and auto-selects all the namespaces with the preset labels. For more information on creating a backup with the listed namespaces, refer to Create a backup.
noteYou can search for a namespace with preset labels by providing either only the key or a key-value string in the Search by backup label field. If you have applied multiple labels to a namespace, you can provide all the applied labels in the Search by backup label field separated by commas (without spaces). The user interface displays the namespaces that match all the specified labels and auto-selects them for backup creation.
Example:
Consider an example where you create two namespaces,
ns1andns2, and apply the following labels to them:kubectl create ns ns1kubectl label namespaces ns1 a=bkubectl label namespaces ns1 c=dkubectl create ns ns2kubectl label namespaces ns2 c=dWhen you search with the string
a=b,c=din the Add Namespace label field in the Portworx Backup web console, the search applies anANDoperation to the provided labels and displays onlyns1, not bothns1andns2. -
Click Backup to back up all the namespaces with the preset label in a single-click.
Automate scheduled backups
If a namespace has not yet been created, you can still create a backup schedule for the future namespace with a label. Portworx Backup picks up future namespaces with this label and automatically includes them as part of the schedule. In such a scenario, the manual backup option is disabled because no immediate backup is created.
If you have created a scheduled backup with one or a few namespaces that have preset labels and later add namespaces with the same label, the scheduler automatically includes these namespaces in the scheduled backup.
To create a backup schedule when a namespace has not yet been created:
-
Type the required label in the Add Namespace label field.
-
Click Backup. In the Create Backup window, enter the values for the required fields. Note that the manual backup option Now is disabled.
-
Click Create. For more information on how to create a backup, refer to Create a backup.
Portworx Backup creates a scheduled backup successfully without any namespace in it. You can modify the namespace labels you added during backup creation. For more information, refer to Manage labels.
-
After the backup is created, navigate to the Backups tab of the application cluster, click the vertical ellipsis of the newly created scheduled backup, and choose Show details to view the backup details:
You can view the backup details in the following format:
Namespace label-based backup use cases
The following examples demonstrate how to use namespace label selectors to control which namespaces are included in scheduled backups. Portworx Backup evaluates the selectors during each scheduled backup run, so changes to namespace labels are automatically reflected in subsequent backups without modifying the schedule.
Some examples use exclusion operators (!=, notin, and !<key>), which require advanced filtering. For supported Stork versions, selector syntax, and how include and exclude requirements are combined, see Create Backups with Advanced Filters.
To configure namespace label selectors, navigate to Clusters > your cluster > Applications > NS. Open the Filter by labels drop-down, enter each requirement in the Filter by namespace labels box, and click Apply. When multiple requirements are specified, a namespace must satisfy all of them to be selected.
Namespace label selectors use standard Kubernetes selector syntax. Before creating a backup schedule, you can use kubectl to verify which namespaces match a selector:
kubectl get namespaces -l '<selector>' --show-labels
For example:
kubectl get namespaces -l 'backup-exclude!=true'
After creating a schedule, you can preview the matching namespaces by opening Manage Labels for the schedule and clicking Preview NS List.
Exclude a set of namespaces from a scheduled backup
Use this approach to back up all namespaces on a cluster except specific namespaces, such as CI, development, or sandbox namespaces. Instead of explicitly selecting every namespace to protect, apply an exclusion label to the namespaces that should not be included in the backup.
Namespaces without the exclusion label are automatically included, including namespaces created after the schedule is configured.
-
Apply an exclusion label to each namespace you want to exclude:
kubectl label namespaces ci-runner sandbox-01 sandbox-02 backup-exclude=true -
Verify which namespaces match the selector:
kubectl get namespaces -l 'backup-exclude!=true' -
In the Filter by namespace labels box, enter the following selector and click Apply:
backup-exclude!=trueAlternatively, use:
backup-exclude notin (true)Both selectors match namespaces that do not have the
backup-excludelabel and namespaces where the label value is nottrue. -
Click Backup, keep On a schedule enabled, select a schedule policy, and click Create.
To modify the exclusion list after creating the schedule:
-
Exclude another namespace: Apply the exclusion label:
kubectl label namespaces <namespace> backup-exclude=true -
Include a previously excluded namespace: Remove the exclusion label:
kubectl label namespaces <namespace> backup-exclude-
The updated namespace selection takes effect during the next scheduled backup run.
A namespace label selector that contains only exclusion requirements can match all other namespaces on the cluster, including system namespaces such as kube-system and the namespace where Portworx Backup is installed. To exclude these namespaces, apply the appropriate exclusion labels or combine the exclusion requirements with an include selector.
Back up an environment except for specific namespaces
Use this approach to protect namespaces belonging to a specific environment while excluding selected namespaces, such as those undergoing migration or maintenance.
Combine an include requirement with an exclusion requirement:
env=prod
backup-exclude!=true
A namespace must satisfy both requirements to be included in the backup. The selector matches namespaces labeled env=prod that do not have backup-exclude=true.
Example:
Apply the production environment label to three namespaces:
kubectl label namespaces orders payments inventory env=prod
Exclude the inventory namespace:
kubectl label namespaces inventory backup-exclude=true
With the combined selector, the schedule includes orders and payments but excludes inventory.
If the exclusion label is later removed from inventory, the namespace is included in the next scheduled backup run.
Exclude namespaces that have one of several label values
Use the notin operator to exclude namespaces whose label matches any value in a specified set.
In this example, env is the label key, and dev, test, and prod are values of that label. They are not namespace names.
Example:
Apply the env label to four existing namespaces, and create a fifth namespace without the label:
kubectl label namespaces payments-dev env=dev
kubectl label namespaces payments-qa env=test
kubectl label namespaces payments-stage env=staging
kubectl label namespaces payments-live env=prod
kubectl create namespace shared-tools
Verify the labels:
kubectl get namespaces payments-dev payments-qa payments-stage payments-live shared-tools -L env
NAME STATUS AGE ENV
payments-dev Active 5m dev
payments-qa Active 5m test
payments-stage Active 5m staging
payments-live Active 5m prod
shared-tools Active 1m
To exclude the namespaces whose env label value is dev or test, use the following selector:
env notin (dev,test)
This selector matches namespaces where the env label has a value other than dev or test. It also matches namespaces that do not have the env label. In this example, the schedule includes payments-stage, payments-live, and shared-tools, and excludes payments-dev and payments-qa.
To select only namespaces that have an env label while excluding dev and test, combine the notin operator with a label existence requirement:
env
env notin (dev,test)
With these requirements, namespaces must have an env label, and its value must not be dev or test. In this example, the schedule includes only payments-stage and payments-live. The shared-tools namespace is excluded because it has no env label.
You can confirm the result with kubectl before you create the schedule:
kubectl get namespaces -l 'env,env notin (dev,test)' -L env
Exclude namespaces that have a label key, regardless of its value
Use the !<key> operator (DoesNotExist) to exclude namespaces that contain a specific label key, regardless of its value.
This approach is useful for labels such as deprecated or decommission, where the presence of the label determines whether the namespace should be excluded.
For example:
!deprecated
This selector matches only namespaces that do not have the deprecated label.
To exclude a namespace that is being decommissioned, apply the label:
kubectl label namespaces legacy-billing deprecated=2026-q4
The legacy-billing namespace is excluded from the next scheduled backup run.
Backups created before the label was applied remain available for restore according to their retention settings.
Assign namespaces to different schedules by protection tier
Use this approach when namespaces require different backup frequencies or retention settings. Create separate backup schedules for each protection tier and use namespace labels to determine which schedule protects each namespace.
-
Assign a protection-tier label to the namespaces:
kubectl label namespaces orders payments backup-tier=goldkubectl label namespaces reporting analytics backup-tier=silver -
Create a backup schedule with an hourly schedule policy and the following namespace label selector:
backup-tier=gold -
Create another backup schedule with a daily schedule policy and the following selector:
backup-tier=silver
With this configuration:
- The
ordersandpaymentsnamespaces are included in the hourly backup schedule. - The
reportingandanalyticsnamespaces are included in the daily backup schedule.
To move a namespace from one protection tier to another, update its label:
kubectl label namespaces reporting backup-tier=gold --overwrite
The reporting namespace is included in the gold-tier schedule and no longer matches the silver-tier schedule during subsequent backup runs.
Because a namespace can have only one value for the backup-tier label, it matches at most one of these tier-based schedules. Ensure that every namespace requiring protection has an appropriate tier label.
Automatically protect new namespaces
Use this approach to automatically include newly created namespaces in scheduled backups based on their labels.
Create a backup schedule with an include selector such as:
backup=enabled
Add this label to the standard namespace definitions used by teams, such as those maintained in a GitOps repository or namespace template.
For example:
apiVersion: v1
kind: Namespace
metadata:
name: team-a-app
labels:
backup: enabled
When a namespace is created with the backup=enabled label, Portworx Backup automatically includes it in the next scheduled backup run. You do not need to modify the existing schedule.
You can also create a backup schedule before any namespaces match the selector. For more information, see Automate scheduled backups.
Change the selector of an existing schedule
You can modify the namespace label selector of an existing backup schedule to include or exclude additional namespaces.
For example, if a schedule currently uses:
env=prod
You can add an exclusion requirement:
env=prod
backup-exclude!=true
To update the selector:
- Open the existing backup schedule.
- Click Manage Labels.
- Update the requirements in Filter by namespace labels.
- Click Preview NS List to verify which namespaces match the updated selector.
- Save the changes.
The updated selector is applied during subsequent scheduled backup runs.
You can modify the namespace label selector of a schedule that was created with label-based namespace selection. However, you cannot add a namespace label selector to a schedule that was originally created without one.
For more information, see Manage Labels.