k3s hostmount

Tue, Feb 7, 2023 One-minute read

k3s hostmount

Using a host mount for local k3s development. This negates the need for skaffold, okteto etc for 80% use cases.

PROGNAME="hostmount"
usage="${PROGNAME} <container> -- Host mount local files into k3s

Must be located in the root directory of the git repo"

test ! -n "${1}" && echo "$usage" && exit 1

service="${1:-}"
jsonfile=/tmp/patch.json

json='
{
  "spec": {
    "template": {
      "spec": {
        "containers": [
          "name": "%s",
          "command": [
            "sh",
            "-c",
            "apk add py-watchdog && watchmedo auto-restart -d $PWD -R -- entrypoint.py"
            ],
            "volumeMounts": [
              "mountPath": "/usr/src/app",
              "name": "host-mount"
              }
            ]
          }
        ],
        "volumes": [
          {
            "name": "host-mount",
            "hostPath": {
              "path": "%s"
              }
            }
          ]
        }
      }
    }
  }
}
'

printf "$json" "$service" "$(pwd)" > $jsonfile 
cat $jsonfile

kubectl patch deployment "${service}" -n "${namespace:-default}" --patch-file $jsonfile

This needs a bit of massaging for each project but after that this works great for python.

I’ll create Go version using air when I have a project that needs it.

Tags:

#k3s #development