Join my course at Udemy (Python Programming Bible-From beginner to advanced )

Blogger templates

Tuesday 27 August 2024

Kubernetes Architecture, POD, ReplicaSet & Replica Controller

 Kubernetes Architecture, POD, ReplicaSet & Replica Controller


1. Introduction

What is Node ? 

  •  Node is a machine - Physical or Virtual on which Kubernetes is installed,
  • Node is where containers will be launched by Kubernetes.
What will happen to Nodes in which application is running fails.
  • Application goes down -- Means Kubernetes goes down.
Why do we need to have more than one node OR Why do we need CLUSTER?
  • Because of one node on which Kuberneres is installed goes down then application will also go down. So for redundancy, we need to have more than one node.
  • So we have more than one node grouped together.
    • So even if one node goes down, application is still accessible from other nodes.
Now we have CLUSTER ? 
- So who is managing cluster ? 
- Where is the information about members of clusters stored ?
- How are nodes monitored ?
- When a node failed, how do u move workload of one node to other worked node ?

  • This is the work of Master node. ( M, S, M, F )
    • Manage, Store, Monitor, Failure Handling
When u install Kubernetes, what components are installed?
  •  Api server
    • Acts as Front end for kubernetes
    • User, Management Devices, Command Line interfaces
      • talk to API server to interact with Kubernetes cluster.
  • etcd
    • distributed, reliable key-value storage to store
      • all data used to manage cluster.
      • Also implements locks in the cluster, so that there is no conflict

  • kubelet
    • agent that runs on each node of the cluster.
    • makes sure that conatiners are running on nodes as expected.
  • container runtime
    • Underlying software that run containers.
      • For example - docker.
  • controller
    • Brain behind orchestration.
    • Responds when nodes, containers or end points goes down.
    • Take decision to bringup new containers in case of failure.
  • schedular
    • For distributing work across containers on various nodes.

Master & Worker Nodes


How some node became master and some node became worker nodes ?
- Master node that has 
  •     Kube-API server, becomes master node.
    • All information is stored in KEY-VALUE ( etcd ) store in master.
  • Master also has
    • controller and 
    • schedular
- Worked node has 
  •     kubelet agent, so it becomes worker node.
    • Through kubelet-agent worker node can interact with MASTER.
      • TO provide health information of worker node.
      • And CARRY out instruction from MASTER.

What is kubectl

- This is KUBE command line control
  • kubectl run
    • to install cluster
  • kubectl cluster-info
    • View information about cluster
  • kubectl get nodes 
    • To list all nodes of the cluster

Docker Vs Container




Docker 

- container solution.

Kubernetes 

  • kubernetes came to orchestrate docker. and they were tightly coupled.
  • But later Kubernetes provided a layer Container Runtime Interface ( CRI ) that can take into any container solution ex - docker, rkt, as long as they adhere to OCI standard (Open Container standard)
  • CRTICTL 
    • This is command line interface (CLI command) for CRI
    • This will work for any container intarface.

POD

Steps - 
- Application is built
- Has been put in docker image
- And Kubernetes can pull the image.

Aim 
- Deploy APplication
- In the form of containers
- On a set of machines
- That are configured as worker nodes.
- In a cluster

But
- Kubernetes does not deploy container directly, it has to be put in POD.

Scale up/down
- We can't add new container inside POD
  • We need to create new POD in same node.
  • We can create new POD and new Node.

Concept of multi-container pod
  • POD can have multiple container.
  • But generally they dont have multiple container of same kind
    • We can have hlper container
      • Ex - Processing file, 

YAML with Kubernetes

Creating POD using YAML based configuration file


Command to create a POD

kubectl create -f pod.yml

Mandatory Values in K8 yaml file

- apiVersion
    - Not a KEY/VALUE, we just pass a string
    - API version that we are using to create Kubernetes
        - Example value - v1 , app/v1

- kind 
    - Not a KEY/VALUE, we just pass a string
    - type of objext we are trying to create 
        - Example - POD, Service, ReplicaSet, Deployment

- Metadata
   - This is dictionary.
   - This is data of the object.
       -- Example - Name, Label etc
       -- Here 'name' and labels are dictionary. (have KEY/Value)
    - NAME and IMAGE are sibling
    - Under "MetaData" we can have only "name" and "label"
        - But under "label" we can define any "key". "value" pair.

- Spec
   -  'Spec' is a dictionary, So Add property under it.
   - Below 'spec', there is 'container'. 'container' is also a list/array 
         - because there can be multiple containers in a pod.
         - So first element in a list indicate that it is first item in the list ( - name )
   - Here name & spec is element inside 'container'


Replica Sets / Replica Controllers.

Replica Controllers

- Are the brain behind Kubernetes
- Have processes that
    - Monitor Kubernetes objects
    - And manage accordingly.

Question  - What is a replica and why do we need replication-controller ?

  •     Suppose we have a node + POD + Container running our application
  •     What will happen if our application crashes ?
  •     So users will no longer to access our application.

Solution

 - If we don't want any disruption, we will need to have multiple POD so that there will not disruption. Even if one fails, other POD will still be accessible.

    Replication Controller 

  •   helps running multiple instance of same POD in a Kubernetes cluster.
Two reasons for Replication Controller
  • High Availability
    •   And so it provides high availability.
    •   So if one pod goes down, replication controller quickly instantiates another pod.

  • Load Balancing
    • If demand increases, replication controller creates Pods
      • On same Node
      • Or No multiple Nodes.

Two terms - Replication Controller vs Replica Set

There is minor difference between both but more or less they are same.
  • Replication Controller - older way
  • Replica Set - newer way.

How to create Replication Controller - using Yaml file.

apiVersion: v1
kind: Replication Controller

metadata:
  name: myapp-rc
  labels:
    app: myapp
    type: fromt-end

spec spec defines what in inside the KIND. As POD is inside Replication Controller. So provide template of the Pod.
  template:   <<< Here need to provide template for POD. We just to insert metadata + spec section of POD.
    metadata:
      name: myapp-pod
      labels:
        app: myapp
        type: front-end
    
    spec:   
      containers: 
        name: nginx-container
        image: nginx

But we have something still missing. 

  • We have not specified how many PODs to be created? 
  • Or how many replicas of POD to be available all the time?
  • Solution : Add 'replicas: 3
apiVersion: v1
kind: Replication Controller

metadata:
  name: myapp-rc
  labels:
    app: myapp
    type: fromt-end

spec:   spec defines what in inside the KIND. As POD is inside Replication Controller. So provide template of the Pod.
  template:   <<< Here need to provide template for POD. We just to insert metadata + spec section of POD.
    metadata:
      name: myapp-pod
      labels:
        app: myapp
        type: front-end
    
    spec:   
      containers: 
        name: nginx-container
        image: nginx
replicas: 3   <<< This is sibling of template

How to create ReplicaSet - using Yaml file.

apiVersion: apps/v1    << Note for ReplicationController this is - v1
kind: ReplicaSet

metadata:
  name: myapp-replicaset
  labels:
    app: myapp
    type: front-end

spec:
  template:   <<< Here need to provide template for POD. We just to insert metadata + spec section of POD.
    metadata:
      name: myapp-pod
      labels:
        app: myapp
        type: front-end
    
    spec:   
      containers: 
        name: nginx-container
        image: nginx
replicas: 3   <<< This is sibling of template
selector:           << Only got replicaSet
  matchLabels:
    type: front-end

selector

Defines which POD fall in it.We can define multiple labels.

  • Can have labels of PODS define in the same definition
  • Can have labels defined somewhere else.

Labels & Selectors

  • Monitor POD of specified LABEL specified as part of matchLabels
  • Always keep the 3 replicas of the POD instance.
  • ReplicaSet is 'process' that monitors the PODS and if any POD fails, it creates new POD.

How ReplicaSet knows which POD to monitor ?

  • This is done by labels provided as part of matchLabels.





How to change replicas ?

Two commands
  • replace
  • scale


Share:

0 comments:

Post a Comment

Feature Top (Full Width)

Pageviews

Search This Blog