Welcome to the Kubernetes Tutorial Series. I made this series of tutorials to help beginners quickly get up to speed with Kubernetes as quickly as possible. At the end of the tutorial you will have enough knowledge which will help you in getting comfortable with Kubernetes so that you can use it in your environment for deploying applications.

Hope you are excited to begin this journey. Let’s get started.

Prerequisites:

  • Basic Knowledge of Docker
  • An AWS Account with an Ubuntu machine running and an IAM role attached to this machine with administrator privileges (Only for this tutorial series. Not a good idea in production).
  • A Domain name with appropriate mapping in Route 53. You can use freenom to purchase a free domain name. Edit the NS records and you are good to go.

Series Overview:

We will cover the below topics during this tutorial series:

Introduction:

What is Kubernetes? You would have heard this word a lot that is why you are reading this article.

Kubernetes or K8s is a container-orchestration system that helps in automating deployment, scaling, and managing of containerized applications. Before Google open sourced Kubernetes and handed it over to CNCF in 2014, it was pain in the rear to manage containers at scale. There were so many challenges like communication between containers at scale, autoscaling of the containers, managing volumes etc. But after Kubernetes it’s way more simple than earlier to manage containers. That is why it is one of the most famous projects on Github with more than 45,000 stars as of this writing.

Architecture

Let’s take a look into some of the internals of Kubernetes. What are the different components that make Kubernetes and how they work together.

Kubernetes follows Master and Worker architecture. Master is responsible for all the heavy lifting which we will dive in a moment and assigning tasks to workers or just nodes.

Master Components:

API Server: This component on the master exposes the Kubernetes API. It is the front-end for the Kubernetes control plane. It means that all the requests that we as an user send using kubectl (we will install this in the below section) or any other services send inside of Kubernetes is handled by API Server and then forwarded to other appropriate components.

Controller: It helps in maintaining the desired state of your cluster. In Kubernetes we use declarative language, it means that we do not tell Kubernetes each and every step of how to do something, we only tell we want this state and Kubernetes takes care of all the underlying implementation of how to achieve it. Whenever there is a change between the desired state and the current state of the cluster, controller kicks in again makes the current state similar to desired state. Also, controller consists of many other controllers like:

  • Node Controller: Responsible for noticing and responding when nodes go down.
  • Replication Controller: Responsible for maintaining the correct number of pods for every replication controller object in the system. We will learn later in this series what a Pod is.
  • Any many more…

Scheduler: It watches api server for newly created pods that have no node assigned, and selects a node for them to run on. Basically, it schedules pods onto nodes.

etcd: This is a persistent storage which stores all cluster data. Always have a backup plan for etcd’s data for your Kubernetes cluster. If etcd is gone your whole cluster goes. There’s no way to get your Kubernetes Cluster back if you lose all your etcd data.

Worker Components:

Kubelet: An agent that runs on each node in the cluster. It does the following tasks:

  • Makes sure that containers are running in a pod.
  • Reports back to master
  • Register nodes with cluster
  • Watches api-server.

Kube Proxy: It takes care of Kubernetes networking. It enables the Kubernetes service abstraction by maintaining network rules on the host and performing connection forwarding. Don’t worry if you don’t understand this now but you will definitely understand it later in the series.

Container Runtime: It is responsible for running containers. Kubernetes supports multiple runtimes for example: Docker and rkt.

Kubernetes Architecture

Installation

We will setup our Kubernetes cluster on AWS using kops.

Installing kops:

kops helps you create, destroy, upgrade and maintain production-grade, highly available, Kubernetes clusters from the command line.Run the below commands to install kops on your ubuntu machine. This tutorial assumes that you have root or sudo privileges. Copy and paste the below code:

function install_kops {
    if [ -z $(which kops) ]
       then
           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
           chmod +x kops-linux-amd64
           mv kops-linux-amd64 /usr/local/bin/kops
       else
           echo "kops is most likely installed"
       fi
}
 
install_kops

Hit enter and kops binary should be automatically installed to your Ubuntu machine.

Check the installation by running kops in the terminal.

Installing AWS CLI:

AWS CLI will help us in communicating with our AWS account from CLI. Copy and paste the below code to install aws cli:

sudo apt-get install python2-pip
sudo pip install awscli

Check the installation by running aws in the terminal.

Installing Terraform:

Terraform will be used for spinning up the cluster. Copy and paste the below code to install Terraform:

TERRAFORM_ZIP_FILE=terraform_0.11.7_linux_amd64.zip
TERRAFORM=https://releases.hashicorp.com/terraform/0.11.7
TERRAFORM_BIN=terraform
 
function install_terraform {
    if [ -z $(which $TERRAFORM_BIN) ]
       then
           wget ${TERRAFORM}/${TERRAFORM_ZIP_FILE}
           unzip ${TERRAFORM_ZIP_FILE}
           sudo mv ${TERRAFORM_BIN} /usr/local/bin/${TERRAFORM_BIN}
           rm -rf ${TERRAFORM_ZIP_FILE}
    else
       echo "Terraform is most likely installed"
    fi
 
}
 
install_terraform 

Check the installation by running terraform in the terminal.

Installing kubectl:

Kubectl will be used to interact with the Kubrnetes Cluster. Copy and paste the below code to install kubectl:

KUBECTL_BIN=kubectl
 
function install_kubectl {
    if [ -z $(which $KUBECTL_BIN) ]
       then
           curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/$KUBECTL_BIN
           chmod +x ${KUBECTL_BIN}
           sudo mv ${KUBECTL_BIN} /usr/local/bin/${KUBECTL_BIN}
    else
       echo "Kubectl is most likely installed"
    fi
 
}
install_kubectl

Check the installation by running kubectl in the terminal.

Spinning up Kubernetes Cluster:

First of all provision a S3 bucket for storing kops state of your cluster. For this  tutorial we will use kops-bucket-sam as the name of the S3 bucket.

After that a key pair that will be used for the Kubernetes EC2 instances. Run the below commands to generate the keys. In this tutorial we will use kops as the key name.

ssh-keygen -f ~/.ssh/kops

Change the –name, –state(S3 bucket name), –dns-zone(Route 53 hosted zone), –ssh-public-key(the one which you created above) appropriately. All the fields are pretty much self explanatory.

kops create cluster \
  --name=ha.kopscms.tk \
  --state=s3://kops-bucket-sam \
  --authorization RBAC \
  --zones=eu-central-1a \
  --node-count=1 \
  --node-size=t2.micro \
  --master-size=t2.micro \
  --master-count=1 \
  --dns-zone=ha.kopscms.tk \
  --out=kops_terraform \
  --target=terraform \
  --ssh-public-key=~/.ssh/kops.pub

Now do a ls and you will be able to see a directory by the name kops_terraform. Move into that directory and run the below command to provision your Kubernetes Cluster.

terraform init
terrraform apply

Type yes and your Kubernetes Cluster should be running after 10 minutes. Check that by running kubectl get node. You should see a master and a node and both in ready state. 

Kubernetes Nodes

Once everything is installed successfully we are ready to move forward and learn about cool features like Deployment, Services etc.

Kubernetes Tutorial Series: Kubernetes Objects, Pod, Deployment, Service

Feel free to comment if you have any doubts or face any issues while setting up the cluster.

Subscribe to my newsletter if you want to learn more about Cloud and related technologies.

Leave a comment

Your email address will not be published. Required fields are marked *