Deploy a Container Web App on Amazon EKS

GETTING STARTED GUIDE

Module 4: Deployment with AWS CDK

In this module, you will create a deployment using CDK and lauch your application

Introduction

In this module, we will deploy an application into you newly created EKS cluster using the configuration files generated by CDK8s from previous module. These config files will be used by CDK to deploy the application.

What You Will Learn

  • Integrate AWS CDK8S into AWS CDK
  • Deployment using AWS CDK
  • Operating the cluster with Kubectl

 Time to Complete

10 minutes

 Module Prereqs

  • AWS Account with administrator-level access**
  • Recommended browser: The latest version of Chrome or Firefox

[**]Accounts created within the past 24 hours might not yet have access to the services required for this tutorial.

Implementation

Define Application

The Kubernetes config files are stored in Yaml format, so you will need to import the yaml library to be able to parse them. Add import yaml under the other imports at the top of eks/cluster/cluster/cluster_stack

You will then add 2 blocks of code to read the 2 Yaml files into variables to pass to the cluster:

Note: please mind the Indentation of the code

        # Read the deployment config
        with open("../cdk8s/dist/cdk8s-deployment.k8s.yaml", 'r') as stream:
             deployment_yaml = yaml.load(stream, Loader=yaml.FullLoader)

        # Read the service config
        with open("../cdk8s/dist/cdk8s-service.k8s.yaml", 'r') as stream:
             service_yaml = yaml.load(stream, Loader=yaml.FullLoader)

You will then pass this Yaml to the EKS cluster using the add_manifest method, once for each file:

Note: please mind the Indentation of the code

        eks_cluster.add_manifest(f"{construct_id}-app-deployment", deployment_yaml)
        
        eks_cluster.add_manifest(f"{construct_id}-app-service", service_yaml)

Your cluster_stack.py file should now look like this:

from aws_cdk import (
    Stack,
    aws_iam as iam,
    aws_eks as eks,
    aws_ec2 as ec2
    
)
from aws_cdk.lambda_layer_kubectl_v28 import KubectlV28Layer
from constructs import Construct
import yaml

class ClusterStack(Stack):

    def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        # Create a master role 
        iam_role = iam.Role(self, id=f"{construct_id}-iam",
                    role_name=f"{construct_id}-iam", assumed_by=iam.AccountRootPrincipal())
         
         # Create and EKS Cluster 
        eks_cluster = eks.Cluster(
            self, id=f"{construct_id}-cluster",
            cluster_name=f"{construct_id}-cluster", 
            masters_role=iam_role,
            default_capacity_instance=ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE3, ec2.InstanceSize.MICRO),
            version=eks.KubernetesVersion.V1_28,
            kubectl_layer=KubectlV28Layer(self, "KubectlLayer")
        )
        
        # Read the deployment config
        with open("../cdk8s/dist/cdk8s-deployment.k8s.yaml", 'r') as stream:
             deployment_yaml = yaml.load(stream, Loader=yaml.FullLoader)

        # Read the service config
        with open("../cdk8s/dist/cdk8s-service.k8s.yaml", 'r') as stream:
             service_yaml = yaml.load(stream, Loader=yaml.FullLoader)

        eks_cluster.add_manifest(f"{construct_id}-app-deployment", deployment_yaml)

        eks_cluster.add_manifest(f"{construct_id}-app-service", service_yaml)      

To deploy the application, we need to switch back to the eks/cluster directory. If you are currently in the eks/cdk8s directory, run the following (otherwise change into eks/cluster):

cd ../cluster
cdk deploy

Once that AWS CDK deployed the application stack, we can check the pods statuses with kubectl. Before using kubectl, make sure you're using the right context using kubectl config current-context.

To see the status of the application in the pods, we can use kubectl get all command and we will see the following results:

$ kubectl get all
NAME                                             READY   STATUS    RESTARTS   AGE
pod/cdk8s-deployment-c8087a1b-659fb88f56-6qj52   1/1     Running   0          3m22s
pod/cdk8s-deployment-c8087a1b-659fb88f56-s82ft   1/1     Running   0          3m22s

NAME                             TYPE           CLUSTER-IP      EXTERNAL-IP                                                                    PORT(S)        AGE
service/cdk8s-service-c844e1e1   LoadBalancer   xxx.xxx.xxx.xxx   xxxxxxxxxxxxxx.eu-west-1.elb.amazonaws.com   80:30132/TCP   3m22s
service/kubernetes               ClusterIP      xxx.xxx.xxx.xxx                                                                               443/TCP        4h17m

NAME                                        READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/cdk8s-deployment-c8087a1b   2/2     2            2           3m22s

NAME                                                   DESIRED   CURRENT   READY   AGE
replicaset.apps/cdk8s-deployment-c8087a1b-659fb88f56   2         2         2       3m22s

To test the application, you can copy the EXTERNAL-IP from the LoadBalancer value above and open it in your browser - it looks like xxxxxxxxxxxxxx.eu-west-1.elb.amazonaws.com

Once loaded, you should see this page:

You have successfully deployed an application into a Kubernetes cluster using CDK to provision the cluster and deploy the manifest files generated by CDK8s.

Conclusion

In this guide, you learned how to use the Kubernetes config files generated by CDK8s and deploy them to your EKS cluster using CDK. In the next module, you will clean up all the resources created in this guide.

Up Next: Clean-up Resources

Was this page helpful?