Contents

Docker & Kubernetes

Docker

About Docker

Problem: It works on my machine, but not works on others'

Solution:

  • VM
  • Docker

Docker is a technology for creating and running containers, so we don’t need redudent VMs!

Docker allows different machines to share the same environments by using sharing docker image and create same containers

Docker images

DockerFile + App files –> Docker –> Image (shared to others)

Docker image explains what your environment looks like Docker image contains everything a container needs to run:

  • Application runtime (JDK/Python/NodeJS)
  • Application code
  • Dependencies

When run docker images, what happens?

  1. Download images from Docker hub
  2. Running locally in a container

A Container is a collection of one or more processes, organized under a single name and identifier. A container is isolated from the other processes running within a computing environment, be it a physical computer or a virtual machine (VM).

Run docker containers on

  • Local machine
  • Cloud
    • Azure: Azure Container Service
    • AWS: Elastic Container Service

Docker commands

/images/dockercommand.jpg

More commands

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
docker --version
docker run -p 5000:5000 abc/to-do-rest-api:1.0.0.RELEASE
docker run -p 5000:5000 -d abc/to-do-rest-api:1.0.0.RELEASE // run in background
docker images
docker images history [image-id]
docker image remove [image-id] // remove from local
docker tag [repo-name].RELEASE [repo-name]:latest
docker container stop [container-id] // shutdown container
docker container ls
docker container pause [container-id]
docker container umpause [container-id]
docker container inspect [container-id]
docker events
docker stats
docker system df

How do we build an image?

We use docker file to specify image

Think of building a machine from scratch (OS, Environment, Dependencies…)

Dockerfile Example

1
2
3
4
5
6
FROM python:3 // Docker pull Python & OS image from DockerHub
WORKDIR /usr/src/app    
COPY requirements.txt . // Copy Python dependencies to current Docker workdir
RUN pip install --no-cache-dir -r requirements.txt // Install Python dependencies
COPY . . // copy local files to current docker workdir
CMD ["python", "app.py"] // run on docker machine

Docker compose

Config a YAML file, launch multi-container Docker applications

docker-compose.yml

1
2
3
4
5
6
7
8
9
version: '3'

services:
    web:
        build: ./web
        ports:
            - "5000:5000"
    db:
        build: ./db

Basics

/images/docker.jpg

Architecture

1
2
3
4
5
6

Docker Client(Terminal)
    Docker Engine
        Containers
        Local Images
        Image Registry

Kubernetes

What is Kubernetes

Kubernetes is a container orchestration option

Kubernetes groups the containers that support a single application or microservice into a pod. A pod is exposed to the network by way of another Kubernetes abstraction called a service. In short, the network knows about Kubernetes services and a service knows about the pod(s) that has its logic. Within each pod is one or many containers that realize the logic in the given pod.

Kubernetes is a tool for running a bunch of different containsers. We give it some configs to describe how we want our containers to run and interact with each other

  1. Pull image from docker hub & Deploy it to Kubernetes
  2. Expose service

Why we want Kubernetes

We can run Kubernets on:

  • AWS - Elastic Kubernetes Service (EKS)
  • Azure - Azure Kubernetes Service (AKS)
  • GCP - Google Kubernetes Engine (GKE)

Pros:

  • Service Discover: Makes communications between services easy
  • Auto scaling: Scale containers based on demand
  • Load Balancer: Distribute load among muliple instances of a microservice
  • Deployment: Release new versions without shutting service down

Terminology

/images/kubeterms.jpg

A pod is a wrapper of a set of containers Pod contains:

  • Name
  • Namespace
  • Priority
  • PriorityClassName
  • Node
  • Labels
  • Status
  • IP
  • Controlled by
  • Containers
    • Contianer ID
    • Image
    • Image ID

A replica set ensures that a specific number of pods are always running

A deploymnet ensures that a release upgrade happens without a down time

  • Split traffic to V1 and V2 by 50%
  • Rolling update (↑ V2 ↓ V1 per instance)

Service: takes care of the communications between pods thru a permanent lifetime address (cuz pods might be shut down and broght up with new IPs)

Master Node:

  • Distirbuted Database
    • All config changes, servives deployments we create, scaling operations are stored here
    • Have multiple replica of this ensures data backup
  • API Server
    • When run kubectl, it enalbes Google cloud console talks to Kuberenetes cluster
  • Scheduler
    • Assign pods to appropriate nodes
  • Controller Manager
    • Manage overall health of cluster

Node:

  • Pods
  • Node Agent
    • Monitor nodes and communicate to master node
  • Networking component
  • Container runtime

WorkFlow

What it takes to move a Docker conatiner to a Kubernetes Cluster

  • Docker Image from a Dockerfile
  • Kubernetes config file (YAML)

/images/kube.jpg

Config file: written in YAML, it tells kubernetes about different deployments, pods, and services that we want to create (what our cluster is running)

config file eg (xxx.yaml)

1
2
3
4
5
6
7
8
apiVersion: v1
kind: Pod
metadata:
    name: posts
spec:
    containers:
        - name: posts
            image: abc/posts:0.0.1

Kubernetes commands

In cloud shell

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
docker run -p 8080:8080 in28min/hello-world-rest-api:0.0.1.RELEASE
 
