Namespaces and Services in k8s

Namespaces and Services in k8s

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

Task - Create a namespace and add existing deployment into the namespace

Today, I will add a deployment file for the to-do app. Then create a namespace and add the namespace to the file as well as apply the update to the namespace.

Deployment File

apiVersion: apps/v1
kind: Deployment
metadata:
  name: django-todo-deployment
  labels:
    app: django
spec:
  replicas: 3
  selector:
    matchLabels:
      app: django
  template:
    metadata:
      labels:
        app: django
    spec:
      containers:
        - name: django-todo-app
          image: neelsoni26/django-todo-app:latest
          ports:
            - containerPort: 80

The above code is for the deployment.yml file.

yaml file

apply deployment

Create Namespace

creating namespace through the command:

kubectl create namespace django-todo-app

  • django-todo-app is the namespace name

create namespace

Update Deployment file

apiVersion: apps/v1
kind: Deployment
metadata:
  name: django-todo-deployment
  labels:
    app: django
  namespace: django-todo-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: django
  template:
    metadata:
      labels:
        app: django
    spec:
      containers:
        - name: django-todo-app
          image: neelsoni26/django-todo-app:latest
          ports:
            - containerPort: 80

The namespace was added to the metadata. Now apply and add deployment in the namespace.

apply deployment

Apply Changes

kubectl apply -f deployment.yml -n django-todo-app

apply change

Verify

verify

Task 2 - Services, Load Balancing, and Networking in Kubernetes

In Kubernetes, Services, Load Balancing, and Networking are key components that help manage and distribute network traffic among different Pods (the smallest deployable units of computing that can be created and managed in Kubernetes).

Services:

  • In Kubernetes, a Service is a method for exposing a network application that is running as one or more Pods in your cluster.

  • The Service API lets you expose an application running in Pods to be reachable from outside your cluster.

    • Ingress provides extra functionality specifically for exposing HTTP applications, websites and APIs.
  • Services can be exposed in different ways by specifying a type in the ServiceSpec, like ClusterIP, NodePort, LoadBalancer, and ExternalName.

Load Balancing

  • Load Balancing: Load balancing in Kubernetes is a process that helps to distribute network traffic across multiple Pods. This ensures that no single Pod gets overwhelmed with too much traffic.

  • Load balancing can be achieved through different types of Services. For example, a Service of the type LoadBalancer will create an external load balancer in the current cloud (if supported) and assign a fixed, external IP to the Service.

Networking

  • Networking in Kubernetes is a broad topic that covers several aspects. At a high level, every Pod gets its own IP address, so you do not need to explicitly create links between Pods and you almost never need to deal with mapping container ports to host ports.

  • Pods can communicate with all other Pods, regardless of which Node they land on. There's no need for NAT to get outbound connectivity. This can be achieved through different network plugins which ensure that the networking rules and configurations are correctly applied.

Conclusion

To sum up, We learned how to create namespace and commands to run it. We also looked at services, load balancing, and networking in Kubernetes.