Skip to main content
This article provides a comprehensive lab walkthrough on namespaces in Kubernetes, addressing questions related to namespaces, pods, and services. Follow the steps below to learn how to manage namespaces effectively.

Identifying the Number of Namespaces

To determine the number of namespaces in your cluster, you can run either of the following commands:
or its shorthand:
The sample output below shows a list of namespaces:
And using the shorthand command:
In this example, there are a total of 10 namespaces.

Counting Pods in the Research Namespace

To determine the number of pods running in the research namespace, execute the following command:
The output might look like this:
Alternatively, you can use the shorthand option:
Both commands confirm that there are 2 pods in the research namespace.

Creating a Pod in the Finance Namespace

Before creating a pod in the finance namespace, ensure that the namespace exists by running:
You should see output similar to:
Next, create a Redis pod named “redis” in the finance namespace with the following command:
After successfully executing this command, you will see a confirmation message:
You can verify the pod creation by listing the pods in the finance namespace:
Sample output:

Locating the Blue Pod’s Namespace

To find out which namespace the blue pod is running in, you have two options:
  1. Manually inspect each namespace using:
  2. List all pods across every namespace with:
The sample output below illustrates pods across namespaces:
From the output, it is clear that the blue pod is located in the marketing namespace.

Accessing the Database Service from the Blue Application

The blue application, a simple web application for connectivity tests, interacts with a database service. Depending on where the DB service is running, the access method varies.

Accessing the DB Service in the Same Namespace (Marketing)

In the marketing namespace, the blue application and its associated DB service coexist. To check the pods in this namespace, run:
Expected output:
Next, list the services in the marketing namespace:
This yields an output similar to:
Since both the blue application and the DB service are in the same namespace, the application can reach the DB service simply by using its DNS name, which is:   db-service and connecting via port 6379.

Accessing the DB Service in the dev Namespace

Another DB service is running in the dev namespace. To view it, execute:
The output might be as follows:
To access the DB service in the dev namespace from the blue application located in the marketing namespace, you must use the fully qualified DNS name. The DNS naming convention is:   <service-name>.<namespace>.svc.cluster.local In this case, the DNS name is:   db-service.dev.svc.cluster.local Using this fully qualified domain name ensures traffic is correctly directed to the DB service in the dev namespace on port 6379.
When performing connectivity tests from the blue application, the console output should verify that the connection to the DB service is successfully established.
The image shows a "Connectivity Test" interface with a host name and port, indicating a successful connection.

This concludes the lab solution for namespaces. Each step demonstrates how to manage and interact with namespaces, pods, and services in a Kubernetes cluster. For more details on Kubernetes concepts, visit the Kubernetes Documentation.

Watch Video