Skip to Content
Alzo

packages/config/src/examples/cluster.config.ts

/** * Worked example — N peers that must know about each other. * * Type-checked local configuration/parity example. Loader and graph tests do not * certify an actual Kafka deployment or support on every execution target. * * Modelled on Apache Kafka's published KRaft cluster Compose file * (`docker/examples/docker-compose-files/cluster/isolated/plaintext/`). * * This is the shape a survey of ~40 real dev-environment configs found that * **no tool in the category expresses**. Every observed instance is one of: * *   - copy-paste, with the full voter list repeated in all N services, so *     changing N is an N-squared edit (Kafka's own file, CockroachDB, Redis *     Sentinel, protoactor-go); *   - external templating that generates the config file (AWX's Jinja loop, *     Nix, Tilt's Starlark `for`); *   - a `replicas: 3` construct that produces names appearing nowhere in the *     source, which consumers must then enumerate by hand — `nats-cluster-node-0`, *     `-1`, `-2` (process-compose). * * The reason it is hard everywhere else is that the peer list is *mutually* * recursive: broker 1's configuration contains broker 2's address and vice * versa, so no declaration order works. `forward()` exists for exactly this — * it hoists the address surface, and nothing else, so the cycle is in addresses * (which allocation resolves) rather than in liveness (which must stay acyclic). * * `CLUSTER_SIZE` is an ordinary constant. Changing it to 5 is a one-character * edit here and a rewrite in every file listed above. */import { defineConfig, defineService, forward } from 'alzo';import type { Node } from 'alzo';const CLUSTER_SIZE = 3;const IMAGE = 'apache/kafka:3.8.0';function graph(): Node[] {  // ---------------------------------------------------------------------------  // Controllers — the quorum  //  // Each controller needs the address of every controller including itself.  // `forward` gives us the addresses before any of them are defined.  // ---------------------------------------------------------------------------  const controllers = Array.from({ length: CLUSTER_SIZE }, (_, i) =>    forward(`controller-${i + 1}`, { controller: { port: 9093, internal: true } }),  );  // The voter string every controller and broker needs. A plain function, so it  // is evaluated after allocation — the same rule as `env` (D139).  const quorum = () =>    controllers.map((peer, i) => `${i + 1}@${peer.address.controller}`).join(',');  const controllerNodes = controllers.map((peer, i) =>    defineService({      name: peer,      group: 'controllers',      provision: { image: IMAGE },      env: () => ({        KAFKA_NODE_ID: i + 1,        KAFKA_PROCESS_ROLES: 'controller',        KAFKA_CONTROLLER_QUORUM_VOTERS: quorum(),        KAFKA_LISTENERS: 'CONTROLLER://:9093',        KAFKA_CONTROLLER_LISTENER_NAMES: 'CONTROLLER',        CLUSTER_ID: '4L6g3nShT-eMCtK--X86sw',      }),      ready: { tcp: { on: 'controller' }, period: '5s', failureThreshold: 12 },      restart: { policy: 'always', minUptime: '15s', startupDelay: '30s' },    }),  );  // ---------------------------------------------------------------------------  // Brokers  //  // Note what is NOT here: no wait-for-quorum script, no `depends_on` on a  // hand-rolled join node. `dependsOn` already means ready (D6), and a list of  // handles is an ordinary array.  // ---------------------------------------------------------------------------  const brokers = Array.from({ length: CLUSTER_SIZE }, (_, i) =>    defineService({      name: `broker-${i + 1}`,      ports: { broker: 9092 },      group: 'brokers',      provision: { image: IMAGE },      env: () => ({        KAFKA_NODE_ID: CLUSTER_SIZE + i + 1,        KAFKA_PROCESS_ROLES: 'broker',        KAFKA_CONTROLLER_QUORUM_VOTERS: quorum(),        KAFKA_CONTROLLER_LISTENER_NAMES: 'CONTROLLER',        CLUSTER_ID: '4L6g3nShT-eMCtK--X86sw',      }),      dependsOn: controllerNodes.map((c) => c.available),      ready: { tcp: { on: 'broker' }, period: '5s', failureThreshold: 12 },      restart: { policy: 'always', minUptime: '15s', startupDelay: '30s' },    }),  );  return [...controllerNodes, ...brokers];}export default defineConfig({  name: 'kafka-cluster',  requires: [{ command: 'docker', hint: 'Install Docker Desktop' }],  nodes: graph,});