Working with Namespaces and Services in Kubernetes

Working with Namespaces and Services in Kubernetes

Welcome back, to my DevOps blog series!

In my previous blog post, we discussed the essential steps for Launching your Kubernetes Cluster with Deployment. If you missed it, I highly recommend you go through it as it lays the foundation for what we're about to delve into here.

You can check my previous blog here: Launching your Kubernetes Cluster with Deployment

In today's blog, we will explore an integral aspect of Kubernetes - Namespaces and Services. These are fundamental concepts that every DevOps Engineer should be well-versed in.

What are Namespaces in Kubernetes?

Kubernetes namespace is a way to create virtual compartments within a single physical cluster. These compartments act like personalized spaces for different users, teams, or projects. By organizing resources into these compartments, called namespaces, it becomes simpler to handle and separate various tasks. This segregation guarantees that different groups can share the same cluster without their work clashing with one another.

Why Use Namespaces?

  • Resource Isolation: In Kubernetes, namespaces play a crucial role in preventing resources such as Pods, Deployments, ConfigMaps, and Secrets from conflicting with one another. This separation becomes vital, especially when different teams or applications are using the same Kubernetes cluster simultaneously.

  • Access Control: Namespaces provide a means to regulate access to resources. You can establish RBAC (Role-Based Access Control) policies, which restrict who has the authority to manage resources within a specific Namespace.

  • Resource Quotas: Each Namespace can have specific resource quotas allocated to it. This feature ensures that one application cannot monopolize all the available CPU, memory, or storage resources, promoting fair and efficient resource usage.

Task 01

Create a Namespace for your Deployment.

  • Use the command to create a namespace in your EC2 instance.

      kubectl create namespace <namespace-name>
    
  • Get the list of namespaces using the command.

      kubectl get namespaces
    

  • Modify the deployment.yml file to incorporate the Namespace.

  • Apply the updated deployment using the command.

      kubectl apply -f deployment.yml -n <namespace-name>
    

    The namespace in which the resources should be generated or changed is specified using the -n flag.

Verify the creation of the namespace by inspecting the status of namespaces in your cluster.

Now you have a namespace ready to be used for organizing your resources.

Task 02

Read about Services, Load Balancing, and Networking in Kubernetes.

When a pod in a deployment fails, another pod is automatically launched to maintain the desired replica count. However, the new pod will have a different IP address compared to the terminated one. If someone attempts to access the pod using its previous IP address, it won't be successful due to the IP change. This is where Kubernetes services come into play.

In Kubernetes, a Service acts as a way to make a network application, running in one or more Pods within your cluster, accessible. Once a service is created, it allows access to your application through labels and selectors, eliminating the need to use specific IP addresses. Instead of relying on IP addresses, services use labels and selectors to identify pods, ensuring consistent access to the application.

Services: Services act as a layer of abstraction for pods, enabling communication between them without requiring knowledge of specific IP addresses. By grouping pods using labels, services facilitate dynamic scaling and failover. Kubernetes offers various types of services to cater to different networking needs.

  • ClusterIP (default): This type of service exposes the Service on an internal IP within the cluster. It restricts accessibility solely to entities within the cluster, ensuring that only those with permission to access the Kubernetes cluster can reach this Service.

  • NodePort: This service type exposes the Service on the same port of each selected Node in the cluster using NAT. It enables access to the Service from outside the cluster using the format <NodeIP>:<NodePort> . It extends the functionality of ClusterIP and allows accessibility within your organization.

  • LoadBalancer: This service type creates an external load balancer in the current cloud environment (if supported) and assigns a fixed, external IP to the Service. It offers a broader scope than NodePort, allowing external access to the Service.

Load Balancing: Kubernetes Services come with built-in load balancing capabilities. When a service consists of multiple pods, incoming traffic is automatically divided among these pods. This guarantees an equitable distribution of requests, enhances fault tolerance, and supports scalability.

Networking: It stands as a foundational element facilitating communication among containers (pods), exposing services externally, and regulating network traffic within the cluster. This aspect is vital in preserving the functionality, scalability, and security of containerized applications.

If you need help with Namespaces? Feel free to watch this video for guidance.

Conclusion

Understanding the concept of namespaces in Kubernetes is essential for managing clusters smoothly and organizing resources effectively. By applying the techniques shared in this blog and incorporating them with the insights from our earlier post on setting up your Kubernetes Cluster with Deployment, you're on a solid path to becoming proficient in Kubernetes.

If you wish to delve deeper into Kubernetes, DevOps, or related topics, don't hesitate to reach out to me on LinkedIn. Let's keep the conversation going and navigate the expansive realm of Kubernetes together!