Skip to content

Add a sample that runs a cluster on Kubernetes - #8274

Merged
piscisaureus merged 1 commit into
mainfrom
piscisaureus/cluster-k8s
Nov 25, 2026
Merged

piscisaureus merged 1 commit into
mainfrom
piscisaureus/cluster-k8s

Conversation

@piscisaureus

@piscisaureus piscisaureus commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

This sample runs a Wrangler project on three workerd pods in cluster
mode, with no Cap'n Proto config. Kubernetes rolls out new versions of
the app image, and each pod hands off its Durable Objects to the other
pods before it stops.

The sample works on many Kubernetes clusters, but it requires a load
balancer implementation that can provision Services of type
LoadBalancer. Managed Kubernetes services and k3s provide one; a kind
cluster needs cloud-provider-kind.

The sample doesn't use a real object storage bucket. A single RustFS
instance in the cluster emulates an S3 bucket. For production use, the
README recommends a real bucket, such as R2, S3, or Cloud Storage, and
describes how to configure one.

The sample consists of the following:

  • app/ is a Wrangler project that sets no_bundle. It returns
    hello world on / and runs a Durable Object counter on
    /counter/NAME. The response shows which pod accepted the request
    and which pod runs the object. The Dockerfile copies the project into
    docker.io/cloudflare/workerd. WORKERD_IMAGE selects a different
    workerd image, such as a pinned release or one built from source.
  • In kubernetes.yaml, each pod joins the cluster with
    --cluster-addr=$(POD_IP):7000 and keeps its state on an emptyDir
    volume with --data-dir=/data. The cluster key is a Secret that's
    mounted as a file, and the readiness probe is /ready on the cluster
    port. WORKERD_SHUTDOWN_DELAY_MS keeps a terminating pod serving for
    20 seconds while a cloud load balancer might still send it requests,
    and WORKERD_CLUSTER_STOP_TIMEOUT_MS limits its handoff to
    8 seconds. Rollouts hand off one pod at a time, a PodDisruptionBudget
    limits drains, and a NetworkPolicy lets only the workerd pods connect
    to the cluster port. The pods meet the "restricted" Pod Security
    Standard and have read-only root file systems.
  • The app's Service is of type LoadBalancer on port 80, so the README
    sends requests from outside the cluster. On a cloud provider, the
    load balancer's address is public, and the README cautions about
    this.
  • The demo bucket is a RustFS StatefulSet with one pod and a persistent
    volume. A Job creates the bucket with curl --aws-sigv4. RustFS and
    the workerd pods share credentials from a Secret, and a NetworkPolicy
    lets only the workerd pods and the Job connect to RustFS.
  • The README covers running the sample, a load test during a rollout
    that counts failed requests and skipped or repeated counts, cleanup,
    building an image for another project, the Wrangler fields that a
    cluster supports, using a real bucket, keeping versions compatible
    during a rollout, and the limitations of cluster mode.

When a Wrangler config file is missing, the error message now names the
wrangler.json, wrangler.jsonc, or wrangler.toml file next to the
given path, if one exists. The CLI test translates the sample app the
same way that its pods do.

On k3s, the README's rollout test through the load balancer served
2,699 requests with no failed requests and no skipped or repeated
counts.

Kind Added Removed
README 358 0
Manifests, Dockerfile, configs 367 0
Sample JavaScript 37 0
Rust 21 1
Rust tests 74 0
Bazel 7 1
Total 864 2

@piscisaureus
piscisaureus requested a review from ry November 7, 2026 01:33
@codecov-commenter

codecov-commenter commented Nov 7, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 14 lines in your changes missing coverage. Please review.
✅ Project coverage is 67.61%. Comparing base (269180d) to head (ee682fa).

Files with missing lines Patch % Lines
src/workerd/server/cli/wrangler.rs 0.00% 14 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8274      +/-   ##
==========================================
- Coverage   67.61%   67.61%   -0.01%     
==========================================
  Files         871      871              
  Lines      220304   220317      +13     
  Branches    32941    32941              
==========================================
+ Hits       148959   148961       +2     
- Misses      51847    51859      +12     
+ Partials    19498    19497       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment thread samples/kubernetes/bucket.capnp Resolved
@piscisaureus
piscisaureus force-pushed the piscisaureus/cluster-k8s branch 2 times, most recently from cec265d to 15a195d Compare November 10, 2026 01:29
@piscisaureus piscisaureus changed the title Run a cluster on Kubernetes: cluster key from the environment, and a sample Run a wrangler project as a cluster on Kubernetes Nov 10, 2026
@piscisaureus
piscisaureus force-pushed the piscisaureus/cluster-k8s branch 5 times, most recently from ee562f1 to e0392fd Compare November 11, 2026 17:57
@piscisaureus
piscisaureus force-pushed the piscisaureus/cluster-k8s branch from e0392fd to 428b36f Compare November 13, 2026 23:46
@piscisaureus
piscisaureus marked this pull request as ready for review November 13, 2026 23:49
@piscisaureus
piscisaureus requested review from a team as code owners November 13, 2026 23:49
@piscisaureus
piscisaureus marked this pull request as draft November 13, 2026 23:52

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of 5566ada2.

