Giter Site home page Giter Site logo

digihunch / kubelab Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 1.0 58 KB

AWS CDK Sample in Python: DIY a Kubernetes cluster with EC2 instance

Home Page: https://www.digihunch.com/2020/12/instance-initialization-with-aws-cdk-in-python/

Python 87.17% Shell 12.83%
kubernetes cdk kubespray python python3 cfn-init cdk-examples kubernetes-nodes infrastructure-as-code

kubelab's Introduction

KubeLab - Create VMs on AWS (with CDK) to self-host Kubernetes Cluster

While there is AKS, for learning at a closer distance it is beneficial to self-manage a cluster of K8s nodes, instead of a single node. This project provides Infrastructure as Code (AWS CDK in Python) to provision private instances on AWS, then provision the Kubernetes nodes with Kubespray.

Diagram

AWS CDK

AWS CDK is a development kit that can be used to generate CloudFormation template. It support a number of programming languages and significantly increase code reusability. To install or upgrade AWS CDK, you need npm:

npm install -g aws-cdk

npm update -g aws-cdk

To initialize a project, e.g. in Python, create an empty directory with project name(e.g. kubelab). Then run the following from within the directory:

ckd init app --language python

To re-create virtual environment in existing project directory, run:

python3 -m venv .venv

In order to not mess up existing Python environment, we will use virtual environment for the project. To start virtual environment, run:

source .venv/bin/activate

Within virtual environment, when we run the project for the first time after initialization, we need to install the pip3 packages as listed in requirements.txt. Make sure to include required packages for the project such as aws_cdk.aws_autoscaling in the txt. To install, run:

pip install -r requirements.txt

We can load the code in Visual Studio code from within the directory:

code .

We can now validate syntax with cdk, by running:

cdk ls

To actually deploy a stack, we can run:

cdk deploy vpc-stack

cdk deploy security-stack

cdk deploy bastion-stack --require-approval=never

cdk deploy private-stack --require-approval=never

The cluster

The kube-cdk directory includes the CDK files required to create Cloudformation stacks. The VCP stack provisions the public and private networks. The security stack includes security groups required for each instance. For example, on the private instances, it opens the ports that are required by Kubernetes Master and nodes, to the entire VPC. The bastion stack includes the bastion host, to be placed in public subnet. During creation of bastion host, it generates a new RSA key pair and ec2-user will use the newly generated private key. With the public key registered with AWS and used during the creation of other instances. It is expected that once you SSH to the bastion host, you can connect to any other instance with key authentication.

Kubespray

Kubespray is a project to simplify the configuration of Kubernetes nodes. The configuration files are hosted in this repository, and this is a good instruction. By default, three EC2 instances (t2.small) will be created in private subnet to serve as Kubernetes cluster. The existing environment needs to meet the requirement as listed the documentation. I created a little helper script (kube-helper.sh) to smooth out this step. The helper script looks up on the autoscaling group for private instances, with IP address returned, the script uses the tool provided in Kubespray to configure the Ansible inventory.

/home/ec2-user/kubespray/kube-helper.sh

The helper script will provide the command to run at the end, which is:

ansible-playbook cluster.yml -b -v

This will kick of the playbooks required for configuration on the target hosts. The configuration process should take about 10 minutes. There is also an illustrated, but kind of out-dated instruction here.

After the kubespray work, you need to copy configuration file:

ansible-playbook setup-kubectl-local.yml -v

Then you can test with kubectl commands.

kubectl cluster-info

kubectl get nodes

kubectl get pods

kubelab's People

Contributors

digihunch avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

mansong1

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.