Kubernetes Namespace Tutorial

Kubernetes Namespace Tutorial

What is kubernetes

Kubernetes namespace will provide the feature of dividing the cluster into multiple virtual clusters. Let's say that you have 2 teams working in project like dev and prod. So using namespace we can divide the cluster into two virtual clusters like dev and prod and we can easily identify the resources of particular environment with namespace. Using namespace we can launch our resources like pod, deployment, services, replica-set in particular environment. and using namespace we can easily identify the resources of particular environment. in this tutorial i will explain about what is kubernetes namespace in detail.

List the kubernetes namaspaces in your cluster

using kubectl get command we can list the namespaces.

master $ kubectl get namespaces
NAME              STATUS   AGE
default           Active   40m
kube-node-lease   Active   40m
kube-public       Active   40m
kube-system       Active   40m

Create the kubernetes namespace

we can create namespace using below commands:

kubectl create namespace <namespace name>     

or

kubectl create ns  <namespace>

master $ kubectl create ns dev

namespace/dev created


master $ kubectl create namespace prod

namespace/prod created

Creat Namespace with Yml file

we can also create namespaces with manifest yml file

cat myns.yml

apiVersion: v1
kind: Namespace
metadata:
  name: qa
master $ kubectl apply -f myns.yml

namespace/qa created

Create the pods with namespace

cat mypod.yml

apiVersion: v1
kind: Pod
metadata:
  name: mypod
  namespace: dev
  labels:
     app: mypod
spec:
  containers:
  - name: mypod
    image: nginx

create the pod with kubectl apply -f mypod.yml command.

List the pods under particular namespace

To list the pods under particular namespace we can use below command.

kubectl  get pods  -n <namespace name>

master $ kubectl get pod -n dev

NAME    READY   STATUS    RESTARTS   AGE
mypod   1/1     Running   0          29s

Crete the service with namespace

apiVersion: v1
kind: Service
metadata:
  name: mynginxsvc
  namespace: dev
spec:
  type: NodePort
  ports:
    - port: 80
      nodePort: 30180
      name: http
    - port: 443
      nodePort: 31443
      name: https
  selector:
    myapp: mynginxapp

in the same way as we created the pod, we can create the service. Just add namespace: <namespcae name> in your service manifest yml file and create the service. Once you created the above service, you can see this service in 'dev' namespace.

List the service under particular service

master $ kubectl get service -n dev

NAME         TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)                      AGE
mynginxsvc   NodePort   10.99.159.254   <none>        80:30180/TCP,443:31443/TCP   6s

using kubectl get service -n <namespace> command we can list the services of particular environment.

in the same-way we can launch all the kubernetes components(pod, service, replicaset, deployments....)into particular namespace.

Delete the kubernetes namespace

you can delete a namespace with kubectl delete command.

kubectl delete namespaces <insert-some-namespace-name>

This deletes everything like pods/services/deployments..... under the namespace. so be-careful when you are deleting namespace.

Set the context for different user groups

# Bind dev context to dev namespace
$ kubectl config set-context dev --namespace=dev --cluster=kubernetes --user=dev
 
 
# Bind production context to production namespace
kubectl config set-context production --namespace=prod --cluster=kubernetes --user=production

This specific namespaces will act as default namespace for particular environment assigned to it.

 

  • what is kubernetes namespace
  • kubernetes namespace tutorial
  • what is kubernetes namespace

 

Leave a Reply

Your email address will not be published. Required fields are marked *