Introduction to Kubernetes
The introduction to Kubernetes first happened in the year 2014, by
none other than the technology giant Google. A few Google engineers
worked upon it, alongside Brendan Burns, Joe Breda, and Craig
McLuckie. The design and development of Kubernetes relied heavily
upon the Borg System of Google. Incidentally, a majority of the
contributors had already worked upon Borg.
The project was initially given the name of Project Seven of Nine,
based on a Star Trek reference. The logo contains seven spokes,
which stand for that codename. They used C++ language to write the
original Borg project. However, the rewritten system is implemented in
Go. Kubernetes v1.0 was introduced in 2015 for use. Google, along
with the release, decided to partner with Linux Foundation.
What is Kubernetes
It's essential to have an understanding of containers before talking about
Kubernetes. Containers are executable software packages containing all
the code and dependencies necessary to run any application. And by
anywhere, I mean anywhere - my laptop, your laptop, or even on "the
cloud."
Are containers starting to sound similar to virtual machines? Well, they're
not, and they're even better! Why? Because, unlike VMs, containers do not
contain operating system images. This means they're much more
lightweight! To create and run containers locally, you would use a
container engine like Docker.
Containers grew in popularity because they genuinely embraced the
ideology of write once, run anywhere. Because of how fast and easy it is to
deploy containers, they increased developer productivity significantly.
But a new problem arose as more companies started using containers.
How do you manage thousands of containers running on distributed
systems? And that's where Kubernetes saved the day!
What is Kubernetes Used For?
Kubernetes, often abbreviated as K8s, is a container orchestration
platform. Let me explain this in straightforward terms.
Let's say you have your app, which you have containerized. So you run a
bunch of containers to serve your app to users. But how do you manage
these different containers? The situation is complicated even more by the
fact that these containers needn't necessarily be running on the same
machine!
This is where K8s comes to the rescue. Kubernetes makes deploying,
managing, and scaling containerized workloads a breeze! I want to
explicitly call out here that K8s is NOT a container engine like Docker.
Rather it is a container orchestrator.
Kubernetes Architecture Basics
In the Kubernetes world, a machine is called a node. A K8s "cluster" is
simply a group of nodes that run your containerized application. These
nodes may be bare metal servers in your home or instances you're running
on a cloud provider. These nodes are also of two types: worker and master.
The worker nodes are where your application's containers would be
running. Kubernetes uses an object called Pod to run these containers but
more on that in a section below. Three essential things running on a
worker node in a Kubernetes cluster are:
1. Container runtime: This should be no surprise since a container
runtime is what is responsible for running containers. Kubernetes
supports multiple container runtimes, including Docker and
container.
2. kubelet: This is the agent which is responsible for communicating
with the Kubernetes control plane running on the master node. It is
responsible for executing any instructions it receives from the control
plane. It also makes sure that containers are running in the pods
properly.
3. kube-proxy: This is a network proxy running on each node. Not
going into much detail, you can think of it as being responsible for
network communications within and outside the cluster
The master node is the one that has all the components of the Kubernetes
control plane running on it. You can also set up multiple master nodes for
high availability. Following are some important control plane components
you should be familiar with:
1. kube-apiserver: This exposes the Kubernetes API, which we can
use to talk to our K8s cluster. You can think of the kube-apiserver as
the frontend for the Kubernetes control plane.
2. etcd: This is the distributed key-value store where all cluster-related
data is stored. For security reasons, it is only accessible through the
API server. This restriction also helps ensure that the data stored in
etcd is consistent.
3. kube-scheduler: This is responsible for scheduling pods on available
worker nodes. While scheduling, it takes into consideration the
resource requirements of the pod and any other constraints that we
specify.
4. kube-controller-manager: A controller in K8s is responsible for
observing an object and making sure that the current state of that
object matches the desired state we specified. There are various
controllers in K8s for different objects. All these are combined into a
single binary which is the kube-controller-manager.
5. cloud-controller-manager: This component lets you link your cluster
to a cloud provider. This binary contains all the cloud provider-
specific controllers. So when something like a node termination
happens in the cloud provider, this component will be responsible for
handling that.
Some major advantages to learn Kubernetes are:
Make better use of the hardware to maximize the
resources needed to run enterprise apps
Orchestrate containers over multiple hosts
Mount or add storage to run apps
Control and automate application deployment
Scale the containerized applications
Health check and also self-heal the apps
Installation of Kubernetes
Install these commands on Master and Workstation
sudo apt-get update
sudo apt-get install apt-transport-https
apt install docker.io -y
systemctl start docker
systemctl enable docker
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add
vi /etc/apt/sources.list.d/kubernetes.list
copy this line
deb https://apt.kubernetes.io/ kubernetes-xenial main
apt-get update
sudo apt-get install -y kubectl kubeadm kubelet kubernetes-cni
Master
Bootstrapping the master node
kubeadm init
To start using your cluster, you need to run the following as a regular user:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
Alternatively, if you are the root user, you can run:
export KUBECONFIG=/etc/kubernetes/admin.conf
You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
https://kubernetes.io/docs/concepts/cluster-administration/addons/
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 172.31.93.230:6443 --token nhrsdf.1to4wb29823cjqq4 \
--discovery-token-ca-cert-hash
sha256:c02c9066dffd734513cb75f975fa3ba7d8155791b10f6162b614c8178b248eb8
kubectl apply -f
https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
-allow port inbound 6443
-apply on master
1. sudo -i
2. swapoff -a
3. exit
4. strace -eopenat kubectl version
$ sudo systemctl stop kubelet
$ sudo systemctl start kubelet
$ strace -eopenat kubectl version
then
kubectl get pods --all-namespaces
kubectl get nodes