DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. The Why and How of Kubernetes Namespaces

The Why and How of Kubernetes Namespaces

This Kubernetes primer will get you situated with namespaces and how to create them for different environments, how to assign contexts to them, and how to delete them.

Ajitesh Kumar user avatar by
Ajitesh Kumar
CORE ·
Jan. 07, 18 · Tutorial
Like (14)
Save
Tweet
Share
32.98K Views

Join the DZone community and get the full member experience.

Join For Free

This article represents concepts related to Kubernetes Namespaces and how to create/delete namespaces. The following topics are discussed in this article:

  • What are namespaces?
  • How do you create namespaces?
  • How do you work with namespaces?
  • How do you delete namespaces?

What Are Namespaces?

Kubernetes namespaces can be seen as a logical entity used to represent cluster resources for usage of a particular set of users. This logical entity can also be termed as a virtual cluster. One physical cluster can be represented as a set of multiple such virtual clusters (namespaces). The namespace provides the scope for names. Names of resources within one namespace need to be unique.

By default, Kubernetes starts with the following three namespaces:

  • Default: Catch-all namespace for all objects not belonging to either of the kube-public or kube-system namespaces. The default namespace is used to hold the default set of pods, services, and deployments used by the cluster.
  • Kube-public: Namespace for resources that are publicly available/readable by all
  • Kube-system: Namespace for objects/resources created by Kubernetes systems

The following command can be used to get a list of all namespaces:

kubectl get namespaces


The following command displays namespaces with labels:

kubectl get namespaces --show-labels


The namespaces list can be accessed in the Kubernetes dashboard as shown in the image below:

kubernetes namespaces

Figure 1. Kubernetes Namespaces

In real-world scenarios, you can create a namespace for your development (dev), testing (QA), and production (prod) environments. The objects in the dev/QA namespaces such as pods, services, and deployments will be available for developers/testers respectively to build and run applications. There will be lesser restrictions on modifying the resources in the dev/QA namespaces. In the production (prod) namespace, there will be greater control on who can manage the resources.

In this article, we will look into the creation/deletion of namespaces for dev/QA/prod.

How to Create Namespaces

Let’s create namespaces for 0ur development/QA/prod environments. The following would be required to be done:

  • Create JSON files representing the namespaces
  • Execute the kubectl command for creating namespaces

Create JSON Files Representing Namespaces

Development namespace: Save the filename as namespace-dev.json:

{
    "kind": "Namespace",
    "apiVersion": "v1",
    "metadata": {
        "name": "dev",
        "labels": {
            "name": "dev"
        }
    }
}


QA Namespace: Save the filename as namespace-qa.json:

{
    "kind": "Namespace",
    "apiVersion": "v1",
    "metadata": {
        "name": "qa",
        "labels": {
            "name": "qa"
        }
    }
}


Production Namespace: Save the filename as namespace-prod.json:

{
    "kind": "Namespace",
    "apiVersion": "v1",
    "metadata": {
        "name": "prod",
        "labels": {
            "name": "prod"
        }
    }
}


Execute the kubectl Command for Creating Namespaces

The following commands will create the namespaces for Dev/QA/Prod environments:

# Namespace for Developers
kubectl create -f namespace-dev.json
# Namespace for Testers
kubectl create -f namespace-qa.json
# Namespace for Production
kubectl create -f namespace-prod.json


Check whether the namespaces got created by executing the following command:

kubectl get namespaces --show-labels | grep name=


The following image shows the output of the above command:

kubectl create namespace get namespace details

Figure 2. kubectl get namespace details

One can also check the details of newly created namespaces by accessing the dashboard:

kubernetes dashboard create namespace

Figure 3. Kubernetes dashboard displaying newly created namespaces

How to Work With Namespaces

Once the namespaces have been created, in order to have kubectl commands work with a specific namespace, the following needs to be done:

  • Assign a context to each namespace
  • Switch to the appropriate context for working with that namespace

Assign a Context to Each Namespace

# Assign dev context to development namespace
kubectl config set-context dev --namespace=dev --cluster=minikube --user=minikube
# Assign qa context to QA namespace
kubectl config set-context qa --namespace=qa --cluster=minikube --user=minikube
# Assign prod context to production namespace
kubectl config set-context prod --namespace=prod --cluster=minikube --user=minikube


Switch to the Appropriate Context

Execute the following commands to switch to the specific context. Once switched to a context, any execution of kubectl command would create/update/delete objects in that namespace.

# Switch to Dev context
kubectl config use-context dev
# Switch to QA context
kubectl config use-context qa
# Switch to Prod context
kubectl config use-context prod


Just to check what the current context is, execute the following command:

kubectl config current-context


Once switched to a context, commands such as the following would give object detail for that namespace:

kubectl get pods
kubectl get deployments


How to Delete Namespaces

The commands below would delete the namespaces:

# Delete dev namespace
kubectl delete namespaces dev
# Delete qa namespace
kubectl delete namespaces qa
# Delete prod namespace
kubectl delete namespaces prod


The above commands execute in asynchronous mode. Thus, the status of the namespace would show up as terminating until the deletion is fully completed.

Kubernetes Command (computing) cluster Object (computer science) Production (computer science) Dashboard (Mac OS) JSON

Published at DZone with permission of Ajitesh Kumar, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Introduction To OpenSSH
  • 10 Things to Know When Using SHACL With GraphDB
  • Beyond Coding: The 5 Must-Have Skills to Have If You Want to Become a Senior Programmer
  • 19 Most Common OpenSSL Commands for 2023

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: