Skip to main content

Search the Dev Center

Search tutorials, templates, examples, and more.

Kubernetes F# Starter

A minimal Kubernetes F# Pulumi program

Before you begin#

For this walkthrough, you'll need:

Create a new project#

This starter creates an NGINX Deployment with one replica in your cluster and exports the Deployment’s name.

Terminal window
mkdir my-project; cd my-project
pulumi new kubernetes-fsharp

The pulumi new command walks you through creating and configuring the new project and stack. A stack is an instance of a project, and you can have many of them (dev, staging, prod), each with its own configuration settings. Depending on the language, you may also be asked for your preferred package manager or toolchain.

The template has no settings of its own.

Review the code#

When setup completes, you’ll see a few files:

  • Pulumi.yaml contains the project name and the language it’s written in.
  • Pulumi.dev.yaml contains the dev stack’s settings, including the values you just entered.
  • Program.fs contains the program code that declares your Kubernetes resources.

Take a moment to open them up and have a look. Here’s what’s in Program.fs:

Program.fs
module Program
open Pulumi.FSharp
open Pulumi.Kubernetes.Types.Inputs.Core.V1
open Pulumi.Kubernetes.Types.Inputs.Apps.V1
open Pulumi.Kubernetes.Types.Inputs.Meta.V1
let infra () =
let appLabels = inputMap ["app", input "nginx" ]
let containers : Pulumi.InputList<ContainerArgs> = inputList [
input (ContainerArgs(
Name = "nginx",
Image = "nginx",
Ports = inputList [ input(ContainerPortArgs(ContainerPortValue = 80)) ]
))
]
let podSpecs = PodSpecArgs(Containers = containers)
let deployment =
Pulumi.Kubernetes.Apps.V1.Deployment("nginx",
DeploymentArgs
(Spec = DeploymentSpecArgs
(Selector = LabelSelectorArgs(MatchLabels = appLabels),
Replicas = 1,
Template =
PodTemplateSpecArgs
(Metadata = ObjectMetaArgs(Labels = appLabels),
Spec = podSpecs))))
let name =
deployment.Metadata
|> Outputs.apply(fun metadata -> metadata.Name)
dict [("name", name :> obj)]
[<EntryPoint>]
let main _ =
Deployment.run infra

Deploy the project#

Deploy with pulumi up. It starts with a preview of what it’s about to create and asks before it changes anything. Choose yes to deploy.

pulumi up deploys to the cluster your current kubeconfig context points at, the same one kubectl uses.

Pulumi adds a random suffix to most resource names, so the Deployment the program calls nginx gets a name like nginx-3o5i0y8b.

This is autonaming. It lets you deploy the same program to more than one stack, and lets Pulumi create a replacement before it deletes the old resource, without names colliding.

Terminal window
pulumi up

When it finishes, Pulumi prints the stack’s outputs, values it only knows once the resources exist. This program exports the Deployment’s name as name. Read it any time with pulumi stack output name, or pass it to another command. This one shows the Deployment with its ready replica count, 1/1:

Terminal window
kubectl get deployment $(pulumi stack output name)
Need help?

If pulumi up reports that the cluster is unreachable, check which cluster kubectl points to and whether it’s running:

Terminal window
kubectl config current-context
kubectl cluster-info

Restart your local cluster if necessary, or switch to another cluster with kubectl config use-context <CONTEXT_NAME>.

Still stuck? Let us know.

Customize the project#

Make the number of copies configurable. In Program.fs, read replicas from the stack’s configuration instead of hard-coding it:

let infra () =
let replicas = Pulumi.Config().GetInt32("replicas").GetValueOrDefault(1)

Then use it for the Deployment’s replica count:

(Selector = LabelSelectorArgs(MatchLabels = appLabels),
Replicas = replicas,

Set replicas to 3. pulumi config set saves the value in Pulumi.dev.yaml, the dev stack’s settings file:

Terminal window
pulumi config set replicas 3

Deploy again. The preview shows the Deployment as an update:

Terminal window
pulumi up

Then check the Deployment again. It shows 3/3 ready:

Terminal window
kubectl get deployment $(pulumi stack output name)

Cleaning up#

Terminal window
pulumi destroy
pulumi stack rm

Related

The infrastructure platform for humans and agents.