Day 33 Task: Working with Namespaces and Services in Kubernetes - 90DaysOfDevOps

Day 33 Task: Working with Namespaces and Services in Kubernetes - 90DaysOfDevOps

What are Namespaces and Services in k8s

In Kubernetes, Namespaces are used to create isolated environments for resources. Each Namespace is like a separate cluster within the same physical cluster. Services are used to expose your Pods and Deployments to the network. Read more about Namespace Here

Today's Task:

Task 1:

  • Create a Namespace for your Deployment

  • Use the command kubectl create namespace <namespace-name> to create a Namespace

ubuntu@ip-172-31-60-234:~$ kubectl create namespace todo
namespace/todo created
  • Update the deployment.yml file to include the Namespace
apiVersion: apps/v1
kind: Deployment
metadata:
    name: todo-app
    namespace: todo
    labels:
      app: todo
spec:
  replicas: 4
  selector:
    matchLabels:
      app: todo
  template:
    metadata:
      labels:
        app: todo
    spec:
      containers:
        - name: todo
          image: amana6420/node-todo-app
          ports:
          - containerPort: 3000
  • Apply the updated deployment using the command: kubectl apply -f deployment.yml -n <namespace-name>
ubuntu@ip-172-31-60-234:~$ kubectl apply -f deployment.yml -n todo
deployment.apps/todo-app created
  • Verify that the Namespace has been created by checking the status of the Namespaces in your cluster.

  • Commands to check -

kubectl get ns - list all namespaces

kubectl get all -n <ns name> - To list all resources of ns

Below are the outputs -

ubuntu@ip-172-31-60-234:~$ kubectl get ns
NAME              STATUS   AGE
default           Active   3d16h
kube-node-lease   Active   3d16h
kube-public       Active   3d16h
kube-system       Active   3d16h
todo              Active   2m48s
ubuntu@ip-172-31-60-234:~$ kubectl get all -n todo
NAME                            READY   STATUS    RESTARTS   AGE
pod/todo-app-5d54b856cf-mx857   1/1     Running   0          75s
pod/todo-app-5d54b856cf-nsj4t   1/1     Running   0          75s
pod/todo-app-5d54b856cf-psfkc   1/1     Running   0          75s
pod/todo-app-5d54b856cf-xswrh   1/1     Running   0          75s

NAME                       READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/todo-app   4/4     4            4           75s

NAME                                  DESIRED   CURRENT   READY   AGE
replicaset.apps/todo-app-5d54b856cf   4         4         4       75s

Task 2:

  • Read about Services, Load Balancing, and Networking in Kubernetes. Refer to the official documentation of Kubernetes - Link.

Thank you :)