|
| 1 | +# Additional clusters |
| 2 | + |
| 3 | +With Coder, you can deploy workspaces in additional Kubernetes clusters using |
| 4 | +different |
| 5 | +[authentication methods](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs#authentication) |
| 6 | +in the Terraform provider. |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +## Option 1) Kubernetes contexts and kubeconfig |
| 11 | + |
| 12 | +First, create a kubeconfig file with |
| 13 | +[multiple contexts](https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/). |
| 14 | + |
| 15 | +```shell |
| 16 | +kubectl config get-contexts |
| 17 | + |
| 18 | +CURRENT NAME CLUSTER |
| 19 | + workspaces-europe-west2-c workspaces-europe-west2-c |
| 20 | +* workspaces-us-central1-a workspaces-us-central1-a |
| 21 | +``` |
| 22 | + |
| 23 | +### Kubernetes control plane |
| 24 | + |
| 25 | +If you deployed Coder on Kubernetes, you can attach a kubeconfig as a secret. |
| 26 | + |
| 27 | +This assumes Coder is deployed on the `coder` namespace and your kubeconfig file |
| 28 | +is in ~/.kube/config. |
| 29 | + |
| 30 | +```shell |
| 31 | +kubectl create secret generic kubeconfig-secret -n coder --from-file=~/.kube/config |
| 32 | +``` |
| 33 | + |
| 34 | +Modify your helm values to mount the secret: |
| 35 | + |
| 36 | +```yaml |
| 37 | +coder: |
| 38 | + # ... |
| 39 | + volumes: |
| 40 | + - name: "kubeconfig-mount" |
| 41 | + secret: |
| 42 | + secretName: "kubeconfig-secret" |
| 43 | + volumeMounts: |
| 44 | + - name: "kubeconfig-mount" |
| 45 | + mountPath: "/mnt/secrets/kube" |
| 46 | + readOnly: true |
| 47 | +``` |
| 48 | +
|
| 49 | +[Upgrade Coder](../../install/kubernetes.md#upgrading-coder-via-helm) with these |
| 50 | +new values. |
| 51 | +
|
| 52 | +### VM control plane |
| 53 | +
|
| 54 | +If you deployed Coder on a VM, copy the kubeconfig file to |
| 55 | +`/home/coder/.kube/config`. |
| 56 | + |
| 57 | +### Create a Coder template |
| 58 | + |
| 59 | +You can start from our |
| 60 | +[example template](https://github.com/coder/coder/tree/main/examples/templates/kubernetes). |
| 61 | +From there, add [template parameters](../../templates/concepts/parameters.md) to allow |
| 62 | +developers to pick their desired cluster. |
| 63 | + |
| 64 | +```hcl |
| 65 | +# main.tf |
| 66 | +
|
| 67 | +data "coder_parameter" "kube_context" { |
| 68 | + name = "kube_context" |
| 69 | + display_name = "Cluster" |
| 70 | + default = "workspaces-us-central1-a" |
| 71 | + mutable = false |
| 72 | + option { |
| 73 | + name = "US Central" |
| 74 | + icon = "/emojis/1f33d.png" |
| 75 | + value = "workspaces-us-central1-a" |
| 76 | + } |
| 77 | + option { |
| 78 | + name = "Europe West" |
| 79 | + icon = "/emojis/1f482.png" |
| 80 | + value = "workspaces-europe-west2-c" |
| 81 | + } |
| 82 | +} |
| 83 | +
|
| 84 | +provider "kubernetes" { |
| 85 | + config_path = "~/.kube/config" # or /mnt/secrets/kube/config for Kubernetes |
| 86 | + config_context = data.coder_parameter.kube_context.value |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +## Option 2) Kubernetes ServiceAccounts |
| 91 | + |
| 92 | +Alternatively, you can authenticate with remote clusters with ServiceAccount |
| 93 | +tokens. Coder can store these secrets on your behalf with |
| 94 | +[managed Terraform variables](../templates/concepts/variables.md). |
| 95 | + |
| 96 | +Alternatively, these could also be fetched from Kubernetes secrets or even |
| 97 | +[Hashicorp Vault](https://registry.terraform.io/providers/hashicorp/vault/latest/docs/data-sources/generic_secret). |
| 98 | + |
| 99 | +This guide assumes you have a `coder-workspaces` namespace on your remote |
| 100 | +cluster. Change the namespace accordingly. |
| 101 | + |
| 102 | +### Create a ServiceAccount |
| 103 | + |
| 104 | +Run this command against your remote cluster to create a ServiceAccount, Role, |
| 105 | +RoleBinding, and token: |
| 106 | + |
| 107 | +```shell |
| 108 | +kubectl apply -n coder-workspaces -f - <<EOF |
| 109 | +apiVersion: v1 |
| 110 | +kind: ServiceAccount |
| 111 | +metadata: |
| 112 | + name: coder-v2 |
| 113 | +--- |
| 114 | +apiVersion: v1 |
| 115 | +kind: Secret |
| 116 | +metadata: |
| 117 | + name: coder-v2 |
| 118 | + annotations: |
| 119 | + kubernetes.io/service-account.name: coder-v2 |
| 120 | +type: kubernetes.io/service-account-token |
| 121 | +--- |
| 122 | +apiVersion: rbac.authorization.k8s.io/v1 |
| 123 | +kind: Role |
| 124 | +metadata: |
| 125 | + name: coder-v2 |
| 126 | +rules: |
| 127 | + - apiGroups: ["", "apps", "networking.k8s.io"] |
| 128 | + resources: ["persistentvolumeclaims", "pods", "deployments", "services", "secrets", "pods/exec","pods/log", "events", "networkpolicies", "serviceaccounts"] |
| 129 | + verbs: ["create", "get", "list", "watch", "update", "patch", "delete", "deletecollection"] |
| 130 | + - apiGroups: ["metrics.k8s.io", "storage.k8s.io"] |
| 131 | + resources: ["pods", "storageclasses"] |
| 132 | + verbs: ["get", "list", "watch"] |
| 133 | +--- |
| 134 | +apiVersion: rbac.authorization.k8s.io/v1 |
| 135 | +kind: RoleBinding |
| 136 | +metadata: |
| 137 | + name: coder-v2 |
| 138 | +subjects: |
| 139 | + - kind: ServiceAccount |
| 140 | + name: coder-v2 |
| 141 | +roleRef: |
| 142 | + kind: Role |
| 143 | + name: coder-v2 |
| 144 | + apiGroup: rbac.authorization.k8s.io |
| 145 | +EOF |
| 146 | +``` |
| 147 | + |
| 148 | +The output should be similar to: |
| 149 | + |
| 150 | +```text |
| 151 | +serviceaccount/coder-v2 created |
| 152 | +secret/coder-v2 created |
| 153 | +role.rbac.authorization.k8s.io/coder-v2 created |
| 154 | +rolebinding.rbac.authorization.k8s.io/coder-v2 created |
| 155 | +``` |
| 156 | + |
| 157 | +### 2. Modify the Kubernetes template |
| 158 | + |
| 159 | +You can start from our |
| 160 | +[example template](https://github.com/coder/coder/tree/main/examples/templates/kubernetes). |
| 161 | + |
| 162 | +```hcl |
| 163 | +variable "host" { |
| 164 | + description = "Cluster host address" |
| 165 | + sensitive = true |
| 166 | +} |
| 167 | +
|
| 168 | +variable "cluster_ca_certificate" { |
| 169 | + description = "Cluster CA certificate (base64 encoded)" |
| 170 | + sensitive = true |
| 171 | +} |
| 172 | +
|
| 173 | +variable "token" { |
| 174 | + description = "Cluster CA token (base64 encoded)" |
| 175 | + sensitive = true |
| 176 | +} |
| 177 | +
|
| 178 | +variable "namespace" { |
| 179 | + description = "Namespace" |
| 180 | +} |
| 181 | +
|
| 182 | +provider "kubernetes" { |
| 183 | + host = var.host |
| 184 | + cluster_ca_certificate = base64decode(var.cluster_ca_certificate) |
| 185 | + token = base64decode(var.token) |
| 186 | +} |
| 187 | +``` |
| 188 | + |
| 189 | +### Create Coder template with managed variables |
| 190 | + |
| 191 | +Fetch the values from the secret and pass them to Coder. This should work on |
| 192 | +macOS and Linux. |
| 193 | + |
| 194 | +To get the cluster address: |
| 195 | + |
| 196 | +```shell |
| 197 | +kubectl cluster-info |
| 198 | +Kubernetes control plane is running at https://example.domain:6443 |
| 199 | +
|
| 200 | +export CLUSTER_ADDRESS=https://example.domain:6443 |
| 201 | +``` |
| 202 | + |
| 203 | +To fetch the CA certificate and token: |
| 204 | + |
| 205 | +```shell |
| 206 | +export CLUSTER_CA_CERTIFICATE=$(kubectl get secrets coder-v2 -n coder-workspaces -o jsonpath="{.data.ca\.crt}") |
| 207 | +
|
| 208 | +export CLUSTER_SERVICEACCOUNT_TOKEN=$(kubectl get secrets coder-v2 -n coder-workspaces -o jsonpath="{.data.token}") |
| 209 | +``` |
| 210 | + |
| 211 | +Create the template with these values: |
| 212 | + |
| 213 | +```shell |
| 214 | +coder templates push \ |
| 215 | + --variable host=$CLUSTER_ADDRESS \ |
| 216 | + --variable cluster_ca_certificate=$CLUSTER_CA_CERTIFICATE \ |
| 217 | + --variable token=$CLUSTER_SERVICEACCOUNT_TOKEN \ |
| 218 | + --variable namespace=coder-workspaces |
| 219 | +``` |
| 220 | + |
| 221 | +If you're on a Windows machine (or if one of the commands fail), try grabbing |
| 222 | +the values manually: |
| 223 | + |
| 224 | +```shell |
| 225 | +# Get cluster API address |
| 226 | +kubectl cluster-info |
| 227 | +
|
| 228 | +# Get cluster CA and token (base64 encoded) |
| 229 | +kubectl get secrets coder-service-account-token -n coder-workspaces -o jsonpath="{.data}" |
| 230 | +
|
| 231 | +coder templates push \ |
| 232 | + --variable host=API_ADDRESS \ |
| 233 | + --variable cluster_ca_certificate=CLUSTER_CA_CERTIFICATE \ |
| 234 | + --variable token=CLUSTER_SERVICEACCOUNT_TOKEN \ |
| 235 | + --variable namespace=coder-workspaces |
| 236 | +``` |
0 commit comments