Getting Started
Verified against the
cdk8sengine at commit558f788(npmcdk8s, latest releasev2.70.80). Commands assume Node.js 18 or newer and npm on a Unix-like shell.
Prerequisites
- Node.js 18+ and npm.
- A terminal. No Kubernetes cluster is needed to synthesize; you only need one when you apply the output.
Install
The official shortest path is the command line tool, which scaffolds a project and pulls in the cdk8s engine for you.
npm install -g cdk8s-cliA first working setup
This produces a manifest from TypeScript and writes it to disk.
Create and enter a project directory.
bashmkdir hello-cdk8s && cd hello-cdk8sScaffold a TypeScript app. This generates
main.ts, installs dependencies, and writes acdk8s.yamlconfig whoseoutputdirectory must match theAppoutdir.bashcdk8s init typescript-appReplace the body of
main.tsso the chart defines one resource. TheApp,Chart, andApiObjectclasses come fromcdk8s.typescriptimport { App, Chart, ApiObject } from 'cdk8s'; import { Construct } from 'constructs'; class MyChart extends Chart { constructor(scope: Construct, id: string) { super(scope, id); new ApiObject(this, 'configmap', { apiVersion: 'v1', kind: 'ConfigMap', data: { hello: 'world' }, }); } } const app = new App(); new MyChart(app, 'hello'); app.synth();Synthesize. The CLI compiles the code and runs the app, writing one file per chart into
dist/.bashcdk8s synth
Verify it works
List the output and inspect the generated manifest.
ls dist/
cat dist/hello.k8s.yamlYou should see a hello.k8s.yaml file whose content begins with the resource header, with apiVersion, kind, and metadata ordered first (the key ordering is enforced at src/api-object.ts:215):
apiVersion: v1
kind: ConfigMap
metadata:
name: hello-configmap-c87d4c10
data:
hello: worldThe exact name suffix is an 8 character hash derived from the construct address (src/names.ts:202), so it will differ in your output.
Where to go next
- Apply the synthesized manifest yourself:
kubectl apply -f dist/. CDK8s never talks to a cluster. - For typed workload classes (Pod, Deployment, Service) instead of raw
ApiObject, add thecdk8s-pluslibrary. - To generate typed constructs from CRDs or the Kubernetes API, use
cdk8s importfrom the CLI. - See the official documentation at cdk8s.io for production topics such as multi chart apps, resolvers, and importing Helm charts.