Getting started with Kubernetes Clusters on AWS Using Kops
This article is specially beneficial for those who want to manage Kubernetes Cluster with Amazon EC2(free tier) for learning purposes.

Getting started with Kubernetes Clusters on AWS Using Kops

Kubernetes and Kops overview :

Kubernetes is an open-source, container orchestration platform. Applications packaged as Docker images can be easily deployed, scaled and managed in a Kubernetes cluster. 

Know more about Kubernetes

Some of the key features of Kubernetes are:

  1. Self-healing.
  2. Failed containers are restarted to ensure that the desired state of the application is maintained. If a node in the cluster dies, then the containers are rescheduled on a different node. Containers that do not respond to application-defined health checks are terminated and thus rescheduled.
  3. Horizontal scaling.
  4. Number of containers can be easily scaled up and down automatically based upon CPU utilization, or manually using a command.
  5. Service discovery and load balancing.
  6. Multiple containers can be grouped together discoverable using a DNS name. The service can be load balanced with integration to the native LB provided by the cloud provider.
  7. Application upgrades and rollbacks.
  8. Applications can be upgraded to a newer version without an impact to the existing one. If something goes wrong, Kubernetes rolls back the change.
Kops, short for Kubernetes Operations, is a set of tools for installing, operating and deleting Kubernetes clusters in the cloud. A rolling upgrade of an older version of Kubernetes to a new version can also be performed. It also manages the cluster add-ons. After the cluster is created, the usual kubectl CLI can be used to manage resources in the cluster.

Know more about Kops

Prerequisites: AWS Free tier eligible account, know how to use AWS services like EC2, IAM, S3, Route53, and a desire to learn & play with Kubernetes.

Setup Kubernetes (K8s) Cluster on AWS:

  • Create an Ubuntu EC2 instance. You can start by creating a t2.micro Ubuntu Server.
  • SSH to your Ubuntu Instance and run the following commands to install AWS-CLI.
sudo apt-get update
sudo curl https://s3.amazonaws.com/aws-cli/awscli-bundle.zip -o awscli-bundle.zip
sudo apt install unzip python
# sudo apt-get install unzip - if you dont have unzip in your system
sudo ./awscli-bundle/install -i /usr/local/aws -b /usr/local/bin/aws

  • Install Kubectl on Ubuntu Instance.
sudo curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
sudo chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin/kubectl

  • Install Kops on Ubuntu Instance.
sudo curl -LO 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
sudo chmod +x kops-linux-amd64
sudo sudo mv kops-linux-amd64 /usr/local/bin/kops

  • Create an IAM Role with Route53, EC2, IAM, and S3 full access.
No alt text provided for this image
No alt text provided for this image
No alt text provided for this image
No alt text provided for this image
  • Attach IAM role to Ubuntu Instance.
No alt text provided for this image
No alt text provided for this image
  • Go back to your EC2 instance and run command to configure aws.
aws configure
# Note: If you create IAM user with programmatic access then provide Access keys. # Otherwise region information is enough.
# As I am working in Mumbai region so region name is ap-south-1

No alt text provided for this image
  • Create a Route53 Private hosted zone (you can create a Public hosted zone if you have a domain). Kops uses DNS for discovery, both inside the cluster and outside so that you can reach the Kubernetes API server from clients. For learning purposes, I am using the domain "example.com".
No alt text provided for this image
No alt text provided for this image
  • Create an S3 bucket to store your cluster's state. Kops lets you manage your clusters even after installation. To do this, it must keep track of the clusters that you have created, along with their configuration, the keys they are using etc. This information is stored in an S3 bucket. S3 permissions are used to control access to the bucket.
aws s3 mb s3://rounak.example.com

No alt text provided for this image
  • Expose the environment variable and then kops will use this location by default.
export KOPS_STATE_STORE=s3://rounak.example.com

  • Don't forget to generate ssh-keys before creating a cluster. This will be used for logging into our Kubernetes Cluster.
No alt text provided for this image
  • Create Kubernetes cluster definitions on the S3 bucket.
kops create cluster --cloud=aws --zones=ap-south-1b --name=rounak.example.com --dns-zone=example.com --dns private

No alt text provided for this image
  • List your clusters using below command:
kops get cluster

No alt text provided for this image
  • Edit your node instance group and change machineType to t2.micro. You can also change maxSize/minSize to 1 for creating only one node.
kops edit ig --name=rounak.example.com nodes

No alt text provided for this image
  • Edit your master instance group and change machineType to t2.micro:
kops edit ig --name=rounak.example.com master-ap-south-1b

Finall
No alt text provided for this image
  • Finally, configure your Kubernetes cluster using below command:
kops update cluster --name rounak.example.com --yes

No alt text provided for this image
  • You will now see new EC2 instances in your AWS EC2 running instances dashboard as "nodes" and "master".
No alt text provided for this image
  • Please wait about 5-10 minutes for a master to start, dns-controller to launch, and DNS to propagate and then validate your cluster.
kops validate cluster

No alt text provided for this image
  • List your nodes in the cluster.
No alt text provided for this image
  • Finally, SSH to the master where we will do our Kubernetes deployments. Give input "yes" when the prompt appears for continue connecting.
ssh -i ~/.ssh/id_rsa [email protected]

OR

ssh -i ~/.ssh/id_rsa admin@<public_ip_of_your_master>
No alt text provided for this image
  • After logging into our master node, run the following command to get nodes. We can create Kubernetes deployments, pods, and services on this master node.
kubectl get nodes

No alt text provided for this image
  • To get back to our earlier k8s-management-server(Ubuntu), type the following command.
exit

No alt text provided for this image
  • Finally, delete the cluster and clean up things.
kops delete cluster rounak.example.com --yes
No alt text provided for this image
No alt text provided for this image
  • Confirm with: kops get cluster on the k8s-management-server.
No alt text provided for this image
With the deletion of cluster, you will see the EC2 instances(nodes and master) which rolled up automatically during cluster creation will get terminated. Also, don't forget to delete the created hosted zone on AWS Route53 and your S3 bucket. Finally you can stop/terminate your k8s-management-server also.


I hope you enjoyed and learned the proper way of creating Kubernetes Cluster on AWS with Kops. Please share with your friends and connections if you find it useful and worth reading.

No alt text provided for this image

For any queries, please feel free to reach out to me on [email protected] or Linkedin.

Sandeep Mane

Head- Software Development, Head- Software Engineering, Delivery Head, CSD Practice Lead | Presales Lead | Agile Coach

4 年

Rounak Surana - Much appreciable effort. Keep writing articles

Raghav Agarwal

Senior DevOps Engineer at Tata Consultancy Services | TCS AI Cloud

4 年

Highly Appreciate ???? Much needed article for getting stared with k8 on aws.

要查看或添加评论,请登录

Rounak Surana的更多文章

  • Kubernetes the HARD WAY with Kubeadm

    Kubernetes the HARD WAY with Kubeadm

    The kubeadm tool is used to bootstrap a smaller Kubernetes cluster where you can experience all Kubernetes features…

    9 条评论
  • Deploying a Kubernetes Cluster with Amazon EKS

    Deploying a Kubernetes Cluster with Amazon EKS

    In 2018, AWS, Oracle, Microsoft, VMware and Pivotal all joined the CNCF as part of jumping on the Kubernetes bandwagon.…

    9 条评论