CDK8S for Kubernetes: How to Interact With Kubernetes Without Kubectl
A simple way to work with K8s.
Join the DZone community and get the full member experience.Join For Free
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 that 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?
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
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!
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.
Published at DZone with permission of Juan Ignacio Giro. See the original article here.
Opinions expressed by DZone contributors are their own.