LongCut logo

Build and Deploy your First MLOps project in 40 minutes to Kubernetes

By Abhishek.Veeramalla

Summary

## Key takeaways - **MLOps: DevOps for Machine Learning**: MLOps is essentially DevOps principles applied to machine learning, making it easier for those with a DevOps background to learn. It bridges the gap between development and operations in the ML lifecycle. [01:13], [01:16] - **Diabetes Prediction Model Inputs**: The diabetes prediction model uses parameters like pregnancies, glucose, blood pressure, BMI, and age to determine if a person is diabetic or non-diabetic. Pregnancy is included as a parameter to cater to both male and female predictions. [09:43], [09:51] - **Leveraging Free Datasets for ML Projects**: For proof-of-concept projects like diabetes prediction, readily available datasets from sources like Plotly can be utilized, eliminating the need for manual data collection. [11:04], [11:13] - **Model Deployment via FastAPI and Docker**: Machine learning models are typically deployed within software applications, such as those built with FastAPI, and then containerized using Docker for easier deployment and management. [15:52], [27:17] - **Kubernetes for Scalable ML Model Deployment**: Models are often deployed onto Kubernetes clusters for high availability and scalability, utilizing deployment manifests and services to manage the application. [31:38], [32:35]

Topics Covered

  • MLOps is the DevOps for machine learning models.
  • Why 80% of ML models never reach production.
  • You don't need to collect your own data.
  • Models are useless without a software application wrapper.

Full Transcript

Hello everyone, my name is Abhishek and

welcome back to my channel. In today's

video, you will learn how to create,

train, evaluate, build and deploy a

machine learning model. So this is going

to be a beginnerfriendly MLOps project

and you will learn this entire project

using a realtime machine learning model

that is dabetis prediction model.

So I have written this model. So in this

video I will also walk you through the

code. I will explain you how does this

model work and then I'll explain you how

to train, evaluate, build and deploy

this model.

But before that a lot of you might be

new to MLOps and some of you might be

worried as well that MLOps is difficult.

Let me tell you if you come from DevOps

background. I'm sure lot of our

subscribers come from DevOps background.

So learning MLOps is just a piece of

cake. Why? Because MLOps is considered

as DevOps for machine learning.

Why? To understand this, let's go a step

back and understand what exactly is

DevOps.

So DevOps is a culture that unifies

development and operations.

In other words, you can say DevOps is a

culture that bridge the gap between

development and operations.

If you look at software development life

cycle,

for a company to ship a feature,

multiple stages are involved. So

initially requirements have to be

gathered. Once the requirements are

gathered

then the developer works on design and

developing the feature. Once the design

and development of the feature is done,

application is built with the feature.

Then the application is tested.

Then the application is deployed and

finally application is monitored for a

good amount of time.

And this is not a one-time process

because initially developer might

succeed or developer might succeed in

the first attempt in developing the

feature or maybe a Q engineer says hey

the feature is not working fine. So

developer goes back again updates the

feature might again again and do it n

number of times. So typically even for a

single feature all of these activities

have to be repeated n number of times.

So this is where for companies to ship a

feature before DevOps it used to take 2

to 3 months.

Now DevOps came into picture to reduce

the gap between development and

operations and to reduce this entire

time. So now a DevOps engineer tells the

developer hey developer just focus on

development rest I will focus on

automating the things for you. So once

you feel that your feature is ready,

you can run the pipeline that I have

created and that pipeline will build

your application, it will test your

application, it will deploy and it will

also monitor the application for the

results.

So this way even if the developer has to

go through multiple iterations, it will

not take 2 to 3 months but it might take

some 20 to 30 days. So DevOps will

significantly reduce the gap between

development and operations

increasing the efficiency of the

software development life cycle.

Now just like software development life

cycle there is also something called as

model development life cycle because end

of the day whether it is software

development or model development

developers have to go through number of

iterations.

Imagine there is a engineer who is

working on developing a model let's say

climate prediction model right or a

weather prediction model let's just say

WM

now to develop this model the engineer

has to go through multiple stages

initially data has to be provided

because for weather prediction model to

predict weather in the future initially

huge huge amount of data has to be

provided to it. Maybe 100 years of data

or at least 50 years of data of

temperature of a particular region. By