kubectl create deployment hello-world-rest-api --image=in28min/hello-world-rest-api:0.0.1.RELEASE
kubectl expose deployment hello-world-rest-api --type=LoadBalancer --port=8080
kubectl scale deployment hello-world-rest-api --replicas=3
kubectl delete pod hello-world-rest-api-58ff5dd898-62l9d
kubectl autoscale deployment hello-world-rest-api --max=10 --cpu-percent=70
kubectl edit deployment hello-world-rest-api #minReadySeconds: 15
kubectl set image deployment hello-world-rest-api hello-world-rest-api=in28min/hello-world-rest-api:0.0.2.RELEASE
 
gcloud container clusters get-credentials in28minutes-cluster --zone us-central1-a --project solid-course-258105
kubectl create deployment hello-world-rest-api --image=in28min/hello-world-rest-api:0.0.1.RELEASE
kubectl expose deployment hello-world-rest-api --type=LoadBalancer --port=8080
kubectl set image deployment hello-world-rest-api hello-world-rest-api=DUMMY_IMAGE:TEST
kubectl get events --sort-by=.metadata.creationTimestamp
kubectl set image deployment hello-world-rest-api hello-world-rest-api=in28min/hello-world-rest-api:0.0.2.RELEASE
kubectl get events --sort-by=.metadata.creationTimestamp
kubectl get componentstatuses
kubectl get pods --all-namespaces
 
kubectl get events
kubectl get pods
kubectl get replicaset
kubectl get deployment
kubectl get service
 
kubectl get pods -o wide
 
kubectl explain pods
kubectl get pods -o wide
 
kubectl describe pod hello-world-rest-api-58ff5dd898-9trh2
 
kubectl get replicasets
kubectl get replicaset
 
kubectl scale deployment hello-world-rest-api --replicas=3
kubectl get pods
kubectl get replicaset
kubectl get events
kubectl get events --sort.by=.metadata.creationTimestamp
 
kubectl get rs
kubectl get rs -o wide
kubectl set image deployment hello-world-rest-api hello-world-rest-api=DUMMY_IMAGE:TEST
kubectl get rs -o wide
kubectl get pods
kubectl describe pod hello-world-rest-api-85995ddd5c-msjsm
kubectl get events --sort-by=.metadata.creationTimestamp
 
kubectl set image deployment hello-world-rest-api hello-world-rest-api=in28min/hello-world-rest-api:0.0.2.RELEASE
kubectl get events --sort-by=.metadata.creationTimestamp
kubectl get pods -o wide
kubectl delete pod hello-world-rest-api-67c79fd44f-n6c7l
kubectl get pods -o wide
kubectl delete pod hello-world-rest-api-67c79fd44f-8bhdt
 
gcloud container clusters get-credentials in28minutes-cluster --zone us-central1-c --project solid-course-258105
docker login
docker push in28min/mmv2-currency-exchange-service:0.0.11-SNAPSHOT
docker push in28min/mmv2-currency-conversion-service:0.0.11-SNAPSHOT
 
kubectl create deployment currency-exchange --image=in28min/mmv2-currency-exchange-service:0.0.11-SNAPSHOT
kubectl expose deployment currency-exchange --type=LoadBalancer --port=8000
kubectl get svc
kubectl get services
kubectl get pods
kubectl get po
kubectl get replicaset
kubectl get rs
kubectl get all
 
kubectl create deployment currency-conversion --image=in28min/mmv2-currency-conversion-service:0.0.11-SNAPSHOT
kubectl expose deployment currency-conversion --type=LoadBalancer --port=8100
 
kubectl get svc --watch
 
kubectl get deployments
 
kubectl get deployment currency-exchange -o yaml >> deployment.yaml 
kubectl get service currency-exchange -o yaml >> service.yaml 
 
kubectl diff -f deployment.yaml
kubectl apply -f deployment.yaml
 
kubectl delete all -l app=currency-exchange
kubectl delete all -l app=currency-conversion
 
kubectl rollout history deployment currency-conversion
kubectl rollout history deployment currency-exchange
kubectl rollout undo deployment currency-exchange --to-revision=1
 
kubectl logs currency-exchange-9fc6f979b-2gmn8
kubectl logs -f currency-exchange-9fc6f979b-2gmn8 
 
kubectl autoscale deployment currency-exchange --min=1 --max=3 --cpu-percent=5 
kubectl get hpa
 
kubectl top pod
kubectl top nodes
kubectl get hpa
kubectl delete hpa currency-exchange
 
kubectl create configmap currency-conversion --from-literal=CURRENCY_EXCHANGE_URI=http://currency-exchange
kubectl get configmap
 
kubectl get configmap currency-conversion -o yaml >> configmap.yaml
 
watch -n 0.1 curl http://34.66.241.150:8100/currency-conversion-feign/from/USD/to/INR/quantity/10
 
docker push in28min/mmv2-currency-conversion-service:0.0.12-SNAPSHOT
docker push in28min/mmv2-currency-exchange-service:0.0.12-SNAPSHOT

Docker vs Kubernetes

Basically, Docker deinfes/creates/runs containers. Kubernetes manages containers, it doesn’t make containers.


ref: https://tanzu.vmware.com/developer/guides/from-docker-to-kubernetes/