Kubernetes in AWS with kops
Tags: devops, aws, kubernetes, docker
Published: 2022-06-23 15:59:00

Kops is an official Kubernetes project for managing a Kubernetes clusters in aws. 
Kops Stands for Kubernetes Operations.
Kops is currently the best tool to deploy Kubernetes clusters to Amazon Web Services.
Kops has commands for creating clusters, updating settings, and applying changes , Kops automates a large part of operating Kubernetes on AWS.
Kops only availabe for Linux and Mac Platforms.

<u>Prepare AWS for Kops</u>


Management Node (Local System - ubuntu 20.0.4)

In this management node below requirements must be required for kops.


1.kops
2.kubectl
3.aws cli
4.s3 bucket access         


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8enhbzali5ainyiion7o.png)

<u>AWS </u>
1. Create an IAM User
2. Assign the Permissions
3. Create S3 bucket for storing KOPS_STORE_STATE
4. Route53 

_Install kops on Ubuntu 20.0.4_

```
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
chmod +x kops
sudo mv kops /usr/local/bin/kops
```
_Install kubectl on Ubuntu 20.0.4_

**Update the apt package index and install packages needed to use the Kubernetes apt repository**

```
sudo apt-get update
```    
```
sudo apt-get install -y apt-transport-https ca-certificates curl
```
**Download the Google Cloud public signing key:**

```
sudo curl -fsSLo /usr/share/keyrings/kubernetes-archive-keyring.gpg https://packages.cloud.google.com/apt/doc/apt-key.gpg
```
**Add the Kubernetes apt repository**

```
echo "deb [signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
```
**Update apt package index with the new repository and install kubectl**
```
sudo apt-get update
       
sudo apt-get install -y kubectl
```
**Installing AWS CLI**    

```
sudo  apt install awscli
```

       
**Verify AWS CLI using command**

```
 aws
```
_Create/Log-in AWS Console Account._

_SetUp AWS IAM permission for Kops._

_Create a user(kops) and give them permission._

**Permission required for Kops use**r
       

```
AmazonEC2FullAccess
AmazonRoute53FullAccess
AmazonS3FullAccess
IAMFullAccess
AmazonVPCFullAccess
OR
AdministratorAccess
```
 
            
_Configure User with AWS Account._

**Run command on your machine**

```
aws configure
```
_Provide AWS access Key and AWS Secret Access Key._

_Specify Default region or Output format._

**Verify credentials and config.**
  
```
 ls -lrt ~/.aws/
```
_S3 bucket for the KOPS_STATE_STORE._

_KOPS_STATE_STORE is the source of truth for all clusters managed by Kops_.

_Get fastest Region for Deploy the S3 Bucket._

**Create an S3 bucket for KOPS_STATE-STORE**

```
aws s3 mb s3://<bucket-name>
aws s3 mb s3://k8s-test-123
```
_User can use [cloudping](https://www.cloudping.info) to choose the fastest region as per their location._

_Kops clusters must be valid DNS names._

_We need to SetUp DNS for the Kops Clusters._

_SetUp DNS in AWS Route53._ 

I have a Domain _cloudmates.in_ which is availabe in **Godaddy**

## For creating DNS in Route53
_Go to Route53_
_Create Hosted Zone_
Zone Name _kops.cloudmates.in_
Copy The NS records and create NS  in your Domain Name Provider once completes validate the dns

```
dig -t ns=kops.cloudmates.in

```

_With Kops 1.6.2 or later, then DNS configuration is optional._
_The only requirement to trigger this is to have the cluster name end with .k8s.local_
<u>SetUp Kubernetes Cluster on AWS with Kops</u>

**Generate SSH Key**

```
ssh-keygen -f .ssh/id_rsa
```

## Create Cluster Syntax

```
kops create cluster --yes --state=<s3://<Define S3 Bucket Name>> --zones=<One or more Zones> --node-count=<Number of Nodes> --node-size=<Define Machine Size> --master-size=<Master Node Size>  --name=<Define DNS Name>

```


## Create Cluster with Route53 hosted zone


```
kops create cluster --yes --state=s3://k8s-storage-a12345 --zones=ap-south-1a --node-count=2 --node-size=t2.micro --master-size=t2.micro --name=kops.cloudmates.in

```

## Verify Node Status

```
kubectl get node

```

## Validate Cluster

```
kops validate cluster --state=<s3://<Define S3 Bucket Name>
kops validate cluster --state=s3://k8s-test-123`

```
######### Execute Custom Image on AWS kubernetes######
**Create AWS Kubernetes Cluster Without Domain Name **

```
   kops create cluster --yes --state=s3://k8s-test-123 --zones=ap-south-1a --node-size=t2.micro --node-count=2 --master-size=t2.micro --name=test.k8s.local

```
**Verify Kubernetes Cluster.(Different Formats)**

```
   kops validate cluster
   kops validate cluster -o json
   kops validate cluster -o yaml

```
**Start the Deployment on Kubernetes Cluster**.

```
 kubectl create deployment myweb --image=cloudmates/customnginx

```
**Get Information of Running Deployments**

```
kubectl get deployments

```
**Describe the Running Deployment**

```
 kubectl describe deployment myweb

```
**Make the myweb container accessible via the internet loadbalancer**

```
kubectl create service loadbalancer myweb --tcp=80:80

```
**Get Running Services**

```
kubectl get svc

```
**Remove Services**

```
 kubectl delete services myweb

```
**Remove Deployment**

```
 kubectl delete deployment myweb

```
**Delete Cluster**

```
kops delete cluster cloudmates.in --yes --state=s3://k8s-test-123

```