looking at this data and by training on

this data through some machine learning

algorithm, what does the model do? Model

predicts what can be the temperature of

a particular place on 1st of July in

2025.

So data is important, algorithm is

important, developing the model is also

important. So to do all of these things

multiple stages are involved. Initially

data has to be ingested.

Then the data has to be prepared because

this data is collected from various

sources. The data might be not properly

organized. So the data has to be

prepared so that the data or model can

be trained on the data. And once the

model is trained on the data, who knows

this weather prediction model might

return accurate results or might not

return the accurate results.

Let's say 100 years of data is provided

but still the model developer has used a

weak algorithm.

Right? So in that cases you need to

evaluate the model and maybe model might

fail at the evaluation or if developer

has used a very good machine learning

algorithm something like random forest

the performance of the model will be

good. So model training model evaluation

then the model also has to be deployed

right before the model has to be built

and finally model has to be monitored.

Usually in case of most of the models

model has to be retrained as well

because when it comes to models

performance usually goes down as new

data keeps adding. So model also has to

be retrained. Now what I'm trying to

tell you here just like software

development life cycle the stages within

the model development life cycle will

also be repeated multiple times because

in terms of model development as well.

The developer might not be successful at

the very first attempt or the engineer

might not develop model accurately at

the very first attempt. So there will be

number of iterations. As there are

number of iterations, model training has

to be repeated. Model has to be built

multiple times. Model has to be

evaluated multiple times. It has to be

deployed multiple times. And this

activity is taken care by MLOps in

snare. So that is why MLOps is inspired

by DevOps or inspired from DevOps.

Before MLOps it used to be very

difficult because they say that 80 to

90%age of machine learning models

actually never reach to production

because it used to take a lot of time.

So most of the people they used to

develop the models locally and they used

to ignore all of these stages.

In the recent times MLOps has become

very significant for the same reason.

Now a lot of companies are working on

developing their models and for the

development of their models and

releasing the models they are using

MLOps.

So MLOps is inspired by DevOps because

if you are a DevOps engineer you already

know and you have expertise in platforms

like GitHub actions, Docker, Kubernetes,

Terraform for creating infrastructure.

Most of these platforms are also used in

MLOps. That's why it's actually a piece

of cake to learn. Only challenge that

you'll find is understanding model

development life cycle. So in this

video, I will first explain you how

models are created

because I have created this model and

I'm the author of this model. I can

quickly walk you through the source code

and I'll explain you.

Then I will show you how data is

collected. Then I'll show you how model

is evaluated. Once you understand these

things only then I'll proceed with

explaining you how to build the model,

how to deploy the model onto Kubernetes.

So you will be learning all of these in

next 15 minutes.

Perfect. So let's get started with

understanding how data is collected and

how the models are created.

So in our example we are using database

prediction model. But Abishek what

exactly is this data prediction model

doing?

As the name suggests if you provide it

with bunch of parameters it will tell if

a person is diabetic or non-diabetic

and the parameters that it expects. So

it will expect these parameters in the

JSON format pregnancies

glucose blood pressure BMI and age.

So this should work for both men and

women or male and female. So that's why

pregnancy is also important parameter.

If you want to know if a man is

diabetic, maybe you can just mark the

pregnancies as zero. Then you provide

glucose levels, blood pressure, BMI,

age. Of course uh it is trained on huge

number of other parameters as well. But

these are the important parameters. If

you just provide these five parameters

of a patient, it will say if the

particular person is diabetic or

non-diabetic.

But Abishek, how is this developed? And

first of all, where did you get the data

from? Like in the theory part you

explained for a model to function, it

needs huge amount of data. So where did

you get the data of huge number of

patients? At least you must have

gathered some thousands of patients.

only you can say that this model is

returning accurate results right only

then you can consider this model as even

something in the working state so you

don't have to worry about that

there are lot of sources on internet and

one of such source is plotly so if

you're working on a proof of concept uh

let's say something like weather

prediction or something that I've done

uh diabetes prediction so So you can use

data sets that are available on

internet. Fortunately, most of them are

freely available. Plotly also provides

uh huge uh data sets as uh free

resources. For example, uh you're

working on world GDP or maybe you're

working on uh US airport traffic. In my

case, I'm working on uh diabetes. So

