Tatenda Magondo


CDK8s: A Simple Approach To Deploy Kubernetes Imperatively.

CDK8s: A Simple Approach To Deploy Kubernetes Imperatively.
Tatenda Magondo's photo
Tatenda Magondo

Published on Feb 25, 2021

4 min read

Cover Photo by Shaah Shahidh on Unsplash

Kubernetes has become almost the standard de facto of how we orchestrate, deploy and scale modern-day applications with containers. It's continuously evolving and bringing new shiny features, with the surge of cloud computing over the recent years, these technologies have blossomed together.

In 2019 AWS launched a new code-first approach to defining cloud application infrastructure named the Cloud Development Kit in short CDK which empowered developers to define and build infrastructure cloud using their favorite programming language and still enjoy the power of cloud formation as a provisioning engine in the background. That brought about a very promising tool to work with complex K8s clusters called CDK8s..... (CDK + k8s). CDK8s follows similar principles except it synthesis (cdk8s synth ) your code to a Kubernetes manifest that you're able to apply any k8s cluster whether locally or in the cloud.

In this brief article, I will be going through how to get started with cdk8s. I am going to be using python so I recommend you set up and hygienic python environment if you want to try it out or alternative fire up an AWS Cloud9 env and install pipenv if you don't have it set up already, it'll save a few headaches.

Side note for some reason I've found cdk8s to only work with Python 3.7.x I'm guessing what is supported at the moment, I'm not sure there isn't really much documentation yet to go about.

Getting started

1 . Install the cdk8s CLI

$ npm install -g cdk8s-cli

2 . Create an empty directory and initialize your project in the empty dir I called my directory cdk8s-app, you can name yours anything.

          $ mkdir cdk8s-app &&  cd cdk8s-app

          $ cdk8s init python-app

Initializing a project from the python-app template

This is going to:

  1. Create a new project directory

  2. Install CDK8s as a dependency

  3. Import all Kubernetes API objects

Your folder should now contain the following.....

Screenshot from 2021-02-25 16-12-31.png

Github code for main.py

Inside the main.py file, you can see some python code with no Kubernetes resource defined in it. Edit this main.py file using constructs in CDK8s you can add resources and make it look like the below example.

carbon (2).png

At this point, you should run

$ cdk8s synth which should synthesize to a k8s manifest file store stored in dist/cdk8s-example.k8s.yaml

It should look like the something below

carbon (3).png

Github link for the yaml file

The manifest synthesized by your app is ready to be applied to any Kubernetes cluster using standard tools like kubectl apply:

$ kubectl apply -f dist/cdk8s-app.k8s.yaml

That's it, simple but complicated, let me know what you think.

LInk for All code snippets

Share this