Managing namespaces in a project
Project namespaces allows you to organize your resources and maintain their isolation. Each project can contain several namespaces.
Getting started
Make sure that:
- You have the project administrator or cluster administrator role.
- The project already exists. If not, create one.
Creating a namespace
The names of project namespaces must be prefixed with <project_name>-. For example, for a project named test-project, the possible names would be test-project-main or test-project-backend.
-
In the left-hand menu, select Projects.
-
Select the project from the list.
-
Click Namespaces on the project’s side panel.
-
Click + Create namespace.
-
Specify the parameters:
- Name: Namespace suffix. The
<project_name>-prefix is added automatically. This is a required field. - Allow deletion: Remove this flag to protect the namespace from accidental deletion. The flag is removed by default.
- Name: Namespace suffix. The
-
Click Create.
To create a namespace, use the ProjectNamespace resource. This resource is created in the service namespace of the project-<project_name> project.
-
Create a manifest file, e.g.,
namespace.yaml:apiVersion: stackland.yandex.cloud/v1alpha1 kind: ProjectNamespace metadata: namespace: project-team-alpha name: team-alpha-backend spec: template: labels: environment: production annotations: description: "Backend services"Where:
metadata.namespace: Project's service namespace inproject-<project_name>format. This is a required field.metadata.name: Name of the new namespace. Must be prefixed with<project_name>-. This is a required field.spec.template.labels: Additional labels for the namespace. This is an optional field.spec.template.labels: Additional annotations for the namespace. This is an optional field.
-
Apply the manifest:
kubectl apply -f namespace.yaml
Stackland will automatically create the team-alpha-backend namespace labeled with stackland.yandex.cloud/project-name: team-alpha.
Alternative method: creating via Namespace
You can create a namespace directly by specifying a project label:
apiVersion: v1
kind: Namespace
metadata:
name: team-alpha-backend
labels:
stackland.yandex.cloud/project-name: team-alpha
Note
To create a namespace directly, you need permissions to create a Namespace resource in the cluster. The name of your namespace must be prefixed with <project_name>-.
Viewing the project's namespaces
- In the left-hand menu, select Projects.
- Select the project from the list.
- Click Namespaces on the project’s side panel.
To get a list of the project's namespaces, run this command:
kubectl get namespaces -l stackland.yandex.cloud/project-name=team-alpha
To get a list of ProjectNamespace resources:
kubectl get projectnamespaces -n project-team-alpha
Deletion protection
By default, namespaces are deletion-protected. This protection is controlled by the Allow deletion flag:
- When the flag is removed, the namespace is deletion-protected.
- When the flag is set, the namespace can be deleted.
To change the protection setting via the management console:
- In the left-hand menu, select Projects.
- Select the project from the list.
- Click Namespaces on the project’s side panel.
- Click the name of the namespace.
- Click Edit.
- Set or remove the Allow deletion flag.
- Click Save.
To change the protection setting via the CLI:
apiVersion: stackland.yandex.cloud/v1alpha1
kind: ProjectNamespace
metadata:
namespace: project-team-alpha
name: team-alpha-backend
spec:
allowDeletion: true
Deleting a namespace
- In the left-hand menu, select Projects.
- Select the project from the list.
- Click Namespaces on the project’s side panel.
- In the namespace row, click ⋯ and select Delete.
- Confirm the deletion.
-
Disable deletion protection, if it is on:
kubectl patch projectnamespace team-alpha-backend \ -n project-team-alpha \ --type merge \ -p '{"spec":{"allowDeletion":true}}' -
Delete the
ProjectNamespaceresource:kubectl delete projectnamespace team-alpha-backend -n project-team-alpha
Stackland will automatically delete the associated namespace and all resources inside it.
Warning
Deleting a namespace deletes all resources inside it. This operation is irreversible.