How do I set up a k3s cluster consisting of one server and two agents in Python?
To set up a k3s cluster with one server and two agents, you need to configure virtual machines (VMs) for each of the nodes: one server node and two agent nodes. We will use cloud resources to provision these VMs and then install k3s on them. The server node will initialize the Kubernetes cluster, and the agent nodes will join it.
Below is an example of how to define this setup using infrastructure as code:
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
const k3sServer = new aws.ec2.Instance("k3s_server", {
ami: "ami-12345678",
instanceType: aws.ec2.InstanceType.T2_Micro,
tags: {
Name: "k3s-server",
},
userData: `#!/bin/bash
curl -sfL https://get.k3s.io | sh -
`,
});
const k3sAgent1 = new aws.ec2.Instance("k3s_agent1", {
ami: "ami-12345678",
instanceType: aws.ec2.InstanceType.T2_Micro,
tags: {
Name: "k3s-agent1",
},
userData: pulumi.interpolate`#!/bin/bash
curl -sfL https://get.k3s.io | K3S_URL=https://${k3sServer.publicIp}:6443 K3S_TOKEN=${k3sServer.id} sh -
`,
});
const k3sAgent2 = new aws.ec2.Instance("k3s_agent2", {
ami: "ami-12345678",
instanceType: aws.ec2.InstanceType.T2_Micro,
tags: {
Name: "k3s-agent2",
},
userData: pulumi.interpolate`#!/bin/bash
curl -sfL https://get.k3s.io | K3S_URL=https://${k3sServer.publicIp}:6443 K3S_TOKEN=${k3sServer.id} sh -
`,
});
export const serverPublicIp = k3sServer.publicIp;
export const agent1PublicIp = k3sAgent1.publicIp;
export const agent2PublicIp = k3sAgent2.publicIp;
In this setup:
- We define a provider corresponding to the cloud where the infrastructure will be provisioned.
- We create an instance for the k3s server. It utilizes user data to automatically run a script that installs k3s and initializes the cluster.
- We create two instances for the k3s agents, each using user data to install k3s and join the server node using the server’s IP and a specified token.
- We export the public IP addresses of the server and agent instances for reference.
This example demonstrates how to set up a k3s cluster with one server and two agents programmatically. The server initializes the cluster, while the agents join the cluster using the information provided. This process is automated using user data to run installation scripts on VM startup.
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.