Docker & Kubernetes
Machine Learning Operation
You have been assigned some projects and you completed it on your system with good results. Now you deployed you application on the server or on different local system and you started facing environment issue related with package missing or different package versions or python is not compatible with your application.
Now resolving this type of issue is nightmare. It could take your days and night in resolving these issue. Like if you resolve one issue by changing the NumPy version for pandas installation there could be another library which might require another version of NumPy.
There is one solution for these is to use a virtual environment. With this we can create only for python environment. It doesn’t bundle OS libraries.
Another solution could be to use entirely different virtual machine. virtual machine is like a house. you can consider the application like a room in the house. It waste of resources unless you are going to create a very application.
Best solution could be to use container in your system. Its like isolated environment with host OS and its kernel. It will isolated processes, filesystem, network and this helps in starting this system within a sec instead of mins. This container can be shared.
Architecture
Docker daemon - dockerd - is the actual engine running in the background on the host OS. It helps in building , running, managing containers.
docker client - This will what running on top of daemon. It will do the communication with the daemon. so whenever you write any command it will pass to daemon for processing.
registry - This is place where all the prebuild images exits. Its private one.
docker hub - Here we use to register the image publicly.
docker swarn - docker own built in orchestration tool. it take several machine (nodes) and turn it into single virtual cluster.
images - its blueprint of the application with all the requirements
containers - its an instance of the image. one can initiate multiple container using the same image.
DockerFile
It’s a step by step instructions for running your application.
FROM node:18
WORKDIR /app
COPY . .
RUN npm install
CMD [”npm”, “start”]Here CMD is default argument that we can override. It can be replaced with at docker run. ENTRYPOINT is fixed command and whatever passed with docker run its become arguments to it.
COPY and ADD both copy files to container. ADD has some extra feature like it extract zip or tar file before copying to container. It is always safe to use COPY as ADD can be unstable.
All these line FROM, COPY, RUN, etc add layers in stack. All these layers are cached. we make any changes to one layer, it check if this layer is modified or not. If not it tries to use caches. Otherwise without cache, the building process will keep getting slow.
.dockerignor are used to keep junks like log files out of COPY .. command.
Docker compose
Using docker compose we can initiate multiple container on the same host. To trigger it on different host we need to use docker swarn or Kubernetes.
Inside docker compose we can define docker volume for storage.
services:
db:
image: mysql:8
volumes:
- db-data:/var/lib/mysql # named volume
# or, for a bind mount:
# - ./local-db-folder:/var/lib/mysql
volumes:
db-data: # declared here so Compose manages itHow to reduce the docker image size?
use slim or alpine base image instead of full OS.
combines RUN into one.
Cleanup cache files using apt-get clean.
copy only final artifacts which are necessary after deployment.
Env variable
All the configurations such as API key are not hardcoded. It is passed at the runtime like docker run -e dataset_url=postgres://….
If the container stop, all the files generated inside this container are deleted. All these are temporary files. To store the files permanently, we need to assign volume to the container. use command
docker volume create mydata
docker run -d —name mydb -v mydata:/var/
Commands
Now we can’t run container from same image on different machine. We can’t scale it. This is where Docker Swarn and Kubernetes comes in.
Kubernetes
It is open sourced system developed by Google. Its an orchestration tolls. It helps in managing containerized applications. It is a system for running and coordinating between containerized application across different cluster of machine. It manages the lifecycle of the containerized application.
It helps in
load balancing - load across container
scaling, scale up and down base on demand
storage - keeps storage consistent with multiple instances of an application
self-healing - automatically restart container that fail and kill container
Installation
We can install locally or from cloud. if we are choosing cloud, only we need to do configuration and rest of the things will be taken care by cloud. This is best ad easy way to start with Kubernetes. If we are moving aged with local setup. We can use Docker desktop for using Kubernetes.
Architecture
Node is virtual or physical machine. there is master and worker node. worker nodes is mostly referred as nodes. Each nodes will have kubelet. Its primary node agent. It help each node to communicate with other and execute task on those nodes. Each of these node can have container from different application. Master nodes run several Kubernetes process that are absolutely necessary to run and manage cluster properly. one of such process is API server which itself is a container. Its an entry point to the Kubernetes cluster. Kubernetes UI, CLI and API are processor to API server. There is another process running on master node is controller manager. It keep track of what’s happing in the cluster like if container diet or need to be restarted. Another process running in master node is scheduler for scheduling containers on different nodes based on workload and available server resources. There is another process etcd key value storage which holds current state of the Kubernetes cluster at any time. There is a very important layer known as virtual network helping master node to communicate with the worker node. This virtual network helps to create one unified virtual machine. Worker nodes have higher workload. Can have multiple master nodes.
Components
Pod is a smallest unit in Kubernetes. It is an abstraction over container and it’s usually 1 application per pod. Service is permanent IP address. Each pod gets its own IP address. Lifecycle of pod and service are not connected. If pod dies, its IP address stay. external service is when we needed to access the application from external source. we need to create internal service for data access. This is mostly done for testing purpose. The request from the external service goes to ingress first and then to the internal services. Config-map helps in external configuration of the application. It contain URL of the database or something like helps in connecting with the pod. Secret is similar to config-map helping in storing configuration related with credentials in base64 encoded format.
Similar to docker, if the pod is restarted, the data generated during the execution are deleted. This is resolved using volume from the local storage and attaching it to the dataset pod. The volume could be also remote. Service is also a load-balancing.
Instead of relaying on a single node, we need to create replica of it with blueprint. This will help in distributed system. Deployment is a blueprint of application pod. We can create deployments and abstraction of pods. same service will be used for replica pod. The database pod can not be replicated because it has state. It is done in statefulset. We use deployment for stateless app and statefulset for stateful app or dataset.
The Kubernetes configuration are declared in the yaml file. Each file have 3 parts: metadata, specification and status. The status will automatically generated by Kubernetes.
apiVersion: v1
kind: Pod
metadata:
name: my-app
spec:
containers:
- name: app
image: myapp:1.0
ports:
- containerPort: 3000










Discussion about this post