Skip to content

Commit ca3c049

Browse files
authored
chore: k8s example persistence & coder images (#3619)
* add: persistence & coder images * add: code-server * chore: README updates * chore: README example
1 parent 123fe01 commit ca3c049

File tree

3 files changed

+155
-101
lines changed

3 files changed

+155
-101
lines changed

examples/templates/kubernetes-multi-service/main.tf

Lines changed: 0 additions & 101 deletions
This file was deleted.

examples/templates/kubernetes-multi-service/README.md renamed to examples/templates/kubernetes-pod/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,40 @@ roleRef:
7272

7373
Then start the Coder host with `serviceAccountName: coder` in the pod spec.
7474

75+
## Namespace
76+
77+
The target namespace in which the pod will be deployed is defined via the `coder_workspace`
78+
variable. The namespace must exist prior to creating workspaces.
79+
80+
## Persistence
81+
82+
The `/home/coder` directory in this example is persisted via the attached PersistentVolumeClaim.
83+
Any data saved outside of this directory will be wiped when the workspace stops.
84+
85+
Since most binary installations and environment configurations live outside of
86+
the `/home` directory, we suggest including these in the `startup_script` argument
87+
of the `coder_agent` resource block, which will run each time the workspace starts up.
88+
89+
For example, when installing the `aws` CLI, the install script will place the
90+
`aws` binary in `/usr/local/bin/aws`. To ensure the `aws` CLI is persisted across
91+
workspace starts/stops, include the following code in the `coder_agent` resource
92+
block of your workspace template:
93+
94+
```terraform
95+
resource "coder_agent" "main" {
96+
startup_script = <<EOT
97+
#!/bin/bash
98+
99+
# install AWS CLI
100+
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
101+
unzip awscliv2.zip
102+
sudo ./aws/install
103+
EOT
104+
}
105+
```
106+
107+
## code-server
108+
109+
`code-server` is installed via the `startup_script` argument in the `coder_agent`
110+
resource block. The `coder_app` resource is defined to access `code-server` through
111+
the dashboard UI over `localhost:13337`.
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
terraform {
2+
required_providers {
3+
coder = {
4+
source = "coder/coder"
5+
version = "0.4.9"
6+
}
7+
kubernetes = {
8+
source = "hashicorp/kubernetes"
9+
version = "~> 2.12.1"
10+
}
11+
}
12+
}
13+
14+
variable "use_kubeconfig" {
15+
type = bool
16+
sensitive = true
17+
description = <<-EOF
18+
Use host kubeconfig? (true/false)
19+
20+
Set this to false if the Coder host is itself running as a Pod on the same
21+
Kubernetes cluster as you are deploying workspaces to.
22+
23+
Set this to true if the Coder host is running outside the Kubernetes cluster
24+
for workspaces. A valid "~/.kube/config" must be present on the Coder host.
25+
EOF
26+
}
27+
28+
variable "coder_namespace" {
29+
type = string
30+
sensitive = true
31+
description = "The namespace to create workspaces in (must exist prior to creating workspaces)"
32+
default = "coder-namespace"
33+
}
34+
35+
variable "disk_size" {
36+
type = number
37+
description = "Disk size (__ GB)"
38+
default = 10
39+
}
40+
41+
provider "kubernetes" {
42+
# Authenticate via ~/.kube/config or a Coder-specific ServiceAccount, depending on admin preferences
43+
config_path = var.use_kubeconfig == true ? "~/.kube/config" : null
44+
}
45+
46+
data "coder_workspace" "me" {}
47+
48+
resource "coder_agent" "main" {
49+
os = "linux"
50+
arch = "amd64"
51+
startup_script = <<EOT
52+
#!/bin/bash
53+
54+
# install and start code-server
55+
curl -fsSL https://code-server.dev/install.sh | sh | tee code-server-install.log
56+
code-server --auth none --port 13337 | tee code-server-install.log &
57+
EOT
58+
}
59+
60+
# code-server
61+
resource "coder_app" "code-server" {
62+
agent_id = coder_agent.main.id
63+
name = "code-server"
64+
icon = "/icon/code.svg"
65+
url = "http://localhost:13337?folder=/home/coder"
66+
relative_path = true
67+
}
68+
69+
resource "kubernetes_pod" "main" {
70+
count = data.coder_workspace.me.start_count
71+
metadata {
72+
name = "coder-${data.coder_workspace.me.owner}-${data.coder_workspace.me.name}"
73+
namespace = var.coder_namespace
74+
}
75+
spec {
76+
security_context {
77+
run_as_user = "1000"
78+
fs_group = "1000"
79+
}
80+
container {
81+
name = "dev"
82+
image = "codercom/enterprise-base:ubuntu"
83+
command = ["sh", "-c", coder_agent.main.init_script]
84+
security_context {
85+
run_as_user = "1000"
86+
}
87+
env {
88+
name = "CODER_AGENT_TOKEN"
89+
value = coder_agent.main.token
90+
}
91+
volume_mount {
92+
mount_path = "/home/coder"
93+
name = "home-directory"
94+
}
95+
}
96+
volume {
97+
name = "home-directory"
98+
persistent_volume_claim {
99+
claim_name = kubernetes_persistent_volume_claim.home-directory.metadata.0.name
100+
}
101+
}
102+
}
103+
}
104+
105+
resource "kubernetes_persistent_volume_claim" "home-directory" {
106+
metadata {
107+
name = "home-coder-java-${data.coder_workspace.me.owner}-${data.coder_workspace.me.name}"
108+
namespace = var.coder_namespace
109+
}
110+
spec {
111+
access_modes = ["ReadWriteOnce"]
112+
resources {
113+
requests = {
114+
storage = "${var.disk_size}Gi"
115+
}
116+
}
117+
}
118+
}

0 commit comments

Comments
 (0)