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 Kubernetes 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 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 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.
We can also get the details about a namespace using the Kubernetes dashboard by selecting the abc2018sg namespace from the list of namespaces.
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.
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.
Originally published at www.handsonarchitect.com.