# 🌐 (Day-01) Set Up a Kubernetes Cluster on AWS Using Kops

### What is a Kubernetes (K8s) Cluster? 🛠️

A **Kubernetes cluster** is a group of machines (nodes) that work together to run and manage containerized applications.

* **Nodes**: Machines (physical or virtual) that make up the cluster, consisting of:
    
    * **Master Node**: Manages and controls the cluster, scheduling and monitoring workloads.
        
    * **Worker Nodes**: Run the actual applications and services.
        
* **Pods**: The smallest unit in Kubernetes. A pod can hold one or more containers sharing the same resources (network, storage).
    
* **Containers**: Lightweight, standalone packages that include everything needed to run an application (code, runtime, libraries).
    
* **Services**: Expose applications to make them accessible within and outside the cluster.
    
    ### Why Use a Kubernetes Cluster? 🚀
    
    * **Scalability**: Automatically adjusts the number of running applications based on demand.
        
    * **Resilience**: Automatically restarts or replaces failed containers or nodes to keep apps running.
        
    * **Management**: Simplifies deployment and management of containerized applications.
        

### Common Kubernetes Cluster Installation Methods🔧

* **Minikube**: Ideal for local development & testing, it creates a single-node cluster on your local machine.
    
* **Kubeadm**: A tool for setting up Kubernetes clusters on your own infrastructure but requires more manual setup and configuration.
    
* **Kops**: A tool to create, destroy, upgrade, and maintain Kubernetes clusters on AWS
    

### Managed Kubernetes Services:

* **Amazon EKS**: Fully managed Kubernetes on AWS.
    
* **Google GKE**: Managed Kubernetes on Google Cloud.
    
* **Azure AKS**: Managed Kubernetes on Azure.
    

## Why Kops? 🤔

Kops (Kubernetes Operations) is a CLI tool that helps automate the creation, management, and maintenance of production-grade Kubernetes clusters.

### Key Features of Kops

* **Cluster Setup**: Automates the creation of a highly-available Kubernetes cluster, handling everything from instance provisioning to cluster configuration.
    
* **Configuration Management**: Manages cluster configurations including networking setups, IAM roles, and instance types.
    
* **Auto-Scaling**: Supports auto-scaling for Kubernetes worker nodes based on resource demands.
    
* **Upgrade and Rollback**: Provides mechanisms to upgrade or rollback Kubernetes clusters easily.
    
* **Auto-Healing**: Automatically replaces failed nodes or instances to maintain high availability
    

### How Kops Works 🛠️

I used a script to automate the entire process of setting up a Kubernetes cluster using Kops. Here’s a simplified breakdown:

1. **Run the Script**: Execute `./`[`script.sh`](http://script.sh) to handle the setup process.
    
2. **Automated Setup**:
    
    * **Cluster Creation**: Kops sets up the cluster, provisioning EC2 instances for master and worker nodes.
        
    * **Networking Configuration**: Configures necessary networking settings.
        
    * **S3 Bucket Setup**: Creates an S3 bucket to store the cluster state.
        
    * **Finalization**: Configures the Kubernetes control plane, validates the cluster, and sets up the Kubernetes Dashboard.
        

### When to Use Kops ⏰

* **Setting up a Kubernetes cluster on AWS**: Kops is a preferred tool for its automation and adherence to best practices.
    
* **For other environments**: For cloud-agnostic setups or flexible setups on Google Cloud or Azure, other tools like Kubeadm or Terraform may be used.
    

### Task 1: Launching an EC2 Instance ☁️

* **Create an EC2 Instance**:
    
    * **Ubuntu Server 22.04 LTS (HVM)**
        
    * **SSD Volume type**: t2.micro
        

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726306476866/32726568-d1f8-4f01-abe7-e709d3ef88e6.png align="center")

### Task 2: Create an IAM Role for Your Instance 🔐

* **Create an IAM Role**: `kops-admin-role` with `AdministratorAccess` policy.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726306782979/975c97ee-8d01-4121-984e-bca0f1d72499.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726306841880/ae4e12be-6405-46dd-9c18-1dd359f7c8a5.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726307093452/a66611fd-2b4e-406d-aa09-3b7dfa1730f2.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726307105954/a1874a1f-41cb-4cc4-9d0c-21bef6e11b53.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726307116125/b2194c38-cf09-42af-b2a9-882e45ebc9e3.png align="left")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726307146936/a68c6f12-1e33-4f5d-9131-dc718b053697.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726307160886/05b1e991-472b-496d-bb72-f1c8c02f0f86.png align="left")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726307195088/2485627c-eee3-4058-a262-ae5092a1c8e9.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726307206981/8041796f-b90e-4247-b562-ff32276c25c4.png align="left")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726307232549/a1778183-99ab-40a4-b176-28055bf495ba.png align="center")

**Attach IAM Role**:

* Select EC2 instance `Kubernetes Cluster`.
    
* Go to Action &gt; Security &gt; Modify IAM role.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726307331610/70a9fa63-4d41-40fe-a43d-23139b4a18c7.png align="center")

* Select `kops-admin-role` and click Update IAM role.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726307363166/3ac27ddf-a392-4c6f-9718-0c9dbbe448d4.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726307475466/669eec0a-9875-4cbc-87bc-30d2ed590eae.png align="center")

Before running the `./kops` script, ensure you install AWS CLI and Configure AWS CLI. These steps are crucial for provisioning resources in AWS when executing the Kops script.

