Docker && Kubernetes

Docker && Kubernetes

From Zero to Hero

In a nutshell

Communities

Social Developer

Recognitions

Cloud Solution Architect @Microsoft

GDE,MCT and former MVP from SL

Top stackoverflow contributor

 

I'm Sajeetharan Sinnathurai

@sajeetharan
@kokkisajee
@sajeetharan
@sajeetharan

About this Session

👍A “crash course" and strictly not for Experts

 

👍It won’t teach you everything,  But it will help you learn what you need to know to get started

 

👍A mixture of discussion, jokes and demo

 

👍Be prepared to Unmute and ask questions

THE CHALLENGE

"Build and Deploy Dotnet Core App to K8s"

  1. Build the application and containerize it using docker
  2. Deploy the app to K8s

and we need only 30 steps to go there!

Disclaimer: If you don't understand anything or feel bored

let's change the topic!

Story time -> Software = Shipping?

Then

@kokkisajee

Then

Now

Actual Problem

Solved!

Top 3  lies by developer

It works on my machine

We have not changed anything

You're testing it wrong

The Problem

The solution

What is Docker ?

What is an Image?

  • A fully self-contained “thing”
  • Application source code
  • All runtime dependencies, config files, and binaries

 

 Images are STATELESS and IMMUTABLE

What is a container then?

●While a container looks like a VM, it isn’t!

●A container is JUST another process on the machine

●It uses namespaces and control groups (cgroups) to provide isolation

        Namespaces include network, process, user, IPC, mount, and others

How to create a Image?

●Best practice is to use a Dockerfile

●A text file that contains a script used to create an image

●Allows various commands, including:

      FROM - specify the parent image (almost always the first command)

      COPY - copy files from the host into the image

      RUN - run a command using binaries inside the container (install services, etc.)

      CMD - specify the default command (if one not specified in parent image)

How to share an Image?

  • When building an image, it only exists on the building machine
Docker Git Description
image repository collection of commits
container clone used for local execution
docker hub GitHub popular remote server

Image Registry

Docker registries hold images.
The public Docker registry is called Docker Hub.

Containers vs VMs

Architecture Overview

Demo Time

PreRequisites

Let's fight together

Steps to run Dotnetcore app with docker

Step 1: Create a dotnet api application

          

 

Step 2: Coment out Startup.cs to run on local

             az login -u <username> -p <password> 

Step 3: Run the application

cd dotnet-app
dotnet run

Step 4: Visit http://localhost:5000/WeatherForecast

dotnet new webapi --name dotnet-app
// app.UseHttpsRedirection();

Let's Dockerize the Dotnet Core application

Step 5: create a docker image for the dotnet-app

                   

Step 7: Build our docker image with the dockerfile.

docker build -t dotnet-app .
or use
Ctrl+Shift+P Build Image

Step 8: Once then image buit, check the image or use explorer

Ctrl + Shift + p -> Add docker files to workspace or create manually
docker images

Step 9: Run the docker image and verify it works.

docker run --rm -it  -p 5000:5000 womenwhocodeapp:latest 

Step 6: Use ENV instruction to add an environment variable

                   

ENV ASPNETCORE_URLS=http://*:5000

Yeah! Success!

Let's move to bigger challenge!

While you are sitting in front of TV

Here is how an automated containers world look like

Container Orchestration

Kubernetes

Packing Things Together

At Scale

Cuban Yeti(s)

  • Kubernetes is a portable, extensible open-source platform for managing containerized workloads and services

 

  • Environment to run applications on containers (Docker, but other engines are supported)

Why K8S

  • It's a uniform platform for running containers
  • It's portable (runs on clouds and bare metal)
  • Makes managing and deploying infrastructure easy
  • Uses a declarative approach (no scripting)
  • It's a self-healing system
  • It's very well suited for microservices
  • Flexible: you could run your own serverless stack!

 

Clustering

Clustering

Scheduling

Scaling

Deployment

Clustering

Load balancing

Fault Taulerence

K8S architecture

A kubernetes cluster contains:

 

  • 1+ Nodes: workers, they run containerized apps

 

  • 1 Master: runs k8s APIs and administrates changes to the cluster, can be replicated

K8S architecture: Nodes

Nodes on K8S are the basic worker machine

  • On cloud providers, Nodes are VMs
  • Each Node runs a Kubelet process, for k8s related communication
  • They also run Docker, for managing containers (or an equivalent container engine)

K8S architecture: Masters

Masters run a set of components:

 

  • API server: a REST API for operating the cluster
  • The state store (etcd): stores configuration data
  • Controller manager server: handles lifecycles and business logic
  • Scheduler: schedules containers on the cluster

 

Masters can be replicated, to ensure High Availability

 

A cluster with a failed master can still work (Nodes will execute containers) but won't be able to change/self-heal

The Pod

The basic building block of K8S

  • Smallest unit to deploy in k8s
  • It encapsulates a containerized application
  • Each pod has a network IP
  • Pods can have a volume assigned
  • Normally one container, but can be used to run multiple

You never really run Pods directly, a Controller does!

The Deployment

A Deployment is used to manage Pods

  • It's declarative: describes what you want to deploy
  • Allows rolling deploys (no downtime)
  • Manages application restarts in case of crashes
  • Manages scaling up (or down) your Pods

To be more specific:

Deployments manage ReplicaSets, which in turn are responsible for managing Pods

The Deployment: cont'd

When you change a Deployment:

 

  • The deployment updates its spec/status
  • It creates a new ReplicaSet (if needed)
  • Each set is a group of Pods of the same version
  • When the new ReplicaSet is ready, the old one is deleted

The Deployment: example

 An example Deployment (yaml file)

  • Runs the public nginx:1.7.9 image
  • Will mantain 3 replicas
  • Exposes port 80

 

But your apps are not reachable from outside the cluster yet...

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80

Service Discovery

The Service

Pods are mortal:

  • They have an IP, but it can change
  • They can be restarted/recreated
  • They can be scaled up or down

To make sure you can always reach Pods from the same group, Kubernetes uses Services 

The Service

Services offer:

  • An external or internal IP to reach your Pods
  • An internal DNS name for Pods
  • A port on your Node's IP
  • Lightweight load balancing of requests
  • In cloud environments they can provision Load Balancers

Ingresses: an example

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: test
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: k8s.io
    http:
      paths:
      - path: /foo
        backend:
          serviceName: foo-service
          servicePort: 80
      - path: /bar
        backend:
          serviceName: bar-service
          servicePort: 80

http://k8s.io/foo

http://k8s.io/bar

foo-service

bar-service

IngressControllers are usually implemented with reverse proxies (e.g. nginx)

ConfigMaps and Secrets
Persistent Volumes
Network and CNI
Network Policies
HELM Package Manager
RBAC
Horizontal Pod Autoscaler

Additional concepts

Additional Controllers

K8s Cluster

Deployment / ReplicaSet

The kubelet is responsible for maintaining a set of pods

High Available Design
etcd
etcd
LB
kubectl, ui, api clients
storage 
log mgmt
monitoring
nodes

It's Time for k8s Demo

Wake up! I'M done

Q&A

Where to go from here?

Docker && Kubernetes - Zero to Hero

By Sajeetharan Sinnathurai

Docker && Kubernetes - Zero to Hero

Talk about AKS and Docker for beginners at Women Who Code - KL

  • 1,349