Namespaces

What is a namespace

Its a kubernetes object which partition a single k8 cluster into multiple virtual clusters. By default all the resource created in kubernetes cluster are created in the default namespace.

A pod running in the default namespace will run with unbounded CPU and memory requests/limits. It allows to partition created resources into a logically named group. Each namespace provides following things.

  • A unique scope for resources.
  • Policies to run resouces.
  • Ability to specify resource consumptoin policies.
  • Names of resources are unique with in namespace

By default kubernetes will have three namespace

  • How to list namespace
$ kubectl get ns
NAME          STATUS   AGE
default       Active   39h
kube-public   Active   39h
kube-system   Active   39h
  • default : All Pods that we manually create will go to this namespace (There are ways to change it , but for now that is what it is).

  • kube-public : All common workloads can be assigned to this namespace . Most of the time no-one use it.

  • kube-system : Kubernetes specific Pods will be running on this namespace

  • How to list all the pods in a specific namespace?

$ kubectl get pods --namespace=NAMESPACE_NAME
  • How to list all resources in a namespace?
$ kubectl get all -n kube-system
  • How to create a new namespace called mydemonamespace?
$ kubectl create ns mydemonamespace
$ kubectl get ns 
  • How do i create a namespace in declarative way?

Create a yaml file with below content.

apiVersion: v1
kind: Namespace
metadata:
  name: mydemonamespace-new
$ kubectl create -f yourfilename.yaml 

Now check you see the nmaespace you just created.

$ kubectl get ns 
  • How do I add a pod to this namespace?

Lets create a nginx pod with below content.

apiVersion: v1
kind: Pod
metadata:
  name: webserver
  labels:
     app: nginx
spec:
  containers:
  - name: webserver
    image: nginx:1.9.1
    ports:
    - containerPort: 80
  • Lets create the pod in the new namespace and check the namespace to see the pod is running.
$ kubectl create -f mywebserver.yaml -n mydemonamespace-new

$ kubectl get pods -n mydemonamespace-new
  • How to delete the namespace?
$ kubectl delete ns mydemonamespace-new

TASK:

I. Create a multi container pod with name my-multi-pod-demo with below image

  • nginx
  • mysql

II. Create a namespace with $YOURNAME-namespace in a declarative way. Create a pod with a name webserver using nginx image inside this namespace you just created.