### **Install AWS CLI and Configure AWS CLI**:

**Install AWS CLI**

**Update Your Package List:**

```bash
sudo apt-get update
```

**Install Required Packages**

```bash
sudo apt-get install -y unzip
```

**Download the AWS CLI v2 Installer:**

This command downloads the AWS CLI v2 installer package as a ZIP file to your current directory.

```bash
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
```

**Unzip the AWS CLI Installer**

This command extracts the contents of the [`awscliv2.zip`](http://awscliv2.zip) file. It will create a directory with the installation files.

```bash
unzip awscliv2.zip
```

**Run the AWS CLI Installer:**

This command runs the installation script for AWS CLI v2.

```bash
sudo ./aws/install
```

**Verify the Installation :**

```bash
aws --version
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726335355443/be4c6f2c-b916-4de0-b3a8-bb7fd1891b2b.png align="center")

### Configure AWS CLI

```bash
aws configure
```

You will be prompted to enter your

* **AWS Access Key ID:** Enter your access key
    
* **AWS Secret Access Key:** Enter your secret access key
    
* **Default region name**: Enter your desired region (e.g., `us-west-2`)
    
* **Default output format**: Set to `json`, `text`, or `table`(e.g., `json`)
    

**Test the Configuration:** Run a simple command to verify that the AWS CLI is correctly configured:

```bash
aws s3 ls
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726336510711/a51cdfac-6303-4205-969c-7100a8277533.png align="center")

### Creating Access Key and Secret Access Key in AWS:

1. Sign in to the **AWS Management Console**.
    
2. Navigate to **IAM (Identity and Access Management).**
    
3. On the left, **click Users and select or click on your username**.
    
4. Go to the **Security credentials tab.**
    
5. Scroll to **Access keys and click Create access key**.
    
6. Download or **copy the Access Key ID and Secret Access Key**.
    

## Task 3: Setting Up a Kubernetes Cluster 🛠️

```bash
sudo hostnamectl set-hostname kops
bash
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726307854392/db3fe19e-95ef-4dfe-bace-7751319249f9.png align="left")

**Create a shell script file and copy the content from the** [**GitHub repo**](https://github.com/Sandhyagito/k8s-kops-cluster-setup/blob/main/README.md)**.**

```bash
vi kops.sh
```

**Execute the Kops script:**

```bash
. ./kops.sh
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726336278082/7974f24a-f4df-489e-9a88-2da06cea5cb9.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726337079590/4c1ea933-7c1b-4233-aeab-2ebaba857203.png align="center")

**Retrieve information about existing clusters:**

```bash
kops get cluster
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726337046945/1bd1a329-3121-4dbf-be11-d6d3dd72b0bb.png align="left")

**Get information about Kubernetes nodes:**

```bash
kubectl get nodes
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726337104099/7d86c40d-b19b-469c-931b-3d244424a3b2.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726338372801/1b523820-77b2-446c-938a-4d8e6bd4031a.png align="center")

When you run the `./kops` script, three key AWS resources are created to support the Kubernetes cluster:

**S3 Bucket**: Stores the cluster's desired state (track any changes), configuration, and secrets.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726338623437/b8a7ff03-6295-4dfd-9d61-1833594956b7.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726339042015/839d55d6-cfa5-445f-88fb-357d2308289b.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726338666332/301deecf-f8dd-4347-bcc7-055ddebd0d3d.png align="center")

**Auto Scaling Groups (ASGs)**

Two auto-scaling groups are typically created.

* **Master (Control Plane) Auto Scaling Group**: Manages the control plane nodes (<mark>cluster management, API server, and etcd (cluster data)</mark>).
    
* **Worker Node Auto Scaling Group:** Manages the worker nodes, where your <mark>containerized applications are deployed.</mark>
    

**Scaling**: These groups ensure that nodes can automatically scale up or down based on the load or manually set limits.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726338466091/c1fe85a9-d734-4e1c-8260-97d482b89025.png align="center")

**Load Balancer**: An Elastic Load Balancer (ELB) is created to balance traffic between the master nodes. It handles requests to the Kubernetes API server. This ensures high availability for the control plane by routing traffic to healthy master nodes.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726338578417/46066dc8-d6f9-4bc9-bf66-d317a04a8290.png align="center")

### Command to Retrieve Kubernetes Configuration

If you've downscaled and then scaled up your Kubernetes cluster, or if you've restarted your Kubernetes cluster, you might need to update your local Kubernetes configuration to reflect the changes.

It ensures that your <mark>local configuration is updated with the latest cluster details after scaling or restarting your nodes.</mark>

```bash
kops export kubeconfig --admin
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726478423032/8513dfc4-7852-4875-98f1-22d2ef8eb4c9.png align="center")

### To delete the cluster

```bash
kops delete cluster --name <clustername> --state s3://<clustername>
```

## Conclusion 🎯

Setting up a Kubernetes cluster on AWS using Kops simplifies the deployment and management of production-grade clusters, offering robust automation and flexibility. By leveraging Kops, you can efficiently handle cluster creation, configuration, and scaling while adhering to best practices.

With Kops, you automate the setup process, manage configurations, and ensure high availability with auto-healing and scaling capabilities. Whether you’re launching a new cluster or maintaining an existing one, Kops streamlines these tasks, making it an invaluable tool for managing Kubernetes on AWS.

Stay tuned for more in-depth tutorials and best practices on Kubernetes and cloud-native technologies! 🚀
