Skip to content

Rahul-Chauhan-2212/Kubernetes

Repository files navigation

Kubernetes

Kubernetes Introduction

Kubernetes
Independent Container Orchestration

Kubernetes, also known as K8s, is an open-source system for automating deployment, scaling, and management of containerized applications.

Problems with Manual Deployment

  • Manual Deployment of containers is hard to maintain, error-prone and annoying. These issues are beyond the security and configuration concerns.
  • Containers might go down/crash and need to be replaced.
  • We might need more containers when traffic spikes.
  • Incoming traffic should be distributed equally.

Why Kubernetes

  • Container health-checks and re-deplyoment
  • Auto-scaling
  • Load Balancer

Note: These all problems are solved if we use AWS ECS, but we are locked to AWS. If we want to migrate to Azure or other cloud container service provider, then we have to learn about that service.

What Kubernetes is NOT

  • a cloud provider - a cloud provider service (though cloud provider might offer Kubernetes-specific services)
  • a tool or service that manages infrastructure - Kubernetes will NOT create and launch anymachines or do anything like that (managed Kubernetes services by cloud providers might dothat)
  • a single tool or software - Kubernetes is a collection of concepts and tools

What is Kubernetes exactly

An open-source system(de-facto standard) for orchestrating container deployments. Helps us in below tasks:

  • Automatic Deployment
  • Scaling and Load Balancing
  • Management
Kubernetes Configuration: Standardized way of describing the to-be-created and to-be-managed resources of Kubernetes Cluster
Kubernetes is like Docker Compose for Multiple Machines

Kubernetes Core Components

  • Cluster: A set of Node machines which are running the Containerized Application (Worker Nodes) or control other Nodes (Master Node)
  • Nodes: Physical or virtual machine with a certain hardware capacity which hosts one or multiple Pods and communicates with the Cluster
    1. Master Node: Cluster Control Plane, managing the Pods across Worker Nodes
    2. Worker Node: Hosts Pods, running App Containers (+ resources)
  • Pods: Pods hold the actual running App Containers + their required resources (e.g. volumes).
  • Containers: Normal (Docker) Containers
  • Services: A logical set (group) of Pods with a unique, Pod- and Containerindependent IP address

Kubernetes in Action : Diving into Core Concepts

Kubernetes' Work
What Kubernetes Does What You have to Do
Create your objects(Pods) and Manage them Create the Cluster and the Node instances(Master and Worker Nodes)
Monitor Pods, re-create them, Scale Pods Setup API server, kubelet and other Kubernetes services/softwares on nodes
Kubernetes utilizes the provided resources to apply your configuration/goals Create other(cloud) provider resources that might be needed(File Systems and Load Balancers)
Kubernetes: Required Setup and Installation Steps
  1. kubectl A command line tool to send instructions to Kubernetes Cluster(Mainly Master Node) using Kubernetes APIs
  2. minikube A local Kubernetes, focusing on making it easy to learn and develop for Kubernetes
Start Minikube using Docker
minikube start --driver=docker
To check status of cluster
minikube status
To open Minikube Browser Dashboard
minikube dashboard
To delete Minikube cluster
minikube delete

Kubernetes works with Objects

Objects
  • Pods
  • Deployments
  • Service
  • Volumes
Objects can be created in two ways
  • Imperatively
  • Declartively
The "Pod" Object

The smallest unit Kubernetes interacts with

  • Contains and runs one or multiple containers(mainly one container per pod)
  • Pod contains shared resources(volumes) for all Pod containers
  • Has a cluster internal IP by default(containers inside a Pod can communicate via localhost)

Pods are designed to be ephemeral: Kubernetes will start, stop and replace them as needed.

For Pods to be managed for you, you need a Controller(i.e Deployment)

The "Deployment" Object

Controls(Multiple) Pods

  • You set a desired state, Kubernetes then changes the actual state

    Define which pods and container to run and number of instances

  • Deployments can be paused, deleted and rolled back
  • Deployments can be scaled dynamically(and automatically)

Deployments manage a Pod for you, you can also create deployments.

You therefore don't directly control the Pods, instead you use deployment to set up the desired end state


A First Deployment using Imperative approach

Project used ---> kub-action-starting-up

    Commands used to create first deployment
  • kubectl create deployment name-of-deployment --image=image-name-with-repo-on-dockerhub

    Used to create deployment on the basis of an image on Docker registry. kubectl create deployment first-app --image=rchauhan9102/kub-first-app

  • kubectl get deployments

    Used to get the deployments and their info

  • kubectl get pods

    Used to get the pods and their info

  • kubectl delete deployment name-of-deployment

    used to delete deployment and in result pods are also deleted. kubectl delete deployment first-app

