Intro to Kubernetes

Credits Due : this is From: Nigel Poulton. “The Kubernetes Book. and Kubernetes in action and kubernetes up and running book notes together. These are recommended buys.

Kubernetes - An Orchestrator for Microservice App.

Premise/UseCase

There are multiple microservice apps each running in its own container. But these individual microservices will need to talk to each other and each have its own job to do. Each service will have its own configurations and number of instances it need to run. Every service need to be scaled up and down. So here is where Kubernetes ( which is usually mentioned as K8 ) comes into play.

Infrastructure

K8 has a master ( is incharge of worker nodes ) and nodes ( many nodes or workers which do the work ) .

Master makes the decision which node to do the work. It is the controller.

This master and multiple nodes make up K8 cluster

Then we have a deployment object , which is a configuration file which has instructions for the master to implement using the worker nodes. You can think of Deployment as a high level requirement which the master or manager will implement using the worker or team member :) Best Analogy I can come up with.

service_k8

Master

Kubernetes is platform agnostic. It runs on Linux, but not opinionated underlying platform. Multi-master HA is doable and complex.

Keep the Master is free of workload. It is clean that way.

Apiserver

Cluster Store

Controller Manager

Kube-scheduler

APIServer is the only component that master exposes to the outside world.

Kubectl is the command issuer to the APIServer in the master component.

Nodes

Kubelet

Container Engine

Kube-proxy

Desired State

How the workflow of K8 looks like internally.

Nodes

Deploying Pods

Services

service_k8

Labels

service-labels

Pod Theory

Container -> Pod -> ReplicaSets -> Deployments

Services

Service Discovery ( Need to be Updated )

Service Type

# svc.yml

apiVersion: v1
kind: Service
metadata:
  labels:
    run: myapp-service
  name: myapp-service
spec:
  ports:
  - port: 9000
    protocol: TCP
    targetPort: 9000
  selector:
    run: myapp-service
  sessionAffinity: None
  type: NodePort

Kubectl describe pods | grep app
 # selector part of svc should match with the pod. 

# create the service
Kubectl create -f svc.yml

# now lets see the created service.
Kubectl describe svc myapp-service

# we can see the endpoint 
Kubectl get ep my app-service

# describe the endpoint
Kubectl describe ep myapp-service

Deployments

# deployment.yml

apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: myapp-service
spec:
  replicas: 10
  selector:
    matchLabels:
      app: myapp-service
  minReadySeconds: 10
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 1
      maxSurge: 1
  template:
    metadata:
      labels:
        app: myapp-service
    spec:
      containers:
      - name: myapp-service
        image: localhost:5000/myimage:latest
        ports:
        - containerPort: 8080 

Kind - field to tell kind of objects. This will send to Deployment Controller.

Kubectl create deployment -f deployment.yml

# to see deploy details 
Kubectl get deploy hello-deploy

# more details on deployment
Kubectl describe deploy hello-deploy

# replica sets are automatically created when we create deployment

kubectl get rs 

We create service to access the application from an IP or DNS.

Service: its wrapper for pods access, see above for details

# svc.yml is your declarative file you create with what you # want.  see above for sample svc.yml file.

kubectl create svc svc.yml

Rolling Updates

spec:
  replicas: 10
  selector:
    matchLabels:
      run: hello-deploy
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
    type: RollingUpdate

# we can monitor rollout status 
kubectl rollout status deployment hello-deploy

#Excerpt From: Nigel Poulton. “The Kubernetes Book.”