Sunday, September 4, 2022

Kubernetes

 What is Kubernetes?



Kubernetes — also known as “k8s” or “kube” — is a container orchestration platform for scheduling and automating the deployment, management, and scaling of containerized applications.

Kubernetes was first developed by engineers at Google before being open sourced in 2014. 

It is a descendant of Borg, a container orchestration platform used internally at Google. Kubernetes is Greek for helmsman or pilot, hence the helm in the Kubernetes logo

Today, Kubernetes and the broader container ecosystem are maturing into a general-purpose computing platform and ecosystem that rivals — if not surpasses — virtual machines (VMs) as the basic building blocks of modern cloud infrastructure and applications. 

This ecosystem enables organizations to deliver a high-productivity Platform-as-a-Service (PaaS) that addresses multiple infrastructure-related and operations-related tasks and issues surrounding cloud-native development so that development teams can focus solely on coding and innovation. 

  • Kubernetes is a container management Platform
  • Created by Google
  • Written in Go/GoLang
  • Also known as K8s

Docker Compose V/c Kubernestes:

Difference Between Docker-Compose and Kubernetes | Baeldung



Kubernetes Architecture:







Kubernetes - Master Machine Components

Following are the components of Kubernetes Master Machine.

etcd

It stores the configuration information which can be used by each of the nodes in the cluster. It is a high availability key value store that can be distributed among multiple nodes. It is accessible only by Kubernetes API server as it may have some sensitive information. It is a distributed key value Store which is accessible to all.

API Server

Kubernetes is an API server which provides all the operation on cluster using the API. API server implements an interface, which means different tools and libraries can readily communicate with it. Kubeconfig is a package along with the server side tools that can be used for communication. It exposes Kubernetes API.

Controller Manager

This component is responsible for most of the collectors that regulates the state of cluster and performs a task. In general, it can be considered as a daemon which runs in nonterminating loop and is responsible for collecting and sending information to API server. It works toward getting the shared state of cluster and then make changes to bring the current status of the server to the desired state. The key controllers are replication controller, endpoint controller, namespace controller, and service account controller. The controller manager runs different kind of controllers to handle nodes, endpoints, etc.

Scheduler

This is one of the key components of Kubernetes master. It is a service in master responsible for distributing the workload. It is responsible for tracking utilization of working load on cluster nodes and then placing the workload on which resources are available and accept the workload. In other words, this is the mechanism responsible for allocating pods to available nodes. The scheduler is responsible for workload utilization and allocating pod to new node.

Kubernetes - Node Components

Following are the key components of Node server which are necessary to communicate with Kubernetes master.

Docker

The first requirement of each node is Docker which helps in running the encapsulated application containers in a relatively isolated but lightweight operating environment.

Kubelet Service

This is a small service in each node responsible for relaying information to and from control plane service. It interacts with etcd store to read configuration details and wright values. This communicates with the master component to receive commands and work. The kubelet process then assumes responsibility for maintaining the state of work and the node server. It manages network rules, port forwarding, etc.

Kubernetes Proxy Service

This is a proxy service which runs on each node and helps in making services available to the external host. It helps in forwarding the request to correct containers and is capable of performing primitive load balancing. It makes sure that the networking environment is predictable and accessible and at the same time it is isolated as well. It manages pods on node, volumes, secrets, creating new containers’ health checkup, etc.


The following illustrations show the structure of Kubernetes Master and Node.



#Master Node
Master is the control-plane or the brain of k8s cluster. A Master comprises of few components:
api-server - Exposes REST API to talk to k8s cluster, consumes json, only api-server talks to Cluster Store.
Cluster Store (KV) - Cluster state and config management.
Scheduler - Watches api-server for new pods  and assign node to work
Controller -  A daemon that watches the state of the cluster to maintain desired state. Example are replication-controller, namespace-controller etc.
Other than this it performs garbage collection of pods, nodes, events etc.




#Node
Kubelet - k8s agent which register nodes with cluster, watches api-server, instantiate pods, report back to the api-server. If pod fails, it reports to master and master decides what to do. Exposes port 10255 on node
Container Engine -  It does container management like pulling images, starting/stopping containers. Usually Docker is used for container runtime.
kube-proxy - Responsible for networking, Provide unique IP to Pods, All container in a pod share same IP, Load balances across all pods in a service 


