Package: cdk8s-plus
cdk8s+ (cdk8s-plus)This library is in very early stages of development, as such, and in correspondence with a0.xsemantic major version line, itsAPIis
likely to rapidly change in breaking ways. It is therefore not recommended to use library for production workloads.cdk8s+is a software development framework that provides high level abstractions for authoring Kubernetes applications.
Built on top of the auto generated building blocks provided bycdk8s, this library includes a hand craftedconstructfor each native kubernetes object, exposing richer API's with reduced complexity.Kubernetes Speccdk8s+is currently built on top of version1.17.0of the kubernetes API specifications.
If you are deploying manifests produced bycdk8s+onto clusters of a lower version, you might encounter some unsupported spec properties or invalid manifests.SeeSupporting various k8s API specsfor more details and progress on this issue.Letter Of IntentWe strive to develop this library with full transparency and as much community feedback and contributions as possible.
To that end, we publish this development version. The lack of features/capabilities is intentional, we look forward to build and expand this framework with the help of the community.If you are interested in contributing, seeContribution Guide.At a glance# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826importcdk8s_plusaskplusimportcdk8sascdk8simportpathaspath# our cdk appapp=cdk8s.App()# our kuberentes chartchart=cdk8s.Chart(app,"Chart")# lets create a volume that contains our app.# we use a trick with a config map!app_data=kplus.ConfigMap(chart,"AppData")app_data.add_directory(path.join(__dirname,"app"))app_volume=kplus.Volume.from_config_map(app_data)# now we create a container that runs our appapp_path="/var/lib/app"port=80container=kplus.Container(image="node:14.4.0-alpine3.12",command=["node","index.js",f"{port}"],port=port,working_dir=app_path)# make the app accessible to the containercontainer.mount(app_path,app_volume)# now lets create a deployment to run a few instances of this containerdeployment=kplus.Deployment(chart,"Deployment",replicas=3,containers=[container])# finally, we expose the deployment as a load balancer service and make it rundeployment.expose(8080,service_type=kplus.ServiceType.LOAD_BALANCER)# we are done, synthapp.synth()apiVersion:v1data:index.js:|-var http = require('http');var port = process.argv[2];//create a server object:http.createServer(function (req, res) {res.write('Hello World!'); //write a response to the clientres.end(); //end the response}).listen(port); //the server object listens on port 80kind:ConfigMapmetadata:annotations:{}labels:{}name:chart-appdata-configmap-da4c63ab---apiVersion:apps/v1kind:Deploymentmetadata:annotations:{}labels:{}name:chart-deployment-pod-d4285cc9spec:replicas:3selector:matchLabels:cdk8s.deployment:ChartDeploymentCFC2E30Ctemplate:metadata:annotations:{}labels:cdk8s.deployment:ChartDeploymentCFC2E30Cspec:containers:-command:-node-index.js-"80"env:[]image:node:14.4.0-alpine3.12name:mainports:-containerPort:80volumeMounts:-mountPath:/var/lib/appname:configmap-chart-appdata-configmap-da4c63abworkingDir:/var/lib/appvolumes:-configMap:name:chart-appdata-configmap-da4c63abname:configmap-chart-appdata-configmap-da4c63ab---apiVersion:v1kind:Servicemetadata:annotations:{}labels:{}name:chart-deployment-service-pod-42f50c26spec:externalIPs:[]ports:-port:8080targetPort:80selector:cdk8s.deployment:ChartDeploymentCFC2E30Ctype:LoadBalancerInstallation and UsageWe currently support both Python and TypeScript/JavaScript. More languages are coming soon.We would love to hear which languages you want to see next:Languages SupportTypeScript/JavaScript❯ npm install cdk8s-plus cdk8s# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826importcdk8s_plusaskplusimportcdk8sascdk8sapp=cdk8s.App()chart=cdk8s.Chart(app,"Chart")kplus.Deployment(chart,"Deployment",replicas=3,containers=[kplus.Container(image="ubuntu")])Python❯ pip install cdk8s-plus cdk8simportcdk8s_plusaskplusimportcdk8sapp=cdk8s.App()chart=cdk8s.Chart(app,'Chart')kplus.Deployment(chart,'Deployment',replicas=1,containers=[kplus.Container(image='ubuntu')])In DepthFollowing are excerpts for the usage of everyconstructprovided by this library. It details the commonly used patterns and configuration properties.
In general, every such construct can be configured using two mechanisms:Spec Constructor PropertiesPost Instantiation Spec MutationsThe documentation presented here focuses on post instantiation mutations, however, every such mutation can also be pre-configured
using constructor properties for the corresponding spec. A complete API reference can be found inhere.ContainerDefine containers that run in a pod using theContainerclass.API Reference:ContainerEnvironment variablesEnvironment variables can be added to containers using various sources, via semantically explicit API's:# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826importcdk8s_plusaskpluscontainer=kplus.Container(image="my-app")# explicitly use a value.container.add_env("endpoint",kplus.EnvValue.from_value("value"))# use a specific key from a config map.backends_config=kplus.ConfigMap.from_config_map_name("backends")container.add_env("endpoint",kplus.EnvValue.from_config_map(backends_config,"endpoint"))# use a specific key from a secret.credentials=kplus.Secret.from_secret_name("credentials")container.add_env("password",kplus.EnvValue.from_secret(credentials,"password"))Volume MountsA very common capability is to mount a volume with some data onto a container. Using pure kubernetes API, this would require writing something like:kind:PodapiVersion:v1spec:containers:-name:mainvolumeMounts:-mountPath:/path/to/mountname:'config-volume'volumes:-name:'config-volume'configMap:name:'config'Notice the apparent redundancy of having to specify the volume name twice. Also, if you happen to need the same mount in other pods,
you would need to duplicate this configuration. This can get complex and cluttered very fast.In contrast, here is how to do this withcdk8s+:# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826importcdk8s_plusaskplusconfig=kplus.ConfigMap.from_config_map_name("config")volume=kplus.Volume.from_config_map(config)container=kplus.Container(image="my-app")# Cool alert: every pod that will later be configured with this container,# will automatically have access to this volume, so you don't need to explicitly add it to the pod spec!.container.mount("/path/to/mount",volume)ProbesAProbeis a diagnostic performed periodically by the kubelet on a Container. To
perform a diagnostic, the kubelet calls a Handler implemented by the container.AProbeinstance can be created through one of thefromXxxstatic methods:Probe.fromHttpGet()Probe.fromCommand()Readiness probes can be configured at the container-level through thereadinessoption:# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826kplus.Container(# ...readiness=kplus.Probe.from_http_get("/ping"))See the API reference for details.VolumeVolume represents a named volume in a pod that may be accessed by any container in the pod.API Reference:VolumeCreate from a ConfigMapA very useful operation is to create a volume from aConfigMap. Kubernetes will translate every key in the config map to a file,
who's content is the value of the key.# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826importcdk8s_plusaskplusconfig_map=kplus.ConfigMap.from_config_map_name("redis-config")config_volume=kplus.Volume.from_config_map(config_map)Create from an EmptyDirThe easiest way to allocate some persistent storage to your container is to create a volume from an empty directory.
This volume, as the name suggests, is initially empty, and can be written to by containers who mount it.
The data in the volume is preserved throughout the lifecycle of the pod, but is deleted forever as soon as the pod itself is removed.# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826importcdk8s_plusaskplusdata=kplus.Volume.from_empty_dir(config_map)redis=kplus.Container(image="redis")# mount to the redis container.redis.mount("/var/lib/redis",data)JobJobs are a very useful concept in kubernetes deployments.
They can be used for add-hoc provisioning tasks, as well as long running processing jobs.API Reference:JobIn configuration, they don't differ much from regular pods, but offer some additional properties.Delete a Job after its finishedYou can configure a TTL for the job after it finished its execution successfully.# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826importcdk8saskimportcdk8s_plusaskplusapp=k.App()chart=k.Chart(app,"Chart")# let's define a job spec, and set a 1 second TTL.load=kplus.Job(chart,"LoadData",ttl_after_finished=kplus.Duration.seconds(1))# now add a container to all the pods created by this jobjob.add_container(kplus.Container(image="loader"))ServiceUse services when you want to expose a set of pods using a stable network identity. They can also be used for externalizing
endpoints to clients outside of the kubernetes cluster.API Reference:ServiceSelectorsServices must be configured with selectors that tell it which pods should it serve.
The most common selector method is using labels.# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826importcdk8saskimportcdk8s_plusaskplusapp=k.App()chart=k.Chart(app,"Chart")frontends=kplus.Service(chart,"FrontEnds")# this will cause the service to select all pods with the 'run: frontend' label.frontends.select_by_label("run","frontend")PortsPorts that the service will listen and redirect to can be configured like so:# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826importcdk8saskimportcdk8s_plusaskplusapp=k.App()chart=k.Chart(app,"Chart")frontends=kplus.Service(chart,"FrontEnds")# make the service bind to port 9000 and redirect to port 80 on the associated containers.frontends.serve(port=9000,target_port=80)DeploymentCreate a deployment to govern the lifecycle and orchestration of a set of identical pods.API Reference:DeploymentAutomatic pod selectionWhen you specify pods in a deployment, you normally have to configure the appropriate labels and selectors to
make the deployment control the relevant pods. This construct does this automatically.# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826importcdk8saskimportcdk8s_plusaskplusapp=k.App()chart=k.Chart(app,"Chart")kplus.Deployment(chart,"FrontEnds",containers=[kplus.Container(image="node")])Note the resulting manifest contains a specialcdk8s.deploymentlabel that is applied to the pods, and is used as
the selector for the deployment.apiVersion:apps/v1kind:Deploymentmetadata:annotations:{}labels:{}name:chart-frontends-pod-a48e7f2espec:replicas:1selector:matchLabels:cdk8s.deployment:ChartFrontEndsDD8A97CEtemplate:metadata:annotations:{}labels:cdk8s.deployment:ChartFrontEndsDD8A97CEExposing via a serviceFollowing up on pod selection, you can also easily create a service that will select the pods relevant to the deployment.# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826# store the deployment to created in a constantfrontends=kplus.Deployment(chart,"FrontEnds")# create a ClusterIP service that listens on port 9000 and redirects to port 9000 on the containers.frontends.expose(9000)Notice the resulting manifest, will have the samecdk8s.deploymentmagic label as the selector.
This will cause the service to attach to the pods that were configured as part of the said deployment.apiVersion:v1kind:Servicemetadata:annotations:{}labels:{}name:chart-frontends-service-pod-1f70150bspec:externalIPs:[]ports:-port:9000selector:cdk8s.deployment:ChartFrontEndsDD8A97CEtype:ClusterIPConfigMapConfigMap are used to store configuration data. They provide a dictionary based data structure that can be consumed in
various shapes and forms.API Reference:ConfigMapUse an existingConfigMapYou can reference to an existingConfigMaplike so. Note that this does not create a new object,
and will therefore not be included in the resulting manifest.# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826importcdk8s_plusaskplusconfig=kplus.ConfigMap.from_config_map_name("config")# the 'config' constant can later be used by API's that require an IConfigMap.# for example when creating a volume.volume=kplus.Volume.from_config_map(config)Adding dataYou can create config maps and add some data to them like so:# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826importcdk8s_plusaskplusimportcdk8saskapp=k.App()chart=k.Chart(app,"Chart")config=kplus.ConfigMap(chart,"Config")()config.add_data("url","https://my-endpoint:8080")Creating a volume from a directoryHere is a nifty little trick you can use to create a volume that contains a directory on the client machine (machine that runscdk8s synth):# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826importcdk8s_plusaskplusimportcdk8saskimportpathaspathapp=k.App()chart=k.Chart(app,"Chart")app_map=kplus.ConfigMap(chart,"Config")()# add the files in the directory to the config map.# this will create a key for each file.# note that only top level files will be included, sub-directories are not yet supported.app_map.add_directory(path.join(__dirname,"app"))app_volume=kplus.Volume.from_config_map(app_map)# for here, just mount the volume to a container, and run your app!mount_path="/var/app"container=kplus.Container(image="node",command=["node","app.js"],working_dir=mount_path)container.mount(mount_path,app_volume)PodA pod is essentially a collection of containers. It is the most fundamental computation unit that can be provisioned.API Reference:PodAdding Containers/VolumesContainers and volumes can be added to pod definition like so:# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826importcdk8s_plusaskpluscontainer=kplus.Container(image="node")storage=kplus.Volume.from_empty_dir("storage")container.mount("/data",storage)app=k.App()chart=k.Chart(app,"Chart")pod=kplus.Pod(chart,"Pod")()# this will automatically add the volume as well.pod.add_container(container)# but if you want to explicitly add it, simply use:pod.add_volume(storage)Applying a restart policy# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826importcdk8s_plusaskplusapp=k.App()chart=k.Chart(app,"Chart")pod=kplus.Pod(chart,"Pod",restart_policy=kplus.RestartPolicy.NEVER)()Assigning a ServiceAccount# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826importcdk8s_plusaskplusapp=k.App()chart=k.Chart(app,"Chart")pod=kplus.Pod(chart,"Pod",service_account=kplus.ServiceAccount.from_service_account_name("aws"))()SecretSecrets are used to store confidential information. Never store such information on the definition of the pod itself.API Reference:SecretUse an existingSecretTo reference a secret created outside of your deployment definition, use the following. Note that this does not create a new object,
and will therefore not be included in the resulting manifest.# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826importcdk8s_plusaskplussecret=kplus.Secret.from_secret_name("aws-creds")Adding dataTo create a new secret with some data, use:# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826importcdk8s_plusaskplusimportcdk8saskapp=k.App()chart=k.Chart(app,"Chart")secret=kplus.Secret(chart,"Secret")secret.add_string_data("password","some-encrypted-data")ServiceAccountUse service accounts to provide an identity for pods.API Reference:ServiceAccountUse an existingServiceAccountTo reference a service account created outside of your deployment definition, use the following. Note that this does not create a new object,
and will therefore not be included in the resulting manifest.# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826importcdk8s_plusaskplusservice_account=kplus.ServiceAccount.from_service_account_name("aws-service")Allowing access to secretsTo create a new service account, and give it access to some secrets, use the following:# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826importcdk8s_plusaskplusimportcdk8saskapp=k.App()chart=k.Chart(app,"Chart")aws_creds=kplus.Secret.from_secret_name("aws-creds")aws_service=kplus.ServiceAccount(chart,"AWS")# give access to the aws creds secret.aws_service.add_secret(aws_creds)IngressIngressmanages external access to services in a cluster, typically through
HTTP. Ingress may provide load balancing, SSL termination and name-based virtual
hosting.You must have anIngress controllerto satisfy an Ingress. Only creating an
Ingress resource has no effect.API Reference:IngressThe following example will route HTTP requests sent to the/hellourl prefix
to a service associated with a deployment of thehashicorp/http-echoimage.# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826hello_deployment=kplus.Deployment(self,text,containers=[kplus.Container(image="hashicorp/http-echo",args=["-text","hello ingress"])])hello_service=hello_deployment.expose(5678)ingress=Ingress(self,"ingress")ingress.add_rule("/hello",kplus.IngressBackend.from_service(hello_service))You can useaddHostRule(host, path, backend)to define a route that will only
apply to requests with thisHostheader. This can be used to implement virtual
hosts.TheaddDefaultBackend(backend)andaddHostDefaultBackend(host, backend)methods can be used to define backends that will accept all requests that do not
match any other rules.The TCP port used to route requests to services will be determined based on
which ports are exposed by the service (e.g. throughserve()). If the service
exposes multiple ports, then a port must be specified viaIngressBackend.fromService(service, { port }.