Installing Kubernetes with Kubespray on AWS

With Ansible playbooks, Kubespray provides added flexibility in deploying Kubernetes clusters.

Kubespray is a composition of Ansible playbooks aimed at providing users with a flexible method of deploying a production-grade Kubernetes cluster. However, deploying Kubernetes with Kubespray can get tricky if you are not too familiar with the technology.

In this tutorial, we will show how to deploy Kubernetes with Kubespray on AWS.

 

Installing dependencies

Before deploying, we will need a virtual machine (hereinafter Jumpbox) with all the software dependencies installed. Check the list of distributions supported by Kubespray and deploy the Jumpbox with one of these distributions. Make sure to have the latest version of Python installed. Next, the dependencies from requirements.text in Kubespray’s GitHub repo must be installed.

Lastly, install Terraform by HashiCorp. Simply download the latest version of Terraform according to your distribution and install it to your /usr/local/bin folder. For example:

 

Building a cloud infrastructure with Terraform

Since Kubespray does not automatically create virtual machines, we need to use Terraform to help provision our infrastructure. To start, we create an SSH key pair for Ansible on AWS.

An example of a key pair being created

The next step is to clone the Kubespray repository into our jumpbox.

We then enter the cloned directory and copy the credentials.

After copying, fill out credentials.tfvars with our AWS credentials.

In this case, the AWS credentials were as follows.

Next, we edit terraform.tfvars in order to customize our infrastructure.

Below is an example configuration.

Next, initialize Terraform and run terraform plan to see any changes required for the infrastructure.

After, apply the plan that was just created. This begins deploying the infrastructure and may take a few minutes.

Once deployed, we can check out the infrastructure in our AWS dashboard.

Deployed instances shown in the AWS dashboard

 

Deploying a cluster with Kubespray

With the infrastructure provisioned, we can begin to deploy a Kubernetes cluster using Ansible. Start off by entering the Kubespray directory and use the Ansible inventory file created by Terraform.

Next, load the SSH keys, which were created in AWS earlier on. First, create a file (in our case, it will be located at ~/.ssh/Altoros/kubespray.pem) and paste the private part of the key created at AWS there.

Once the SSH keys are loaded, we can now deploy a cluster using Ansible playbooks. This takes roughly 20 minutes.

 

Configuring access to the cluster

Now that the cluster has been deployed, we can configure who has access to it. First, find the IP address of the first master.

After identifying the IP address, we can SSH to the first master.

Once connected, we are set as a core user. Switch to the root user and copy the kubectl config located in the root home folder.

Highlight and copy the kubectl config as shown in the following image.

Example kubectl config

Return to the jumpbox and go to kube/config.

Paste the copied kubectl config here.

Copying kubectl config

Next, copy the URL of the load balancer from the inventory file. In our case, the URL is kubernetes-elb-altoros-cluster-458236357.us-east-2.elb-amazonaws.com. Paste this URL into the server parameter in kubectl config. Do not overwrite the port.

 

Running test deployments

After configuring access to the cluster, we can check on our cluster.

Node and cluster details will be shown in the console.

Cluster and node details

With the cluster ready, we can run a test deployment.

Entering this commands should deploy NGINX and also return the status of the pods and deployments.

A successful test deployment

With this, we have successfully provisioned our cloud infrastructure with Terraform. We then deployed a Kubernetes cluster using Kubespray. We also configured access to the cluster and were finally able to run test deployments.

More on Kubespray can be found in its GitHub repository, as well as in the project’s official documentation.

 

Want details? Watch the video!

The video demonstrates how to deploy Kubernetes clusters on AWS using Kubespray.

 

Further reading

 


The post was written by Arsenii Petrovich, Viachaslau Matsukevich, and Carlo Gutierrez;
edited by Sophia Turol and Alex Khizhniak.