kubectl: Behind the Scenes

kubectl create deployment name-of-deployment --image=image-name-with-repo-on-dockerhub
Commands goes to Master Node(Control Plane) ---> Scheduler analyses currently running pods and find the best node(Worker node) for our pod which is free or doing the least work. kubelet manages Pods and Containers.

The "Service" Object(Resource)

Exposes Pods to the cluster or externally

  • Pods have an internal IP address which changes when the Pod is replaced

    Finding Pods is hard if the IP address changes all the time

  • Service group a Pod with a shared IP address
  • Services can allow external access to Pods

    The default(internal only) can be overwritten

Without services Pods are very hard to reach and communication is difficult

Reaching ad Pod from outside of the cluster is not possible at all without services

Exposing a deployment with a service

    Commands used to expose deployment with service
  • kubectl expose deployment first-app --type=LoadBalancer --port=8080

    This command exposes the deployment first-app on port 8080 create service with type LoadBalancer to so that we can get a shared IP which can be accessed from outside of the cluster.

  • kubectl get services

    used to get the list of services available

  • minikube service first-app

    exposes the deployment using the service to the localhost machine. This command is not required if we are running the kubernetes cluster on a Cloud provider

Types of Service Objects
  • ClusterIP

    IP address only available to Kubernetes Cluster

  • NodePort

    IP address of the Worker node(Machine IP at which Worker node is running)

  • LoadBalancer

    Shared IP for all the services and available to outside of the cluster

Restarting Containers

If there is any error in the running container, the Kubernates will restart the container as well as Pod

Scaling in action

kubectl scale deployment/first-app --replicas=3

create replicas of the first-app pod

Updating Deployments

    ### Steps
  • Build new image with latest source code and the incrementing tag
  • Push the image with new tag to dockerhub
  • kubectl set image deployment/first-app kub-first-app=rchauhan9102/kub-first-app:1

    to update the deployment with newly pushed docker image on dockerhub

  • kubectl rollout status deployment/first-app

    to get the rollout status of updated deployment

To make kubernetes pull the image, there should be change in new image tag

Deployment Rollbacks and History

  • kubectl rollout undo deployment/first-app

    To undo the latest deployment and rolled back to the previous one

  • kubectl rollout history deployment/first-app

    To get the history to the deployment

  • kubectl rollout history deployment/first-app --revision=6

    To rollback to a specific deployment

    Deletion Commands
  • kubectl delete service first-app
  • kubectl delete deployment first-app

The Imperative vs Declarative approach

Imperative Declarative
kubectl create deployment .. kubectl apply -f config.yaml
Individual commands are executed to trigger certain kubernetes actions A config file is applied and used to change the desired state
Comparable to using docker run only Comparable to using docker compose with compose files

Declarative Deployment

Reference Doc to create yaml file for Declarative Approach

  • kubectl apply -f=deployment.yaml

    Used to apply the deployment related changes

  • kubectl apply -f=service.yaml

    Used to apply the service related changes

  • kubectl delete -f=deployment.yaml,service.yaml

    Used to delete the deployment and services

To update the deployment we just have modify the yaml file and apply those changes

Labels and Selectors are necessary... Labels and Selectors are used to make a link between pods and services

We can have multiple resources in one single deployment file by adding --- in yaml file. It is better pratice to create service before deployment.

More on Labels and Selectors
  • matchExpressions:
    - key: app
    operator: In
    values:
    - second-app
    - first-app
    We can use matchExpressions in place of matchLabels to select on the basic of key value pair and operator.

  • kubectl delete -l labelKey=labelValue deployments,services

    Used to delete the resoures on the basis label key value defined in applied config file

Liveness Probes
  • livenessProbe:
    httpGet:
    path: /
    port: 8080
    periodSeconds: 10
    initialDelaySeconds: 5

    Used to get the health of the running container

  • Configuration Options

    • imagePullPolicy: Always

    Force kubernetes to pull the image always during update in deployment


Managing Data and Volumes with Kubernetes

    State

    State is data created and used by your application which should not be lost

  • User-generated data, user-accounts

    Often stored in databases but can also be file(e.g. Upload files)

  • Intermediate results derived by the app

    Often stored in memory, temporary database tables or files

Kubernetes and Volumes

    Kubernetes can mount volumes into containers
  • A broad variety of volumes/driver types are supported
    1. Local Volumes(i.e. On Nodes)
    2. Cloud provider specific volumes
  • Volume lifetime depends on the Pod lifetime
    1. Volume survives containers re-starts(and removal)
    2. Volumes are deleted if the Pods are destroyed
  • Kubernetes Volumes vs Docker Volumes

    Kubernetes Volumes Docker Volumes
    Supports many different drivers and Types Basically no driver/type support
    Volumes are not necessarily persistent Volumes persists until manually cleared
    Volumes survives container restarts and removal Volumes survives container restarts and removal

