Kubernetes OVHcloud Java Starter
A Java program to deploy a Kubernetes cluster on OVHcloud
Before you begin#
For this walkthrough, you'll need:
- The Pulumi CLI
- An OVHcloud account with a Public Cloud project
- The kubectl CLI
Create a new project#
This starter creates a managed Kubernetes cluster with a node pool in your OVHcloud Public Cloud project and exports the cluster’s kubeconfig and the node pool’s ID.
mkdir my-project; cd my-projectpulumi new kubernetes-ovh-javamkdir my-project; cd my-projectpulumi new kubernetes-ovh-javaThe 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.
Then it asks for the template’s settings. Every one has a default except ovhServiceName, the ID of your Public Cloud project:
| Key | Description | Default |
|---|---|---|
| ovhServiceName | The OVHcloud Public Cloud Project to deploy into | — |
| ovhRegion | The OVHcloud region to deploy into | GRA9 |
| clusterName | Name of the cluster | my_desired_cluster |
| nodePoolName | Name of the node pool | my-desired-pool |
| nodePoolDesiredNodes | Number of desired nodes in the cluster | 1 |
| nodePoolMaxNodes | Number of max nodes in the cluster | 3 |
| nodePoolMinNodes | Number of min nodes in the cluster | 1 |
| flavorName | Name of the flavor/type of the machine | b3-8 |
pulumi config set
:
pulumi config set ovhServiceName <value>pulumi config set ovhRegion <value>pulumi config set clusterName <value>pulumi config set nodePoolName <value>pulumi config set nodePoolDesiredNodes <value>pulumi config set nodePoolMaxNodes <value>pulumi config set nodePoolMinNodes <value>pulumi config set flavorName <value>pulumi config set ovhServiceName "<value>"pulumi config set ovhRegion "<value>"pulumi config set clusterName "<value>"pulumi config set nodePoolName "<value>"pulumi config set nodePoolDesiredNodes "<value>"pulumi config set nodePoolMaxNodes "<value>"pulumi config set nodePoolMinNodes "<value>"pulumi config set flavorName "<value>"When setup completes, you’ll see a few files:
Pulumi.yamlcontains the project name and the language it’s written in.Pulumi.dev.yamlcontains thedevstack’s settings, including the values you just entered.App.javacontains the program code that declares your cloud resources.
Take a moment to open them up and have a look. Here’s what’s in App.java:
package myproject;
import com.ovhcloud.pulumi.ovh.CloudProject.Kube;import com.ovhcloud.pulumi.ovh.CloudProject.KubeArgs;import com.ovhcloud.pulumi.ovh.CloudProject.KubeNodePool;import com.ovhcloud.pulumi.ovh.CloudProject.KubeNodePoolArgs;import com.pulumi.Context;import com.pulumi.Pulumi;
public class App {
public static void main(String[] args) { Pulumi.run(App::stack); }
public static void stack(Context ctx) {
var config = ctx.config(); var ovhServiceName = config.require("ovhServiceName"); var ovhRegion = config.get("ovhRegion").orElse("GRA9"); var clusterName = config.get("clusterName").orElse("my_desired_cluster"); var nodePoolName = config.get("nodePoolName").orElse("my-desired-pool");
var nodePoolDesiredNodes = config.getInteger("nodePoolDesiredNodes").orElse(1); var nodePoolMaxNodes = config.getInteger("nodePoolMaxNodes").orElse(3); var nodePoolMinNodes = config.getInteger("nodePoolMinNodes").orElse(1); var flavorName = config.get("flavorName").orElse("b3-8");
// Deploy a new Kubernetes cluster var myKube = new Kube(clusterName, KubeArgs.builder() .region(ovhRegion) .serviceName(ovhServiceName) .name(clusterName) .build());
// Export kubeconfig file to a secret ctx.export("kubeconfig", myKube.kubeconfig());
//Create a Node Pool var nodePool = new KubeNodePool(nodePoolName, KubeNodePoolArgs.builder() .desiredNodes(nodePoolDesiredNodes) .flavorName(flavorName) .kubeId(myKube.id().asPlaintext()) .maxNodes(nodePoolMaxNodes) .minNodes(nodePoolMinNodes) .serviceName(ovhServiceName) .build());
ctx.export("nodePoolID", nodePool.id().asPlaintext()); }}Deploy the project#
The OVHcloud provider needs your OVHcloud API credentials. Set them up before you deploy, as the OVH provider’s setup guide describes.
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.
The cluster and its nodes take several minutes to come up.
Pulumi adds a random suffix to most resource names, which is called autonaming. It lets you deploy the same program to more than one stack without names colliding. This program sets the cluster’s name itself from the clusterName setting, so the cluster is called my_desired_cluster unless you entered another name.
pulumi upWhen it finishes, Pulumi prints the stack’s outputs, values it only knows once the resources exist. This program exports kubeconfig, the file kubectl needs to reach the new cluster, and nodePoolID. The kubeconfig is a secret, so Pulumi shows it as [secret] unless you ask for it with --show-secrets. Save it to a file and list the cluster’s nodes:
pulumi stack output kubeconfig --show-secrets > kubeconfig.ymlKUBECONFIG=./kubeconfig.yml kubectl get nodesCustomize the project#
The program reads the node pool’s size from nodePoolDesiredNodes. Raise it from 1 to 2:
pulumi config set nodePoolDesiredNodes 2Deploy again. The preview shows the node pool as an update:
pulumi upThen list the nodes again. A second node joins the pool:
KUBECONFIG=./kubeconfig.yml kubectl get nodesThe pool can grow to nodePoolMaxNodes, which defaults to 3.
To see what else you can add, browse the OVH provider in the Pulumi Registry.
Cleaning up#
You can destroy the stack and all of its infrastructure with pulumi destroy, then remove the stack with pulumi stack rm:
pulumi destroypulumi stack rm