I've used the CSV file that is provided

by these data sets.

So this is the CSV file that I've used.

Now if you look at uh this way you might

feel what exactly it is. Let me

simplify.

So these uh these are the headings or

this is the header where first column is

number of pregnancies. Second column is

the glucose levels of the person. Then

blood pressure, skin thickness, insulin,

BMI, uh diabetes, pedigree function, age

and finally the outcome. So basically

what we are telling the model right we

are asking the model to use an

algorithm. So basically I have used

random forest as an algorithm. I'll also

show you the source code in next 1

minute. But for now just understand to

train the model I have used random

forest as algorithm and I'm telling the

model

these are the parameters and this is the

outcome. That means if number of

pregnancies is six, if glucose level is

148, blood pressure is 72, skin

thickness is 35, insulin is 0ero, BMI is

33.6

and diabetes pedigree function is 0.62

and age is 50, then the person is

diabetic.

Similarly I said here is information

about other person

where number of pregnancies is one

glucose levels is 85 blood pressure is

66 290 26.6 six and this person is

non-diabetic.

So this way you know I have provided 1

th00and plus uh data sets to the

model and I have asked the model to use

random forest algorithm which works

perfectly fine in these kind of data

sets. So after training on these data

sets now my model can say if you provide

the following things as input right if

you provide these things pregnancies

glucose blood pressure BMI and age you

don't have to provide all of them just

provide these things in the JSON format

and it will immediately tell you if a

person is diabetic or non-diabetic

right so this way open source helps you

a lot uh or these free resources helps

you a lot. You don't have to collect the

data. Okay, perfect. Now, now that we

understood how the application works,

let me quickly go through the source

code as well. Now, this is up to you. Uh

if you're not interested in the source

code, you can completely ignore it. But

for people who are curious how this

works.

So, I've written this train.py

and within the train.py, Pi I'm first

getting the information from the CSV

right this is data injection um if I

explain you through this chart again so

first we have ingested the data right so

we got the data then I'm serializing the

data or you can consider I'm preparing

the data using pandas so I'm reading the

data from the CSV file and I'm preparing

it okay this is also done now we have to

train the data uh Sorry, train the

model. And for training the model, we

need an algorithm. Either you can

develop your own algorithm or

fortunately there are so many good

algorithms that are available on the

internet, right? So once we have the

data prepared uh into the x-axis and

y-axis right?

We have good algorithms like random

forest. So I'm using this random forest

algorithm which is publicly available

and training my model.

And finally because users cannot

directly interact with the model like

imagine if I just provide this model you

cannot directly interact with it. So if

you want a user to interact with the

model usually models are placed in

software applications.

So in Python you can use flask or you

can use fast API which is very widely

used these days. So I'm writing a

software application in fast API. I will

come to it but for now

and I'm placing the model within the

software application. So user can

interact with the model not directly but

through this application because fast

API will expose something called a rest

API like predict.

So user will interact with the software

application on the predict API. From

there interaction takes place to the

model and model again returns the

information to the application and

application returns the information to

the user.

So usually models are not directly

interacting with the users. Models are

mostly put in the software applications

and shipped as software applications.

Okay. But Abishek how did you do it?

Very simple.

So we will

save the model in the pkl file format.

So I'm using job lip which is basically

a module or a package. Using that I'm

saving the model in the pkl format. Dab

it is model.pkl

and this pkl file if you see I've

written something called main.py which

is this fast api which is written using

fast api. And here I'm using this file

dabbit is model.pkl

and in the fast API I'm creating using

fast API I'm creating /predict endpoint.

So user will interact with the /predict

endpoint.

So what we will do as a next step

because we are done with so let me

quickly show you my bad. Okay. So

because we are done with uh data

injection, data preparation, model

training as well. So all that we need to

do is execute the previous file

that is train.py. Uh once you execute

this uh train.py file. So it will create

uh the pkl file and then we will

evaluate the model using fast API. So

let us let us check the training and

evaluation stages. Okay, I just

explained it theory theory part. Now

I'll show you how to train the model in

real time and how to evaluate the model.

Then we will look at building and

deploying.

For that I will pull my terminal. Okay.

And within this terminal, let me create

a folder MKD. Let's say demo MLOps.

Okay. I will switch the directory to