Getting started with Kubernetes Volumes

Kubernetes Volumes Documentation

    Some Common Kubernetes volume types
  • emptyDir

    Creates an empty directory and is linked with a container path so content can be written and saved at the newly created empty directly in Worker Node. It is not suitable for multiple replicas.

  • hostPath

    Used for Multiple Replicas. It stores data in host machine(Worker Node) and available for all replicas.

  • csi (Container Storage Interface)

    Used for any outside file system which is very flexible. You just need integration driver available for that file system.

Persistent Volumes

Volumes are destroyed when a Pod is removed. hostPath partially work around that is one-node environments like minikube

Pod- and Node-dependent volumes are sometimes required.--->Persistent Volumes

Persistent Volumes

"Normal" Volumes vs Persistent Volumes
"Normal" Volumes Persistent Volumes
Allows to persist Data Allows to persist Data
Volume is attached to Pod and Pod Lifecycle Volume is a Standalone Cluster Resource(Not attached to a Pod)
Defined and Created together with Pod Created standalone and claimed via PVC
Repetitive and hard to administer on a global level Can be defined once and used multiple times
    Steps to create Persitent Volumes
  • Define a Persistent Volume
  • Define a Persistent Volume Claim
  • Using a Claim in Pod
    Commands for Persistent Volume Deployment
  • kubectl get sc

    To get the Storage Classes

  • kubectl apply -f="host-pv.yaml"

    To apply Persistent Volume Config

  • kubectl get pv

    To get the List of Persistent Volumes

  • kubectl apply -f="host-pvc.yaml"

    To apply Persistent Volume Claim

  • kubectl get pvc

    To get the List of Persistent Volume Claims

  • kubectl apply -f="deployment.yaml"

    To update the deployment

Environment Variables

We can set using env tag at containers tag in deployment.yaml

  • kubectl apply -f="environment.yaml"

    To Apply config Map

  • kubectl get configmap

    To get Config Map


Kubernetes Networking

  • Pod-internal Communication

    For Pod internal Communication(Same Pod Container), we can user localhost as the address for the second container.

  • Pod to Pod Communication
    1. Using Pod Address and Environment Variables

      Pod to Pod Communication can be achieved using the IP address of Pod(Cluster IP of the Service) and environment variables generated by Kubernetes

    2. Using DNS
      Core DNS - Domain Name Service

      We can use servicename.namespace too for Pod to Pod communication

      kubectl get namespaces

      To get the namespaces

kubectl logs -f podId

To get the logs of the Pods


Kubernetes Deployment(AWS EKS)

Deployment Options and Steps

  • Custom Data Center

    You need to install and configure everything(i.e. Machines and Kubernetes Software)

  • Cloud Provider
    1. You do all the installation and configuration on your own(Manually or via Kops)
    2. Managed Service(Just need to define cluster architecture) i.e. AWS EKS
Steps to create EKS Cluster and Deploy Apps on it
  • Create EKS Cluster

    1. Configure Cluster
    2. Create EKS role for EKS Cluster
    3. Create VPC using Cloud Formation Stack Creation
  • Update Kubectl Config

    {UserHome}/.kube/config

    This file is used to by kubectl to connect with minikube We need to override this file. This we can user AWS CLI. We need to download and install it so that we can communicate with the AWS Cluster from our local machine. For that we need to create Access Key from IAM.

    1. aws configure
      1. enter access key
      2. enter access secret key
      3. enter default region
    2. aws eks --region regionName update-kubeconfig --name clusterName

      This will update local kubectl config file to connect to AWS Cluster

  • Adding Worker Nodes

      On Cluster-->Compute
      Create Node Group
    1. Create EC2 Node Role
      Add Permissions AmazonEKSWorkerNodePolicy, AmazonEKS_CNI_Policy, AmazonEC2ContainerRegistryReadOnly
    2. Set Compute and Scaling Configurations
    3. Specify Networking
  • Applying Kubernetes Config

    Simply run the kubectl apply to apply the configs.
    AWS automatically create a Load Balancer in EC2 if the service is of type Load Balancer
    for Load Balancer type service we will get an AWS url using which we can access our app.
  • Adding Volumes to EKS

      Adding EFS as a Volume(with CSI Volume Type)
    1. Install AWS EFS CSI Driver into Cluster using deploy command on this page
    2. Create Security Group for EFS using VPC being used for EKS. Set the Inboud/outbound rules.
    3. Create EFS using EKS VPC.
    4. Create Persistence Volume and Claim for EFS
      kubectl get sc

      To get the storage Classes

  • Use the EFS Volume