Kubernetes Namespaces – One Cluster for Different Environments

13 / Dec / 2016 by Mayank Negi 0 comments

Kube

In one of the project that I was working, I had to set up multiple environments in the same Kubernetes cluster. The environments are namely QA and UAT. The blog will guide the reader on how to segregate different environments in same Kubernetes cluster and have control over each environment in such a way that the other environments are not impacted.

About Kubernetes
Kubernetes is an open-source tool that manages deployment, scaling, and orchestration of containerized applications. It groups containers that make up an application into logical units for easy management and discovery.

Read my previous blog to understand how to set up Kubernetes cluster, so I assuming that the reader has a running Kubernetes cluster and plans to implement multiple environments in it.

Before we get started, let’s first understand about a functionality which is provided by Kubernetes called namespaces. 

What namespace does?
Namespace sets up virtual clusters over the same physical cluster. Rather than the need to set up an entire physical cluster for a new environment. We can set up several different virtual cluster over the same physical cluster using namespaces.

kube

–  Helps in easy management of applications of each environment
–  Control multiple apps with same names in a single cluster
–  Cost-effective

How to create namespaces?
cmd: kubectl create namespace qa
cms: kubectl create namespace uat

The above two commands create 2 separate namespaces QA and UAT on the existing physical cluster. Once the creation is done, let’s understand how to use it

How do we deploy our applications on namespace?

To deploy applications into particular namespaces, we only need to add a namespace option in the existing application YAML. The configuration looks as follows:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
 name: nginx
 namespace: qa
spec:
 replicas: 1
 strategy:
 rollingUpdate:
 maxSurge: 1
 maxUnavailable: 0
 type: RollingUpdate
 template:
 metadata:
 labels:
 run: sample
 spec:
 containers:
 - name: nginx
 image: sample/sample:custom_nginx
 imagePullPolicy: Always
 ports:
 - containerPort: 80

The above YAML will create a Nginx pod in QA namespace.

The following configuration will create a Nginx pod in UAT namespace.

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
 name: nginx
 namespace: uat
spec:
 replicas: 1
 strategy:
 rollingUpdate:
 maxSurge: 1
 maxUnavailable: 0
 type: RollingUpdate
 template:
 metadata:
 labels:
 run: sample
 spec:
 containers:
 - name: nginx
 image: sample/sample:custom_nginx
 imagePullPolicy: Always
 ports:
 - containerPort: 80

The pods of each namespace can be listed by following commands

kubectl get pods –namespace=qa or
kubectl get pods –namespace=uat

All resources created in a particular namespace can be listed using the parameter “–namespace=” after any Kubernetes command.

The usage and implementation of a namespace is very simple but it solves an essential use case of segregating multiple environments without the need to launch multiple clusters.

This helps in reducing the infrastructure cost and efforts to manage applications on different environments.

The best example for namespaces lies in Kubernetes base setup itself. The pods and services that are essential for Kubernetes functioning, run in a separate  namespace named “kube-system“. This is how Kubernetes segregates its own system environment from the one that is used by the users. This ensures that no user activity can impact the resources running in Kubernetes system environment.

FOUND THIS USEFUL? SHARE IT

Leave a comment -