demo MLOps.

Great. Now I need the GitHub repository

uh URL.

This is a public repository and all the

steps that I'm going to implement are

already put in this readme file. Right?

Every step in case you are stuck in the

video, don't worry. Just go to the

readme file, look at the commands that

I'm executing, you will find everything.

Anyways, I'll also explain them. Okay.

So, copy the URL.

Go back to the terminal.

Get clone followed by URL.

Okay great.

Now, let's switch the directory to first

MLOps project.

Cool.

Okay. So we have the source code here.

Everything is fine. Whenever you are

working with Python project, make sure

you create a virtual environment. Why

virtual environment is important?

Because on this particular machine that

you see, I'm running multiple Python

projects. And with Python, a common

challenge is that you will find

dependency conflicts. one project might

be using a specific Python dependency

and other project might be using a

specific uh dependency version. So

always it is good to create virtual

environment so that

at the namespace level or at the uh

packages level isolation is created very

simple python 3 - m venv

right if I'm already in a virtual

environment let me first deactivate it

okay now python 3

- m v nv

Let's call this as MLOps.

I'm using dot because I want to keep

this virtual environment in a hidden uh

format so that whenever you do ls you

will not find the virtual environment.

You'll just find the source code and

this way I can also add it to g ignore

and not push to the g repository.

Okay. Now let's source the virtual

environment. source dot mlops

bin activate.

Okay, great. Now, first let's see how to

train the model. Python 3

train.py. That's all. So, to train the

model, you don't have to do anything

additional. Just run Python 3 uh

train.py and it will create the pkl file

where it is saving the model in the pkl

format. Abishek it says no module name

pandas. Don't worry we have the

requirements.txt file. Just run pip 3

install

hyphen r requirements.txt.

Obviously if you want to run the model

or even if you want to uh build the

model you need to have the dependencies.

That's where it failed. It's totally

fine. Now the dependencies are installed

and installed in the virtual

environment. I will run Python 3

train.py.

This will take less than a minute but it

will take some time depending on your

spec uh on your machine spec as well. It

might take more time. But once this

execution is done

in this folder you will find uh the

diabetes model PKL file. And as I told

you, we will use that in the fast API.

Okay, let's wait for few more seconds.

And it says model is saved as dab it is

model.pkl file.

Now what I was trying to say user cannot

interact with this right for user to

interact with this model you need an API

you need an endpoint. So that's why you

will place this in a software

application.

Now we already have uh this file called

main.py.

How do you execute it? Because we wrote

it in fast API. You will use uvcon. So

uvicon main colon app followed by hyphen

reload. So this will start a development

server for us.

And this development server will host

the application on this particular URL.

So I'll just copy it,

go back and in the browser I will open

this endpoint. What does it say? Dab it

prediction API is live.

As I explained in the theory part and

even if you look at the source code

right main.by

what endpoint is exposed? The endpoint

that is exposed is / predict. So I can

just go to my postman or I can just uh

use curl command and pass / predict with

the JSON information.

However, the best part about uh fast

API, it comes with a rest API client. So

all that you need to do is go for the

endpoint /docs.

So here you can directly access get

endpoint and you can directly access

predict endpoint as well.

So you don't need any postman or you

don't need curl or any other rest api

client. Try it out. Click on it. Change

the parameters like you can provide

number of pregnancies, glucose, blood

pressure, BMI, age and the model will

tell you if the person is diabetic or

not.

For safe side, I have provided

information on the GitHub repository as

well or you can directly edit the field

here.

But this is something that I have

verified. If pregnancies is 2, glucose

is 130, blood pressure is 70, BMI is

28.5 and age is 45, this patient should

be diabetic. Let's see if our model

returns accurate information or not. So

we are performing model evaluation. Now

if you scroll down it says yes the

patient is diabetic.

So we have confirmed the model training

stage and we have completed model

evaluation stage as well. Till now model

is running locally on our machine. We

haven't built the model. We haven't

deployed the model to the Kubernetes

cluster.

Right? So

I hope till this point it is clear how

did we uh run the training of the model

and how did we run the evaluation of the

model. Only point which might be

confusing to you why did we use fast API

right Abhishek you have uh written the

model code to train the model you have

written train.py Pi but why did you use

fast API I'm confused there very simple

