I write what I learn here

How to Setup Kubernetes Cluster on AWS Using KOPS - Full Guide

How to Setup Kubernetes Cluster on AWS Using KOPS - Full Guide

What is Kubernetes?

Kubernetes is an open-source platform originally designed by Google for automating deployment, scaling, and ochestrating operations of application containers across clusters of hosts, providing container-centric infrastructure. It has many features especially useful for applications running in production like service naming and discovery, load balancing, application health checking, horizontal auto-scaling or rolling updates.

To get started lets get a brief understanding of important concepts around Kubernetes.

  1. container - A conatiner image is a lightweight, stand-alone, executable package of a piece of software that includes everything needed to run it: code, runtime, system tools, system libraries, settings. Containerized software will always run the same, regardless of the environment (Linux/ Windows/ Mac).

  2. Pod – A pod is a group of one or more containers (such as Docker containers), with shared storage/network, and a specification for how to run the containers.

  3. Service – A set of pods that work together. By default a service is exposed inside a cluster but it can also be exposed onto an external IP address outside your cluster. We can expose it using one of four available behaviors: ClusterIP, NodePort, LoadBalancer and ExternalName.

  4. Replication Controller – A ReplicationController ensures that a specified number of pod replicas are running at any one time. In other words, a ReplicationController makes sure that a pod or a homogeneous set of pods is always up and available.

In this post, we will learn how to setup kubernetes cluster on AWS using KOPS (kubernetes operations). I will begin at instance creation to setting up the environment for installation and finish by testing the cluster using an nginx image. The assumption is that you have access to a AWS account setup. Otherwise visit Amazon to create an account.

Creating 7 configuring an instance to run the installation

  1. Create a t2.micro (Falls under free tier) instance running an Ubuntu Amazon Machine Image (AMI). You can skip this step if you want to do it from your pc.

  2. Install python and pip: sudo apt install python-pip

  3. Install AWS Cloud Line Interface (CLI) pip install awscli --upgrade --user

    Verify if CLI is installed by running aws –version You should get something similar to: aws-cli/1.14.32 Python/2.7.12 Linux/4.4.0-1047-aws botocore/1.8.36

  4. Create group and a user and add give them access to Route53 and full Admin access. Instead of adding thee Admin access you can add AmazonEC2FullAccess, AmazonRoute53FullAccess, AmazonS3FullAccess, IAMFullAccess, AmazonVPCFullAccess to limit the access to only the required resources. When setting the permissions, make sure to download the keys because you will need to export them as environment variables in the config server otherwise your CLI will not work.

    User access permissions

  5. Configure CLI by exporting the keys and default region



    export AWS_DEFAULT_REGION=us-east-1

    Note that the above commands will save the environment variables temporally and the next time you log into the server you will have to re-configure them.

    These can be saved permanently in the server by adding them into the .profile file in your desktop. To see the changes restart the server with sudo shutdown -r now

  6. Install kubectl

    curl -LO$(curl -s release/release/stable.txt)/bin/linux/amd64/kubectl

    chmod +x ./kubectl

    sudo mv ./kubectl /usr/local/bin/kubectl

    Note: The above command installs the latest version of kubectl

  7. Install Kubernetes Operations(KOPS)

    Use this to download a specific version of kops wget

    Use this to get the latest version - Recommended: curl -LO$(curl -s | grep tag_name | cut -d '"' -f 4)/kops-linux-amd64

    chmod +x kops-linux-amd64

    sudo mv kops-linux-amd64 /usr/local/bin/kops

  8. Generate ssh keys in the server ssh-keygen -t rsa -b 4096 -C "[email protected]" -f $HOME/.ssh/id_rsa -N "" List the keys ls ~/.ssh/id_rsa*

AWS Configuration

1.Create Route53 domain for the cluster by creating a new hosted zone. It's preferred if you have a public domain name but if you don't have one you can create a private one on Route53 that will be used internally by the cluster.
Route53 domain names

  1. Create a S3 bucket from the configuration server CLI if you haven’t created one manually aws s3 mb s3:// This bucket acts as a storage for the kubernetes cluster configurations. If you want to use AWS dashboard.

    S3 Storage bucket

Creating a Kubernetes Cluster

  1. Save the storage bucket as an environment variable export KOPS_STATE_STORE=s3:// Test the existence by running echo $KOPS_STATE_STORE. Remember that you are running these commands in thee configuration server now.

  2. Use --dns private if your domain name is not public kops create cluster --cloud=aws --zones=us-east-1b --name= --dns private NOTE: Make sure you have ssh keys already generated otherwise the above command will throw an error.

  3. Run this command to actually create the cluster --yes prints out the results of the command kops update cluster --yes

    This will create the VPC, subnets, autoscaling-groups, nodes etc. which you can observe from AWS dashboard. Master note and slave instances Master note and slave instances

    Autoscaling group Auto scaling group

    Cluster VPC Cluster VPC

  4. You can then edit the cluster settings with one of these commands: List clusters with: kops get cluster

    Edit this cluster with: kops edit cluster

    Edit your node instance group: kops edit ig nodes

    Edit your master instance group: kops edit ig master-us-east-1d

  5. It takes quite some time for the instances to boot and the DNS entries to be added in the zone. Once everything is up you should be able to get the kubernetes nodes: kubectl get nodes

  6. To enable the Kubernetes UI you need to install the UI service: kubectl create -f In case you have errors running this command, check the verison of Kubernetes and kops you are running. They must be compatible.

  7. You can use the kubctl proxy to access the UI from your machine if you run your installations locally. Run kubectl proxy --port=8080 & .The UI should now be available at http://localhost:8080

  8. To delete the cluster run kops delete cluster –yes

Deploying Nginx Container for Testing

To test the Kubernetes cluster, we will deploy a simple service made up of some nginx containers

  1. Create an nginx deployment: kubectl run nginx-app --image=nginx --replicas=2 --port=80

    kubectl get pods

    kubectl get deployments

  2. Expose the deployment as a service. This will create an ELB in front of those 2 containers and allow us to publicly access them:

    kubectl expose deployment nginx-app --port=80 --type=LoadBalancer

    kubectl get services -o wide

    This command will provide a url to an ELB running nginx containers.

    Nginx running

  3. You can now view the UI by accessing master node (using the public IP). This will require you to provide credentials. Run command kubectl config view to see the credentials.

    Kubernetes dashboard

I hope this blog gives a quick introduction into kubernetes to get you started with your deployments.

Read more on how you can leverage Microservice Architecture for your software designs.

Happy coding!


To make a comment you have to login