Open In App

Kubernetes – Namespaces

Last Updated : 17 Jun, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Kubernetes Namespace is a mechanism that enables you to organize resources. It is like a virtual cluster inside the cluster. A namespace isolates the resources from the resources of other namespaces. For example, You need to have different names for deployments/services in a namespace but you can have the same name for deployment in two different namespaces.

What are Kubernetes Namespaces?

Kubernetes namespaces are the way of dividing the cluster resources between multiple users. They comes with a mechanism of creating logical isolated environments within the same kubernetes cluster. Each namespace has its own set of policies, resources, and access controls making them ideal for the environments such as development, staging and production. provides better resource management, security and maintaining an organized structure within large kubernetes deployments.

Why use Kuberentes Namespaces?

The following are the reasons to use kubernetes Namespaces:

  • Resource Isolation: They provide logical separation of resources, ensuring that different applications or teams do not interfere with each other within the same cluster.
  • Access Control: Namespaces enable fine-grained access controls, allowing administrators to define permissions and policies specific to each namespace.
  • Environment Segregation: They facilitate the creation of separate environments (e.g., development, testing, production) within a single cluster, improving organization and management.
  • Efficient Resource Management: Namespaces allow for better resource allocation and quota management, preventing any single application from consuming excessive resources at the expense of others.

Kubernetes Namespaces

When the kubernetes cluster is setup, at that time 4 kubernetes namespaces are created, each with some specific purpose. Those are as follows:

  • kube-system: System processes like Master and kubectl processes are deployed in this namespace; thus, it is advised not to create or modify the namespace.
  • kube-public: This namespace contains publicly accessible data like a configMap containing cluster information.
  • kube-node-lease: This namespace is the heartbeat of nodes. Each node has its associated lease object. It determines the availability of a node.
  • default: This is the namespace that you use to create your resources by default.

Although whatever resources you create will be created in the default namespace but you can also create your own new namespace and create resources there.

Note: Avoid creating namespaces with the prefix Kube-, since it is reserved for Kubernetes system namespaces and you should not try to modify them.

kubectl get namespaces

Namespaces and DNS 

The namespace will isolate the services which need to have limited authorization and DNS will expose the application which you have deployed in the form of containers in some cases bother DNS and namespaces will work together. Kubernetes will assign the DNS to the namespace where our resources need to be exposed with the following naming convention.

<service-name>.<namespace-name>.svc.cluster.local

Here,

  • <service-name>: indicates The name of the service associated with the resource.
  • <namespace-name>: The name of the namespace in which the resource resides.

Kubernetes Namespace Yaml

Kubernetes namespace yaml file is used to create the namespaces in Kubernetes where you can isolate the resources which are going to be deployed in the Kubernetes cluster.

Sample Kubernetes Namespace Yaml File

The following is the sample kubernetes namespace yaml file:

apiVersion: v1
kind: Namespace
metadata:
name: <NameSpaceName>
labels: # Labels are key-value pairs (Metadata)
<key1>: <value1>
<key2>: <value2>

Example of Kubernetes Namespace

By the following yaml manefist file, the name of the namespace will be “test-ns” and the key-value pair will be “team: testing team”

apiVersion: v1
kind: Namespace
metadata:
name: test-ns
labels:
team: testingteam

Create New Namespaces

  • You can create your namespace by using the command:
$ kubectl create namespace your-namespace

create namespace

  • As you can see we have successfully created namespace gfg.

Creating Component in a Namespace

  • To create a component in a namespace you can either give the –namespace flag or specify the namespace in the configuration file.
  • The following command is used for deploying your configuration yaml file in a particular namespace Using –namespace flag:
$ kubectl apply -f  your_config.yaml --namespace=your-namespace
  • Then you can check resources in your namespace using kubectl get and specify namespace using -n

using namespace

Create Pods In Specific Namespace

  • Instead of specifying a namespace using the –namespace flag you can specify your namespace initially in your config file only.

nginx file

  • After saving the above yaml code in the file your_config_file.yaml, apply the configuration with the following command:
$ kubectl apply -f your_config_file.yaml

