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:
- Install Pulumi and Kubernetes Provider: Ensure you have Pulumi installed and the Kubernetes provider configured.
- Define the Kubernetes Secret: Use Pulumi to define the Secret resource with the required data.
- 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
andpassword
. - 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 upNew to Pulumi?
Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.
Sign upThank you for your feedback!
If you have a question about how to use Pulumi, reach out in Community Slack.
Open an issue on GitHub to report a problem or suggest an improvement.