Kubernetes cluster setup on AWS
Tags: kubernetes
Published: 2018-06-22 15:21:27


---
title: Kubernetes cluster setup on AWS
published: true
description: Kubernetes Cluseter setup on AWS 
tags: #Kubernetes
---

# Kubernetes


It is an open source system for managing containerized applications across multiple hosts, providing basic mechanisms for deployment, maintenance, and scaling of applications.

In this article, I am going to setup a Kubernetes cluster on AWS cloud environment up for which I am going to use a tool called KOPS.

What is KOPS? kops is an opinionated provisioning system with

- Fully automated installation
- Uses DNS to identify clusters
- Self-healing: everything runs in Auto-Scaling Groups
- Limited OS support (Debian preferred, Ubuntu 16.04 supported, early support for CentOS & RHEL)
- High-Availability support
- Direct provision or  terraform manifest generation

### Pre-requisite

As a part of the setup, I would need an Ubuntu or Debian instance with latest updates and other supporting utility/tools like AWS-CLI, S3 bucket, Hosted Zone on Route 53 and a registered domain. In this article, I am going to use Ubuntu instance to launch my cluster.

#### Ubuntu EC2-instance

Let me launch an AWS EC2 Ubuntu instance and update with the latest packages.
```shell
$sudo apt-get update

$sudo apt-get -y upgrade
```

#### kops installation

I have downloaded the latest version of kops and changed the permission before moving it to /usr/local/bin 

```shell
$ wget -O 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/

```
#### kubectl installation

I have downloaded the latest version of the kubectl and changed the permission before moving it to /usr/local/bin

```shell
$ curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
$ chmod +x kubectl
$ sudo mv kubectl /usr/local/bin/
```
#### AWS CLI

I have installed AWS CLI to access the AWS instance from the command line to create Kubernetes cluster.

```shell
$ sudo apt-get install python-pip
$ pip install --upgrade pip
$ sudo pip install awscli
```

#### IAM user creation

On AWS console, I have created a new IAM user (for example kops) with full access and saved the access keys as it would be used to configure the AWS CLI. Then I have copied both AWS access key ID and AWS secret access key for later usage.

On the EC2 instance, I have configured the newly created AWS IAM user with following commands

```shell
$aws configure

AWS Access Key ID [None]:  AWS Secret Access Key [None]: Default region name [None]: < Optional : Please enter the region or blank for default>
Default output format [None]: < Optional : Please enter the output format or blank for default>
```

On the same the EC2 instance, I have generated a key pair for AWS EC2 user. It will be used to connect to the kubernetes cluster which I am going to create. In my case, as it is Ubuntu user the ssh keys by default are stored in .ssh folder of the user home directory.

```shell
$ ssh-keygen
```

#### Domain creation

I have created a domain for the cluster user "kops" and DNS for discovery which will be used inside the cluster and to reach the kubernetes API server from the client. It should have a valid DNS name. I recommend using a subdomain for the cluster configuration.

An existing domain or a new domain can be used. In this example, I have created a new domain and hosted it on dot.tk which is a free domain provider.

#####Domain registration on dot.tk / freenom.com: 
I have registered a new domain on freenom.com with the name k8sclustersetup.tk which I going to use for my Kubernetes cluster.

![alt text](https://thepracticaldev.s3.amazonaws.com/i/ec6chd6us2ozi4963xrb.png "Domain registration")

#### Create Hosted Zone
On AWS console, I have created a new Hosted zone on router 53. I have logged into AWS console, navigate to router53 DNS management and created new Hosted Zone. It's advisable to create a subdomain. This creates a set of name servers which can be copied for later usage. I have copied the name server details which starts with ns-xxx.awsdns-xx.com, ns-xxx.awsdns-xx.co.uk, ns-xxx.awsdns-xx.org, ns-xxx.awsdns-xx.net.

![alt text](https://thepracticaldev.s3.amazonaws.com/i/u25buxcbudxxqzd83x8l.png "AWS Route 53 Hosted Zone")

These NameServer (NS) values should be updated on the domain service provider. In this setup, I have updated the NS details on freenom.com.

![alt text](https://thepracticaldev.s3.amazonaws.com/i/5dppiy652bd4a5w7sqtq.png "Update the Name Server details for the domain")

#### S3 bucket creation
I have created a new S3 bucket with a meaningful name (for example "kopsclusterdemo") which is used to store the cluster state. Kubernetes uses S3 to store the cluster details like configuration, keys, etc.

With this, all the pre-requisite has been setup and the environment is ready to create and launch the Kubernetes cluster.

####Kubernetes cluster creation
For learning purpose, I have chosen the t2.micro which is a free EC2 instance. I have executed the below command to create the kubernetes cluster with one master and 2 worker nodes.

```shell
$ kops create cluster --name=k8sclustersetup.tk --state=s3://kopsclusterdemo --zones=eu-west-2a --node-count=2 --node-size=t2.micro --master-size=t2.micro --dns-zone=k8sclustersetup.tk

$kops update cluster k8sclustersetup.tk --yes --state=s3://kopsclusterdemo
```

It takes a couple of minutes for the Kubernetes cluster to get created. To confirm whether the Kubernetes cluster got created I have used the below command to list the cluster details.


```shell
$kops get cluster --state=s3://kopsclusterdemo
```

Cluster node details can be listed by issuing the below command.

```shell
$kubectl get node
```

###Conclusion
In this article, the details to install and configure Kubernets cluster on AWS has been provided.