Kubernetes
Kubernetes is an open source platform designed with Google’s experience in container orchestration and best ideas from the community. The users expect the web applications to be available 24/7, and the developers expect to deploy new versions of these web applications several times in a day. Containerization serve these goals by enabling applications to be released and updated in an easy and fast way without downtime. Kubernetes make sure these containerized applications run where and when required, and helps them find the resources and tools they need.
Create a cluster
Kubernetes coordinates a highly available cluster of computers that are connected to work as a single unit which deploy applications to a cluster without tying them specifically to individual machines. For this, applications need to be containerized. Containerized applications are more flexible and available than in past deployment models, where applications were installed directly onto specific machines as packages deeply integrated into the host. Kubernetes automates the distribution and scheduling of application containers across a cluster in a more efficient way.
A Kubernetes cluster consists of two types of resources:
- Master —It is responsible for managing the cluster. It coordinates all activities in cluster, such as scheduling applications, maintaining applications’ desired state, scaling applications, and rolling out new updates.
- Nodes — Each node has a Kubelet, an agent for managing the node and communicating with the Kubernetes master. It should also have tools for handling container operations.
A Kubernetes cluster that handles production traffic should have a minimum of three nodes.
When applications are deployed on Kubernetes, the master schedules the containers to run on the cluster’s nodes. The nodes communicate with the master using the Kubernetes API, which the master exposes.
Minikube can be used for Kubernetes development. It is a lightweight implementation that creates a VM on the local machine and deploys a simple cluster containing only one node. Minikube CLI provides basic bootstrapping operations for working with cluster, including start, stop, status, and delete.
- The cluster can be started by running minikube start on the terminal.
- Cluster details can be found by using kubectl cluster-info.
- To view the nodes in the cluster, run the kubectl get nodes command. This command shows all nodes that can be used to host our applications.
Create a Deployment
A Kubernetes Deployment configuration needs to be created which instructs on how to create and update instances of the application. The Kubernetes master schedules mentioned application instances onto individual Nodes in the cluster. A Deployment Controller continuously monitors those instances once they are created. If the node hosting an instance goes down or is deleted, the controller replaces the instance with an instance on another node in the cluster.
A deployment can be created and managed by using Kubectl.
Pods and Nodes
When a deployment is created, Kubernetes creates a pod to host the application instance. A pod is anabstraction that represents a group of one or more application containers, and some shared resources for those containers which includes:
- Shared storage, as Volumes
- Networking, as a unique cluster IP address
- Information about how to run each container, such as the container image version or specific ports to use
A Pod can contain different application containers which are relatively tightly coupled. The containers in a Pod share an IP address and port space, are always co-located and co-scheduled, and run in a shared context on the same node. Each pod is tied to the node where it is scheduled, and remains there until termination or deletion. In case of a failure, identical pods are scheduled on other available nodes in the cluster.
A pod always runs on a node. A node is a worker machine and may be either a virtual or a physical machine, depending on the cluster. Each node is managed by the master. A Node can have multiple pods, and the master automatically handles scheduling the pods across the nodes in the cluster by taking into account the available resources on each node.
Every Node runs at least:
- Kubelet, a process responsible for communication between the Master and the Node. It also manages the Pods and the containers running on a machine.
- A container runtime responsible for pulling the container image from a registry, unpacking the container, and running the application.
Kubernetes Services
A service is an abstraction which defines a logical set of pods and a policy by which to access them. Services enable a loose coupling between dependent pods. It is defined using YAML or JSON. It allows pods to die and replicate in Kubernetes without impacting the application.
Each pod has a unique IP address but those IPs are not exposed outside the cluster without a service. Services allow the applications to receive traffic. Services can be exposed in different ways by specifying a type
in the ServiceSpec:
- ClusterIP (default) — Exposes the Service on an internal IP in the cluster. This type makes the Service only reachable from within the cluster.
- NodePort — Exposes the Service on the same port of each selected Node in the cluster using NAT. Makes a Service accessible from outside the cluster using
<NodeIP>:<NodePort>
. Superset of ClusterIP. - LoadBalancer — Creates an external load balancer in the current cloud and assigns a fixed, external IP to the Service.
- ExternalName — Exposes the Service using an arbitrary name by returning a CNAME record with the name. No proxy is used.
Scaling the Application
The deployment creates only one Pod for running theapplication. When traffic increases, the application need to be be scaled to keep up with user demand. It can be accomplished by changing the number of replicas in a deployment. This will create new Pods and ensure that they are scheduled to nodes with the available resources. Kubernetes also supports autoscaling of pods. Running multiple instances of an application will require a way to distribute the traffic to all of them. Services have an integrated load-balancer that will distribute network traffic to all pods. Services will monitor continuously the running Pods using endpoints, to ensure the traffic is sent only to available Pods.
Updating the application
Rolling updates allow deployments’ update to take place with zero downtime by incrementally updating pods instances with new ones. The new pods will be scheduled on nodes with available resources. By default, the maximum number of Pods that can be unavailable during the update and the maximum number of new Pods that can be created, is one. Both options can be configured to either numbers or percentages. In Kubernetes, updates are versioned and any deployment update can be reverted to previous (stable) version.
Thank you for reading!