Install Coder on K3s in an air-gapped environment
Install Coder on a single-node K3s cluster with no internet access, using artifacts staged on a connected workstation.
On this page
This tutorial helps a Coder deployment administrator install Coder on a single-node K3s cluster with no internet access. You stage every artifact on an internet-connected workstation, carry the artifacts to the offline host on removable storage, and finish with a running workspace.
This hands-on tutorial assumes you are familiar with the principles of air-gapped computing and understand the architecture of Kubernetes clusters. For the concepts behind an air-gapped deployment, refer to Air-gapped deployments. To install Coder on a cluster that has internet access, refer to Install Coder on Kubernetes.
What you stage
What you stageThe offline host can't download anything, so you stage each of these components on the workstation and copy it to the host.
| Component | Why the host needs it | When you need it |
|---|---|---|
| K3s binary, install script, and image archive | Runs a single-node Kubernetes cluster without pulling images | Always |
| Helm, the Coder Helm chart, and a values file | Installs and configures Coder without a connection to the chart repository | Always |
| Coder image with a Terraform provider mirror | Builds templates without the public Terraform registry | Always |
| PostgreSQL image and manifest | Runs the database that Coder needs, inside the cluster | For this tutorial. A production deployment uses an external PostgreSQL database. |
| Workspace image with code-server | Provides the editor that the starter template otherwise downloads when a workspace starts | When your template uses code-server |
| Edited Kubernetes starter template | Uses the workspace image and removes the download from the startup script | Always |
Prerequisites
PrerequisitesYou work on two machines: an internet-connected workstation that stages the artifacts, and an offline host that runs Coder. The host has no network connection to the workstation, so you carry the artifacts on removable storage and run the host commands on the host itself.
You need:
- A workstation with Docker that can build images for the host's architecture,
zstd, and thecoderCLI. - An
arm64oramd64host that runs Ubuntu Server 26.04 Long-Term Support (LTS), with at least 4 GB of memory and 20 GB of free storage, and a user who can runsudo. - A host clock that shows the correct time and keeps it across restarts, because the K3s certificates depend on the clock. A host with no battery-backed clock and no time server on its network, such as a Raspberry Pi, loses the time when it restarts.
- A removable storage device that you can write to on the workstation and read from on the offline host, with at least 1.5 GB of free space.
The staged artifacts total about 1.5 GB, and the steps use about 11 GB of storage on the host.
Set the variables
Set the variablesRun the workstation commands in this tutorial in the same shell. If you open a new shell on the workstation, set these variables again. The host loads the variables that it needs from a file that you copy to it.
-
Set the host address.
export HOST_ADDRESS=<host-address>Replace
<host-address>with the host's address or host name on the offline network. Coder uses this address in its access URL. -
Set the host architecture and the matching K3s binary name.
export ARCH=arm64 export K3S_BINARY=k3s-arm64For an
amd64host, setARCH=amd64andK3S_BINARY=k3s, because the K3s release names itsamd64binaryk3s. To check the architecture, rununame -mon the host. The outputaarch64meansarm64, andx86_64meansamd64.
Stage artifacts on the workstation
Stage artifacts on the workstationThis is the only section that needs internet access on the workstation.
-
Create a directory for the artifacts and open it.
Choose a location with at least 2 GB of free space, and stay in this directory for the rest of the workstation steps.
mkdir -p airgap-stage/coder-image airgap-stage/workspace-image cd airgap-stage -
Set the versions that these steps use.
export K3S_RELEASE_URL=https://github.com/k3s-io/k3s/releases/download/v1.37.2%2Bk3s1 export CODER_VERSION=v2.38.6 export CODER_PROVIDER_VERSION=2.19.0 export KUBERNETES_PROVIDER_VERSION=3.5.0 export CODE_SERVER_VERSION=4.145.0 export HELM_VERSION=v4.3.2 export POSTGRES_VERSION=17.11 export WORKSPACE_IMAGE=coder-workspace:ubuntu-cs${CODE_SERVER_VERSION}These are the versions that the tutorial was tested with. To use a newer K3s release, copy its tag from the K3s releases page, and replace the
+in the tag with%2BinK3S_RELEASE_URL.The Coder provider version matches the version that Coder
v2.38.6is built against. To find the provider version for another Coder version, read thegithub.com/coder/terraform-provider-coderline in thego.modfile at that version's release tag. -
Download the K3s binary, the K3s image archive, the checksum list, and the install script.
curl -fsSLO ${K3S_RELEASE_URL}/${K3S_BINARY} curl -fsSLO ${K3S_RELEASE_URL}/k3s-airgap-images-${ARCH}.tar.zst curl -fsSLO ${K3S_RELEASE_URL}/sha256sum-${ARCH}.txt curl -fsSL https://get.k3s.io -o install.sh -
Check the K3s files against the checksum list.
grep -E " (${K3S_BINARY}|k3s-airgap-images-${ARCH}.tar.zst)$" sha256sum-${ARCH}.txt | shasum -a 256 -c -You see
OKfor both files, here forarm64:k3s-airgap-images-arm64.tar.zst: OK k3s-arm64: OK -
Download Helm and its checksum.
curl -fsSLO https://get.helm.sh/helm-${HELM_VERSION}-linux-${ARCH}.tar.gz curl -fsSLO https://get.helm.sh/helm-${HELM_VERSION}-linux-${ARCH}.tar.gz.sha256sum -
Check the Helm archive against its checksum.
shasum -a 256 -c helm-${HELM_VERSION}-linux-${ARCH}.tar.gz.sha256sumYou see
OK, here forarm64:helm-v4.3.2-linux-arm64.tar.gz: OK -
Download the Coder Helm chart.
curl -fsSLO https://helm.coder.com/v2/coder_helm_${CODER_VERSION#v}.tgz -
Check the chart version.
tar -xzOf coder_helm_${CODER_VERSION#v}.tgz coder/Chart.yaml | grep -E '^(appVersion|name|version):'You see the chart and application versions that match
CODER_VERSION:appVersion: 2.38.6 name: coder version: 2.38.6
Build the Coder image
Build the Coder imageThe Coder image needs the Terraform providers that your templates use, because the host cannot download them. You build an image that carries a filesystem mirror of those providers.
A filesystem mirror suits a proof of concept, because it needs no other service. For a long-lived deployment, a provider mirror on your network, such as a Terraform network mirror or an artifact repository, is easier to keep current. Refer to Provider installation in the Terraform documentation and to Air-gapped deployments.
-
Create the Terraform CLI configuration that points to the mirror.
cat > coder-image/filesystem-mirror.tfrc <<'EOF' provider_installation { filesystem_mirror { path = "/home/coder/.terraform.d/plugins" } } EOF -
Create the Dockerfile for the Coder image.
cat > coder-image/Dockerfile <<'EOF' ARG CODER_VERSION FROM ghcr.io/coder/coder:${CODER_VERSION} AS mirror ARG TARGETARCH ARG CODER_PROVIDER_VERSION ARG KUBERNETES_PROVIDER_VERSION USER root WORKDIR /tmp/mirror-src RUN printf 'terraform {\n required_providers {\n coder = {\n source = "coder/coder"\n version = "%s"\n }\n kubernetes = {\n source = "hashicorp/kubernetes"\n version = "%s"\n }\n }\n}\n' \ "${CODER_PROVIDER_VERSION}" "${KUBERNETES_PROVIDER_VERSION}" > main.tf \ && terraform providers mirror -platform=linux_${TARGETARCH} /tmp/plugins FROM ghcr.io/coder/coder:${CODER_VERSION} COPY --from=mirror --chown=coder:coder /tmp/plugins /home/coder/.terraform.d/plugins COPY --chown=coder:coder filesystem-mirror.tfrc /home/coder/.terraformrc ENV TF_CLI_CONFIG_FILE=/home/coder/.terraformrc USER coder EOFThe first stage downloads the providers with the Terraform CLI that the official image already contains. The second stage copies the mirror into the final image.
-
Build the Coder image.
docker build --platform linux/${ARCH} \ --build-arg TARGETARCH=${ARCH} \ --build-arg CODER_VERSION=${CODER_VERSION} \ --build-arg CODER_PROVIDER_VERSION=${CODER_PROVIDER_VERSION} \ --build-arg KUBERNETES_PROVIDER_VERSION=${KUBERNETES_PROVIDER_VERSION} \ -t coder-airgap:${CODER_VERSION} coder-image -
Check that the image contains the Coder provider.
docker run --rm --platform linux/${ARCH} --entrypoint ls coder-airgap:${CODER_VERSION} \ /home/coder/.terraform.d/plugins/registry.terraform.io/coder/coderYou see the provider archive for your version and architecture, here for
arm64:2.19.0.json index.json terraform-provider-coder_2.19.0_linux_arm64.zip
Build the workspace image
Build the workspace imageThe starter template installs code-server when a workspace starts, which needs internet access. You install code-server while you build the workspace image instead.
-
Create the Dockerfile for the workspace image.
cat > workspace-image/Dockerfile <<'EOF' FROM codercom/example-base:ubuntu ARG CODE_SERVER_VERSION USER root RUN curl -fsSL https://code-server.dev/install.sh \ | sh -s -- --method=standalone --prefix=/opt/code-server --version ${CODE_SERVER_VERSION} USER coder EOF -
Build the workspace image.
docker build --platform linux/${ARCH} \ --build-arg CODE_SERVER_VERSION=${CODE_SERVER_VERSION} \ -t ${WORKSPACE_IMAGE} workspace-image -
Check the code-server version in the image.
docker run --rm --platform linux/${ARCH} --entrypoint /opt/code-server/bin/code-server ${WORKSPACE_IMAGE} --version | tail -1The output starts with your code-server version:
4.145.0 ... with Code 1.145.0
Save the images
Save the images-
Pull the PostgreSQL image.
docker pull --platform linux/${ARCH} postgres:${POSTGRES_VERSION} -
Save the three images as compressed archives.
docker save coder-airgap:${CODER_VERSION} | zstd -T0 -10 -q -o coder-airgap_${CODER_VERSION}_${ARCH}.tar.zst docker save ${WORKSPACE_IMAGE} | zstd -T0 -10 -q -o coder-workspace_${ARCH}.tar.zst docker save postgres:${POSTGRES_VERSION} | zstd -T0 -10 -q -o postgres_${POSTGRES_VERSION}_${ARCH}.tar.zst
Create the template
Create the template-
Create a copy of the Kubernetes starter template.
coder templates init --id kubernetes templateYou see the extraction message:
Extracting kubernetes to template... Create your template by running: cd template && coder templates push Examples provide a starting point and are expected to be edited! 🎨 -
Set the agent architecture to the host's architecture.
sed -i.bak "s/arch *= \"amd64\"/arch = \"${ARCH}\"/" template/main.tfThe starter template sets the architecture to
amd64, so anarm64workspace could not run its agent. On anamd64host, the command leaves the template unchanged. -
Use the workspace image and stop Kubernetes from pulling it.
sed -i.bak \ -e "s#codercom/example-base:ubuntu#${WORKSPACE_IMAGE}#" \ -e 's/image_pull_policy = "Always"/image_pull_policy = "IfNotPresent"/' \ template/main.tfWith the
Alwayspull policy, Kubernetes contacts a registry every time a workspace starts, even when the image is already on the host. -
Create the replacement for the
startup_scriptblock.cat > startup-script.txt <<'EOF' startup_script = <<-EOT set -e /opt/code-server/bin/code-server --auth none --port 13337 \ --disable-telemetry --disable-update-check >/tmp/code-server.log 2>&1 & EOT EOF -
Replace the
startup_scriptblock in the template.perl -0pi -e 'BEGIN{local $/; open F,"startup-script.txt"; $r=<F>; close F} s/ startup_script = <<-EOT\n.*?\n EOT\n/$r/s' template/main.tfThe original script downloads code-server from the internet. The new script starts the code-server copy in the image, and its two flags stop code-server from contacting the internet for telemetry and update checks. This command uses Perl, which is installed on macOS and on most Linux distributions.
-
Check the changes.
grep -E 'arch *=|image *=|image_pull_policy|code-server/bin' template/main.tfYou see the architecture, the code-server path, your workspace image, and the pull policy, here for
arm64:arch = "arm64" /opt/code-server/bin/code-server --auth none --port 13337 \ image = "coder-workspace:ubuntu-cs4.145.0" image_pull_policy = "IfNotPresent" -
Remove the temporary files that the edits created.
rm -f template/main.tf.bak startup-script.txt
Create the configuration files
Create the configuration filesYou create the long configuration files on the workstation, so that you don't type them on the host.
-
Create the PostgreSQL manifest.
cat > postgres.yaml <<EOF apiVersion: v1 kind: Service metadata: name: postgresql namespace: coder spec: selector: app.kubernetes.io/name: postgresql ports: - name: postgresql port: 5432 targetPort: postgresql --- apiVersion: apps/v1 kind: StatefulSet metadata: name: postgresql namespace: coder spec: serviceName: postgresql replicas: 1 selector: matchLabels: app.kubernetes.io/name: postgresql template: metadata: labels: app.kubernetes.io/name: postgresql spec: containers: - name: postgresql image: postgres:${POSTGRES_VERSION} imagePullPolicy: IfNotPresent ports: - name: postgresql containerPort: 5432 env: - name: POSTGRES_USER value: coder - name: POSTGRES_DB value: coder - name: POSTGRES_PASSWORD valueFrom: secretKeyRef: name: postgres-credentials key: password - name: PGDATA value: /var/lib/postgresql/data/pgdata readinessProbe: exec: command: ["pg_isready", "-U", "coder", "-d", "coder"] initialDelaySeconds: 5 periodSeconds: 10 resources: requests: cpu: 100m memory: 256Mi limits: memory: 512Mi volumeMounts: - name: data mountPath: /var/lib/postgresql/data volumeClaimTemplates: - metadata: name: data spec: accessModes: ["ReadWriteOnce"] resources: requests: storage: 5Gi EOFThe manifest sets
imagePullPolicy: IfNotPresentso that Kubernetes uses the image that K3s imported. -
Create the Helm values file.
cat > coder-values.yaml <<EOF coder: image: repo: docker.io/library/coder-airgap tag: ${CODER_VERSION} pullPolicy: IfNotPresent resources: requests: cpu: 500m memory: 512Mi limits: cpu: "2" memory: 1Gi service: type: NodePort httpNodePort: "30080" env: - name: CODER_PG_CONNECTION_URL valueFrom: secretKeyRef: name: coder-db-url key: url - name: CODER_ACCESS_URL value: http://${HOST_ADDRESS}:30080 - name: CODER_TELEMETRY_ENABLE value: "false" - name: CODER_UPDATE_CHECK value: "false" - name: CODER_DERP_SERVER_STUN_ADDRESSES value: disable - name: CODER_BLOCK_DIRECT value: "true" - name: CODER_DISABLE_TEMPLATE_BUILDER value: "true" - name: CODER_OAUTH2_GITHUB_DEFAULT_PROVIDER_ENABLE value: "false" EOFThe values make four changes:
- The resource requests and limits replace the chart default of 2 CPU cores and 4 GiB of memory, which a host with 4 GB of memory cannot schedule.
- The
NodePortservice avoids the ports 80 and 443 that the bundled Traefik ingress controller uses. - The
CODER_ACCESS_URLvalue must be reachable from workspaces and from browsers on the offline network. - The remaining environment variables turn off telemetry, the update check, Session Traversal Utilities for Network Address Translation (STUN), the default GitHub sign-in provider, and the template builder, and they send workspace connections through the control plane.
Copy the artifacts to the host
Copy the artifacts to the hostRun the first three steps on the workstation and the rest on the offline host.
-
Save the variables that the host steps use.
cat > airgap.env <<EOF export HOST_ADDRESS=${HOST_ADDRESS} export ARCH=${ARCH} export K3S_BINARY=${K3S_BINARY} export CODER_VERSION=${CODER_VERSION} export HELM_VERSION=${HELM_VERSION} EOF -
Create a checksum list for every staged file.
find . -type f ! -name SHA256SUMS -print0 | xargs -0 shasum -a 256 > SHA256SUMS -
Copy the staged files to removable storage.
mkdir -p <removable-storage>/airgap cp -r ./* <removable-storage>/airgap/Replace
<removable-storage>with the path where the workstation mounts the storage device, such as/Volumes/<name>on macOS. The copy of about 1.5 GB takes a few minutes. When it finishes, eject the storage device. -
On the offline host, insert and mount the removable storage.
If the host doesn't mount the device automatically, find it with
lsblk, and then mount it, for example withsudo mount /dev/sdb1 /mnt. -
Copy the files from the removable storage to your home directory.
cp -r <removable-storage>/airgap ~/airgapReplace
<removable-storage>with the path where the host mounts the storage device, such as/mnt. -
Check the copied files.
cd ~/airgap && sha256sum -c SHA256SUMSThe command prints one line for every staged file, and every line ends with
OK. For example, onarm64:./k3s-arm64: OK ./k3s-airgap-images-arm64.tar.zst: OK ./coder_helm_2.38.6.tgz: OK -
Load the variables.
. ./airgap.envRun the rest of the commands in this tutorial on the host, in the
~/airgapdirectory. If you open a new shell on the host, runcd ~/airgap && . ./airgap.envagain. After you create the cluster configuration in Set up the command-line tools, also run itsexport KUBECONFIGcommand again.
Install K3s
Install K3s-
Check that the host has a default route.
ip route show defaultK3s needs a default route to detect the node address, even when the route leads nowhere. If the command prints a route, skip to the next step. If it prints nothing, add a route through a virtual network interface:
sudo ip link add dummy0 type dummy sudo ip link set dummy0 up sudo ip addr add 203.0.113.254/31 dev dummy0 sudo ip route add default via 203.0.113.255 dev dummy0 metric 1000The route doesn't persist after a restart, and K3s doesn't start without it. After a restart, run these commands again, and K3s starts on its next retry. To keep the route instead, add it to the host's network configuration.
-
Install the K3s binary.
sudo install -o root -g root -m 755 ~/airgap/${K3S_BINARY} /usr/local/bin/k3sThe install script does not download the binary when you skip the download, so the binary must be in place first.
-
Copy the image archives into the K3s image directory.
sudo mkdir -p /var/lib/rancher/k3s/agent/images && \ sudo cp ~/airgap/*.tar.zst /var/lib/rancher/k3s/agent/images/The directory receives the K3s archive and your three image archives. K3s imports every archive in this directory when it starts.
-
Run the K3s install script without downloads.
cd ~/airgap && sudo env INSTALL_K3S_SKIP_DOWNLOAD=true INSTALL_K3S_SKIP_SELINUX_RPM=true sh ./install.shYou see the script skip the download and start the service:
[INFO] Skipping k3s download and verify [INFO] Skipping installation of SELinux RPM ... [INFO] systemd: Starting k3s -
Wait for the node to become ready.
until sudo k3s kubectl get nodes 2>/dev/null | grep -q " Ready"; do sleep 5; done; sudo k3s kubectl get nodesK3s needs a few minutes to start, and the command prints nothing until the node is ready. You see one node with the
control-planerole:NAME STATUS ROLES AGE VERSION <hostname> Ready control-plane 2m v1.37.2+k3s1 -
Wait for K3s to import your three images.
until [ "$(sudo k3s crictl images | grep -cE "coder-airgap|coder-workspace|postgres")" -ge 3 ]; do sleep 10; done; sudo k3s crictl images | grep -E "coder-airgap|coder-workspace|postgres"The import can take several minutes on a host with slow storage. You see the three images:
docker.io/library/coder-airgap v2.38.6 ... docker.io/library/coder-workspace ubuntu-cs4.145.0 ... docker.io/library/postgres 17.11 ...
Set up the command-line tools
Set up the command-line tools-
Install Helm.
tar -xzf helm-${HELM_VERSION}-linux-${ARCH}.tar.gz sudo install -o root -g root -m 755 linux-${ARCH}/helm /usr/local/bin/helm -
Copy the cluster configuration to your home directory.
mkdir -p ~/.kube sudo cat /etc/rancher/k3s/k3s.yaml > ~/.kube/config chmod 600 ~/.kube/config export KUBECONFIG=~/.kube/configThe file contains cluster administrator credentials, so keep it private. Only root can read the original file, so
kubectland Helm read your copy throughKUBECONFIG. -
List the nodes without
sudo.kubectl get nodesThe K3s install script adds the
kubectlcommand. You see the same node with the statusReady:NAME STATUS ROLES AGE VERSION <hostname> Ready control-plane 3m v1.37.2+k3s1
Install PostgreSQL
Install PostgreSQLCoder needs a PostgreSQL database. This tutorial runs the official PostgreSQL image in the cluster, which suits a proof of concept.
-
Create the namespace and the secrets.
kubectl create namespace coder POSTGRES_PASSWORD=$(openssl rand -hex 16) kubectl create secret generic postgres-credentials -n coder --from-literal=password="${POSTGRES_PASSWORD}" kubectl create secret generic coder-db-url -n coder \ --from-literal=url="postgres://coder:${POSTGRES_PASSWORD}@postgresql.coder.svc.cluster.local:5432/coder?sslmode=disable" -
Apply the manifest and wait for the database.
kubectl apply -f postgres.yaml kubectl rollout status statefulset/postgresql -n coder --timeout=300sYou see the rollout finish:
partitioned roll out complete: 1 new pods have been updated...
Install Coder
Install Coder-
Install Coder from the chart file.
helm install coder coder_helm_${CODER_VERSION#v}.tgz \ --namespace coder --values coder-values.yaml --wait --timeout 10mYou see the release status
deployed:STATUS: deployed -
Check that the Coder pod runs.
kubectl get pods -n coderBoth pods show
1/1underREADYandRunningunderSTATUS:NAME READY STATUS RESTARTS AGE coder-6b48dbbc66-q8swg 1/1 Running 0 52s postgresql-0 1/1 Running 0 18m
Create a template and a workspace
Create a template and a workspace-
Install the Coder CLI from your deployment.
curl -fsSL http://${HOST_ADDRESS}:30080/install.sh | shThe script downloads the CLI from Coder instead of the internet, so the CLI version matches the server. It uses
sudoto install the CLI in/usr/local/bin. You see that the CLI is installed:Coder v2.38.6+db7c30f installed. -
Create the first user and log in.
coder login http://${HOST_ADDRESS}:30080 \ --first-user-username <username> --first-user-email <email> \ --first-user-password <password> --first-user-trial=falseReplace
<username>,<email>, and<password>with the values for the first user. If the password contains special characters, put it in single quotes. The first user becomes the owner of the deployment.You see a message that begins by confirming that you are authenticated:
Welcome to Coder, <username>! You're authenticated.To use the Coder dashboard, open
http://<host-address>:30080in a browser on the offline network, and sign in as this user. -
Push the template.
coder templates push kubernetes-airgap --directory template --variable namespace=coder --yesThe Terraform that runs inside the Coder pod resolves its providers from the image's mirror, without internet access. You see that the template was created:
The kubernetes-airgap template has been created at ... -
Create a workspace.
coder create my-workspace --template kubernetes-airgap \ --parameter cpu=2 --parameter memory=2 --parameter home_disk_size=5 --yesYou see that the workspace was created:
The my-workspace workspace has been created at ...The first workspace can take several minutes to start on a host with slow storage, because K3s unpacks the image on first use. Later starts are much faster.
While the workspace starts, you might see the warning
The workspace agent is having trouble connecting. You might also see acontext deadline exceededevent fromkubectl get events -n coder, followed by one pod restart. Both are normal on the first start, and the workspace recovers on its own. -
Run a command in the workspace.
coder ssh my-workspace -- echo connectedYou see
connected. If the agent has not connected yet, the command waits for it. You might also see a notice that the workspace startup scripts are still running.
Verify the air gap
Verify the air gap-
Check that code-server runs in the workspace.
coder ssh my-workspace -- 'wget -q -O - http://localhost:13337/healthz'You see the health response:
{"status":"expired","lastHeartbeat":0}The status
expiredis normal when no browser is connected. If the command prints nothing, code-server is still starting, so wait a few seconds and run the command again. To open the editor from a browser on the offline network, open the Coder dashboard, select my-workspace, and select code-server. -
Confirm that the workspace has no internet access.
coder ssh my-workspace -- 'wget -T 5 -q -O /dev/null https://example.com || echo "wget exit code: $?"'You see the exit code 4, which means that
wgetcould not reach the server:wget exit code: 4A different exit code, or no
wgeterror, means the workspace reached the internet. The result depends on your network, because the workspace can reach the internet if the host itself has a route to it.