kubectl apply

Difference between Kubernetes Cluster and Namespaces

The following are the differences between kubernetes cluster and Namespaces:

Aspect Kubernetes Cluster Kubernetes Namespace
Definition A complete environment consisting of multiple nodes (servers) for running containerized applications. A logical partition within a Kubernetes cluster to isolate resources and manage access.
Scope Encompasses the entire infrastructure, including all nodes, networking, and storage. Operates within the boundaries of a single cluster, isolating resources for different projects or teams.
Resource Isolation Provides isolation at the infrastructure level across different clusters. Provides logical isolation within a single cluster, allowing multiple environments (e.g., dev, test, prod).
Usage Used to deploy and manage containerized applications across multiple nodes and regions. Used to segregate environments, manage permissions, and organize resources within the same cluster.
Access Control Controls are applied at the cluster level, affecting all namespaces within it. Fine-grained access control can be applied to specific namespaces, restricting user permissions and resource usage.

Differences between Namespaces and Context in Kubernetes

The following are the differences between Kubernetes Namespaces and Context:

Aspect Kubernetes Namespaces Kubernetes Contexts
Purpose Logical partitioning within a cluster Configuration setting for kubectl to access different clusters or namespaces
Scope Isolates resources within the same cluster Defines cluster, user, and namespace settings for kubectl
Resource Management Manages resource quotas and limits within the cluster Switches between different clusters or namespaces
Access Control Applies role-based access control (RBAC) within the namespace Determines user access and permissions for kubectl commands
Use Case Ideal for separating environments (e.g., dev, prod) Useful for managing multiple clusters and user contexts

Working with Namespaces Commands

Namespaces in kubernetes are a way to create and organize virtual clusters within physical clusters where we can isolate a group of resources within a single cluster. Namespace helps to organize resources such as pods, services, and volumes within the cluster. Workloads of applications and authorization can be managed by using kubernetes namespaces.

1. Viewing Namespaces in Kubernetes

In real-time situations, you will find no.of namespaces that will be used for different applications to list all the namespaces which are present in the cluster you can use the following command.

kubectl get namespaces

2. Describe a Namespace

  • Use the following command to see more specific information about a particular namespace.
kubectl describe namespace my-namespace

In the above command, “kubectl is the command line interface in place of “my-namespace” you can use the required namespace you want. You will get detailed information about the namespace such as the namespace name, creation timestamp, and labels associated with the namespace.

3. Listing Pods in Specific Namespace

  • You can view all the resources present in the particular namespace by using the following command.
kubectl get pods --namespace=my-namespace

4. Label the Namespace

  • You can add the label to an existing namespace which is further used to help while creating resources.
kubectl label namespaces <namespace> <labelKey>=<value>
  • In the place of <namespace> give the name of the namespace which you want to label and key-value pair format.

5. Delete a Namespace

  • The following command is used for deleting a kubernetes namespace:
 kubectl delete namespace <namespace-name>

6. Setting Default Namespace for Current Context

  • The following command is used for setting the default namespace for current context:
 kubectl config set-context --current --namespace=<namespace-name>

7. Run a Command is specific Namespace

  • The following command is used for running a command in a specific namespace:
 kubectl get pods -n <namespace-name>

8. Setting The Namespace For A Request 

Setting up a namespace in kubernetes can be done in two ways one is the imperative way and another is and declarative way means by using the command line of kubectl and by writing a yaml file. as explained in the following commands and code

  • The following command is used as Imperative way or command line of kubectl for listing the pods in a particular namespace:
kubectl get pods --namespace=my-namespace
  • This command will fetch the pods from the specified namespace.
  • The following is an example for Declarative way ( manifest yaml file ):
apiVersion: v1
kind: Namespace
metadata:
name: <NameSpaceName>
lables: # Labels are key value pairs(Metadata)
<key>: <value>
<key> <value>

9. Setting The Namespace Preference

Setting the namespace preference will make the default namespace for API to interact with the cluster. After setting up namespace preferences you can deploy the resource in that particular namespace where you manage all the resources without any confusion. The namespace preference is typically configured on the Kubernetes API server and can be set to one of the following options:

  1. Cluster-wide default namespace.
  2. User’s default namespace.

