Skip to content

Commit

Permalink
Add example for Digital Ocean
Browse files Browse the repository at this point in the history
  • Loading branch information
xunleii committed Nov 11, 2023
1 parent 68245f6 commit 3791d3f
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 0 deletions.
14 changes: 14 additions & 0 deletions examples/do-k3s/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# K3S example for Digital Ocean

Configuration in this directory creates a k3s cluster resources instances.

## Usage

> [!warning]
> **Note that this example may create resources which cost money. Run `terraform destroy` when you don't need these resources.**
```bash
$ export DIGITALOCEAN_TOKEN=...
$ terraform init
$ terraform apply
```
32 changes: 32 additions & 0 deletions examples/do-k3s/k3s.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module "k3s" {
source = "./../.."

depends_on_ = digitalocean_droplet.node_instances
k3s_version = "latest"
cluster_domain = "do_k3s"

drain_timeout = "60s"
managed_fields = ["label"]
generate_ca_certificates = true

global_flags = [for instance in digitalocean_droplet.node_instances : "--tls-san ${instance.ipv4_address}"]

servers = {
# The node name will be automatically provided by
# the module using the field name... any usage of
# --node-name in additional_flags will be ignored

for instance in digitalocean_droplet.node_instances :
instance.name => {
ip = instance.ipv4_address_private
connection = {
timeout = "60s"
type = "ssh"
host = instance.ipv4_address
private_key = trimspace(tls_private_key.ed25519_provisioning.private_key_pem)
}

labels = { "node.kubernetes.io/type" = "master" }
}
}
}
22 changes: 22 additions & 0 deletions examples/do-k3s/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
data "digitalocean_image" "ubuntu" {
slug = "ubuntu-22-04-x64"
}

resource "tls_private_key" "ed25519_provisioning" {
algorithm = "ED25519"
}

resource "digitalocean_ssh_key" "default" {
name = "K3S terraform module - Provisionning SSH key"
public_key = trimspace(tls_private_key.ed25519_provisioning.public_key_openssh)
}

resource "digitalocean_droplet" "node_instances" {
count = 3

image = data.digitalocean_image.ubuntu.slug
name = "k3s-node-${count.index}"
region = "ams3"
size = "s-1vcpu-2gb"
ssh_keys = [digitalocean_ssh_key.default.fingerprint]
}
14 changes: 14 additions & 0 deletions examples/do-k3s/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
output "summary" {
value = module.k3s.summary
}

output "kubeconfig" {
value = module.k3s.kube_config
sensitive = true
}

output "ssh_private_key" {
description = "Generated SSH private key."
value = tls_private_key.ed25519_provisioning.private_key_openssh
sensitive = true
}
9 changes: 9 additions & 0 deletions examples/do-k3s/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
terraform {
required_providers {
digitalocean = {
source = "digitalocean/digitalocean"
version = "2.31.0"
}
}
required_version = "~> 1.0"
}

0 comments on commit 3791d3f

Please sign in to comment.