In Kubernetes, namespaces provide a logical way to separate resources within an application, forming isolated virtual clusters within the Kubernetes cluster. For example, you can create namespaces for the development and production environments and manage their resources independently.
The operations performed in one namespace do not affect the other namespaces. Thus, it is an ideal option for managing resources separately, especially when working on large projects where every resource has different needs, authorization controls, and varied resource consumption limits.
The short answer
By default, Kubernetes allocates all resources to the default namespace, allowing you to utilize your cluster without the initial step of manually creating a namespace.
To create a custom namespace, you can use the kubectl create namespace command followed by the name of your namespace:
$ kubectl create namespace <namespace_name>
Run in Warp
For example:
$ kubectl create namespace development
namespace/development created.
Run in Warp
Upon execution, the above command will output the name of the newly created namespace in the terminal to confirm its successful creation.
Note that you can use the kubectl get namespaces command to view all available namespaces in your cluster. You can read more about this command by consulting the official documentation page.
Easily retrieve this command using Warp’s AI Command Search
If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Search feature:
Entering k8 namespace in the AI Command Search will prompt a kubectl command, which can be inserted quickly into your shell by doing CMD+ENTER.
Adding labels to existing namespaces
In Kubernetes, labels are key-value pairs that can be attached to various resources, including namespaces. Labels are beneficial to identify and organize namespaces in larger Kubernetes environments.
To label an existing namespace, you can use the kubectl label command as follows:
$ kubectl label namespace <key>=<value>
Run in Warp
key is the label key.
value is the value of the label key.
For example:
$ kubectl label namespace <key>=<value>
Run in Warp
You can learn more about this command on the official documentation page. Note that, by applying labels strategically, you can implement pod security standards, implement access control and organize your resources better.
Simulating namespace creation
Simulating namespace creation helps validate the creation before directly applying it to your live Kubernetes cluster. This simulation will help you avoid unintended changes, such as misconfigurations or potential errors causing instability within the live cluster.
To simulate the creation of a namespace, you can use the kubectl create namespace command followed by the --dry-run flag:
$ kubectl create namespace <namespace> --dry-run=[client|server|none]
Run in Warp
The--dry-run flag offers three possible values:
- client specifies a dry run on the client side, meaning it validates the request locally without sending server requests. This is helpful for quickly verifying the correctness of the command and ensuring it won't trigger any errors.
- server specifies a dry run on the server side, where the request is sent to the server for validation, but any changes are not persisted. The response from the server indicates whether the creation of the namespace would have been successful or not without actually creating the namespace.
- none specifies that no dry run is performed, and the namespace creation request is directly applied to the server, resulting in the actual creation of the namespace. This is the default behavior of the kubectl create namespace command without the --dry-run flag.
For example:
$ kubectl create namespace production --dry-run=server
namespace/production created (server dry run)
Run in Warp
The above command performs a dry run on the server side to create a new namespace named production, and the --dry-run=server flag ensures that the server processes the request as if it were a real creation attempt, providing a response indicating success or failure without actually creating the namespace.
Creating a namespace using a YAML file
To create a namespace using a YAML file, you can define the configurations as follows:
apiVersion: v1
kind: Namespace
metadata:
name: development
labels:
pod-security.kubernetes.io/enforce: baseline
pod-security.kubernetes.io/enforce-version: v1.28
Run in Warp
Where:
- apiVersion specifies the version of the Kubernetes API.
- kind specifies the type of Kubernetes resource.
- metadata specifies metadata about the resource.
- name specifies the name of the resource.
- labels specifies the key-value pairs which you want to assign to the resource.
Then use the kubectl create command with the -f flag to create the namespace defined in the YAML file:
$ kubectl create -f ./my-namespace-file.yaml
Run in Warp
This command reads the configurations from the file and creates a new namespace accordingly.
Note that using a YAML file for namespace creation is beneficial as storing it in repositories enables easy tracking of namespace configurations. This also aids in versioning and automating namespace creation.
Creating multiple namespaces at once
To create multiple namespaces at once using a single YAML file, you can define the configurations as follows:
apiVersion: v1
kind: Namespace
metadata:
name: development
---
apiVersion: v1
kind: Namespace
metadata:
name: production
Run in Warp
Where each section represents a separate namespace configuration.
Note that the three dashes (---) are used as a document separator, allowing you to define multiple resources at once using a single YAML file.
Alternatively, you can use the List resource with the following syntax to achieve the same result:
For example:
apiVersion: v1
kind: List
items:
- apiVersion: v1
kind: Namespace
metadata:
name: mynamespace1
- apiVersion: v1
kind: Namespace
metadata:
name: mynamespace2
- apiVersion: v1
kind: Namespace
metadata:
name: mynamespace3
Run in Warp
Creating a namespace using a JSON file
To create a namespace using a JSON file, you can define the configurations as follows:
{
"apiVersion": "v1",
"kind": "Namespace",
"metadata": {
"name": "development",
"labels": {
"pod-security.kubernetes.io/enforce": "baseline",
"pod-security.kubernetes.io/enforce-version": "v1.28"
}
}
}
Run in Warp
And run the kubectl create command with the -f flag to create this namespace:
$ kubectl create -f ./my-namespace-file.json
Run in Warp
Creating namespaces using either YAML or JSON files is beneficial when orchestrating complex deployments or managing configurations across various environments. These structured files offer a clear and concise way to define Kubernetes resources, aiding in better management and automation of your applications within Kubernetes.
Best practices when choosing a namespace
In Kubernetes, namespaces must be defined according to the following rules:
- It must be unique in a cluster and should not clash with existing ones. If you attempt to create an existing namespace, the command will result in an error (indicating that the namespace already exists), and the creation of the namespace will fail.
- It must be a valid DNS label (i.e. it can only contain lowercase characters, numbers, dash (-), underscore (_), or period (.).
- It must start and end with an alphanumeric character.
It should not use prefixes like kube-, as they are reserved for system namespaces (such as kube-public and kube-system).
Written by
Mansi Manhas
Filed Under
Related Articles
Copy Files From Pod in Kubernetes
Learn how to copy files and directories from within a Kubernetes Pod into the local filesystem using the kubectl command.
Scale Deployments in Kubernetes
Learn how to manually and automatically scale a Deployment based on CPU usage in Kubernetes using the kubectl-scale and kubectl-autoscale commands.
Get Kubernetes Logs With kubectl
Learn how to get the logs of pods, containers, deployments, and services in Kubernetes using the kubectl command. Troubleshoot a cluster stuck in CrashloopBackoff, ImagePullBackoff, or Pending error states.
Forward Ports In Kubernetes
Learn how to forward the ports of Kubernetes resources such as Pods and Services using the kubectl port-forward command.
Tail Logs In Kubernetes
Learn how to tail and monitor Kubernetes logs efficiently to debug, trace, and troubleshoot errors more easily using the kubectl command.
Get Context In Kubernetes
Learn how to get information about one or more contexts in Kubernetes using the kubectl command.
Delete Kubernetes Namespaces With kubectl
Learn how to delete one or more namespaces and their related resources in a Kubernetes cluster using the kubectl command.
Get Kubernetes Secrets With kubectl
Learn how to list, describe, customize, sort and filter secrets in a Kubernetes cluster by name, type, namespace, label and more using the kubectl command.
List Kubernetes Namespaces With kubectl
Learn how to list, describe, customize, sort and filter namespaces in a Kubernetes cluster by name, label, and more using the kubectl command.
How To List Events With kubectl
Learn how to list and filter events in Kubernetes cluster by namespace, pod name and more using the kubectl command.
Kubernetes vs Docker: The Backbone of Modern Backend Technologies
Lean the fundamentals of the Kubernetes and Docker technologies and how they interplay with each other.
Set Context With kubectl
Learn how to create, modify, switch, and delete a context in Kubernetes using the kubectl config command.