Deploy a Container Web App on Amazon EKS

GETTING STARTED GUIDE

Module 3: Create Your Deployment

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

Introduction

In this module, you will create service and deployment configurations for your application using AWS CDK8s. In AWS CDK8s, Kubernetes API Objects are represented as constructs. We can use those constructs to define services and deployments. The output generated by CDK8s is a Kubernetes config file. You will use the output of this module and integrate it with AWS CDK code from the previous modules to deploy your application.

What You Will Learn

  • Use CDK8s constructs to define Kubernetes API objects
  • Define services and deployments using CDK8s
  • Generate Kubernetes config using CDK8s

 Time to Complete

5 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

From this step, we are going to modify main.py to define CDK8s application. It should look like this after initializing:

#!/usr/bin/env python
from constructs import Construct
from cdk8s import App, Chart

class MyChart(Chart):
    def __init__(self, scope: Construct, id: str):
        super().__init__(scope, id)

        # define resources here

app = App()
MyChart(app, "cdk8s")

app.synth()

To deploy your application, there are 4 steps to follow:

  1. Import the k8s library to help generate the strings for the Yaml config file.
  2. Define the label set to apply to the pods to use in the service lookup.
  3. Define a deployment - this will spin up the container on the cluster, but not configure it to receive traffic from the load balancer.
  4. Define a service to expose the deployment to the load balancer.

Labels are key / value pairs that are attached to Kubernetes objects. You will tag the pods as part of the deployment to run them, and the ReplicationController will use the same labels to select the pods when creating the service to expose them to the loadbalancer. You will use the label = {"app": "cdk8s"} code to do this.

You will then use these labels in the deployment. A deployment in Kubernetes is an explicit declaration on deployment units and the desired state. This guide will use a sample application hosted at Amazon ECR Public Gallery. The URL for container image we are going to pull is public.ecr.aws/s9u7u6x1/sample app 001:no-db.

To create the deployment and service, update main.py by adding from imports import k8s at the top of the file under the other imports, and by pasting in the following code below # define resources here. Your file should look like this now:

#!/usr/bin/env python
from constructs import Construct
from cdk8s import App, Chart
from imports import k8s

class MyChart(Chart):
    def __init__(self, scope: Construct, id: str):
        super().__init__(scope, f"{id}-deployment")

        # define resources here

        # Label used for tagging pods to link in the service
        label = {"app": "cdk8s"}

        # Creates the deployment to spin up pods with your container
        k8s.KubeDeployment(self, 'deployment',
            spec=k8s.DeploymentSpec(
                replicas=2,
                selector=k8s.LabelSelector(match_labels=label),
                template=k8s.PodTemplateSpec(
                metadata=k8s.ObjectMeta(labels=label),
                spec=k8s.PodSpec(containers=[
                    k8s.Container(
                    name='cdk8s',
                    image='public.ecr.aws/s9u7u6x1/sample_app_001:no-db',
                    ports=[k8s.ContainerPort(container_port=80)])]))))

        # Creates the service to expose the pods to traffic from the loadbalancer
        super().__init__(scope, f"{id}-service")
        k8s.KubeService(self, 'service',
            spec=k8s.ServiceSpec(
            type='LoadBalancer',
            ports=[k8s.ServicePort(port=80, target_port=k8s.IntOrString.from_number(80))],
            selector=label))

app = App()
MyChart(app, "cdk8s")

app.synth()

You will notice that a call to super() is made twice in this method - it creates the Yaml file with the name specified and appends any config generated after that point by calling the parent class constructor that this class inherits from. The will generate a file for the service config, and a second file for the deployment. 

You can now run cdk8s synth to generate the config files, the output will look like this:

cdk8s synth

dist/cdk8s-deployment.k8s.yaml
dist/cdk8s-service.k8s.yaml

Conclusion

In this module, you learned how to generate Kubernetes config files using CDK8s. Your application is split into a deployment and a service, each with their own config file.

You are now ready to update the CDK project to reference these files and use kubectl to deploy them to the EKS cluster.

Up Next: Deploy with CDK

Was this page helpful?