1. Answers
  2. Creating a Kubernetes Secret with Pulumi

How do I create a Kubernetes Secret with Pulumi?

In this guide, we will create a Kubernetes Secret using Pulumi. Kubernetes Secrets are used to store and manage sensitive information such as passwords, OAuth tokens, and ssh keys. We will use Pulumi to define and deploy a Kubernetes Secret in a specified namespace.

Here’s the step-by-step process:

  1. Install Pulumi and Kubernetes Provider: Ensure you have Pulumi installed and the Kubernetes provider configured.
  2. Define the Kubernetes Secret: Use Pulumi to define the Secret resource with the required data.
  3. Deploy the Secret: Deploy the Secret to your Kubernetes cluster.

Below is the complete Pulumi program in TypeScript that accomplishes this:

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

// Define the namespace
const namespace = new k8s.core.v1.Namespace("my-namespace", {
    metadata: {
        name: "my-namespace",
    },
});

// Define the Secret
const mySecret = new k8s.core.v1.Secret("my-secret", {
    metadata: {
        namespace: namespace.metadata.name,
        name: "my-secret",
    },
    data: {
        // Base64 encode the secret data
        username: Buffer.from("admin").toString("base64"),
        password: Buffer.from("password123").toString("base64"),
    },
    type: "Opaque",
});

// Export the Secret name
export const secretName = mySecret.metadata.name;

Key Points

  • Namespace Definition: We define a Kubernetes namespace where the Secret will be created.
  • Secret Definition: We create a Kubernetes Secret with base64 encoded data for username and password.
  • Exporting the Secret Name: We export the name of the Secret for reference.

Summary

In this guide, we created a Kubernetes Secret using Pulumi. We defined a namespace and a Secret with sensitive data encoded in base64. This Secret can now be used in your Kubernetes applications to manage sensitive information securely.

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