Backup labeled namespaces
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. Here’s 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. Here are the guidelines for defining 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:
- Should start with alpha-numeric character
- It must be a DNS sub-domain (For example: example.com/)
- Should be 253 characters or less. If included, it can end with a /
Name:
Required, not optional
- Must be 63 characters or less
- Consist of lowercase alphanumeric characters, -, and .,
- 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 required labels to the namespaces using the CLI, filter the labeled namespaces with the web console and then backup all the labeled namespaces in a single click. Here are the prerequisites to apply namespace labels:
-
The Stork version on your application cluster must be 23.9.1 to leverage namespace and resource label filters feature.
Add Namespace label field gets disabled if the required Stork version is not deployed on the cluster and web console displays the minimum required Stork version.
Apply labels for namespaces
To apply labels for a namespace, from your CLI:
-
Run the below 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 below command to list the namespace(s) with the applied labels:
kubectl get namespaces --show-labels
noteIf you apply same labels to two different namespaces and list them with the applied labels, only first one gets listed.
-
To remove the labels from the namespace:
kubectl label namespaces <name-of-namespace> <key-of-the-label>-
Filter and backup 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 out and auto-selects all the namespaces with the preset labels. For more information on creating a backup with the listed namespaces, refer Create a backup.
You can search a namespace with preset labels either by providing only key or 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 with a comma separator (without space). User interface displays the namespace(s) that matches all the specified labels and auto-selects them to create a backup.
Example:
Consider an example where you create two namespaces ns1
and ns2
and apply the following labels to those two namespaces:
kubectl create ns ns1
kubectl label namespaces ns1 a=b
kubectl label namespaces ns1 c=d
kubectl create ns ns2
kubectl label namespaces ns2 c=d
When you search with the string a=b,c=d
in the Add Namespace label field in the PXB web console, search result applies AND
operation to the provided labels and displays only ns1
and not both ns1
and ns2
.
- Click Backup to back up all the namespaces with the preset label in a single-click.
Automate scheduled backups
If a namespace is yet to be created, you can still create a backup schedule for the future namespace with a label. Portworx Backup picks up the future namespace(s) with this label and automatically includes the namespace(s) as part of the schedule. In such a scenario, manual backup option gets disabled as no immediate backup is created.
If you have created a scheduled backup with one or few namespaces that have preset labels and add namespaces at a later point of time with the same label, scheduler auto-includes these namespaces to the scheduled backup.
To create a backup schedule when namespace is yet to be 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 manual backup option Now is disabled.
-
Click Create. For more information on how to create a backup, refer Create a backup.
Portworx Backup creates a scheduled backup successfully without any namespace in it.
-
After the backup is created, navigate to the Backups tab of the application cluster, click on the vertical ellipsis of newly created scheduled backup and choose Show details to view the backup details:
You can view the backup details as per the below format: