Running AISdecoder in a Kubernetes cluster on AWS
(Posted 2018-10-12)
Introduction
In recent posts I have shown how to decode AIS messages (what is AIS?), how to create a Spring Boot based AIS message decoder, and how to run this decoder in a Docker container.
Today we will see how to deploy this Docker container into a Kubernetes cluster and to call it once it is running there.
What is Kubernetes?
Explaining Kubernetes can be done both very short and very long.
For in in-depth explanation of Kubernetes, I recommend reading the introduction on kubernetes.io, the documentation on kubernetes.io, or perhaps best: The book “Kubernetes in Action” from Manning Publications.
The short version that I will provide here, is that Kubernetes is a home for Docker containers – both for test and production. Kubernetes is software which allows you to deploy and manage containerized applications in a standardized manner. As “Kubernetes in Action” puts it: “Kubernetes enables you to run your software applications on thousands of computer nodes as if all those nodes were a single, enormous computer”.
I find the standardized container deployment procedure and the management of contains (like scaling and detecting missing ones) especially important.
You can run a Kubernetes cluster on almost any set of machines such as Linux-based PC’s, on Raspberry Pis, virtualized machines (like on AWS or Azure), or even in turn-key hosted environments (read more).
In this post I will focus on setting up a Kubernetes cluster in the Amazon Web Services (AWS) cloud. AWS is a very complex and comprehensive platform. In relation to this post, think of AWS as a place where we will spin up a small number of virtualized standard Linux machines (“EC2 instances”) and a few supporting resources. Nothing else.
To control Kubernetes on AWS we will use kops. Kops is short for “kubernetes operations” and is a fantastic command line tool which we can use to control the operations/sysadmin-stuff of a Kubernetes cluster.
For kops to work, it needs the command line tools for AWS to be installed first. So that’s where we will start.
Installing AWS command line tools – awscli
To get running on AWS you must have an AWS account (create one). Limited use of AWS is free (“free tier”); if you go beyond “limited” you may have to pay for their services.
AWS can be controlled completely through their web interface; but for kops to control AWS we need the alternative AWS user interface installed: The AWS command line tool: awscli. To install awscli on a Mac we will need homebrew. Assuming homebrew is already installed, the installation of awscli is simply:
Configuring an AWS IAM user
For kops to work with AWS we need to create or choose an “IAM user” on your AWS account and grant this user the necessary permissions. Even though awscli is already installed, I find it a lot easier to configure this role and its permissions using the AWS web UI. Whatever approach you choose be sure that you end up with an “IAM user” with following permissions granted:
Awscli initial configuration
With awscli installed and an IAM user prepared we need a one-time configuration to connect awscli to your account:
To know your values of the X’es – follow the AWS user guide.
After this – awscli is now configured and ready to use.
Installing Kubernetes Operations – kops
Next we need kops. To install kops using homebrew:
This usually runs straight with no problems.
Preparing AWS for kops
Before we can use kops, we need to prepare the AWS surroundings.
First, we need to create an “AWS S3 bucket” where kops can store its on administrative information. AWS S3 is a key-value store on AWS, which you can think of as a database. We need to find a name for this bucket – unfortunately the S3 bucket namespace is shared by all users of the system, so you may need to be somewhat creative to find a name which is both descriptive and globally unique. Here I will try with “tbsalling-kops-state-store” and use awscli to create the bucket like this:
That went well. Now kops requires a simple single configuration of this bucket:
AWS S3 is now ready for kops.
Configuring kops
So far we have installed awscli, prepared an AWS IAM user, configured awscli, installed kops, and prepared AWS for kops. Before we reach the real fun, we still need to configure kops. First we need to create an SSH keypair which kops can use:
Before using the kops cli we set two environment varibles:
Note that the KOPS_STATE_STORE
must match the name you chose for the AWS S3 bucket above.
Creating a Kubernetes cluster on AWS using kops
Phew 🙂 Now our preparations are completed and we are ready to create our first Kubernetes cluster on AWS!
Let’s start out by creating a Kubernetes cluster with 3 nodes. We don’t need big machines for this – so I will choose will use AWS instance type “t2.micro”. I prefer the machines to run in Frankfurt, Germany – so I choose AWS availability zone “eu-central-1a”. With these few choices, I perform the following kops command:
It may take a while for the cluster to get completely up to speed, which you can see with kops validate cluster like this:
But after a while you will see something like this:
And this makes us happy: “Your cluster tbsalling-kops.k8s.local is ready” 🙂
So now we have a Kubernetes cluster with 1 master and 3 slave nodes running in Frankfurt!
Deploying AISdecoder to the AWS Kubernetes cluster
With the Kubernetes cluster up and running we will now deploy our AISdecoder Docker image to it.
The main tool to control a kubernetes cluster is kubectl. You can read a lot about this in the official Kubernetes tutorial. Can you also simply type kubectl at the command line to see its usage. For now, we can see that kubectl is working and correctly configured by kops like this:
To deploy our Docker image – which is already on Docker hub – we can use the kubectl run command and point it to our Docker image on Docker hub:
We can verify, that Kubernetes now indeed has created a deployment for us:
This shows, that we desire 1 instance of aisdecoder to be running at any time, and that 1 instance of aisdecoder is actually running.
To be able to make a REST call to the aisdecoder application running inside this Kubernetes cluster we need to go through a “proxy”. This is because applications running in Kubernetes are on a private network which can only be reached through a proxy. To start such a proxy we type:
Now it is time to make a call to our application. So in a second terminal window we will now call the aisdecoder application through the Kubernetes proxy:
The hostname and port number (localhost:8001
) point to the Kubernetes proxy. So this is what came out of kubectl proxy.
For now “/api/v1/namespaces/default/pods/” is just a static string which is appended.
“aisdecoder-c4c46474-rzd2x” is the name of the pod containing our application – this is obtained from kubectl get pods.
“/proxy” is a static string.
And finally “/decode” is the URI defined by our own application.
Hence, we see the following output of the curl command:
Which is absolutely fantastic! Because now we have successfully called our own application running inside the Kubernetes cluster!
After all this work please take time to celebrate a bit before reading on :-).
In a later post I will show how to play more with our application in Kubernetes now that it is running there. But to keep this post at a reasonable length, it is now time for:
Cleaning up the cluster
Once we are finished playing we can stop and delete the application from our Kubernetes cluster:
Cleaning up on AWS
Also, since we are just playing around here we want to avoid unnecessary AWS hosting costs by freeing up the machines, that we just spun up.
One way to stop these machines would be to use the awscli. But since kops knows exactly what it has done and which AWS resources it has allocated (thanks to its S3 bucket), kops can conveniently clean up after itself and free all the AWS resources related to the Kubernetes cluster.
It can be done like this:
It may take a few minutes for kops to get everything spun down; but when the command completes with “Deleted cluster: …” then all the AWS resources (except for the adminsitrative AWS S3 bucket) are freed and we are back where we started.
Conclusion
In this post, I showed how to install the command line interface for Amazon web services and also “kops” – kubernetes operations.
Then I showed how create a 3-node Kubernetes cluster on AWS, how to deploy and run a Docker image there, and finally how to clean it all up.
Important resources
- “Kubernetes in Action” by Marko Lukša, ISBN 9781617293726, Manning Publications, https://www.manning.com/books/kubernetes-in-action.
- “Installing Kubernetes on AWS with kops”, https://kubernetes.io/docs/setup/custom-cloud/kops/.
- Official Kubernetes tutorials – https://kubernetes.io/docs/tutorials/; especially https://kubernetes.io/docs/tutorials/kubernetes-basics/deploy-app/deploy-intro/.