Day 33 : Working with Namespaces and Services in Kubernetes

Sahil KaushalSahil Kaushal
2 min read

Namespaces

Namespaces in Kubernetes are a way to divide cluster resources between multiple users or teams. They provide a mechanism for isolating groups of resources within a single cluster. This is particularly useful in environments with many users spread across multiple teams or projects.

Key Points:

  • Isolation: Namespaces allow you to create isolated environments within the same cluster. Resources in one namespace are not visible to another namespace unless explicitly allowed.

  • Resource Quotas: You can set resource quotas on namespaces to limit the amount of resources (CPU, memory, etc.) that can be consumed.

  • Unique Names: Resource names need to be unique within a namespace but not across namespaces.

Example:

Imagine you have a Kubernetes cluster shared by two teams: Development and Production. You can create two namespaces, development and production, to isolate their resources.

apiVersion: v1
kind: Namespace
metadata:
  name: development
---
apiVersion: v1
kind: Namespace
metadata:
  name: production

You can then deploy resources into these namespaces:

kubectl create -f namespace-development.yaml
kubectl create -f namespace-production.yaml

To deploy a pod in the development namespace:

apiVersion: v1
kind: Pod
metadata:
  name: dev-pod
  namespace: development
spec:
  containers:
  - name: nginx
    image: nginx

Services

Services in Kubernetes are an abstraction that defines a logical set of Pods and a policy by which to access them. Services enable communication between different parts of your application and can expose your application to external traffic.

Key Points:

  • Stable IP Address: Services provide a stable IP address and DNS name for a set of Pods, which can change over time.

  • Load Balancing: Services can load balance traffic across multiple Pods.

  • Types of Services: There are different types of services, such as ClusterIP, NodePort, LoadBalancer, and ExternalName.

Example:

Let’s create a Service to expose a set of Pods running an Nginx server.

  1. Pod Definition:
apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  labels:
    app: nginx
spec:
  containers:
  - name: nginx
    image: nginx
  1. Service Definition:
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
  type: ClusterIP

Deploy the Pod and Service:

kubectl apply -f nginx-pod.yaml
kubectl apply -f nginx-service.yaml

The nginx-service will now route traffic to any Pod with the label app: nginx on port 80.

Summary

  • Namespaces help in organizing and isolating resources within a Kubernetes cluster, making it easier to manage large clusters with multiple teams.

  • Services provide a stable way to access a set of Pods, enabling communication within the cluster and exposing applications to external traffic.

Thank you for reading😉.

1
Subscribe to my newsletter

Read articles from Sahil Kaushal directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sahil Kaushal
Sahil Kaushal