|
| 1 | +--- |
| 2 | +name: Develop in Kubernetes |
| 3 | +description: Get started with Kubernetes development. |
| 4 | +tags: [cloud, kubernetes] |
| 5 | +--- |
| 6 | + |
| 7 | +# Getting started |
| 8 | + |
| 9 | +This template creates a pod running the `codercom/enterprise-base:ubuntu` image. |
| 10 | + |
| 11 | +## RBAC |
| 12 | + |
| 13 | +The Coder provisioner requires permission to administer pods to use this template. The template |
| 14 | +creates workspaces in a single Kubernetes namespace, using the `workspaces_namespace` parameter set |
| 15 | +while creating the template. |
| 16 | + |
| 17 | +Create a role as follows and bind it to the user or service account that runs the coder host. |
| 18 | + |
| 19 | +```yaml |
| 20 | +apiVersion: rbac.authorization.k8s.io/v1 |
| 21 | +kind: Role |
| 22 | +metadata: |
| 23 | + name: coder |
| 24 | +rules: |
| 25 | + - apiGroups: [""] |
| 26 | + resources: ["pods"] |
| 27 | + verbs: ["*"] |
| 28 | +``` |
| 29 | +
|
| 30 | +## Authentication |
| 31 | +
|
| 32 | +This template can authenticate using in-cluster authentication, or using a kubeconfig local to the |
| 33 | +Coder host. For additional authentication options, consult the [Kubernetes provider |
| 34 | +documentation](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs). |
| 35 | +
|
| 36 | +### kubeconfig on Coder host |
| 37 | +
|
| 38 | +If the Coder host has a local `~/.kube/config`, you can use this to authenticate |
| 39 | +with Coder. Make sure this is done with same user that's running the `coder` service. |
| 40 | + |
| 41 | +To use this authentication, set the parameter `use_kubeconfig` to true. |
| 42 | + |
| 43 | +### In-cluster authentication |
| 44 | + |
| 45 | +If the Coder host runs in a Pod on the same Kubernetes cluster as you are creating workspaces in, |
| 46 | +you can use in-cluster authentication. |
| 47 | + |
| 48 | +To use this authentication, set the parameter `use_kubeconfig` to false. |
| 49 | + |
| 50 | +The Terraform provisioner will automatically use the service account associated with the pod to |
| 51 | +authenticate to Kubernetes. Be sure to bind a [role with appropriate permission](#rbac) to the |
| 52 | +service account. For example, assuming the Coder host runs in the same namespace as you intend |
| 53 | +to create workspaces: |
| 54 | + |
| 55 | +```yaml |
| 56 | +apiVersion: v1 |
| 57 | +kind: ServiceAccount |
| 58 | +metadata: |
| 59 | + name: coder |
| 60 | +
|
| 61 | +--- |
| 62 | +apiVersion: rbac.authorization.k8s.io/v1 |
| 63 | +kind: RoleBinding |
| 64 | +metadata: |
| 65 | + name: coder |
| 66 | +subjects: |
| 67 | + - kind: ServiceAccount |
| 68 | + name: coder |
| 69 | +roleRef: |
| 70 | + kind: Role |
| 71 | + name: coder |
| 72 | + apiGroup: rbac.authorization.k8s.io |
| 73 | +``` |
| 74 | + |
| 75 | +Then start the Coder host with `serviceAccountName: coder` in the pod spec. |
| 76 | + |
| 77 | +## Namespace |
| 78 | + |
| 79 | +The target namespace in which the pod will be deployed is defined via the `coder_workspace` |
| 80 | +variable. The namespace must exist prior to creating workspaces. |
| 81 | + |
| 82 | +## Persistence |
| 83 | + |
| 84 | +The `/home/coder` directory in this example is persisted via the attached PersistentVolumeClaim. |
| 85 | +Any data saved outside of this directory will be wiped when the workspace stops. |
| 86 | + |
| 87 | +Since most binary installations and environment configurations live outside of |
| 88 | +the `/home` directory, we suggest including these in the `startup_script` argument |
| 89 | +of the `coder_agent` resource block, which will run each time the workspace starts up. |
| 90 | + |
| 91 | +For example, when installing the `aws` CLI, the install script will place the |
| 92 | +`aws` binary in `/usr/local/bin/aws`. To ensure the `aws` CLI is persisted across |
| 93 | +workspace starts/stops, include the following code in the `coder_agent` resource |
| 94 | +block of your workspace template: |
| 95 | + |
| 96 | +```terraform |
| 97 | +resource "coder_agent" "main" { |
| 98 | + startup_script = <<EOT |
| 99 | + #!/bin/bash |
| 100 | +
|
| 101 | + # install AWS CLI |
| 102 | + curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" |
| 103 | + unzip awscliv2.zip |
| 104 | + sudo ./aws/install |
| 105 | + EOT |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +## code-server |
| 110 | + |
| 111 | +`code-server` is installed via the `startup_script` argument in the `coder_agent` |
| 112 | +resource block. The `coder_app` resource is defined to access `code-server` through |
| 113 | +the dashboard UI over `localhost:13337`. |
0 commit comments