This review, of 428b36fa

💬 Approved with comments — The code changes look correct. Key and bucket loading, the --cluster-addr join, compiled-binary guards and do-storage handling all hold up. One WARNING: the sample README says workflows is an error in a cluster, but the code accepts it.

Findings: 1 warning (1 is an inline comment on the diff)

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.

🔧 Fix with your agent

Paste the following into your coding agent to investigate and fix these issues.

Fix the following code review issues found in pull request #8274.

Before making changes, review each issue and present a brief summary table:
- For each finding, state whether you agree, disagree, or need clarification
- If you disagree (e.g., the fix would require disproportionate changes
  for minimal ROI, or the finding is factually incorrect), explain why
- If you need to clarify something before deciding, ask those questions
- Then share your plan for which issues to tackle and in what order

## WARNING (1)

### [Documentation] README says any wrangler field outside the list is an error in a cluster, but `workflows` is accepted (wrangler.rs check_experimental only rejects kv_namespaces and queues).
- **File:** samples/kubernetes/README.md:131

📝 Documentation: ⚠️ 1 warning

The sample README says workflows is an error in a cluster, but the code accepts it.

Recommendation: 📝 Minor Comments

  • [WARNING] README says any wrangler field outside the list is an error in a cluster, but workflows is accepted (wrangler.rs check_experimental only rejects kv_namespaces and queues). (samples/kubernetes/README.md:131)
View full review
  • samples/kubernetes/README.md:131-133 lists the wrangler fields allowed in a cluster and says "Any other field is an error". workflows is missing from that list. check_experimental in src/workerd/server/cli/wrangler.rs:546-558 rejects only kv_namespaces and queues for --cluster-addr, so workflows is accepted. The PR description says the same.

✅ All other checks passed

🔒 Security: ✅ LGTM

No security issues found. Taking the cluster key from an environment variable or a mounted file keeps the secret out of configs, images and compiled binaries. The key file is size-limited and must be a regular file.


📖 AGENTS.md: ✅ LGTM

The updates to src/workerd/server/AGENTS.md raise no issues.


📋 Code Quality: ✅ LGTM

No problems found in the code:

  • Joining a cluster without --experimental: the join still reaches the server's refusal.
  • Filling in the key and bucket from the environment: the code rejects setting both key variables, a blank key, and a key file that is not a regular file, is over 4096 bytes or is not UTF-8. It checks that the path is a regular file before opening it, so a FIFO or device is never opened.
  • Compiled binaries: run_compiled refuses --cluster-addr when no cluster was compiled in, and compile never reads the environment.
  • Overriding storage with --directory-path: .wrangler/state/workerd is no longer created beside the config.
  • Build wiring: test_compile_data covers the sample files that the tests embed.

⚡ Performance: ✅ LGTM

No performance concerns.


Reviewed commit: 428b36fa

Comment thread samples/kubernetes/README.md Resolved
ry

This comment was marked as resolved.

@piscisaureus
piscisaureus force-pushed the piscisaureus/cluster-k8s branch from 428b36f to 2a83a4c Compare November 24, 2026 02:41
@piscisaureus piscisaureus changed the title Run a wrangler project as a cluster on Kubernetes Add a sample that runs a cluster on Kubernetes Nov 24, 2026
@piscisaureus
piscisaureus force-pushed the piscisaureus/cluster-k8s branch 6 times, most recently from 97c28f3 to 6b060de Compare November 24, 2026 23:36
@piscisaureus
piscisaureus requested a review from ry November 24, 2026 23:42
@piscisaureus
piscisaureus force-pushed the piscisaureus/cluster-k8s branch from 6b060de to 5566ada Compare November 24, 2026 23:42
@piscisaureus
piscisaureus marked this pull request as ready for review November 24, 2026 23:43
This sample runs a Wrangler project on three workerd pods in cluster
mode, with no Cap'n Proto config. Kubernetes rolls out new versions of
the app image, and each pod hands off its Durable Objects to the other
pods before it stops.

The sample works on many Kubernetes clusters, but it requires a load
balancer implementation that can provision Services of type
`LoadBalancer`. Managed Kubernetes services and k3s provide one; a kind
cluster needs cloud-provider-kind.

The sample doesn't use a real object storage bucket. A single RustFS
instance in the cluster emulates an S3 bucket. For production use, the
README recommends a real bucket, such as R2, S3, or Cloud Storage, and
describes how to configure one.

The sample consists of the following:

* `app/` is a Wrangler project that sets `no_bundle`. It returns
  `hello world` on `/` and runs a Durable Object counter on
  `/counter/NAME`. The response shows which pod accepted the request
  and which pod runs the object. The Dockerfile copies the project into
  `docker.io/cloudflare/workerd`. `WORKERD_IMAGE` selects a different
  workerd image, such as a pinned release or one built from source.
* In `kubernetes.yaml`, each pod joins the cluster with
  `--cluster-addr=$(POD_IP):7000` and keeps its state on an `emptyDir`
  volume with `--data-dir=/data`. The cluster key is a Secret that's
  mounted as a file, and the readiness probe is `/ready` on the cluster
  port. `WORKERD_SHUTDOWN_DELAY_MS` keeps a terminating pod serving for
  20 seconds while a cloud load balancer might still send it requests,
  and `WORKERD_CLUSTER_STOP_TIMEOUT_MS` limits its handoff to
  8 seconds. Rollouts hand off one pod at a time, a PodDisruptionBudget
  limits drains, and a NetworkPolicy lets only the workerd pods connect
  to the cluster port. The pods meet the "restricted" Pod Security
  Standard and have read-only root file systems.
* The app's Service is of type `LoadBalancer` on port 80, so the README
  sends requests from outside the cluster. On a cloud provider, the
  load balancer's address is public, and the README cautions about
  this.
* The demo bucket is a RustFS StatefulSet with one pod and a persistent
  volume. A Job creates the bucket with `curl --aws-sigv4`. RustFS and
  the workerd pods share credentials from a Secret, and a NetworkPolicy
  lets only the workerd pods and the Job connect to RustFS.
* The README covers running the sample, a load test during a rollout
  that counts failed requests and skipped or repeated counts, cleanup,
  building an image for another project, the Wrangler fields that a
  cluster supports, using a real bucket, keeping versions compatible
  during a rollout, and the limitations of cluster mode.

When a Wrangler config file is missing, the error message now names the
`wrangler.json`, `wrangler.jsonc`, or `wrangler.toml` file next to the
given path, if one exists. The CLI test translates the sample app the
same way that its pods do.

On k3s, the README's rollout test through the load balancer served
2,699 requests with no failed requests and no skipped or repeated
counts.

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of ee682fa3.

This review, of 5566ada2

✅ Approved — The earlier README issue about workflows is fixed. I found no new problems in the Rust change, the tests, the manifests or the sample app.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

Pods run as non-root with all capabilities dropped and read-only root filesystems. Credentials and the cluster key come from Secrets. NetworkPolicies limit access to the cluster port and to RustFS. The Rust change only adds a hint to a CLI error message.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md concerns.


📋 Code Quality: ✅ LGTM

The missing_config hint, the new tests in wrangler-test.rs that translate the sample app, and the Bazel wiring for the embedded sample files all look correct.


⚡ Performance: ✅ LGTM

No performance concerns.


📝 Documentation: ✅ LGTM

The README's list of supported Wrangler fields now includes workflows. It says that only kv_namespaces and queues are errors, which matches check_experimental in wrangler.rs.


Reviewed commit: 5566ada2

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Approved — The earlier README issue about workflows is fixed. I found no new problems in the Rust change, the tests, the manifests or the sample app.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

Pods run as non-root with all capabilities dropped and read-only root filesystems. Credentials and the cluster key come from Secrets. NetworkPolicies limit access to the cluster port and to RustFS. The Rust change only adds a hint to a CLI error message.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md concerns.


📋 Code Quality: ✅ LGTM

The missing_config hint, the new tests in wrangler-test.rs that translate the sample app, and the Bazel wiring for the embedded sample files all look correct.


⚡ Performance: ✅ LGTM

No performance concerns.


📝 Documentation: ✅ LGTM

The README's list of supported Wrangler fields now includes workflows. It says that only kv_namespaces and queues are errors, which matches check_experimental in wrangler.rs.


Reviewed commit: ee682fa3 — same changes as 5566ada2, so this is that commit's review (no new review was run; line numbers are as of 5566ada2)

@ry ry left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@piscisaureus
piscisaureus merged commit b690a1c into main Nov 25, 2026
38 of 39 checks passed
@piscisaureus
piscisaureus deleted the piscisaureus/cluster-k8s branch November 25, 2026 06:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants