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.
Kubernetes has become the most popular DevOps tools in the industry. From startups to enterprises, everyone is leveraging the power of containerization by...
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.
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.
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.
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!
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.
SQL Polyglot, our new groundbreaking AI-powered solution, significantly accelerates and simplifies complex database migrations, helping you minimize technical debt. Discover how it can reduce your migration time and costs by automating the translation of stored procedures.
Learn how to optimize website performance by converting images to WebP format using Amazon CloudFront and S3, improving load times and user experience.
Explore how we helped a education-focused technology company modernize their cloud infrastructure and data processes.