1. Answers
  2. Setting Up a k3s Cluster in Python

How Do I Set Up a K3s Cluster Consisting of One Server and Two Agents in Python?

Introduction

Setting up a k3s cluster with one server and two agents involves configuring virtual machines (VMs) for each node. This guide outlines the steps to provision these VMs using cloud resources and install k3s on them. The server node will initialize the Kubernetes cluster, and the agent nodes will join it. We’ll use infrastructure as code to automate this setup.

Step-by-Step Process

  1. Define Cloud Provider: Choose a cloud provider to host the infrastructure. In this example, we use AWS.

  2. Create k3s Server Instance:

    • Define an EC2 instance for the k3s server.
    • Use user data to run a script that installs k3s and initializes the cluster.
  3. Create k3s Agent Instances:

    • Define two EC2 instances for the k3s agents.
    • Use user data to install k3s and join the server node using the server’s IP and a specified token.
  4. Export Public IPs:

    • Export the public IP addresses of the server and agent instances for easy access and reference.

Below is an example code snippet demonstrating the setup:

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;

Key Points

  • Infrastructure as Code: Automates the setup process, ensuring consistency and repeatability.
  • User Data Scripts: Automatically install and configure k3s on VM startup.
  • Public IPs: Exported for easy access to the server and agents.

Conclusion

This guide provides a structured approach to setting up a k3s cluster with one server and two agents using infrastructure as code. By automating the process, you ensure a reliable and efficient deployment of your k3s cluster. This setup allows for easy scaling and management of Kubernetes applications.

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up