To set up namespace preference using the command-line tool kubectl can be done by using the following command. This will be my-namespace as a default namespace.

kubectl config set-context --current --namespace=my-namespace

Benefits of Using Kubernetes Namespaces

The following are the benefits of using kubernetes Namespaces:

  1. Isolation of resources for different teams: Namespace will isolate the resources which are going to be used in the Kubernetes cluster. Namespace in Kubernetes is useful for security, performance, or organizational reasons. A namespace can be created for different teams who are going to work on the Kubernetes cluster such as developers teams, testing teams, and other teams.
  2. RBAC: Namespaces can increase the security of resources that are deployed by using role-based access control. For example, if the resources are deployed in the dev namespace by using RABAC we control the permissions to the developer team members in that dev namespace in the Kubernetes.
  3. Organization of resources: In a Kubernetes cluster it is very important to maintain the resources which are deployed in the cluster in an organized manner it can be done by using Kubernetes namespaces where you can track and manage the resource which is deployed.
  4. Increase Performance: Resources that are deployed in the Kubernetes cluster are isolated from each other which will help to reduce the burden on the resources like CPU and memory.

Kubernetes Namespaces – FAQs

What is a Namespace in Kubernetes?

Namespace is a virtual cluster inside the kubernetes cluster.

What is the Role of the Namespace?

Namespace will logically isolate the objects of kubernets cluster.

How many namespaces can we have in Kubernetes?

Kubernetes allows an arbitrary number of namespaces, limited only by the cluster’s capacity.

Can Kubernetes namespace talk to each other?

Yes, Kubernetes namespaces can communicate with each other unless network policies restrict this interaction.

How do you describe a pod in namespace?

By using the following command we can describe a pod in the namespace:

 kubectl describe pod <pod-name> -n <namespace-name>



Previous Article
Next Article

Similar Reads

