# Creating a Kubernetes Cluster on AWS using kops

Creating a Kubernetes cluster on AWS using **kops** (Kubernetes Operations) is a popular method due to its simplicity and powerful feature set. Below is a step-by-step guide to help you set up a Kubernetes cluster on AWS using kops.

**<mark>Step 1 : Create an EC2 Instance for Kops and Kubectl</mark>**

Creating an EC2 instance that will serve as your management machine for kops and kubectl.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723907684072/b52a0300-56f3-4921-a994-ba5e08465fe9.png align="center")

**<mark>Step 2: Switch to the Root User</mark>**

```plaintext
sudo su - root
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723908000375/7484f750-28d8-4b4a-a52e-6fcfb595f92c.png align="center")

**<mark>Step 3: Install Kops on Your Local Machine</mark>**

Download and install the latest version of kops:

```plaintext
curl -Lo kops https://github.com/kubernetes/kops/releases/download/$(curl -s https://api.github.com/repos/kubernetes/kops/releases/latest | grep tag_name | cut -d '"' -f 4)/kops-linux-amd64
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723908047154/c5a017b5-f92a-40b4-be05-ea920c072c47.png align="center")

```plaintext
chmod +x kops
sudo mv kops /usr/local/bin/kops
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723908096257/9cbf58a8-266d-45d9-90dd-2b386ca01a13.png align="center")

```plaintext
kops
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723908350958/17329e2b-2823-489b-a0a9-e867df5d7193.png align="center")

**<mark>Step 4: Install Kubectl on Your Local Machine</mark>**

Download and install kubectl, the Kubernetes command-line tool:

```plaintext
curl -Lo kubectl https://storage.googleapis.com/kubernetes-release/release/v1.27.0/bin/linux/amd64/kubectl
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723908190953/b59d0272-5774-4c7c-98ad-c200f54816f2.png align="center")

```plaintext
chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin/
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723908223015/8f7499c8-1668-4f64-b67c-fba566139383.png align="center")

```plaintext
kubectl
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723908428451/071ac8f1-4535-440d-b269-210c118ddc7a.png align="center")

**<mark>Step 5: Create an IAM User with Admin Access</mark>**

You'll need an IAM user with the necessary permissions to manage the Kubernetes cluster.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723908832188/57abcad5-1aac-4b3e-bb1f-0d839a7509b9.png align="center")

Attach the necessary admin policies:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723918115282/822c0968-6313-48b3-9973-eb2b57a1a814.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723909133783/a4b9f388-9af6-4101-ac78-afc59a3f0c32.png align="center")

**<mark>Step 6 : Configure AWS CLI on your Linux machine</mark>**

Update your system package

```plaintext
sudo apt update
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723909273919/ce6cb03b-28f6-4a29-a7d4-7435c2021300.png align="center")

Install the AWS CLI:

```plaintext
sudo apt install awscli
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723909365348/ee8ce38e-f445-4b70-8388-bd4e871c7303.png align="center")

Configure the AWS CLI with your credentials:

```plaintext
aws configure
```

You'll need to provide:

* **AWS Access Key ID**
    
* **AWS Secret Access Key**
    
* **Default region name**
    
* **Default output format (e.g., JSON)**
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723909535256/58ec56a1-1d48-4d40-a90b-77d1605f4ffe.png align="center")

Verify your configuration:

```plaintext
aws s3 ls
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723909662439/d80fca5d-c3f1-453a-bf2a-c467699b39e9.png align="center")

**<mark>Step 7: Export Environment Variables</mark>**

Export your AWS access keys as environment variables:

```plaintext
export AWS_ACCESS_KEY_ID=$(aws configure get aws_access_key_id)
export AWS_SECRET_ACCESS_KEY=$(aws configure get aws_secret_access_key)
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723911996535/a3ccb428-dab0-4475-9cd3-6fc6f815437e.png align="center")

```plaintext
printenv
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723912086641/6c935595-34b7-4748-8d1f-9f71868afe4f.png align="center")

**<mark>Step 8 : Create an S3 Bucket for State Storage</mark>**

Kops requires an S3 bucket to store the cluster's state files.

Create an S3 bucket in your AWS account to hold this data:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724279288997/b0931167-f02a-438a-a550-b6149f760f30.png align="center")

**<mark>Step 9: Set Environment Variables for the S3 Bucket and Cluster Name</mark>**

Set the environment variables for your S3 bucket and cluster name:

```plaintext
export KOPS_STATE_STORE=s3://bucket name
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723913048960/447f1ea3-ff6c-4d2f-acfc-445f7e97bc59.png align="center")

```plaintext
export NAME=<cluster_name>.k8s.local
export NAME=dinesh.k8s.local
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723913227221/42b2b9fe-0714-4d1e-be27-f9380a839f16.png align="center")

**<mark>Step 10: Create the Kubernetes Cluster</mark>**

Create your Kubernetes cluster in the desired AWS region:

```plaintext
kops create cluster --zones us-east-2a ${NAME}
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724268480122/3a7c1e67-66b7-4562-84db-dbcd8b6123e5.png align="center")

```plaintext
Suggestions:
 * list clusters with: kops get cluster
 * edit this cluster with: kops edit cluster dinesh.k8s.local
 * edit your node instance group: kops edit ig --name=dinesh.k8s.local nodes-us-east-2a
 * edit your control-plane instance group: kops edit ig --name=dinesh.k8s.local control-plane-us-east-2a

Finally configure your cluster with: kops update cluster --name dinesh.k8s.local --yes --admin
```

Edit the Instance Group `if needed`: How many instance we need

```plaintext
kops edit ig --name=dinesh.k8s.local nodes-us-east-2a
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724268976444/38c6c389-c8e6-489c-acfe-4a80748e1bab.png align="center")

**<mark>Step 11 : Update &amp; Validate the Cluster</mark>**

Finally, configure your cluster:

```plaintext
kops update cluster --name dinesh.k8s.local --yes --admin
```

This process may take some time. After it's complete, validate the cluster:

```plaintext
kops validate cluster
```

Your cluster should now be ready with 1 control plane and 2 worker nodes.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724270187449/355f3fc6-ff9e-4d9c-b688-84dce0868dc2.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724269865989/72157126-9859-44d4-bcc6-e1a820bdc5e1.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724269940356/6c5326d0-a5fe-4561-9ce0-3b0db817b1f3.png align="center")

Check the cluster details:

```plaintext
kops get cluster
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724279345139/599109f3-f4c8-4181-ab15-ee2cbc7ba1b0.png align="center")

**<mark>Step 12: Delete the Kubernetes Cluster</mark>**

If you `need to delete` the cluster:

```plaintext
kops delete cluster --name=cluster name.k8s.local --state=s3://bucketname --yes
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724279668803/83835ff0-d795-4862-b00d-b02c2fcdd95a.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724279697311/ae6fba88-cc98-4697-9e5b-3c0321562302.png align="center")
