Monday, September 10, 2018

Kubernetes

Scenario before Kubernetes :  How Kubernetes came into the existence ?

https://github.com/kubernetes/kubernetes/issues/18266

Kubernetes - Developer Guide https://kubernetes.io/docs/community/devel/#setting-up-your-dev-environment-coding-and-debugging

When computing world became distributed, more network-based and more relied on cloud computing; monolithic apps migrated to Microservices.

 These Microservices enabled users to individual scale key functions and has an ability to handle millions of customers.

On the top of that, tools like Docker container, Mesos, AWS ECS emerge into the enterprise, creating a consistent, portable and easy way for the users to deploy Microservices.


But, once the application gets matured and complex, there will be a need to run multiple containers across multiple machines. You need to figure out which is the right containers and at a right time of course, how they can communicate with each other, tackle with the large storage need and deal with the failed container. Doing all this manually can be a mere nightmare!
Hence, to solve the orchestration needs of the containerized application, Kubernetes came to the scenario.





Kubernetes  vs  AWS  EC2

What is Kubernetes ?    - Hottest container management technology.

Kubernetes is an open source container management system which is used in large-scale enterprises in several vertical industries to perform a mission-critical task. It manages
  • Cluster of containers
  • Provide tools for deploying applications
  • Scale applications as and when needed
  • Manage changes to the existing containerized applications
  • Helps to optimize the use of underlying hardware beneath your container
  • Enables the application component to restart and move across the system as and when needed
Kubernetes provides much more beyond the basic framework, enabling users to choose the type of application frameworks, languages, monitoring and logging tools and other tools of their choice. Although it is not Platform as a Service but can be used as a basis for complete PaaS.
Since a few years, it has become a highly popular tool and one of the biggest success stories on the open-source platform.





Where did it come from ?

          -  From  Google,  Donated to CNCF in 2014(open source)
          -  Written in Go/Golang
          -  https://github.com/kubernetes/kubernetes
          -  IRC, @kubernetesio, slack.k8s.io,Meetups...

What does the name mean ?

What does it do ?   &  Why do we have it ?

-->  Containers bring scalability challenges
-->  We are starting to view the data center as a computer!!
-->  Standard package format
--> Manifest - Job done.
--> Kubernetes is strongly positioned.
-->  Very platform agnostic.
-->  Lets you target deployments.



Kubernetes Architecture  