I'm repeating again because there might

be beginners

mostly right models cannot perform

direct interaction with the users models

are shipped through software

applications so they are bundled in a

software application and software

application is the one that exposes the

endpoint to the user you don't have to

use fast API only you can use flask you

can use Django but the good part about

fast API uh even when there are

concurrent users it can handle

effectively it comes with great you know

concurrency as well so that's why fast

API is used it's very fast

okay now let's head back and see how to

build the model and how to deploy the

model okay for that what we will

Um the steps are in the GitHub

repository as well. But if you go to the

code that you have cloned

right let me stop it here.

Right here you need to write a docker

file. So Abishek even for the models we

will be using the docker file. Yeah. So

even for the models you can use the

docker file and you can completely use

these models by building them as docker

images. So that's why I was telling you

if you know DevOps it's actually a piece

of cake for you because most of the

tools that we use as DevOps engineers

the similar tools are used in MLOps as

well. So let's create a docker container

now. For that we will use the uh Python

base image from

okay from

Python 3.10.

Of course you can use the latest version

as well. From Python 3.10 let's uh

select the work directory as / app. It's

up to you. You can go for different

working directory as well. And I will

copy the files. Uh typically our source

code files are here. So I will copy them

from dot to / app right which is a

working directory. Now what did we do

as first step while we were uh training

the model locally and while we were

running it locally we executed pip. So

we will do the same thing pip install-

arc requirements dot txt.

Great. Now in the cmd or entry point you

just have to place the command that we

use for evaluation. So this was the

command that we have used. So I will

pass the same thing uvcon main app. It's

better to use host uh 0.0.0.0

because this will map with all the

networking interfaces uh on the Linux

host.

So 0.0.0.0 0 port 8,000

right so I will save this docker file

and you need to have docker running just

quickly verify docker ps okay I have the

docker image running so I will run the

command docker build t

so let me call this as uh database

model you can provide docker image name

or tag as per your comfort.

Database uh sorry diabetes model demo.

Okay. So now a image is being created.

Once the image is created, you can just

push this image to your docker hub or

you can push this image to your uh image

registry and you can share it with

anyone. So this is how you build the

models. Very similar to software

applications, right? It is very similar

because end of the day you are actually

building a software application and you

are placing the model within this

particular software application.

Okay. But Abishek where did we copy the

PKL file? If you remember because we

executed this commands locally the pkl

file is also copied right. So pkl file

is also in the dolo cat docker file

along with source code

right along with the source code we are

also copying the ppl file using this

dot.

Okay,

we can quickly verify, right? So, I will

run

the docker run command to quickly show

you. Tab it is model

demo. So, I think this was the name of

the tag that we provided.

Cool. So, it is running but this time on

a different URL. Let's access it.

So I can access it and I can see tab is

prediction API is live and again if I go

to the docs endpoint it will work

absolutely fine. So this way we have

built the model and in fact we have also

deployed it locally on our machine but

mostly you know many of you might want

to see how to deploy this on a

Kubernetes cluster because end of the

day usually models are deployed on

Kubernetes cluster. So I'll also quickly

show you how to put this on a Kubernetes

cluster.

Again I'm repeating if you are not sure

about any command that I'm executing you

will find that in the GitHub repository.

Please don't worry about it. Focus on

the commands that I'm executing and the

steps that I'm following. You don't have

to copy anything.

Okay cool.

Now what is the next thing to place it

in a Kubernetes cluster? We need the

Kubernetes manifest, right? So I'm going

to provide you the Kubernetes manifest.

Vim deploy

or YML.

We just need to

So I'm using this uh particular

deployment file. Nothing is different if

you know Kubernetes deployment.

Everything is actually common. only

thing I'm using two replicas for high

availability and I will be using image

that I will push to my container

registry.

So I have to tag the image. I will tag

the image as something like this abishek

fi.

Okay. Abhishek fi

slash I will call this as

tabetisy

model demo col v1

right and then I'm using the service

type as load balancer because if you are

running this on kubernetes cluster maybe

eks or aks you can also expose it you

can share it with your friends you can

share it with your colleagues

In my case, I will just run it on a kind

cluster. So for now the first job, let

me save it.

I cannot directly run this. First I need

a Kubernetes cluster. So on my local

