I'm a big fan of docker (well the concept of containers in general) so recently I became acquainted with Kubernetes.
What we will cover today:
- What is Kubernetes and when would I use it?
- What is cluster & setting up a cluster on AWS/Azure/Google cloud/Digital Ocean/Bare Metal
- What is the Kubectl (spoiler alert, its the Kubernetes Api used to manage your resources).
What is Kubernetes?
If you've not used Docker before, I would suggest covering that first, trust me it's worth it.. i'll wait.
Great, so if like me you love the concept of containers, but now have the challenge of managing them in production Kubernetes is for you.
In the past whilst spinning up and linking docker containers you may have used docker compose. It's fairly simple and allows you to quickly create a number of linked containers on a single docker host (via a docker-compose yaml file). What it doesn't do though is:
- Allow you to deploy containers across multiple hosts (VMs).
- Manage the lifetime and health of each container and provision new containers should existing ones crash.
- Manage scaling, networking, automate deployments, load balancing etc.
This is where Kubernetes (or k8s as it's also known) fits into the picture. (note: Docker swarm was also built to solve these problems. I won't go into Swarm vs K8s in these blog posts.)
(Important) What Kubernetes is not...
It is not the silver bullet (sorry!) to solve all your production infrastructure challenges. You still need to solve those first... yourself. For example, do you need multi master database replication across multiple regions? Well, K8s will not solve scenarios like this for you. However once you have solved such problems conceptually (perhaps a managed database is better than facilitating this via K8s for this scenario) then sure, Kubernetes can provision and manage the lot... easily.
A kubernetes cluster contains a master and a number of worker nodes. (Strictly you can have more than one master, but let's not complicate things this early)
Kubernetes provisions your containers across a cluster of worker nodes via the master. A worker node is simply a VM with some preinstalled runtimes. The master is responsible for scheduling and maintaining the cluster (amongst other things).
The simplest resource you can ask the master to provision for you is a pod.
A pod is a logical wrapper around a number of containers and in Kubernetes it is the unit of scale in a cluster.
For example, to deploy a single container to a Kubernetes stack, you would create a pod definition file (in yaml) and ask the master to provision and manage this for you, via the Kubernetes Api (kubectl).
(There is a lot of background in this post, in the next couple of posts we actually provision a number of pods and start exploring other Kubernetes resources with the goal to deploy a simple distributed containerized app - a NodeJS API with a redis cache and a mysql backend.)
Creating a cluster and installing kubectl
During this series we will start simple and gradually introduce more Kubernetes concepts and resource types as we go. To start lets get a cluster (A single master and 2 working nodes) up and running.
How much will I pay for my cluster? Well that depends on the cloud provider and specifically the size of VMs you use. You can set up a cluster for about $30 a month though on AWS/Azure and GKE.
Before you create your cluster install kubectl on your local machine. (I am using ubuntu)
At the time of writing Azure Container Service (AKS) is in preview. AKS is Azures newest managed Kubernetes offering. The beauty of AKS is that the master is abstracted away from you, you don't see it in your list of resources nor do you pay for it.
You can spin up a cluster in minutes. The easiest way is via the CLI (follow these instructions - https://docs.microsoft.com/en-us/azure/aks/kubernetes-walkthrough) This will also guide you through installing the kubectl (Kubernetes API) and linking your cluster to the API.
You can specify instance size rather than using the default:
az aks create --resource-group myresourcegroup --name mycluster --node-count 2 --generate-ssh-keys --node-vm-size Standard_B2s
You can see the list of instance sizes here:
Edit: 04/04 - You may have problems creating clusters in westeurope, try eastus instead. https://github.com/Azure/AKS/issues/280
*Edit: 09/04 - Seems to be resolved
Digital ocean do not offer a managed / one click service (at the time of writing) although Stackpoint can do all the work for you. Link your account, select your instance sizes and magically you have a cluster ready for you.
The alternative is to spin up CoreOS / Ubuntu boxes and installing the runtimes and link the worker nodes to the master yourself (as you would if you were installing on bare metal).
For the long time the way to set up a Kubernetes cluster on AWS was using KOPS. If you want to go down that route (it is a little more complicated than the options above) here are the instructions:
However Amazon have recently released EKS (https://aws.amazon.com/eks/) in preview, which looks very similar to Azure's offering. Note: This is a managed Kubernetes offering not to be confused with ECS.
Look out for this when it is more widely available, currently you need to be invited to use it.
Google Container Engine (GKE)
This is possibly the easiest of the lot. Presuming you've signed up to Google Cloud (they will offer you a substantial amount of free usage) head to https://console.cloud.google.com/kubernetes.
Click 'Create cluster', pick a Zone and machine size. After clicking 'Create' you will have a fully managed Kubernetes cluster up and running.
After the cluster is created click 'Connect'. Execute the command provided on your local machine (the one with kubectl installed).
Setting up a kubernetes cluster on your own servers / VMs is more complicated than cloud offerings but there is some help out there for you: