CDK8S for Kubernetes: How to interact with Kubernetes without KubeCTL

Migrations
Infrastructure & DevOps Modernization

Kubernetes has become the most popular DevOps tools in the industry. From startups to enterprises, everyone is leveraging the power of containerization by...

Introduction

Kubernetes has become the most popular DevOps tools in the industry. From startups to enterprises, everyone is leveraging the power of containerization by running their application in containers inside the Kubernetes cluster.

To work with Kubernetes, you need to create YAML files which contain all the cluster resources, services, deployments detail. YAML is human-readable and easy to learn, but if it becomes very difficult to work with YAML files when the cluster becomes complex. Adding or removing resources from a big YAML file is not an easy task.

What if we told you there is a solution to this issue? And moreover, that if you’re a developer, you are going to love this solution. The answer lies in CDK8s.

What is CDK8s?

CDK8s is an open-source cloud development kit for managing the Kubernetes clusters. In CDK8s, instead of using YAML files for managing the resources and applications in Kubernetes, you write codes for it using familiar programming languages (JavaScript, TypeScript, Python, etc.), which you would already know. Hence, it is developer-friendly.

You get all of the advantages of using a programming language that you are familiar with, like your ide, type hinting, auto-completion, less context switching, loops and iteration, shareable modules and classes. Testing all of this can help boost your productivity and prevent you from writing a ton of YAML lines. CDK8s will then take that code you write and generate the corresponding YAML for you, at which point you can then use your standard Kubernetes tools to create those resources inside your Kubernetes cluster.

CDK8s runs locally, but it can generate YAML, which can be deployed anywhere on a Kubernetes cluster. So, it maintains standardization across on-premises and any cloud provider. CDK8s was introduced by AWS, but it can be used by anybody with resources outside AWS also.

CDK8s Building Blocks

There are three main components of CDK8s – the core framework, the construct library and the command-line interface.

With the core framework, you can create and structure apps that contain one or multiple stacks. Stacks are logical units of infrastructure that contains multiple resources and mapped one-to-one to cloud formation stacks.

The construct library is a set of components crafted to create resources for a specific service. It can be a Kubernetes resource such as pod, or it can be a complete application also. This helps to decouple libraries and use only the dependencies that you need in your project. It is also built with best practices and security considerations to provide a good developer experience, ease of use and fast iteration cycles.

The CDK CLI helps to interact with the core framework to initialize project structure, inspect differences between deployments.

CDK8s Demo Example

Before we discuss how CDK8s works, here are some prerequisites that you need first. The prerequisite to run CDK8s is that you need to have nodejs and npm installed on your system. And then you can install CDK8s by a single command:

Now to initialize a project in CDK8s, you need to use cdk8s init command with a project template, for example, python-app or typescript-app. To show an example of CDK8s, we will create a directory and initialize a CDK8s application in it. We are using a typescript application in this example.

Initializing a project from the typescript-app template

The YAML file created at dist/cdk8s-example.k8s.yaml will be empty at this moment.

Now that the project is initialized, you can see many files have been imported to this directory depending on the project type we mentioned in the init command.

If we open the typescript application file main.ts, you can see a basic typescript code with no Kubernetes resource defined in it.

Modify the main.ts file and add Kubernetes resources to it. Using Constructs in CDK8s, you can add the resources using the typescript code mentioned below.

This is how the updated main.ts will look like:

Here you are creating a load balancer service and the deployment will have 3 replicas on the Kubernetes cluster.

Now that we have changed the code in the main.ts file, we need to compile it again.

After compiling it, we will run cdk8s synth command. Synth will create the Kubernetes YAML file from the code you have written.

Now, if we go back and check the YAML file inside the dist directory, it won’t be empty. A proper Kubernetes YAML file has been created with resources defined in it.

Let’s apply this to the Kubernetes cluster. We will run the kubectl apply command, which will create both service and deployment defined in the YAML file through the typescript code.

Run the command below to check if the service got created. We can see our service cdk8s-example-service-c8ca0070 has just now started.

Run the command below to check the deployment of the service. Since we had mentioned 3 replicas, you can see the service is running on 3 pods on the cluster.

Congratulations, you have successfully executed a CDK8s example!

Conclusion

CDK8s is a very promising tool to work with complex Kubernetes clusters. It is very easy for devs to work with this tool as you can easily write programs in the programming languages you know to create Kubernetes YAML files. But in case you have a small Kubernetes cluster, which is not a production cluster, you do not necessarily need to use CDK8s, you can simply work with YAML.

Caylent provides a critical DevOps-as-a-Service function to high growth companies looking for expert support with Kubernetes, cloud security, cloud infrastructure, and CI/CD pipelines. Our managed and consulting services are a more cost-effective option than hiring in-house, and we scale as your team and company grow. Check out some of the use cases, learn how we work with clients, and read more about our DevOps-as-a-Service offering.

Migrations
Infrastructure & DevOps Modernization
Mauricio Ashimine

Mauricio Ashimine

View Mauricio's articles

Learn more about the services mentioned

Caylent Services

Infrastructure & DevOps Modernization

Quickly establish an AWS presence that meets technical security framework guidance by establishing automated guardrails that ensure your environments remain compliant.

Accelerate your cloud native journey

Leveraging our deep experience and patterns

Get in touch

Related Blog Posts

Optimizing AWS Data Pipelines for Compliance in Digital Advertising

Learn how we helped an advertising customer setup automated, cost-effective pipelines to ensure compliance for sensitive data in their existing processes.

Infrastructure & DevOps Modernization

Best Practices for Migrating to Aurora MySQL

Aurora MySQL is a high-performance, fully managed database with Amazon RDS benefits, simplifying infrastructure for business focus. Learn migration best practices and essential components for a successful journey toward Aurora MySQL that can lead to increased scalability, resiliency, and cost-effectiveness.

Data Modernization & Analytics
Migrations

Stream Logs to OpenSearch via Kinesis

Data streaming eliminates the need to write custom applications for transferring data. Caylent’s Kennery Serain provides a reference architecture and code examples to showcase how to ingest data on OpenSearch using Kinesis Data Streams in near real-time.

Infrastructure & DevOps Modernization