Kubernetes  -  It's an Orchestrator for Microservice Apps

  • Big Picture View

  • Masters
    • Is the Kubernetes Control Plane.
    • Kubernetes cluster is effectively a bunch of masters and nodes.
    • It runs on LInux, but it's just not opinionated whether underneath that it's bare metal, VMs, cloud.
    • Don't run the user workloads on "Master"
    • Kube-apiserver   -  Front-end  into the  master or  the control plane.  The only  one with a proper external-facing interface.   It exposes a RESTful API and it consumes JSON.
    • Flow - we send the manifest files to Master ( here kube-apiserver) - thse declare the state of our app, like a record of intent, the master validates it, and it attempts to deploy it on the cluster.  Then there's the cluster store( Persistent storage, cluster state of config).  Now if the apiserver is the brains of the cluster.
    • Kube-controller-manager  
      • Controller of Controllers
        • Node Controller
        • Endpoints controller
        • Namespace controller
        • ...
        • ...
        • watches for changes.
        • Helps maintain desired state
    • Kube-Scheduler
      • Watches apiserver for new pods, and it assigns them to workers/nodes  - like  affinity/anti-affinity, constraints, resources( in high level)
    • Scheduler: It helps to schedule the pods on various nodes based on resource utilization and decides where to deploy which service. The scheduler has the information regarding the resources available to the members as well as the one which is left for configuring the service to run.
                                         

  • Nodes a.k.a - Minions  https://kubernetes.io/docs/concepts/architecture/nodes/
    • The Kubernetes workers.
    • The 3 things that we have to care -  
      • The kubelet
        • Main kubernetes agent on node,  install it on a Linux host, it registers the host as a node in the Kubernetes cluster, REgisters the node with cluster, and then it watches the apiserver on the master for work assignments.
        • Any time it sees one, it carries out the task and then it maintains a reporting channel back to the master.
        • Instatiates pods
        • Reports back to master
          • For example,  if POD fails on a node, the kubelet is not responsible for restarting it or finding another node  to run on it,  it simply reports the STATE back to the MASTER, and actually speaking reporting back.
          • The kubelet exposes an endpoint on the local host on port 10255 where you can inspect it.

      • The container runtime
        • Pulling images.
        • Starting/stopping containers.
        • ....
        • pluggable :
          • Usually Docker  (  uses the native Docker API
          • Can be rkt
      • kube-proxy
        • This is like  the network brains of the node
        • Make sure that every POD gets its own unique IP, i.e.,  one IP per POD, so if you are bit advanced and you're running pods with multiple containers in them, all of those containers are going to share a single IP.  So  if you want to address individual containers within the POD, you're going to be using ports, and the likes.
        • Load balances across all PODs in a service.
      • Etcd Storage: It is an open-source key-value data store developed by CoreOS team and can be accessed by all nodes in the cluster. Kubernetes uses ‘Etcd’ to store configuration data of the cluster to represent the overall state of the cluster anytime.

                               
  • Kubernetes Architecture

Declarative  Model  &  Desired State

Kubernates operati
                     A Pod is the basic building block of Kubernetes–the smallest and simplest unit 
               in the Kubernetes object model that you create or deploy. A Pod represents a 
               running process on your cluster.
                A Pod encapsulates an application container (or, in some cases, multiple 
                containers), storage resources, a unique network IP, and options that govern 
                how the container(s) should run. A Pod represents a unit of              
                 deployment: single    instance of an application in Kubernetes, which might 
                consist of either a single container or a small number of containers that are 
                tightly coupled and that share resources.
  • Services
  • Deployments




Briefly  -  Kubernetes   orchestrating  among  Node1, Node 2, Node3.. Node n( Nodes a.k.a MINIONS) ... respective with  Auth, HTTPS,  Load balancer, Search, K/V store, Log, MySQL etc..

1)  We got our code,  and we containerize it.
2)  Then we define it in an object called a deployment, this is in .YML  file  which  is just manifest
     that tells Kubernetes what our app should look like,  what images to use, what ports, networks,
     how many replicas, all that stuff, right, in a file.
3)  Then  - give the deployment s YAML  file to  Kubernetes - Master, then the master looks at the file and deploys the app.







Installing Kubernetes

Minikube


Working with Pods

Kubernetes pod is a group of containers that are deployed together on the same host. If you frequently deploy single containers, you can generally replace the word "pod" with "container" and accurately understand the concept.


Uses of pods :

Pods can be used to host vertically integrated application stacks (e.g. LAMP), but their primary motivation is to support co-located, co-managed helper programs, such as:
  • content management systems, file and data loaders, local cache managers, etc.
  • log and checkpoint backup, compression, rotation, snapshotting, etc.
  • data change watchers, log tailers, logging and monitoring adapters, event publishers, etc.
  • proxies, bridges, and adapters
  • controllers, managers, configurators, and updaters



Working with Deployments

dfdfd



Cheat Sheet  - REf - https://kubernetes.io/docs/reference/kubectl/cheatsheet/


Interacting with Nodes and Cluster

kubectl cordon my-node                                                # Mark my-node as unschedulable
kubectl drain my-node                                                 # Drain my-node in preparation for maintenance
kubectl uncordon my-node                                              # Mark my-node as schedulable
kubectl top node my-node                                              # Show metrics for a given node
kubectl cluster-info                                                  # Display addresses of the master and services
kubectl cluster-info dump                                             # Dump current cluster state to stdout
kubectl cluster-info dump --output-directory=/path/to/cluster-state   # Dump current cluster state to /path/to/cluster-state

# If a taint with that key and effect already exists, its value is replaced as specified.
kubectl taint nodes foo dedicated=special-user:NoSchedule

Interacting with running Pods

