Skip to main content

Understand Kubernetes Objects – Namespace

This is the second part of the series on building highly scalable multi-container apps using AKS. In the earlier post  we provisioned a managed Kubernetes cluster using Azure Container Service (AKS). I will reuse the code which was prepared for my talk during the Global Azure Bootcamp event in Singapore on 21st April 2018. This post we will focus on creating one of the most basic resource in Kubernetes. Lets get started with some basic understanding of the Kubernetes objects.

We will be performing the following actions during this post

  • Create a namespace using kubectl
  • Create a namespace using Kubernetes manifest file
  • Delete the namespace using kubectl
  • Find details about the namespace using kubectl

Kubernetes Objects

Kubernetes or K8s as it is commonly called has support for different types of objects. It provides different ways to create basic objects like Namespace, Pod, Volume, Service, etc. There are higher level API’s which make use of the basic objects. These include things like Deployment, ReplicaSet, StatefulSet, DemonSet etc. To start with, we will look at the most basic objects and in the later part of the series revisit other objects as and when the need arises.

Lets start with the simplest of all basic objects Namespaces.

Namespaces

Namespaces is simple concept which is quite powerful in nature. It allows us to define logical cluster within a physical Kubernetes cluster. Imagine that a Kubernetes cluster is shared by two or more teams. They have the need to isolate each others services within Kubernetes environment. Namespace can be used for this purpose. Another example of using namespace could be to make logical separation of the same Kubernetes cluster for different environments like Dev, QA etc.

Create namespace using kubectl

Lets see how we can create namespace using Kubernetes CLI, kubectl. Fire up the terminal of your choice and execute the following command

kubectl create namespace abc2018sg

Successful completion of the command would create a namespace named abc2018sg. I found this as the simplest way to create a namespace. This is good if we want to do it once. In todays modern day systems which are built using Continuous Integration (CI) and deployed using Continuous Deployment (CD) techniques, we use approaches like Infrastructure as Code. In such cases we can make use of Kubernetes manifest file to declaratively describe the object we wish to create using a yaml or json file.

Create namespace using Kubernetes manifest

Here is an example of the same namespace described using a yaml file. The file is named 00_ABCNamespace.yml. It uses the declarative approach to describe the kind of object we wish to create.

Kubernetes supports multiple versions of the API. It is very important to specify which version if the API we intend to use while creating a resource. In our case we are using the stable version v1. The name of the object is specified using the metadata property. Such files which describe the different attributes of Kubernetes resources are called as manifest file. It can be used to create any Kubernetes resource using the command line. Similar to the process of using a script file with the SQL command line tool like sqlcmd, we can pass the file to the kubectl with the command

kubectl create –-filename 00_ABCNamespace.yml 

from the directory where the file is located on the disk. The result of this command is exactly the same as the previous one.

kubectl is a smart tool. We can use recursive nature to create multiple resources by just specifying the top level folder. All the yaml files will be executed as part of the create or delete or any other action performed using the kubectl command. We will see example of this later in the series.

Delete namespace using kubectl

Deleting a namespace using kubectl is as simple as creating a namespace. Run the following command in the terminal

kubectl delete namespace abc2018sg

Find details about namespace using kubectl & dashboard

kubectl provides a helpful command named describe which can be used to get details about different objects. We can use it to describe the namespace as follows

kubectl describe namespace abc2018sg

We get the output as shown below.

describe namespace

We can also get the details about a namespace using the Kubernetes dashboard by selecting the abc2018sg namespace from the list of namespaces.

Kubernetes Namespace list

We can see multiple namespaces in the screenshot above for the Kubernetes cluster. By default Kubernetes creates 3 namespaces named default, kube-public and kube-system. Kube-public and kube-system are used internally by Kubernetes cluster. If we do not specify any namespace while creating a resource, it will be created under the default namespace.

Conclusion

In this post we saw basic Kubernetes object namespace. As the series progresses, we will discover more objects based on their relevance. Lets conclude this post with a feeling of getting started with Kubernetes using both the Kubernetes CLI as well as the manifest files. There is a wonderful cheatsheet available for Kubernetes. I personally found this quite handy when getting started with Kubernetes. In the next post we will look at another building blocks in Kubernetes the Pod and Deployment. We will be able to put the namespace that we created today to good use in the future parts of this series. Most of the resources we will create, will be part of the namespace abc2018sg.

Until next time code with passion and strive for excellence.

Comments

Popular Posts

Unit Test Application Configuration Settings

BackgroundAll the post I had written this month so far were related to the Entity Framework learning series. This time I thought of taking a diversion and share a quick method of unit testing setting which might be stored in the Application Configuration file (App.config) of a DotNet application. Unit Test Application Configuration SettingsLets fire up Visual Studio and create a simple console application. You can name it whatever you want. I have named the project as AppConfigTest. Once the project is created right click on Add and select Add New Item option in the solution explorer window. Select Application Configuration File option and click Add. This will automatically name the new file as App.config.Since this file used to configure settings at application level there is a possibility of someone changing these setting unknowingly. It can lead to severe problems later. It might also happen that the settings are different in different environments like development machines, test s…

Integrate SQL Server 2017 Linux with ASP.NET Core using Docker

In the previous post, we saw how to initialize a SQL Server 2017 on Linux database running inside Docker container. This post is continuation of my exploration about Docker containers for stateful applications. In this post we will explore how to integrate the SQ Server 2017 on Linux container with ASP.Net Core Web API also running inside a Docker container. We will perform following activitiesUpdate Docker compose file to build the SQL server imageUpdate Web API project to use SQL Server Database instead of In memory databaseBuild and run the application containers using Docker compose commandsQuery Data from SQL container using the MVC web front endUpdate Docker compose file to build the SQL server imageDocker provides Docker Compose as a tool for defining and running multi-container Docker applications. I already have an existing docker compose file which I have been using for my series of post on implementing continuous deployment of multi container apps. In a compose file we defi…

Setup MacBook almost at the speed of light

BackgroundI bought a new MacBook recently. It is always fascinating to setup your new machine. But it is also a pain to look for all the tools that you have on your old machine and port it to the new machine. Sometime back I started learning abount Ansible which helps to automate routine tasks. I came across a blog by Jeff Geerling who is the author of book Ansible for DevOps. Jeff and many others had used Ansible to setup their machines. I took inspiration from these guys blogs to automate the process of setting up a new MacBook Pro. Here is my experience.Why Ansible?Ansible is very easy to understand. It uses human readbale YAML syntax to describe the different actions which needs to be performed. Group of Ansible actions which are executed as part of a playbook are idempotent. It does not have a side effect on the setup. The same playbook can be run multiple times. Only the changes will be applied incrementally.
How did I use Ansible?I started off by cloning the Git repository of Ge…