Managing Namespaces in Kubernetes with Kustomize

In this article, we will cover Managing Namespaces in Kubernetes with Kustomize.

Namespaces are a fundamental feature in Kubernetes that allow you to organize and isolate resources within a cluster. When working with multiple environments or teams, managing namespaces efficiently becomes critical. Kustomize, a Kubernetes-native tool, simplifies the process of automating namespace management without altering the original YAML files. In this article, we will demonstrate how to manage namespaces using Kustomize, avoid common pitfalls like failed deployments, and follow best practices for efficient resource organization.

Prerequisites

Before starting, ensure you have the following:

  1. Kubernetes Cluster: A running Kubernetes cluster (e.g., Minikube, AWS EKS, GKE, AKS, or any provider).
  2. kubectl: The Kubernetes command-line tool should be installed and configured to access the cluster.
  3. Kustomize: Kustomize is included in kubectl by default (v1.14 and above).
  4. Basic Understanding of YAML and Kubernetes Resources:
    Familiarity with resource files like Deployments, Services, and Namespaces.

Why Use Namespaces?

Namespaces help:

  • Isolate Resources: Separate applications and environments (e.g., development, staging, production) within the same cluster.
  • Manage Access: Apply role-based access control (RBAC) for specific namespaces.
  • Organize Resources: Group related resources logically for clarity.

However, manually setting namespaces for every resource can be repetitive and error-prone. This is where Kustomize comes in.

Project Setup

To demonstrate namespace management, we’ll create a project with the following structure:

.
├── base
│   ├── deployment.yaml
│   ├── kustomization.yaml
│   └── service.yaml
└── overlays
    ├── kustomization.yaml
    └── namespace.yaml
  • The base directory contains the original resources.
  • The overlays directory customizes these resources by adding a namespace.

Step #1:Create Base Configuration

The base directory contains the core Kubernetes resources: a Deployment and a Service.

1.1 Deployment File (base/deployment.yaml)

This file defines a simple NGINX deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:1.21.6
          ports:
            - containerPort: 80

1.2 Service File (base/service.yaml)

This file exposes the NGINX deployment as a ClusterIP service:

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: ClusterIP

1.3 Kustomization File (base/kustomization.yaml)

The kustomization.yaml file lists the base resources:

resources:
  - deployment.yaml
  - service.yaml

Step #2:Add Namespace in Overlays

In the overlays directory, we will customize the base configuration by adding a namespace.

2.1 Namespace File (overlays/namespace.yaml)

This file defines the namespace my-namespace:

apiVersion: v1
kind: Namespace
metadata:
  name: my-namespace

2.2 Kustomization File (overlays/kustomization.yaml)

The overlay kustomization.yaml file customizes the base resources and assigns them to a namespace:

namespace: my-namespace

resources:
  - ../base
  - namespace.yaml
  • namespace: my-namespace ensures all base resources are assigned to this namespace.
  • The namespace.yaml file explicitly creates the namespace.

Step #3:Deploy Resources Using Kustomize

3.1 Preview the Configuration

Before deploying, preview the final YAML output using:

kubectl kustomize overlays/

You should see the namespace my-namespace added to all resources:

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
  namespace: my-namespace
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: nginx
  type: ClusterIP
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  namespace: my-namespace
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx:1.21.6
        name: nginx
        ports:
        - containerPort: 80

3.2 Deploy the Resources

  1. Apply the resources without creating the namespace:
kubectl apply -k overlays/

Expected Output:

Error from server (NotFound): namespaces "my-namespace" not found

This error occurs because the namespace does not yet exist.

  1. Create the Namespace:

Manually create the namespace:

kubectl create namespace my-namespace

3. Reapply the Kustomize Configuration:

kubectl apply -k overlays/

Expected Output:

deployment.apps/nginx-deployment created
service/nginx-service created
namespace/my-namespace created

3.3 Verify the Deployment

List the resources in the my-namespace namespace:

kubectl get all -n my-namespace

Expected Output:

NAME                    TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
service/nginx-service   ClusterIP   10.111.20.133   <none>        80/TCP    11s

NAME                               READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx-deployment   2/2     2            2           11s

NAME                                          DESIRED   CURRENT   READY   AGE
replicaset.apps/nginx-deployment-66446666fd   2         2         2       11s

To check the current namespace in Kubernetes, you can use the following command:

kubectl config view | grep namespace:

To change the namespace from default to my-namespace, you can use the following command:

kubectl config set-context --current --namespace=my-namespace

Key Takeaways

  1. Namespace Automation:
    • Use Kustomize’s namespace field to assign namespaces to multiple resources effortlessly.
  2. Explicit Namespace Creation:
    • Include a Namespace resource in your Kustomize configuration to avoid deployment failures.
  3. Clean and Reusable Configurations:
    • Separate base and overlay directories to maintain clean and reusable configurations.

Benefits of Using Kustomize for Namespaces

  • Automation: Reduces manual edits to YAML files.
  • Reusability: Base configurations remain untouched and reusable across environments.
  • Consistency: Ensures all resources are deployed in the correct namespace.

Conclusion:

Using Kustomize to manage namespaces simplifies Kubernetes resource deployments, reduces manual work, and adheres to best practices for clean, reusable configurations. By automating namespace assignment and explicitly creating namespaces, you can avoid common errors and ensure smooth deployments.

Related Articles:

How to Use Kustomize with Remote Git Repositories

Reference:

kustomize official page

Harish Reddy

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap