Kubernetes – Basic concepts and how to initialize a Kubernetes cluster

Tram Ho

Hi everyone, it’s me again. Today we will learn a few concepts about Kubernetes and how to create a complete Kubernetes cluster locally with VirtualBox, Vagrant and Kubectl (go).


What is Kubernetes

Kubernetes (or k8s) is an open source platform, used to automate the management, scaling and deployment of applications in the form of containers, also known as the Container orchestration engine. Kubernetes helps us to greatly eliminate the manual processes involved in the deployment and expansion of containerized applications.

Kubernetes can be abbreviated as k8s – that is, it starts with the letter “k”, the middle is 8 characters and the end is the letter “s”)

Kubernetes orchestration allows us to build extensive application services with multiple containers. It schedules those containers on a cluster, expands containers, and manages the condition of the containers over time.

Kubernetes is a powerful tool developed by Google, before going public, Google used it to manage its billions of containers.

Some definitions


A Kubernetes cluster is a set of physical or virtual machines used by Kubernetes to run applications. These virtual machines or physical machines are nodes, including master node and worker nodes. These nodes need to install docker and Kubernetes.


Kubernetes nodes are virtual machines or physical machines running kubernetes. These nodes need to run Kubernetes and docker, and each of these machines is a docker host.

Node is the smallest unit of computer hardware in Kubernetes. It is representative of a single machine in the Kubernetes cluster. In most Kubernetes systems, a node can be an actual physical machine or the virtual machine of a cloud platform like the Google Cloud Platform or AWS, or simply a virtual machine created by VirtualBox on a single machine.

We can simply treat each node as a collection of CPU and RAM resources that can be used. In this way, any machine can replace any other machine in the Kubernetes cluster.


When an application is packaged, that application will be able to run on a standalone container, although we can run the standalone container just like we would launch a monolythic application, Kubernetes will not run that way, Kubernetes use the pod concept to group containers together. A pod is a group of containers that share resources and the network, and containers in a pod can maintain communication with each other as if on a server but retain the necessary independence.

With Kubernetes, the unit when scaling an application is to scale pods, so pods are usually a group of containers that have the same purpose, for example a pod set of 4 containers running nginx + backend, and a pod set. combine 2 containers running frontend + nginx .vv

Some setup needed

In the scope of this article, I will demonstrate how to initialize a Kubernetes cluster on a single physical machine, using virtual machines running on VirtualBox. The settings are on Ubuntu 16.04

Install VirtualBox

The tool to manage virtual machines is quite familiar, supports many popular operating systems such as Windows, Ubuntu, MacOs .. etc.

Install Minikube

A Kubernetes cluster can be built on physical computers or virtual machines. To start development for Kubernetes, you can use Minikube. Minikube is a Kubernetes installer by creating a virtual machine on your computer and deploying a simple cluster inside that virtual machine that includes only one Node.

Please install the minikube to use for the context switch at the end of the article

Install kubectl

The kubectl tool helps us to control Kubernetes clusters. kubectl uses the configuration in $HOME/.kube

Install Vagrant

Vagrant is an open source software product for building and maintaining mobile virtual software development environments. It helps us to simplify virtualization software configuration management, helping to create virtualized environments quickly and efficiently.

You can learn more about what vagrant can do, basically it supports us to create a virtual machine through only one file called Vagrantfile , the configuration and parameters of the virtual machine are in. This file and we initialize the virtual machine through a single command called vagrant up

Let’s get started!

There are many ways to initialize a Kubernetes cluster, for the scope of this article I will run at the level of localhost – on my PC

Initialize the virtual machine through vagrant

To create a complete cluster we need virtual machines, in this demo I will initialize 3 virtual machines including 1 master + 2 workers

nano vagrant/master/Vagrantfile Paste the following text:

Similar to machine workers, only host name and ip differ as well as configuration options

File vagrant/worker1/Vagrantfile

File vagrant/worker2/Vagrantfile

The docker & kubernetes installation file for the centos virtual machine above, includes docker + kubernetes installation, and other settings.

File vagrant/install-docker-kube.sh

Start the virtual machines master, worker1 and worker2, respectively

Then we can check on the VirtualBox interface whether the virtual machines master, workers have been created or not:

Cluster initialization

To fully initialize the cluster we need ssh into the virtual machines and initialize via kubeadm

On the master computer, at ssh, enter the password in the vagrant config file that we configured earlier, here our password is 123

Initialize the node on the cluster’s master machine

After successful initialization, kubeadm requires us to run the following commands to be able to copy the configuration so that the machine can connect to the cluster:

So, run the following commands on the master machine:

As well as needing to install a network plugin, open the link to select an addon

Here I will use the calico addon

After the above steps, we have successfully initialized a cluster, but this cluster has only one node as master.

Next, we will proceed to join the cluster on the machine workers

In the master machine:

Turn ssh into the worker machines and use this genetically modified command to join the cluster

Then wait a few minutes and check at the master machine, this list means the worker machines have successfully joined the cluster!

So, we have successfully created a complete cluster with 3 nodes using virtual machines of VIrtualBox.

Use kubectl with context

Use contexts in kubectl

After the above demo, we have started a complete cluster, we can create another cluster with minikube

This is a tool to create a cluster locally, now I will start minikube to create a cluster, and we can switch between minikube and [email protected] as follows.

The configuration for kubectl driver is read at $HOME/.kube/config .

To change the context, we can change the environment variable KUBECONFIG to convert the context to another use.

Assuming we have the configuration of a cluster cluster1 in the file /home/chicken/.kube/config-cluster1 , in the cmd window export this environment variable to change the configuration for kubectl export KUBECONFIG=/home/chicken/.kube/config-cluster1

At localhost, check the context of what kubectl is like

This minikube is the cluster created after I started minikube on my localhost, and kubectl only controls this minikube cluster. To change the context, every time we open the terminal we have to export the environment variable as above, which will be quite inconvenient

Now let’s add a context so that kubectl can switch to the cluster we initialized earlier:

Copy the cluster configuration at the master node to localhost, remember to change the appropriate path

Check the context, now at localhost we have 2 contexts of kubernetes that we can switch.

To be able to switch the context, we run

My localhost machine can use kubectl to control the cluster [email protected] already.

So, I just presented the basic concepts of Kubernetes and how to initialize a complete Kubernetes cluster. Hope the article will be useful to those who are learning about k8s Thank you everyone for following the article!


Share the news now

Source : Viblo