🚀 Unleashing the Power of Cloud Magic: Transforming a Lone AWS EC2 Instance into a K8s Powerhouse! 🌐🔥
Tags: cloudnative, aws, devops, kubernetes
Published: 2024-02-07 09:27:55


### Table of Content:

0. Prequisite
1. [Introduction](#Introduction)
2. [SSH into EC2](#ssh-into-ec2)
3. [Install Docker](#install-docker)
4. [Install Kubectl](#install-kubectl)
5. [Install KIND](#install-kind)
6. [Setup Kubernetes Cluster](#setup-kubernetes-cluster)
7. [Setup Visualizer (KubeOps View)](#setup-visualizer)


## Perquisite
1. EC2 instance running Amazon Linux 2023 AMI - How to [Video](https://www.youtube.com/watch?v=V1lyXkDSakk), [Doc](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EC2_GetStarted.html#ec2-launch-instance)
2. Available private key pair for the instance

## Introduction

Welcome to the realm of cloud enchantment! In this captivating journey, we will delve into the art of transforming a solitary AWS EC2 instance into a formidable Kubernetes (K8s) powerhouse. Brace yourself as we unravel the secrets of cloud magic, unlocking the potential of your EC2 instance to orchestrate a dynamic Kubernetes cluster. With a touch of innovation and a dash of determination, you'll soon wield the power of the cloud like never before.


## SSH into EC2

SSH stands for "Secure Shell." It is a cryptographic network protocol used for securely connecting to a remote server or device over an unsecured network. 

To connect via SSH to the virtual Machine (EC2), you would need a secure shell client like [Putty](https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html) or [MobaXterm](https://mobaxterm.mobatek.net/download.html) or just your plain terminal.

I would be using [Tabby Terminal](https://tabby.sh/).

- Open the terminal and navigate to the directory were you downloaded the **EC2 Key Pair **.

In my case its 
```
cd ~/Downloads/
```

- Run this command, if necessary, to ensure your key is not publicly viewable.

```
chmod 400 "your-Key-pair-file.pem"
```

- Connect to your instance using its Public DNS
For example 

```
ssh -i "your-Key-pair-file.pem" ec2-user@ec2-your-ip.compute-1.amazonaws.com
```

Once you are connected to the instance, you should see a welcome screen like the below.


![Amazon Linux Welcome Screen](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ieydb1arp1ylhrbuats9.jpg)

## Install Docker

![docker logo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i9dwascu5q270akb8qi3.png)

Amazon Linux 2023 uses `dnf` as its package manager.

### 1. Update AL2023 Packages
Since its a new linux VM, run the below command to perform an update.

```
sudo dnf update
```
This command is used to update the installed packages and package cache on a Fedora system.

### 2. Installing Docker on Amazon Linux 2023

```
sudo dnf install docker

```
The above installs the Docker Engine, the Docker command-line interface, and the containerd runtime.


![Install docker](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4f9bmi9ljl3203ctgxza.JPG) 

### 3. Start and Enable Docker Service
After installation docker services, don't start up by default, we have to manually start the process.

```
sudo systemctl start docker
```
Also, we want to set docker to automatically start with system boot

```
sudo systemctl enable docker
```

To be sure docker is currently running as expected, we need to check its status.

```
sudo systemctl status docker
```
You should have a similar result, like the image below.

![Docker status](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dlv27cr095omonwfccsl.JPG)

### 4. Enable Docker to run without requiring sudo
Once the installation is finished, it's cumbersome to use sudo every time you want to execute Docker commands. To alleviate this inconvenience, we need to include our current user in the Docker group. Utilize the provided command to accomplish this."

```
sudo usermod -aG docker $USER
```
apply the changes to the docker group
```
newgrp docker
```
To verify and check docker version

```
docker version
```
You should have data similar to the below image

![Docker version](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rvbpnz7aropgcfndrl61.JPG)


## Install Kubectl

![kubectl](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h0l593bz33rl9qhrx1z7.png)
`kubectl` is a command-line interface (CLI) tool used to interact with Kubernetes clusters. It allows users to perform various operations on Kubernetes resources, such as deploying applications, managing pods, services, and deployments, inspecting cluster resources, and debugging cluster issues.

> Note: You should ensure that the version of kubectl you use is within one minor version of your Kubernetes cluster. For instance, a client with version v1.29 can communicate effectively with control planes of versions v1.28, v1.29, and v1.30. Utilizing the most recent compatible kubectl version is essential to prevent unexpected complications.

#### 1. Install the kubectl binary on Linux using curl:
Download the latest release with the command:

```
     curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"

```

#### 2. Validate the binary (optional)
Download the kubectl checksum file:

```
   curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl.sha256"
```
Validate the kubectl binary against the checksum file:

```
echo "$(cat kubectl.sha256)  kubectl" | sha256sum --check
```
If valid, the output is:
`kubectl: OK`

#### 3. Install kubectl

```
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
```
Test to ensure the version you installed is up-to-date:

```
kubectl version --client
```
Or use this for detailed view of version:

```
kubectl version --client --output=yaml
```

## Install KIND

![Kind Logo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z6ndrukonvaiwxbfcwov.png)
We would be using KIND to create our kubernetes cluster.
**What is KIND ?**

In Kubernetes, "Kind" refers to Kubernetes in Docker. It is a tool for running local Kubernetes clusters using Docker container "nodes".
Its is a lightweight and easy-to-use Kubernetes environment for testing and development purposes. 

#### For AMD64 / x86_64

```
[ $(uname -m) = x86_64 ] && curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.21.0/kind-linux-amd64
chmod +x ./kind
sudo mv ./kind /usr/local/bin/kind
```

#### Confirm KIND is installed.

```
 kind --version
```
You should see the current version of KIND installed.

## Setup Kubernetes Cluster
in your terminal create a new file with the below command:

```
nano three-node-cluster.yml
```
Paste this code in the editor

```
# three node (two workers) cluster config
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
  extraPortMappings:
  - containerPort: 32000
    hostPort: 32000
    listenAddress: "0.0.0.0"
    protocol: tcp
  - containerPort: 32100
    hostPort: 32100
    listenAddress: "0.0.0.0"
    protocol: tcp
  - containerPort: 30000
    hostPort: 30000
    listenAddress: "0.0.0.0"
    protocol: tcp
  - containerPort: 30100
    hostPort: 30100
    listenAddress: "0.0.0.0"
    protocol: tcp
  - containerPort: 30200
    hostPort: 30200
    listenAddress: "0.0.0.0"
    protocol: tcp
  - containerPort: 30300
    hostPort: 30300
    listenAddress: "0.0.0.0"
    protocol: tcp
  - containerPort: 30400
    hostPort: 30400
    listenAddress: "0.0.0.0"
    protocol: tcp
- role: worker
  extraPortMappings:
  - containerPort: 80
    hostPort: 80
    listenAddress: "0.0.0.0"
    protocol: tcp
  - containerPort: 8000
    hostPort: 8000
    listenAddress: "0.0.0.0"
    protocol: tcp
  - containerPort: 8080
    hostPort: 8001
    listenAddress: "0.0.0.0"
    protocol: tcp

- role: worker
```
Use `Ctrl+x` To save the changes and exit editing.

**Now let's break down the configuration:**

**kind:** Specifies the kind of resource being defined, which is a Cluster in this case.
**apiVersion:** Specifies the version of the Kubernetes API being used.
**nodes:** Specifies the configuration for the nodes in the cluster.

#### The first node:
Defined is a control-plane node (role: control-plane). This node has extraPortMappings configured, which maps container ports to host ports. This is useful for accessing services running inside Kubernetes from outside the cluster. The listed container ports are mapped to the same host ports (32000, 32100, 30000, 30100, 30200, 30300, 30400) and listen on all available network interfaces (0.0.0.0) using the TCP protocol.

#### The second node (role: worker) 
Also has extraPortMappings configured. It maps container ports 80, 8000, and 8080 to host ports 80, 8000, and 8001 respectively. 

#### The last node 
is simply specified with role: worker, but it doesn't have any extraPortMappings configured.


#### Create the KIND Cluster

```
kind create cluster --config three-node-cluster.yml
```
Once its done, to get the cluster info

```
kubectl cluster-info --context kind-kind
```

![create KIND cluster](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/larbnb17g0ryrojt3y7j.JPG)

Get a list of the running nodes

```
 kubectl get nodes
```

![kubectl get nodes](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/12ngw0va1kta89lzlaht.JPG)

View all running pods across all namespaces

```
 kubectl get pods -A
```

![All pods running across all namespaces](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gjwovoy6b43x6kbiwo94.JPG)


## Setup Visualizer

![kubeops view logo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xrutvffwnlok9h8apbq6.png)
KubeOps View is a read-only system dashboard for multiple Kubernetes clusters, providing a common operational picture for understanding cluster setups in a visual way. It allows users to render nodes, indicate their overall status, show node capacity, and more.

#### 1. Install Git

```
sudo dnf install git
```
#### 2. Clone Git Repo

```
git clone https://github.com/UkemeSkywalker/kube-ops-view
```
#### 3. Apply kubeOps deployment
Navigate to the clone repository 

```
cd kube-ops-view/
```
Apply deployment 

```
kubectl apply -f deploy/
```
#### 4. Check Deployment

```
kubectl get pods
```

![kubectl get pods](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qmqmx8q7x55ih6wah7ks.JPG)

#### 5. Update EC2 security group inbound rules
In your Ec2 instance details page, scroll down and navigate to the security section.
Click on the default security group. It should take you to the dashboard.

![Ec2 security section](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/znumsix1oxp0eek5yni3.JPG)

Click on edit inbound rules, and add a new rule

| Type:   | Custom TCP       |
|---------|------------------|
| Port:   | 32000            |
| Source: | select _My IP_ |

Click on save rule. 


![Inbound Rule](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nnjnp22ytxeps261a7pn.JPG)

#### 6. Finally, Access the visualizer on your browser

```
http://your-ec2-pubic-ip:32000/#scale=2.0
```


![kube-ops view on Ec2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xpvrmdymnblktthkoqf3.png)

## Conclusion

As our adventure draws to a close, you now possess the knowledge and prowess to harness the full potential of your AWS EC2 instance. From the exhilarating setup of Docker and KIND to the orchestration of your very own Kubernetes cluster, you've embarked on a journey filled with discovery and empowerment. 

With KubeOps View offering a visual glimpse into your cloud domain, the possibilities are endless. Embrace the magic of the cloud, and may your Kubernetes endeavors continue to flourish in the ever-expanding landscape of technology. 

Until next time, may your clouds be clear and your clusters be mighty! ✨🌐🚀