kubectl logs my-pod                                 # dump pod logs (stdout)
kubectl logs my-pod --previous                      # dump pod logs (stdout) for a previous instantiation of a container
kubectl logs my-pod -c my-container                 # dump pod container logs (stdout, multi-container case)
kubectl logs my-pod -c my-container --previous      # dump pod container logs (stdout, multi-container case) for a previous instantiation of a container
kubectl logs -f my-pod                              # stream pod logs (stdout)
kubectl logs -f my-pod -c my-container              # stream pod container logs (stdout, multi-container case)
kubectl run -i --tty busybox --image=busybox -- sh  # Run pod as interactive shell
kubectl attach my-pod -i                            # Attach to Running Container
kubectl port-forward my-pod 5000:6000               # Listen on port 5000 on the local machine and forward to port 6000 on my-pod
kubectl exec my-pod -- ls /                         # Run command in existing pod (1 container case)
kubectl exec my-pod -c my-container -- ls /         # Run command in existing pod (multi-container case)
kubectl top pod POD_NAME --containers               # Show metrics for a given pod and its containers

c# Get commands with basic output
kubectl get services                          # List all services in the namespace
kubectl get pods --all-namespaces             # List all pods in all namespaces
kubectl get pods -o wide                      # List all pods in the namespace, with more details
kubectl get deployment my-dep                 # List a particular deployment
kubectl get pods --include-uninitialized      # List all pods in the namespace, including uninitialized ones

# Describe commands with verbose output
kubectl describe nodes my-node
kubectl describe pods my-pod

kubectl get services --sort-by=.metadata.name # List Services Sorted by Name

# List pods Sorted by Restart Count
kubectl get pods --sort-by='.status.containerStatuses[0].restartCount'

# Get the version label of all pods with label app=cassandra
kubectl get pods --selector=app=cassandra rc -o \
  jsonpath='{.items[*].metadata.labels.version}'

# Get all running pods in the namespace
kubectl get pods --field-selector=status.phase=Running

# Get ExternalIPs of all nodes
kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="ExternalIP")].address}'

# List Names of Pods that belong to Particular RC
# "jq" command useful for transformations that are too complex for jsonpath, it can be found at https://stedolan.github.io/jq/
sel=${$(kubectl get rc my-rc --output=json | jq -j '.spec.selector | to_entries | .[] | "\(.key)=\(.value),"')%?}
echo $(kubectl get pods --selector=$sel --output=jsonpath={.items..metadata.name})

# Check which nodes are ready
JSONPATH='{range .items[*]}{@.metadata.name}:{range @.status.conditions[*]}{@.type}={@.status};{end}{end}' \
 && kubectl get nodes -o jsonpath="$JSONPATH" | grep "Ready=True"

# List all Secrets currently in use by a pod
kubectl get pods -o json | jq '.items[].spec.containers[].env[]?.valueFrom.secretKeyRef.name' | grep -v null | sort | uniq

# List Events sorted by timestamp
kubectl get events --sort-by=.metadata.creationTimestamp
10 Kubernetes tutorials that you can try now :
Since we all like #Kubernetes so much, I thought to share some cool tutorials you can try around.

1. Deploy Kubernetes: The Ultimate Guide - https://lnkd.in/eCjzK7d

2. How to deploy a NodeJS app to Kubernetes - https://lnkd.in/eRihjdt

3. 5-Step Kubernetes CI/CD Process - https://lnkd.in/gZCMeyq

4. Follow a Kubernetes and Go tutorial in your home lab - https://lnkd.in/eWzyC_R

5. Getting started with Cloud Endpoints on Kubernetes - https://lnkd.in/eZ3J2XB

6. An illustrated guide to Kubernetes Networking - http://bit.ly/2GbJ6d3

7. Implementing Azure-Managed Kubernetes and Azure Container Service - https://lnkd.in/e7Jme9d

8. Tutorial: Deploying apps into Kubernetes clusters - https://ibm.co/2v2rH10

9. Brigade tutorial: Instant scaling on demand with serverless Kubernetes - https://lnkd.in/e8gFNZa

10. Getting started with CI/CD for Kubernetes, featuring Jfrog, Helm and Draft! - https://lnkd.in/e4S-EF9

Share if you know any.




No comments:

Post a Comment

Hyderabad Trip - Best Places to visit

 Best Places to Visit  in Hyderabad 1.        1. Golconda Fort Maps Link :   https://www.google.com/maps/dir/Aparna+Serene+Park,+Masj...