CDK for Kubernetes (cdk8s)
This is the core library of Cloud Development Kit (CDK) for Kubernetes (cdk8s). cdk8s is an open-source software development framework for defining Kubernetes applications using rich object-oriented APIs in languages like Python, TypeScript, Java, and Go. cdk8s apps synthesize into standard Kubernetes manifests which can be applied to any Kubernetes cluster. The project is actively maintained, with frequent releases.
Warnings
- breaking Auto-generated resource names changed between v0.x and v1.0/v2.x. Deploying new manifests with different hashing could cause existing Kubernetes resources to be replaced, leading to downtime or unexpected behavior.
- gotcha The `cdk8s-cli` tool, essential for project initialization (`init`), generating API constructs (`import`), and synthesizing manifests (`synth`), is written in TypeScript and requires Node.js and npm to be installed globally on your system, even for Python projects.
- gotcha Kubernetes API objects (e.g., `KubeDeployment`, `KubeService`) are not directly available from the `cdk8s` library package. They are generated into a local `imports` directory by running the `cdk8s import k8s` command. This `imports` directory must be committed to your source control.
- breaking The `cdk8s-plus` library, which provides higher-level abstractions, is versioned per Kubernetes API version (e.g., `cdk8s-plus-34` for Kubernetes 1.34) and is only actively maintained for the three latest Kubernetes releases. Using a `cdk8s-plus` version incompatible with your target cluster can lead to invalid manifests.
- gotcha A significant portion of the official `cdk8s` documentation and examples are written for TypeScript. When working with Python, this can lead to confusion regarding property naming (TypeScript's camelCase vs. Python's snake_case) and API structure.
- breaking The deprecated API `Duration.toISOString()` has been removed. Use `Duration.toIsoString()` instead.
- breaking The `cdk8s-plus` library introduced several breaking changes in its 2.0.0 release, including removal of `service.addDeployment`, renaming `service.serve` to `service.bind`, and changing `container.addEnv` to `container.env.addVariable`.
Install
-
pip install cdk8s -
npm install -g cdk8s-cli
Imports
- App
from cdk8s import App
- Chart
from cdk8s import Chart
- Construct
from constructs import Construct
- KubeDeployment
from cdk8s import KubeDeployment
from imports import k8s
Quickstart
import os
from constructs import Construct
from cdk8s import App, Chart, IntOrString
from imports import k8s # This directory is generated by 'cdk8s import k8s'
class MyChart(Chart):
def __init__(self, scope: Construct, id: str):
super().__init__(scope, id)
# Define a Kubernetes Deployment
k8s.KubeDeployment(self, "my-deployment",
spec=k8s.DeploymentSpec(
replicas=1,
selector=k8s.LabelSelector(
match_labels={"app": "my-app"}
),
template=k8s.PodTemplateSpec(
metadata=k8s.ObjectMeta(
labels={"app": "my-app"}
),
spec=k8s.PodSpec(
containers=[k8s.Container(
name="my-container",
image="nginx:latest",
ports=[k8s.ContainerPort(container_port=80)]
)]
)
)
)
)
# Define a Kubernetes Service
k8s.KubeService(self, "my-service",
spec=k8s.ServiceSpec(
type="LoadBalancer",
ports=[k8s.ServicePort(port=80, target_port=IntOrString.from_number(80))],
selector={"app": "my-app"}
)
)
app = App()
MyChart(app, "my-cdk8s-app")
app.synth()