How To Use Kubernetes Namespaces?
Because it is very easy for a team to accidentally overwrite or disrupt another service without even realizing it and Kubernetes Namespaces allows us to share the cluster with multiple teams or projects without impacting others' work. They are logically separated from each other and within a namespace, you can't create any two resources of the same
4 min read
Benefits and Characteristics of Kubernetes Namespaces
In Kubernetes, Namespaces are used to organize resources. You can have multiple Namespaces in a Cluster And these Namespaces are kind of virtual Clusters of their own. The official definition of Namespace says "In Kubernetes, namespaces provide a mechanism for isolating groups of resources within a single cluster". Within a Kubernetes Namespace, re
7 min read
Kubernetes - Monolithic Architecture of Kubernetes
There is a new way of developing software apps using a microservices architecture. That's when all the buzz around containers and container orchestration has increased but we have been developing and using these large software apps even before most of us were born. So in this article, we will be discussing what is that old software architecture we
7 min read
Kubernetes - Creating Deployment and Services using Helm in Kubernetes
Prerequisite: Kubernetes Helm is used for managing your Kubernetes Deployment. With helm, we can tear down and create a deployment with a single command. we will be creating deployment and services using Helm in Kubernetes. For simplicity, we will be using the nginx image. Deployment of nginx using HelmStep 1: We need to install the Helm. You can i
4 min read
Why Kubernetes? Benefits of using Kubernetes
The popularity of container orchestration technologies specially Kubernetes comes from its use cases and the problems that it solves. Kubernetes is the most popular container orchestration and is widely used by Cloud Native Computing Foundation (CNCF), the foundation to which Kubernetes as a project was donated by Google, estimates that about 92% b
8 min read
Kubernetes Controller VS Kubernetes Operator
Kubernetes Controllers are ideal for managing stateless apps and maintaining the correct number of copies, but Kubernetes Operators are more appropriate for complicated, stateful applications that require human-like decision-making abilities. Kubernetes ControllerKubernetes Controllers are ideal for managing stateless apps and maintaining the corre
4 min read
What is Docker Namespaces?
Namespaces have been part of the Linux kernel since around 2002, with more functionality and namespace types introduced over time. Real container functionality was added to the Linux kernel in 2013, however. This is what makes namespaces useful and popular. Namespaces enable you to create an isolated environment in which the container only knows wh
4 min read
How Raspberry Pi and Kubernetes Work Together?
Pre-requisite: Kubernetes and Raspberry Pi Kubernetes and Raspberry Pi are two popular technologies that can be used together to create powerful and scalable applications. In this article, we will look at what each technology is, how they work together, and the benefits and challenges of using the two technologies together. KubernetesKubernetes is
3 min read
Google Cloud Platform - Using Config Sync for Managing Kubernetes
In this article, we will look into how we can manage Kubernetes using Config Sync. To do so let's create a problem statement and resolve the same. Problem Statement: Ravi has a new role, Platform Administrator, and he is tasked with ensuring all the infrastructure created by all of his company's teams is in compliance with governance requirements.
3 min read
Kubernetes - Introduction to Container Orchestration
In this article, we will look into Container Orchestration in Kubernetes. But first, let's explore the trends that gave rise to containers, the need for container orchestration, and how that it has created the space for Kubernetes to rise to dominance and growth. The growth of technology into every aspect of our lives and days has created immense d
4 min read
Microsoft Azure - Starting & Stopping a Azure Kubernetes Service Cluster
In this article, we will learn how to stop and start Azure Kubernetes Service(AKS) clusters. You can stop your entire Azure Kubernetes Service cluster to save costs. To follow along, you will need an existing Azure Kubernetes service that is running. To use start and stop for AKS, we need the AKS preview extension for the Azure CLI. It can be insta
2 min read
Microsoft Azure - Introduction to Kubernetes Diagnostics
In this article, we will learn how to use Azure Kubernetes Service Diagnostics. A solution that runs inside a Kubernetes cluster can quickly become complex. Azure Kubernetes diagnostics can help you to investigate, diagnose and resolve issues in your cluster quickly. Here we have an AKS cluster running, and it is running a sample application. Let u
2 min read
Microsoft Azure - Availability Zones For Kubernetes Cluster
In this article, we will learn how to create a highly available Kubernetes cluster with availability zones. When you create an Azure Kubernetes service or AKS cluster in Azure, its nodes and storage all reside in the same datacenter. The cluster is protected from hardware failure and maintenance within the data center because the nodes and storage
2 min read
Kubernetes - Autoscaling
Pre-requisite: Kubernetes Life before Kubernetes is like writing our code and pushing the code into physical servers in a data center and managing the resources needed by that server to run our application smoothly and another type is deploying our code in virtual machines(VM). With VMs also have problems with hardware and software components requi
8 min read
Kubernetes - Physical Servers vs Virtual Machines vs Containers
Kubernetes is an open-source framework for managing containerized workloads and services that allows declarative configuration as well as automation. It has a huge and fast-expanding ecosystem. Services, support, and tools for Kubernetes are widely available. Kubernetes is a Greek word that means "helmsman" or "pilot." The acronym K8s comes from co
3 min read
Kubernetes - Taint and Toleration
A pod is a group of one or more containers and is the smallest deployable unit in Kubernetes. A node is a representation of a single machine in a cluster (we can simply view these machines as a set of CPU and RAM). A node can be a virtual machine, a physical machine in a data center hosted on a cloud provider like Azure. When a user runs the below-
6 min read
Microsoft Azure Arc Enabled Kubernetes
In this article we will learn how to get started with Azure Arc enabled Kubernetes in the Azure Portal. Azure Arc enables Kubernetes lets you connect to Azure those Kubernetes clusters which are running outside of Azure. So, these clusters could be running on-premises, on the edge, or on other clouds, and you can use Azure Arc to enable Kubernetes
3 min read
How to Enable JMX For Java Application Running in the Kubernetes Cluster?
Many times we want to monitor our application's CPU utilization, background thread behavior, and most importantly memory consumptions for tasks that deal with loads for data (500MB - 1GB) or much more data. Such monitoring helps to find which operation is causing heavy CPU or Memory utilization and helps to find the reason behind Memory leak issues
3 min read
Enable Remote Debugging For Java Application Deployed in Kubernetes Environment
During Development, developers have to debug their applications to resolve code problems. In order to debug a java application which is deployed on remote machine in a Kubernetes cluster, first developer has to do some steps to enable its application ready for debugging. Below are the manual steps to enable remote debugging for any java application
2 min read
Kubernetes - Working With Secrets
Kubernetes Secrets are objects that are used to store secret data in base64 encoded format. Using secrets enables developers not to put confidential information in the application code. Since Secrets are created independently of the pods, there is less risk of secrets being exposed. Uses of Secrets: As files in a volume mounted on one or more of it
1 min read
Kubernetes Resource Model (KRM) and How to Make Use of YAML?
Here we will explain how YAML can simplify system management and automation of most processes so that Kubernetes is a convenient working system. Basic Kubernetes Models: KRM and Everything-as-CodeAccording to Kubernetes co-founder Brian Grant, Kubernetes is very convenient thanks to the Kubernetes Resource Model (KRM) resource model. This is a way
6 min read
Kubernetes Policies
Pre-requisite: Kubernetes In this article, we will be discussing Kubernetes policies, a key feature in the Kubernetes platform that allows administrators to enforce rules and restrictions on the use and management of resources within a cluster. We will cover the basics of Kubernetes policies, including their types and how they are implemented and m
4 min read
Kubernetes - Create ConfigMap From YAML File
A ConfigMap is a dictionary consisting of non-confidential data. Its primary role is to keep the configuration separate from the container image. ConfigMap can be created in different ways. This article will cover the declarative approach to creating ConfigMap from the YAML file. Example: apiVersion: This specifies which version of Kubernetes API w
1 min read
Kubernetes - Create Config Map From Files
Pre-requisite: Kubernetes While creating a manifest file in Kubernetes, we can define environment variables. However, when you have a lot of manifest files, it will become difficult to manage the environment data stored in various manifest files. To overcome this issue, we can manage environment data centrally using ConfigMaps. ConfigMaps are used
2 min read
Kubernetes - Jobs
Pre-requisite: Kubernetes In the Kubernetes world, jobs are considered an object to act as a supervisor or controllers of a task. The Kubernetes job will create a pod, monitor the task, and recreate another one if that pod fails for some reason. Upon completion of the task, it will terminate the pod. Unlike deployment and pods, you can specify a jo
4 min read
Kubernetes - Creating Multiple Container in a Pod
Pre-requisite:- Kubernetes Kubernetes is a container management tool and it automates container deployment, load balancing, and container scaling. It is open-source and developed by Google in 2014 and written in Golang. All cloud providers adopt Kubernetes. It is scheduled runs and manages isolated containers that are running on virtual, physical,
3 min read
Kubernetes - Images
Pre-requisite:- Kubernetes A container image is used to represent binary data that is being used to encapsulate an application and all its software dependencies. Container images can be represented as executable software bundles that run standalone and make very defined assumptions about their runtime environment. Generally, we create a container i
3 min read
Kubernetes - Injecting ConfigMap as Files
Pre-requisite:- Kubernetes The automated deployment, scaling, and administration of software using a system called Kubernetes, an open-source container orchestration tool. K8s is another name for Kubernetes. Kubernetes was initially developed by Google and is now managed by the Cloud Native Computing Foundation. Despite the fact that it now support
3 min read
Kubernetes - Config Map From Directory
Pre-requisite:- Kubernetes Software deployment, scalability, and administration are mostly automated using Kubernetes, an open-source container orchestration framework. K8s is another name for Kubernetes. Kubernetes was initially developed by Google and is now managed by the Cloud Native Computing Foundation. Despite the fact that it now supports C
2 min read
Kubernetes - Services
Software deployment, scaling, and management are all automated using Kubernetes, an open-source container orchestration system. K8s is another name for Kubernetes. Kubernetes was initially developed by Google and is now managed by the Cloud Native Computing Foundation. Despite the fact that it now supports CRI-O in addition to the Docker runtime, w
3 min read
three90RightbarBannerImg