Loading…
Warp is now open-source Learn more
Loading…
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.
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>For example:
$ kubectl create namespace development
namespace/development created.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.
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.
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>key is the label key.
value is the value of the label key.
For example:
$ kubectl label namespace <key>=<value>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 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]The--dry-run flag offers three possible values:
For example:
$ kubectl create namespace production --dry-run=server
namespace/production created (server dry run)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.
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.28Where:
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.yamlThis 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.
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: productionWhere 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: mynamespace3To 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"
}
}
}And run the kubectl create command with the -f flag to create this namespace:
$ kubectl create -f ./my-namespace-file.jsonCreating 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.
In Kubernetes, namespaces must be defined according to the following rules:
It should not use prefixes like kube-, as they are reserved for system namespaces (such as kube-public and kube-system).
Learn how to execute standalone commands and start interactive shell sessions in Pods using the `kubectl exec` command.
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.
Learn how to copy files and directories from within a Kubernetes Pod into the local filesystem using the kubectl command.
Learn how to manually and automatically scale a Deployment based on CPU usage in Kubernetes using the kubectl-scale and kubectl-autoscale commands.
Learn how to tail and monitor Kubernetes logs efficiently to debug, trace, and troubleshoot errors more easily using the kubectl command.
Learn how to forward the ports of Kubernetes resources such as Pods and Services using the kubectl port-forward command.
Learn how to get information about one or more contexts in Kubernetes using the kubectl command.
Learn how to delete one or more namespaces and their related resources in a Kubernetes cluster using the kubectl command.
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.
Learn how to list, describe, customize, sort and filter namespaces in a Kubernetes cluster by name, label, and more using the kubectl command.
Learn how to delete Kubernetes deployments and its associated Pods using the kubectl command.
Learn how to delete a pod in Kubernetes using graceful shutdowns, labels, field selectors, and namespaces.
$ kubectl create namespace <namespace_name>$ kubectl create namespace development
namespace/development created.$ kubectl label namespace <key>=<value>$ kubectl label namespace <key>=<value>$ kubectl create namespace <namespace> --dry-run=[client|server|none]$ kubectl create namespace production --dry-run=server
namespace/production created (server dry run)apiVersion: v1
kind: Namespace
metadata:
name: development
labels:
pod-security.kubernetes.io/enforce: baseline
pod-security.kubernetes.io/enforce-version: v1.28$ kubectl create -f ./my-namespace-file.yamlapiVersion: v1
kind: Namespace
metadata:
name: development
apiVersion: v1
kind: Namespace
metadata:
name: productionapiVersion: 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{
"apiVersion": "v1",
"kind": "Namespace",
"metadata": {
"name": "development",
"labels": {
"pod-security.kubernetes.io/enforce": "baseline",
"pod-security.kubernetes.io/enforce-version": "v1.28"
}
}
}$ kubectl create -f ./my-namespace-file.json