1. Answers
  2. Creating a Kubernetes Namespace with Pulumi

How do I create a Kubernetes namespace?

In this example, we’ll create a Kubernetes namespace. Namespaces in Kubernetes allow you to segment your infrastructure and manage it in a more organized way. This is particularly useful when you want to divide cluster resources among multiple users or teams.

We’ll define our Kubernetes provider first and then create the namespace resource.

import * as pulumi from "@pulumi/pulumi";
import * as kubernetes from "@pulumi/kubernetes";

const example = new kubernetes.core.v1.Namespace("example", {metadata: {
    name: "example-namespace",
}});
export const namespaceName = example.metadata.apply(metadata => metadata.name);

Here’s what the program does:

  • provider “kubernetes”: This block configures the Kubernetes provider to connect to your cluster. You specify the config_context_cluster and config_path to point to your Kubernetes configuration file.
  • resource “kubernetes_namespace” “example”: This defines a namespace resource in Kubernetes. The metadata block sets the name of the namespace to “example-namespace”.
  • output “namespace_name”: This outputs the name of the namespace created, allowing you to verify that the namespace has been successfully created.

This program sets up a Kubernetes namespace and outputs its name when the infrastructure as code is applied. This is a fundamental step in organizing and managing resources within a Kubernetes cluster.

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up