Skip to main content

Command Palette

Search for a command to run...

Day 29 Task: Working with Namespaces and Services in Kubernetes

Published
3 min read
Day 29 Task: Working with Namespaces and Services in Kubernetes

What are Namespaces and Services in k8s

Kubernetes Namespaces 🏰

In Kubernetes, a Namespace is like a virtual cluster inside a physical cluster. It helps you create isolated environments within the same Kubernetes cluster. Imagine your Kubernetes cluster as a big castle 🏰, and within this castle, you can have different areas or rooms (Namespaces) for different purposes.

  • Default Namespace: When you deploy things in Kubernetes without specifying a Namespace, they usually go into the default Namespace. Think of it as the main hall in the castle.

  • Custom Namespaces: You can create custom Namespaces to organize your applications. For example, you might have a Namespace for your web applications, another for databases, and so on. Each Namespace is like a separate room in the castle where different activities are happening.

yamlCopy codeapiVersion: v1
kind: Namespace
metadata:
  name: my-web-apps

Kubernetes Services 🚀

A Service in Kubernetes is like a reliable messenger 🚀 that helps different parts of your application communicate with each other, even if they are in different rooms (Namespaces) of the castle.

  • ClusterIP Service: It's like an internal messenger within the castle. If one application wants to talk to another, they can use this ClusterIP Service to communicate.
yamlCopy codeapiVersion: v1
kind: Service
metadata:
  name: my-internal-service
  namespace: my-web-apps
spec:
  selector:
    app: my-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
  • NodePort Service: This is like a messenger that delivers messages to the castle's gates, making it accessible from outside. Imagine a drawbridge coming down to allow communication.
yamlCopy codeapiVersion: v1
kind: Service
metadata:
  name: my-external-service
  namespace: my-web-apps
spec:
  selector:
    app: my-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
  type: NodePort
  • LoadBalancer Service: It's like having a dedicated messenger just for you, making communication even more accessible and scalable.
yamlCopy codeapiVersion: v1
kind: Service
metadata:
  name: my-loadbalancer-service
  namespace: my-web-apps
spec:
  selector:
    app: my-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
  type: LoadBalancer

Certainly! Below is an example YAML configuration for creating a Namespace and a simple Deployment within that Namespace in Kubernetes:

yamlCopy code# File: my-namespace.yaml

apiVersion: v1
kind: Namespace
metadata:
  name: my-web-apps

Save the above YAML in a file, let's say my-namespace.yaml, and then apply it using the kubectl command:

bashCopy codekubectl apply -f my-namespace.yaml

This will create a Namespace named my-web-apps. Now, let's create a simple Deployment within that Namespace:

yamlCopy code# File: my-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app-deployment
  namespace: my-web-apps
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app-container
        image: nginx:latest
        ports:
        - containerPort: 80

Save the above YAML in a file, let's say my-deployment.yaml, and then apply it using the kubectl command:

bashCopy codekubectl apply -f my-deployment.yaml

This will create a Deployment named my-app-deployment with three replicas, running an Nginx container, within the my-web-apps Namespace.

More from this blog

Untitled Publication

57 posts