Cloud Development Kit for Kubernetes (cdk8s)

Cloud Development Kit for Kubernetes is an experimental project built by AWS but run as an open source project. The goal of this project is to use familiar programming languages instead of configuration standards, to create Kubernetes YAML config files.

2 months ago

Latest Post Cloud Development Kit for Kubernetes (cdk8s) by Tyler Moon

Over the past few years the explosion of Docker containers has lead to the need of advanced organizational frameworks for managing containers. The de-facto standard nowadays is Kubernetes (K8s) which was originally created by Google. The goal of K8s is to allow for the configuration of complex container ecosystems through a series of YAML files. This system is hands down better than manually running Docker commands to keep all of the needed systems up. However, it has now gotten to the point where the YAML systems are getting complex enough to need their own abstractions. This is where cdk8s comes into play from AWS developers.

Cloud Development Kit for Kubernetes (hereafter shorted to cdk8s) is an experimental project built by AWS but run as an open source project under an Apache 2 license. The goal of this project is to build a reusable abstration layer, combined with familiar programming languages instead of configuration standards, to create the needed YAML config files. There are currently two supported languages, TypeScript (strongly-typed JavaScript) or Python. In this article we will dive into using cdk8s to deploy a Ghost Docker container using the TypeScript version.

Prerequisities

Install

To install the cdk8s CLI use the following NPM command to install it globally:

npm install -g cdk8s-cli

The result should look something like the above screenshot. Thats all that is needed to install the CLI.

Initialize Project

The cdk8s CLI comes with a scaffolding init command which will build out the basic project structure and install the required node modules.

cdk8s init typescript-app

Since TypeScript is a compiled language it will need to be compiled into JavaScript before the CDK app can run. To do that continuously in the background run:

npm run watch

Then open another terminal window for use after we write some quick code.

Custom WebService Construct

In the cdk8s world, Constructs are the basic building blocks. They are the organizational units that combine different Kubernetes objects into useful groups. In this instance we will combine a Service and a Deployment in order to create a new instance of the Ghost blog web app.

Create a new file lib/web-service.ts and add the following TypeScript to it:

Note: After saving the file the terminal window that is running the npm run watch command should start showing that its compiling the .ts files into .js in the background

Then head over to the main.ts file and replace the contents with the following:

import { Construct } from 'constructs';
import { App, Chart } from 'cdk8s';
import { WebService } from './lib/web-service';

export class MyChart extends Chart {
  constructor(scope: Construct, name: string) {
    super(scope, name);

    new WebService(this, 'ghost', { image: 'ghost', containerPort: 2368 });
  }
}

const app = new App();
new MyChart(app, 'cdk8s_example');
app.synth();

This will create a new instance of our WebService, which in turn creates the Kubernetes Service and Deployment objects.

Run the following command to create the K8s YAML config file:

npm run synth

And now in the dist/cdk8sexample.k8s.yaml file there should be a config something like the following:

apiVersion: v1
kind: Service
metadata:
  name: cdk8sexample-ghost-service-920d83d1
spec:
  ports:
    - port: 80
      targetPort: 2368
  selector:
    app: cdk8sexampleghostBFE22175
  type: LoadBalancer
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: cdk8sexample-ghost-deployment-32ad0064
spec:
  replicas: 1
  selector:
    matchLabels:
      app: cdk8sexampleghostBFE22175
  template:
    metadata:
      labels:
        app: cdk8sexampleghostBFE22175
    spec:
      containers:
        - image: ghost
          name: app
          ports:
            - containerPort: 2368

Apply to K8s

The cdk8s project will compile to the YAML file but it will not apply that file to your Kubernetes cluster. Any K8s cluster can take this file but if you are using the microk8s local cluster use the following command to apply the newly created file:

micro8ks kubectl apply -f ./bin/cdk8sexample.k8s.yaml

Running the services command:

microk8s kubectl get services

Should result in a listing like the following:

Note the "LoadBalancer" and the CLUSTER-IP property next to it. After a few seconds the new Ghost blog should be available on your localhost at that IP address!

Summary

In this short article, we saw how to use the TypeScript strongly-typed programming language to generate Kubernetes configs using the cdk8s project from AWS. This is an experimental project with a lot of new features coming along but it looks very promising as a way of managing larger K8s projects.

Tyler Moon

Published 2 months ago