machine I will say

kind. So I'm using kind. If you want you

can use mini cube as well. kind create

cluster hyphen name is equals to demo

mlops

at some places you might feel that you

know I'm going fast with respect to

docker or kubernetes commands the reason

is that many of you are already good

with docker and kubernetes because you

come from a devops background so that's

why I'm not stressing on the commands

that I'm using for example I did not

talk about kind here because I Believe

most of you are aware of a kind

kubernetes cluster. If you are not aware

just search for abhishek virala kind or

abhishek virala docker. You have

complete courses for free on this

YouTube channel. Okay. So let's see

cubectl config

um

current uh context.

My bad. So cubectl or or I can just do

cubectl get nodes to see if I'm

connected to the cluster.

Yeah. So demo mlops uh control plane.

Great. It's a single node cluster.

Now if I do cubectl apply f deploy.l

the pods will obviously fail because I

haven't pushed the docker image. So this

is the docker image abhishek fi diabetes

API sorry uh diabetes model demo. So I

need to take this image first of all I

need to tag it and then I need to push

to docker hub.

So for that I will copy this image ID

right which was created 5 minutes ago.

copied

docker tag

the image to

abishek

fi/

what was the thing that we used uh in

the deploy.yamel yl manifest. Let me

check again. deploy.

Better to copy it.

Great.

So

I will copy it and before that I will

run

docker tag the image that we copied.

Perfect.

Okay. Now we should see if I run docker

images. So you would see abhishek fi

diabetes model demo colon v1.

So now I will run docker login.

So docker login.

I have logged into my docker hub using

abishek fi as the username.

Docker push

followed by this particular thing.

Once this is pushed then I can run

cubectl apply - f. My pods and service

will be created.

Okay perfect. So we have this pushed.

Now I will run cubectl apply f deploy.l.

Okay. So it says deployment is created,

service is created. But let's check the

status of the parts.

Yeah. So they are still in the creating

stage. Deployment is created but the

pods are still in the creating stage.

Once the pods move to the running state

only then we can verify

right. So let's wait for the pods to be

in the running state.

Okay. So the pods are in the running

state

because

I'm using a kind kubernetes cluster.

Although the service is exposed in the

load balancer service type, I cannot

directly access it. So what I will do? I

will use cube uh port forwarding cubectl

port

forward

svc/

the name of the service diabetic uh

diabetes API service

followed by the port on which I want to

expose the application let's say 111

colon the port of the service

colon 80 and I I will also mention

hyphen address

is equals to 0.0.0.0.

Okay. So this is the port forwarding

command. And now it says abishek you can

access the application on localhost

1111.

Let's verify

if the application is deployed on the

kubernetes cluster and if I can access

the model. So localhost 11 one one

followed by demo

sorry followed by docs so that I can

directly access the rest api client okay

I can access it I will also try to uh

test the evaluate the model so I'll say

pregnancy is as one uh glucose as uh 60

blood pressure as something like 90 BMI

is uh 23 and age is 25.

Execute and let's see what does the

model say diabetic as false.

So this is how you can deploy the model

onto the Kubernetes cluster as well.

Overall you will find everything that

I've done in the readme file. In case

you don't find it by the time the video

is uploaded, don't worry. I will just

recheck the readme file and I will

update with the relevant files.

So this is about the video. I hope you

enjoyed and I hope you understood how

MLOps project is implemented.

The only missing thing is implementing

the uh continuous pipeline because there

are a lot of beginners. I didn't want to

complicate it. If you just add all of

these steps to a GitHub uh actions

pipeline, that's it. You have automated

end to- end MLOps. But for now, the

intention is to explain you how MLOps is

implemented. What is training? What is

evaluation? What is

building of the model? How model is

deployed to a Kubernetes cluster. I

wanted to show all of these things

including the data injection and data

preparation.

So we have covered data injection, data

preparation, model training, model

evaluation, model building, model

deployment. Only thing we have to place

all of this in a GitHub action pipeline

and your MLOps project is implemented

end to end. And don't worry, we will do

that in the next lectures of the YouTube

channel.

So, it will be on the same channel in

the coming days.

Thank you so much for watching today's

video. See you all in the next one. Take

care. Bye-bye.

Loading...

Loading video analysis...