Day 33 of 90 Days of DevOps Challenge: Working with Namespaces and Services in Kubernetes

Welcome to Day 33 of the 90 Days of DevOps challenge! 🎉 After setting up your Kubernetes deployment yesterday, today we’re diving into Namespaces and Services in Kubernetes. These are essential concepts for organizing and managing your Kubernetes resources effectively.

Let’s get started! 🚀


What are Namespaces in Kubernetes?

Namespaces in Kubernetes are used to create isolated environments for different resources, like Pods, Services, and Deployments. Think of each namespace as a separate cluster within the same Kubernetes environment. This helps you avoid conflicts when you have multiple teams or applications running in the same cluster.

Namespaces are particularly useful when:

  • You want to organize resources by environment (e.g., dev, staging, prod).

  • You need to limit the resources available to specific teams or projects.

  • You want to enforce access control and resource quotas across different teams.


What are Services in Kubernetes?

Services in Kubernetes are responsible for exposing your Pods and Deployments to the network, allowing them to communicate with each other or external users. Services ensure that Pods are discoverable and accessible, even as the underlying Pods scale up or down.

Kubernetes offers several types of services:

  • ClusterIP: Exposes the service internally within the cluster.

  • NodePort: Exposes the service on a static port on each node in the cluster.

  • LoadBalancer: Exposes the service externally using a cloud provider’s load balancer.

Services abstract away the complexity of how Pods are created and destroyed, allowing you to reliably connect to them even when their IP addresses change.


Task 1: Create a Namespace for Your Deployment

Today’s task involves creating a Namespace for your Kubernetes Deployment. This will help isolate your deployment from other resources in the cluster.

Step 1: Create a Namespace

To create a namespace, use the following command:

kubectl create namespace my-namespace

Replace my-namespace with the desired name for your namespace.

Step 2: Update the Deployment YAML File

Next, update your deployment.yml file to include the namespace you just created. You’ll add the namespace field in the metadata section like this:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: todo-app-deployment
  namespace: my-namespace
spec:
  replicas: 3
  selector:
    matchLabels:
      app: todo-app
  template:
    metadata:
      labels:
        app: todo-app
    spec:
      containers:
      - name: todo-app
        image: trainwithshubham/todo-app:latest
        ports:
        - containerPort: 5000
        resources:
          limits:
            cpu: "500m"
            memory: "256Mi"
          requests:
            cpu: "250m"
            memory: "128Mi"

Make sure the namespace matches the one you created in Step 1.

Step 3: Apply the Updated Deployment

Now, apply the updated deployment YAML file using the following command:

kubectl apply -f deployment.yaml -n my-namespace

This will create the deployment within the specified namespace.

Step 4: Verify the Namespace

You can verify that the namespace was created successfully by listing all namespaces in your cluster:

kubectl get namespaces

You should see your new namespace listed along with other default Kubernetes namespaces like default, kube-system, and kube-public.


Task 2: Learn About Services, Load Balancing, and Networking in Kubernetes

Understanding Services, Load Balancing, and Networking is crucial for managing communication between Pods and external users.

1. Services

Services provide a stable endpoint for Pods. They ensure that even as Pods scale up or down or get replaced, traffic is still routed correctly.

Here’s a quick example of how a Service is defined in Kubernetes:

apiVersion: v1
kind: Service
metadata:
  name: todo-app-service
  namespace: my-namespace
spec:
  selector:
    app: todo-app
  ports:
    - protocol: TCP
      port: 5000
      targetPort: 5000
  type: LoadBalancer
  • ClusterIP: Exposes the service only within the cluster.

  • NodePort: Exposes the service on a static port on each node’s IP.

  • LoadBalancer: Exposes the service to the internet via a cloud provider's load balancer.

2. Load Balancing

Load balancing ensures that traffic to your service is distributed evenly across multiple Pods, improving reliability and performance. Kubernetes handles load balancing internally through the Service object, specifically when you use the LoadBalancer or NodePort service types.

3. Networking

Kubernetes networking is designed to allow every Pod to communicate with every other Pod within the cluster, regardless of where they are running. Each Pod gets its own IP address, and Kubernetes provides network isolation between different namespaces.

For external communication, Ingress controllers can be used to route traffic into your cluster based on domain names or paths.


Conclusion

Today, you learned about Namespaces and Services in Kubernetes, two essential components for organizing and exposing your applications. By creating a namespace, you can isolate your deployment, and by understanding services, you ensure reliable communication between your application’s Pods and the outside world.

As you continue to explore Kubernetes, these concepts will become fundamental building blocks for managing production-scale environments.

Don’t forget to share your progress on LinkedIn using the #90DaysOfDevOps tag! 🚀