Setting Up Kubernetes ===================== A project runs on Kubernetes once you have four things: a container image with runtraj and HYSPLIT, a cluster that ``kubectl`` points at, a bucket for the inputs and results that the pods can reach, and the met. runtraj keeps no database and runs no service, so the Jobs and CronJobs of :doc:`containers` are all it leaves on the cluster. This page makes each of them. `Try it on your computer`_ runs a small project on a cluster on your own machine first, with no cloud account. `Google Cloud`_ makes a real cluster and bucket, and `Another cluster`_ says what changes elsewhere. .. _kubernetes-image: The image --------- runtraj publishes no image, since an image has to hold HYSPLIT's ``hycs_std``, which you download from NOAA ARL (:doc:`../getting_started/installation`). Build one from a Linux HYSPLIT folder (``exec/hycs_std`` and ``bdyfiles/``), copied next to this ``Dockerfile`` as ``hysplit/``: .. code-block:: docker FROM python:3.13-slim RUN apt-get update \ && apt-get install -y --no-install-recommends tini \ && rm -rf /var/lib/apt/lists/* RUN pip install --no-cache-dir "runtraj[download]" gcsfs COPY hysplit/ /opt/hysplit/ ENV PATH=/opt/hysplit/exec:$PATH RUN useradd --create-home --uid 10001 runtraj USER 10001 ENTRYPOINT ["/usr/bin/tini", "--"] ``gcsfs`` reads and writes a Google Cloud Storage bucket; use ``s3fs`` for an S3 one. ``tini`` runs as process 1, passes Kubernetes' stop signal on to ``runtraj`` (which exits with 3 and is retried) and reaps the finished HYSPLIT processes. The image runs as user 10001 rather than root, the group runtraj gives the pods' volumes (``fsGroup``), so a new volume is writable without further setup. Add the package of each stage your project names, and pin the versions you ran with. Then build it, and push it to a registry the cluster can pull from: .. code-block:: bash docker build -t us-central1-docker.pkg.dev/my-project/runs/runtraj-hysplit:1 . docker push us-central1-docker.pkg.dev/my-project/runs/runtraj-hysplit:1 and name it in the project's ``config.yaml``: .. code-block:: yaml execution: backend: kubernetes kubernetes: image: us-central1-docker.pkg.dev/my-project/runs/runtraj-hysplit:1 We recommend a private registry: the image holds HYSPLIT, whose terms are NOAA ARL's. Every pod must run the same ``hycs_std``, since its checksum is part of each run's settings (:ref:`kubernetes-submit`), and ``runtraj submit`` and ``status`` look for the results by the checksum of the ``hycs_std`` where they run: submit and check from a machine with the same build. Give each build a new tag rather than reusing one, so that the pods of a Job all pull the same image. Try it on your computer ----------------------- This runs a small project on a Kubernetes cluster on your own computer, with `kind `_ (Kubernetes in Docker) and a stand-in S3 server inside the cluster. It is the run runtraj's end-to-end test makes on every pull request, so it is known to work. It takes about ten minutes, on Linux: the HYSPLIT build below is for Linux on x86-64. You need Docker, ``kind``, ``kubectl``, `uv `_ and a clone of the repository: .. code-block:: bash git clone https://github.com/jmineau/runtraj.git cd runtraj uv sync --extra download uv pip install s3fs The project is ``tests/kubernetes/demo``: two releases at the University of Utah early on 2021-01-15, three hours back, 100 particles each, with its inputs and results on the stand-in S3 server (its ``config.yaml`` has the ``kubernetes`` block). It needs a Linux HYSPLIT folder at ``/opt/hysplit``, here and in the image. The script the test uses installs one (it asks for ``sudo``): .. code-block:: bash .github/scripts/install_hysplit.sh The meteorology ^^^^^^^^^^^^^^^ The pods read the met from a folder your computer shares with the cluster. Download the one HRRR file the project needs, cropped to Utah (about 30 MB once cropped; the download itself is 3.4 GB): .. code-block:: bash uv run python -c ' from arlmet.archives import HRRRArchive HRRRArchive().fetch("2021-01-15 00:00", "2021-01-15 05:59", dest_dir="met", bbox=(-114.0, 39.0, -110.0, 42.0)) ' runtraj reads the met's first file to name the results, so the met must be at ``/met`` on your computer too, as in the pods: .. code-block:: bash sudo ln -s "$PWD/met" /met The cluster ^^^^^^^^^^^ Make a cluster whose node sees that folder at ``/met``: .. code-block:: bash cat > kind.yaml <`_, and ``kubectl`` with its Google sign-in plugin: .. code-block:: bash gcloud components install kubectl gke-gcloud-auth-plugin export PROJECT=my-project ZONE=us-central1-a gcloud config set project $PROJECT gcloud services enable container.googleapis.com storage.googleapis.com \ artifactregistry.googleapis.com A registry for the image (`The image`_), private to the project: .. code-block:: bash gcloud artifacts repositories create runs --repository-format=docker \ --location=us-central1 gcloud auth configure-docker us-central1-docker.pkg.dev The bucket ^^^^^^^^^^ .. code-block:: bash gcloud storage buckets create gs://my-runs --location=us-central1 \ --uniform-bucket-level-access The inputs go under ``gs://my-runs/projects/``, and the results under ``gs://my-runs/output``, so ``config.yaml`` says: .. code-block:: yaml output: gs://my-runs/output execution: backend: kubernetes kubernetes: image: us-central1-docker.pkg.dev/my-project/runs/runtraj-hysplit:1 inputs: gs://my-runs/projects/my_project The cluster ^^^^^^^^^^^ Make a cluster with one small node, which runs Kubernetes' own pods, and a pool of bigger nodes for the runs. The pool has no nodes until a Job asks for them, and goes back to none when the Job is done, so it costs nothing between runs: .. code-block:: bash gcloud container clusters create runs --zone=$ZONE \ --workload-pool=$PROJECT.svc.id.goog \ --addons=GcpFilestoreCsiDriver \ --machine-type=e2-standard-2 --num-nodes=1 gcloud container node-pools create work --cluster=runs --zone=$ZONE \ --machine-type=e2-standard-8 --spot \ --enable-autoscaling --min-nodes=0 --max-nodes=20 gcloud container clusters get-credentials runs --zone=$ZONE ``--spot`` makes the pool's nodes Spot VMs, much cheaper, which Google can take back at any time. A pod lost that way is retried without counting against its task's retries, so nothing is lost but time; leave ``--spot`` out for nodes that stay. ``--max-nodes`` caps the cost of one run. ``--addons=GcpFilestoreCsiDriver`` is for the met volume below. A pod asks for ``cpus`` CPUs, and a node keeps a little of its own, so a pod that asks for all 8 of an 8-CPU machine never starts. Ask for a bit less (``cpus: 7``), or for half (``cpus: 4``, two pods to a node). The pods' access to the bucket ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ``--workload-pool`` lets a Kubernetes service account act on the bucket with no key to keep. Make one and give it the bucket: .. code-block:: bash kubectl create serviceaccount runs NUMBER=$(gcloud projects describe $PROJECT --format='value(projectNumber)') MEMBER=principal://iam.googleapis.com/projects/$NUMBER/locations/global/workloadIdentityPools/$PROJECT.svc.id.goog/subject/ns/default/sa/runs gcloud storage buckets add-iam-policy-binding gs://my-runs \ --role=roles/storage.objectUser --member=$MEMBER ``roles/storage.objectUser`` reads, writes and deletes files in that bucket only. Name the service account in the ``kubernetes`` block: .. code-block:: yaml execution: backend: kubernetes kubernetes: image: us-central1-docker.pkg.dev/my-project/runs/runtraj-hysplit:1 service_account: runs Your own computer reads and writes the bucket too (``runtraj submit`` copies the inputs there, ``runtraj status`` counts the results). Sign in once, and install ``gcsfs``: .. code-block:: bash gcloud auth application-default login pip install runtraj gcsfs The met ^^^^^^^ With ``download: hrrr``, the met comes from NOAA's archive, which is public. Keep it on a volume every pod mounts, so each file is downloaded once, and set ``fetch`` so the pods get it in turn before they run (:ref:`fetch-once`). The pods run on many nodes at once, so the volume must be one that many nodes can mount and write (``ReadWriteMany``). On Google Cloud that is Filestore, whose driver the cluster above has: .. code-block:: bash kubectl apply -f - < aws.env <