Skip to content
Python CDK code for "Kubernetes the (right) hard way (AWS)"
Python
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
cdk_python_k8s_right_way_aws
.gitignore
LICENSE
README.md
app.py
cdk-python-k8s-real-hard-way.png
cdk.json
requirements.txt
setup.py

README.md

CDK Python Kubernetes the (right) hard way on AWS!

This little project creates the infrastructure in CDK Python for my blog post Kubernetes the (right) hard way on AWS.

Terraform code available 🔗HERE

You can practice creating a multi node K8s Cluster yourself for training purposes or CKA exam preparation.

Alt text

Requirements

  • Existing AWS EC2 Key Pair
  • Existing AWS Route53 Public Hosted Zone

Features

  • AWS CDK Python
  • 1x VPC, 3x Public Subnets, Route Tables, Routes
  • 3x Worker Nodes (editable)
  • 3x Master Nodes (editable)
  • 3x Etcd Nodes (editable)
  • Route53 Records for internal & external IPv4 addresses
  • LoadBalancer for Master Node (external kubectl access)
  • Gets most recent Ubuntu AMI for all regions
  • Install awscli, cfssl, cfssl_json via UserData
  • Allows external access from workstation IPv4 address only

Variables

Name Description Type Default
aws_account AWS account ID to deploy infrastructure string ''
aws_region AWS region string 'us-east-1'
etcd_nodes Number of etcd nodes int 3
master_nodes Number of Kubernetes master nodes int 3
ssh_key_pair AWS EC2 Key Pair name string ''
vpc_cidr AWS VPC network CIDR string '10.5.0.0/16'
worker_nodes Number of Kubenretes worker nodes int 3
zone_fqdn AWS Route53 Hosted Zone name string ''

CDK Python Tutorial

The cdk.json file tells the CDK Toolkit how to execute your app.

This project is set up like a standard Python project. The initialization process also creates a virtualenv within this project, stored under the .env directory. To create the virtualenv it assumes that there is a python3 (or python for Windows) executable in your path with access to the venv package. If for any reason the automatic creation of the virtualenv fails, you can create the virtualenv manually.

To manually create a virtualenv on MacOS and Linux:

$ python3 -m venv .env

After the init process completes and the virtualenv is created, you can use the following step to activate your virtualenv.

$ source .env/bin/activate

If you are a Windows platform, you would activate the virtualenv like this:

% .env\Scripts\activate.bat

Once the virtualenv is activated, you can install the required dependencies.

$ pip install -r requirements.txt

At this point you can now synthesize the CloudFormation template for this code.

$ cdk synth

To add additional dependencies, for example other CDK libraries, just add them to your setup.py file and rerun the pip install -r requirements.txt command.

Useful commands

  • cdk ls list all stacks in the app
  • cdk synth emits the synthesized CloudFormation template
  • cdk deploy deploy this stack to your default AWS account/region
  • cdk diff compare deployed stack with current state
  • cdk docs open CDK documentation

Enjoy!

You can’t perform that action at this time.