Day 33: Introduction to Kubernetes Namespaces and Services for Beginners

Day 33: Introduction to Kubernetes Namespaces and Services for Beginners

Congrats🎊🎉 on updating your Deployment yesterday💥

What are Namespaces in K8s?

Namespaces are a way to create a virtual cluster within the same physical cluster. They are a logical partitioning mechanism that allows you to divide a Kubernetes cluster into multiple smaller clusters, each with its resources and objects.

Key Points of Namespaces

  • Namespaces provide a level of isolation between resources within different namespaces.

  • Namespaces are used for resource management.

  • Namespaces are used to scope Kubernetes objects.

  • Kubernetes provides a namespace called "default" where resources are created if no namespace is specified.

  • You can create custom namespaces to organize your resources based on your application's needs.

What are Services in K8s?

Services are a fundamental concept which enables network communication between different parts of your application running in containers. Services help in load balancing, service discovery and ensuring that your application components can communicate with each other reliably.

Key Aspects of Services

  • Services enable service discovery within a cluster.

  • When multiple pods are part of the service, Kubernetes automatically load balances the incoming network traffic across those pods.

  • ClusterIP, Loadbalancer, NodePort, and ExternalName are the types of services.

  • Services use label selectors to determine which pods are part of services.

  • You can create a headless service that does not allocate ClusterIP.

  • Services maintain a list of endpoints (IP addresses of pods) that are part of the service.

Load Balancing

Load balancing is a distribution of network traffic across multiple pods (container instances) that make up an application or service. Kubernetes provides load-balancing capabilities to ensure that incoming requests are evenly distributed among the available pods, which helps in improving the availability, scalability, and fault tolerance of applications.

Networking in Kubernetes

Networking in Kubernetes is a complex topic due to the distributed nature of containerized applications running in clusters. Kubernetes provides a rich set of networking features to facilitate communication between pods, services, and external resources while ensuring security, isolation, and scalability.

Pod Networking: Pods are the smallest deployable units in Kubernetes, and each pod has its IP address.

Service Networking: Kubernetes introduces the concept of services to provide stable network endpoints for accessing pods.

Cluster Networking: Kubernetes clusters often span multiple physical or virtual machines (nodes). To enable communication between pods on different nodes, a cluster network is established.

Ingress Controllers: Ingress controllers enable the management of HTTP and HTTPS traffic entering the cluster.


Task 01

Create a Namespace for your Deployment

Update the deployment.yml file to include the Namespace

Apply the updated deployment using the command

Verify that the Namespace has been created by checking the status of the Namespaces in your cluster.

Steps:

  • To create the namespace for the deployment file, use the following command:

      kubectl create namespace my-app
    

    This will create a namespace with the name "my-app" to organize the pods in Kubernetes. You can choose any name you want to write.

  • To update the deployment.yml file, go to the deployment.yml file and write the content:

      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: todo-deployment
        namespace: my-app
        labels:
          app: todo-app
      spec:
        replicas: 3
        selector:
          matchLabels:
            app: todo-app
        template:
          metadata:
            namespace: my-app
            labels:
              app: todo-app
         spec:
           containers:
             - name: todo-app
               image: shahzaibsana/todo-app
               ports:
                 - containersPort: 3000
    

    We mentioned the namespace inside the deployment file to make sure whenever you need to see the status of our todo-app deployment file, we should only call it by the given namespace. So there will be no mess up with a lot of pods.

  • Apply the updated deployment to the cluster, by writing the following command

      kubectl apply -f deployment.yml -n my-app
    

    -n is a short form of calling the namespace.

  • Check the status of the namespace created before whether active or not, by giving the following command

      kubectl get namespaces
    

    It will show you the list of created namespaces along with their status.


<That's all for today. Hope you like it. FOLLOW to join me in the journey of DevOps>