#Pods
An environment to run containers
It have network stack, kernel namespaces and one or more container running
Container always runs inside a pod
Pod can have multiple containers
It is unit of scaling in k8s
 
#Services
Pods comes and go with different IPs. To distribute load and act as a single source of interaction to all pods of an application, service play the role.
Has single IP and DNS
Created with a manifest JSON file
All new pods gets added/registered to the service
Which pod should be assigned to which services is decided by labels
service and pods have labels on the basis of which service identifies its pods
only sends traffic to healthy pods
service can point things outside the cluster
uses tcp by default (udp is also supported)


#Deployments
It is a k8s object whose task is to manage identical pods running and upgrading them in controlled way.
Deployed using YAML/JSON manifest
Deployed via api-server
Provide update of pods
Provide rollbacks 


Detail Architecure:



#Overall Flow

  • kubectl writes to the API Server
  • API Server validates the request and persists it to Cluster store(etcd)
  • Cluster store (etcd) notifies back the API Server
  • API Server invokes the Scheduler
  • Scheduler decides where to run the pod on and return that to the API Server
  • API Server persists it to etcd
  • etcd notifies back the API Server. 
  • API Server invokes the Kubelet in the corresponding node
  • Kubelet talks to the Docker daemon using the API over the Docker socket to create the container
  • Kubelet updates the pod status to the API Server
  • API Server persists the new state in etcd




Kubernetes Cluster Setup Using  kubeadm:

Kubernetes is a most popular container orchestrator tool. Here we will learn how to set it up in production environment for very low scale using two nodes.

#Installation on Master and Worker Node

1. Update the existing packages and install required dependencies.

#apt-get update && apt-get install -y apt-transport-https

2. Add key.

#curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -

3. Setup kubernetes repository

#cat <<EOF > /etc/apt/sources.list.d/kubernetes.list  
deb http://apt.kubernetes.io/ kubernetes-xenial main  
EOF

4. Update repository to fetch kubernetes package.

#apt-get update

5. Install Container runtime- Docker

#apt-get install -y docker.io

6.Install kubelet (Kuberenetes node agent), kubeadm, kubectl (client tool to manage kubernetes) and kubernetes-cni (kubernetes conatiner network interface).

# apt-get install -y kubelet kubeadm kubectl kubernetes-cni


Setup Master Node

Initialise the master node using kubeadm (Run this command only on master node)

#kubeadm init

After running above command, you will get commands to setup your kubectl client and to join other nodes this cluster.
Setup kubectl client by run those commands and save cluster join command for later use.

After setting up the kubectl client, install the pod network plugin. For this tutorial, we will use the weave plugin.

#kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')"

curl https://docs.projectcalico.org/manifests/calico-typha.yaml -o calico.yaml

kubectl apply -f calico.yaml

See, node status

#kubectl get node  
NAME               STATUS            AGE  
masterserver   Ready,master   1m

===================
Error:
root@ip-172-31-37-42:~# kubectl get nodes
The connection to the server localhost:8080 was refused - did you specify the right host or port?


sudo mkdir ~/.kube
sudo cp /etc/kubernetes/admin.conf ~/.kube/
cd ~/.kube
sudo mv admin.conf config
sudo service kubelet restart


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


=====================
#Setup Worker Node

Run the command that got printed after initializing the cluster

Here, the kubernetes cluster is ready to use.

#Setup Nginx webserver
1. Create deployment

   #kubectl create deployment nginx --image=nginx:latest

2. Create a service to expose applications on node port

   #kubectl expose deployment nginx --type=NodePort --port 80

3. Check on which node port the application is running

#kubectl get svc

4.Surf the application on browser.


=====================


Minikube Setup:



Interactive Terminal:









 



No comments:

Post a Comment

Sample Game App Deployment on EKS cluster

 https://padmakshi.medium.com/setting-up-an-eks-cluster-and-deploying-a-game-application-a-step-by-step-guide-08790e0be117