# kubetwo
kubetwo is a simple CLI tool to create a Kubernetes cluster on AWS EC2 with just one command.

kubetwo will interact with Terraform and Ansible (Kubespray) for provisioning.

# Requirements
To use kubetwo, following tools are necessary.

 - [Python (3.6 and newer)](https://www.python.org/downloads/)
 - [Terraform (1.0.0 and newer)](https://learn.hashicorp.com/tutorials/terraform/install-cli)

# Quick Start
## 1. Prerequisites
First, please install `kubetwo` package using pip or pip3 depending on your environment.

```sh
pip install kubetwo
```

For kubetwo to interact with Terraform and create AWS resources, it's necessary to set environment variables about AWS credentials.

```sh
export AWS_ACCESS_KEY_ID="your_anaccesskey"
export AWS_SECRET_ACCESS_KEY="your_asecretkey"
export AWS_DEFAULT_REGION="your_region"
```

Then, you need to create ssh key to access the EC2. If you've already used ssh key, you can use it. 

```sh
ssh-keygen -t rsa -b 4096 -f ~/.ssh/kubetwo_id_rsa
```

## 2 Spin up Kubernetes cluster
By running the following `kubetwo create` command, kubetwo will spin up AWS EC2 with Terraform and set Kubernetes cluster with Ansible ([Kubespray v2.16.0](https://github.com/kubernetes-sigs/kubespray/tree/release-2.16)).

```sh
kubetwo create \
--cluster_name="kubetwo-cluster" \
--ssh_public_key="~/.ssh/kubetwo_id_rsa.pub" \
--ssh_private_key="~/.ssh/kubetwo_id_rsa" \
--availability_zone="ap-northeast-1a"
```

## 3. Access to the Kubernetes cluster
After the Kubernetes cluster is created, you can see admin.conf in the workspace.
Setting `KUBECONFIG` will allow you to access the Kubernetes cluster.

```sh
export KUBECONFIG=$(pwd)/kubetwo_cluster/admin.conf
```

Let's check the Kubernetes cluster from your local machine. (If you don't have `kubectl`, please install it)

```sh
kubectl get nodes
```

## 4. Clean up
You can clean up newly created resources with `kubetwo delete` command.
It will also delete workspace for kubetwo on your machine.

```sh
kubetwo delete \
--cluster_name="kubetwo-cluster"
```

# Commands

## kubetwo create
It will spin up AWS EC2 and create Kubernetes cluster.

### Parameters

| Name | Type | Default | Description |
| :---: | :---: | :---: | :--- |
| cluster_name | string | - | Kubernetes cluster name |
| ssh_public_key | string | - | Path of public key for SSH |
| ssh_private_key | string | - | Path of private key for SSH |
| ami | string | ami-0c3fd0f5d33134a76 | Amazon Machine Image of EC2 instance |
| instance_type | string | t3.medium | Instance type of EC2 instance |
| availability_zone | string | us-west-1a | Availability zone of EC2 instance |
| control_plane | int | 1 | Number of control planes |
| worker_node | int | 1 | Number of worker nodes |
| open_ports | array[string] | [-1] | Ports to open for global (If you use -1, all ports will be open) |
| approve | boolean | false | If true, kubetwo won't prompt you to approve for execution |

### Example
```sh
kubetwo create \
--cluster_name="kubetwo-cluster" \
--ssh_public_key="~/.ssh/kubetwo_id_rsa.pub" \
--ssh_private_key="~/.ssh/kubetwo_id_rsa" \
--availability_zone="ap-northeast-1a" \
--control_plane=1 \
--worker_node=1
```

## kubetwo scale
It will scale in/out worker nodes in Kubernetes cluster.

### Parameters

| Name | Type | Default | Description |
| :---: | :---: | :---: | :--- |
| cluster_name | string | - | Kubernetes cluster name |
| worker_node | int | - | Number of worker nodes |
| approve | boolean | false | If true, kubetwo won't prompt you to approve for execution |

### Example
```sh
kubetwo scale \
--cluster_name="kubetwo-cluster" \
--worker_node=2
```

## kubetwo delete
It will remove newly created AWS resources and Kubernetes cluster. And also clean up the workspace on local machine.

### Parameters

| Name | Type | Default | Description |
| :---: | :---: | :---: | :--- |
| cluster_name | string | - | Kubernetes cluster name |
| approve | boolean | false | If true, kubetwo won't prompt you to approve for execution |

### Example
```sh
kubetwo delete \
--cluster_name="kubetwo-cluster"
```
