Pulumi is a Modern Infrastructure as Code (IaC) to create, deploy, and manage infrastructure on any cloud using familiar programming languages and tools.
Pulumi’s Cloud Native SDK makes it easy to target any Kubernetes environment to provision a cluster, configure and deploy applications, and update them as required.
This article is about how to use Pulumi and Go SDK to manager Namespace and Deployment within Kubernetes (K8S).
By default, Pulumi will look for a kubeconfig file in the following locations, just like kubectl:
The environment variable: $KUBECONFIG,
Or in current user’s default kubeconfig directory: ~/.kube/config
If the kubeconfig file is not in either of these locations, Pulumi will not find it, and it will fail to authenticate against the cluster. Set one of these locations to a valid kubeconfig file, if you have not done so already.
Run Pulumi
Create the workspace directory.
1 2 3
$ mkdir -p cloudolife-pulumi-go-alicloud-k8s $ cd cloudolife-pulumi-go-alicloud-k8s
Pulumi login into local file system.
1 2 3
$ pulumi login file://. Logged in to cloudolife as cloudolife (file://.) or visit https://pulumi.com/docs/reference/install/ for manual instructions and release notes.
$ pulumi new kubernetes-go This command will walk you through creating a new Pulumi project.
Enter a value or leave blank to accept the (default), and press <ENTER>. Press ^C at any time to quit.
project name: (cloudolife-pulumi-go-alicloud-k8s) project description: (A minimal Kubernetes Go Pulumi program) Created project 'cloudolife-pulumi-go-alicloud-k8s'
stack name: (dev) Created stack 'dev' Enter your passphrase to protect config/secrets: Re-enter your passphrase to confirm:
Enter your passphrase to unlock config/secrets (set PULUMI_CONFIG_PASSPHRASE or PULUMI_CONFIG_PASSPHRASE_FILE to remember): Installing dependencies...
Finished installing dependencies
Your new project is ready to go! ✨
To perform an initial deployment, run 'pulumi up'
The above command will create some files within the current directory.
$ pulumi up Enter your passphrase to unlock config/secrets (set PULUMI_CONFIG_PASSPHRASE or PULUMI_CONFIG_PASSPHRASE_FILE to remember): Previewing update (dev): Type Name Plan pulumi:pulumi:Stack cloudolife-pulumi-go-alicloud-k8s-dev + ├─ kubernetes:core/v1:Namespace pulumi-go-alicloud-k8s create + └─ kubernetes:apps/v1:Deployment app-dep create Outputs: + name: "app-dep-rx8h9grh"
Do you want to perform this update? yes Updating (dev): Type Name Status pulumi:pulumi:Stack cloudolife-pulumi-go-alicloud-k8s-dev + ├─ kubernetes:core/v1:Namespace pulumi-go-alicloud-k8s created + └─ kubernetes:apps/v1:Deployment app-dep created Outputs: + name: "app-dep-rx8h9grh"
kubectl get namespace NAME STATUS AGE pulumi-go-alicloud-k8s Active 3m29s
See pods about pulumi-go-alicloud-k8s.
1 2 3
$ kubectl get pods -n pulumi-go-alicloud-k8s NAMESPACE NAME READY STATUS RESTARTS AGE pulumi-go-alicloud-k8s app-dep-rx8h9grh-f89759699-2hn27 1/1 Running 0 14s
$ pulumi destroy Enter your passphrase to unlock config/secrets (set PULUMI_CONFIG_PASSPHRASE or PULUMI_CONFIG_PASSPHRASE_FILE to remember): Previewing destroy (dev): Type Name Plan - pulumi:pulumi:Stack cloudolife-pulumi-go-alicloud-k8s-dev delete - ├─ kubernetes:core/v1:Namespace pulumi-go-alicloud-k8s delete - └─ kubernetes:apps/v1:Deployment app-dep delete Outputs: - name: "app-dep-rx8h9grh"
Resources: - 3 to delete
Permalink: file:///Users/cloudolife/WorkSpaces/cloudolife/cloudolife-devops/cloudolife-pulumi/cloudolife-pulumi-go-alicloud-k8s/.pulumi/stacks/dev.jsonn Do you want to perform this destroy? details - kubernetes:core/v1:Namespace: (delete) [id=pulumi-go-alicloud-k8s] [urn=urn:pulumi:dev::cloudolife-pulumi-go-alicloud-k8s::kubernetes:core/v1:Namespace::pulumi-go-alicloud-k8s] - kubernetes:apps/v1:Deployment: (delete) [id=pulumi-go-alicloud-k8s/app-dep-rx8h9grh] [urn=urn:pulumi:dev::cloudolife-pulumi-go-alicloud-k8s::kubernetes:apps/v1:Deployment::app-dep] - pulumi:pulumi:Stack: (delete) [urn=urn:pulumi:dev::cloudolife-pulumi-go-alicloud-k8s::pulumi:pulumi:Stack::cloudolife-pulumi-go-alicloud-k8s-dev] --outputs:-- - name: "app-dep-rx8h9grh"
Do you want to perform this destroy? yes Destroying (dev): Type Name Status - pulumi:pulumi:Stack cloudolife-pulumi-go-alicloud-k8s-dev deleted - ├─ kubernetes:core/v1:Namespace pulumi-go-alicloud-k8s deleted - └─ kubernetes:apps/v1:Deployment app-dep deleted Outputs: - name: "app-dep-rx8h9grh"
Resources: - 3 deleted
Duration: 30s
Permalink: file:///Users/cloudolife/WorkSpaces/cloudolife/cloudolife-devops/cloudolife-pulumi/cloudolife-pulumi-go-alicloud-k8s/.pulumi/stacks/dev.jsonn The resources in the stack have been deleted, but the history and configuration associated with the stack are still maintained. If you want to remove the stack completely, run 'pulumi stack rm dev'.