Skip to content

hajowieland/cdk-py-k8s-the-real-hard-way-aws

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 

CDK Python - Kubernetes The (real) Hard Way on AWS!

This little project creates the infrastructure in CDK Python for my blog post Kubernetes The (real) 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
  • aws-cli Profile
  • AWS CDK (npm install -g cdk)
  • Python3

Features

Default values - you can adapt all of them to your needs

  • AWS CDK Python
  • 1x VPC, 3x Public Subnets, 3x Private Subnets, Route Tables, Routes
  • 3x Worker Nodes
  • 3x Master Nodes
  • 3x Etcd Nodes
  • 1x Bastion Host
  • Route53 Records for internal & external IPv4 addresses
  • 1x Public LoadBalancer for Master Nodes (external kubectl access)
  • 1x Private LoadBalancer for Master Nodes (fronting kube-apiservers)
  • 1x Public LoadBalancer for Bation Host (AutoScalingGroup)
  • Gets most recent Ubuntu AMI for all regions (via Boto3)
  • Install awscli, cfssl, cfssl_json via UserData
  • Allows external access from workstation IPv4 address only (to Bastion & MasterPublicLB)

Variables

Name Description Type Default
aws_account AWS account ID to deploy infrastructure string ''
aws_region AWS region string 'us-east-1'
bastion_desired_capacity Bastion ASG desired nodes int 1
bastion_instance_type Bastion EC2 instance type string 't3a.small'
bastion_min_capacity Bastion ASG min. nodes int 1
bastion_max_capacity Bastion ASG max. nodes int 1
etcd_desired_capacity etcd ASG desired nodes int 3
etcd_instance_type etcd EC2 instance type string 't3a.small'
etcd_min_capacity etcd ASG min. nodes int 3
etcd_max_capacity etcd ASG max. nodes int 3
master_desired_capacity K8s-Master ASG desired nodes int 3
master_instance_type K8s-Master EC2 instance type string 't3a.small'
master_min_capacity K8s-Master ASG min. nodes int 3
master_max_capacity K8s-Master ASG max. nodes int 3
worker_desired_capacity K8s-Worker ASG desired nodes int 3
worker_instance_type K8s-Worker EC2 instance type string 't3a.small'
worker_min_capacity K8s-Worker ASG min. nodes int 3
worker_max_capacity K8s-Worker ASG max. nodes int 3
ssh_key_pair AWS EC2 Key Pair name string ''
pod_cidr Pod CIDR network first octets (for POD_CIDR envvar) string '10.200'
tag_owner Owner Tag for all resources string 'napo.io'
tag_project Project Tag for all resources string 'k8s-the-real-hard-way-aws'
vpc_cidr AWS VPC network CIDR string '10.5.0.0/16'
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!