Home United States USA — software Write Your Kubernetes Infrastructure as Go Code – Combine CDK8s With AWS...

Write Your Kubernetes Infrastructure as Go Code – Combine CDK8s With AWS CDK

108
0
SHARE

Learn how to write your Kubernetes infrastructure as a Go code.
Join the DZone community and get the full member experience.
In an earlier blog post you saw how to use cdk8s with AWS Controllers for Kubernetes (also known as ACK), thanks to the fact that you can import existing Kubernetes Custom Resource Definitions using cdk8s! This made it possible to deploy DynamoDB along with a client application, by using cdk8s and Kubernetes.
But, what if you continue using AWS CDK for AWS infrastructure and harness the power cdk8s (and cdk8s-plus!) to define Kubernetes resources using regular code? Thanks to the native integration between the AWS EKS module and cdk8s, you can have the best of both worlds!
The goal of this blog post is to demonstrate that with a few examples. We will start off with a simple (nginx-based) example before moving on to a full-fledged application stack (including DynamoDB etc.). Both will be using the Go programming language which is well supported in AWS CDK as well as cdk8s.
All the code discussed in this blog is available in this GitHub repo
To follow along step-by-step, in addition to an AWS account, you will need the following CLIs – AWS CLI, cdk8s CLI and kubectl. Also, don’t forget to install AWS CDK, the Go programming language (v1.16 or above) as well as Docker, if you don’t have them already.
As with most things in life, there are two ways – the easy way or the hard way 😉 You will see both of them! Let’s try things out first, see them working and then look at the code.
To start off, clone the repo and change to the right directory:
To set up everything, all you need is a single command:
you can also use cdk synth to generate and inspect the Cloud Formation template first
You will be prompted to confirm. Once you do that, the process will kick off – it will take some time since lots of AWS resources will be created, including VPC, EKS cluster etc.
Feel free to check the AWS Cloud Formation console to track the progress.
Once the process is complete, you need to connect to the EKS cluster using kubectl. The command required for this will be available as a result of the cdk deploy process (in the terminal) or you can refer to the Outputs section of the AWS Cloud Formation stack.
Once you’ve configured kubectl to point to your EKS cluster, you can check the Nginx Deployment and Service.
You will see that two Deployments have been created – more on this soon. Similarly, if you check the Service (kubectl get svc), you should see two of them – nginx-service-cdk and nginx-service-cdk8s.
To access Nginx, pick  EXTERNAL-IP of any of the two Services. For example:
If you get a Could not resolve host error while accessing the LB URL, wait for a minute or so and re-try
Let’s look at the code now – this will clarify why we have two Nginx Deployments.

Continue reading...