Loading video player...
Learn about serverless architecture and
microservices with C. This course is
taught by Muhammad Abdullah who is an
Azure certified solution architect. You
will build practical skills step by
step. Starting from the fundamentals of
serverless and microservices, then
moving into real world patterns like
onion architecture and containerization
with Docker. You'll build Azure
functions and triggers plus background
and IoT functions. You will learn to
organize microservices with Kubernetes
and Azure container apps. Security and
observability are covered to prepare you
for production and a full case study
project ties everything together.
>> Hi everyone, welcome to practical
serverless and microservices with C#
short course. My name is Mohammad Abula
and if you are wonder how to build model
scalable application without care about
the server the infrastructure then you
are at the right place. In this course,
we will not cover the theory about the
serverless and micros service, but we
will do real world uh practical work. We
will fix real business problem with
serverless and microservices in C. So uh
if you are new to this channel, so
please uh subscribe and hit the bell
icon to get latest video about this
course. So let's start this course.
First I will give you some introduction
about the serverless. uh so serverless
in serverless environment you don't care
about the infrastructure developer only
focus on the business uh requirements
and uh not managing the server so uh
serverless promise you a build and run
application without managing uh
infrastructure uh the cloud provider
like a Azure Google AWS uh handle server
allocation and provisioning and uh next
we mean the journey of the serverless
started in 2014 by Amazon they first
introduced use the uh AWS Lambda the
first uh serverless solution then
Microsoft introduced Azure function in
2016 and presently also Google also
provide uh serverless cloud functions.
So in this course we will only focus on
the Azure Microsoft Azure function.
Okay. So next step why we choose uh like
uh serverless. So there is few reason
few benefits like uh it autoscale uh
like no auto manual uh no manual scaling
required and uh the pricing model for
the Azure for cloud is pay as you go uh
only pay for what you use and it's a
reliable secure and uh we can only focus
on the development so we can do fast
development and uh a sustainable growth.
So this is the few benefits of using
serverless.
Okay. So serverless equal to is equal to
the microservices. So we will check this
uh uh fact like microservices in
microservices uh architecture each
component is loosely covered independent
component uh and uh each component cover
some business uh capability and uh
serverless is the way to build uh
microser architecture. So to deliver uh
microservices architecture uh serverless
is the best option. So
this is one example uh for example if
you have an application this is one
microservices of that uh uh application
the user authentication in user
authentication we will have like u
registration function blocking function
reset function so these three uh
component create a user this is one
example of the uh user authentication
microservices
okay uh so next uh uh is your function
launched by already cover 2016 by
Microsoft uh it's world as a uh function
as a service uh and we can uh write a
function in any any language like C#,
JavaScript, Java but in this course we
will focus on the uh C#. So um Azure
function also have a capability of
integration. We can integrate uh Azure
function with uh database uh events uh
workflows and third party APIs. So we
can easily integrate Azure function with
these uh technologies and uh uh there is
some advante if you want a stateful u uh
function so you can use a durable
function. We will see uh durable
function in uh other upcoming videos.
Uh we can also build uh CI/CD blind. We
can monitor our Azure function that we
can diagnose. We can troubleshoot. AWS
provide this uh sorry Azure provide this
uh facility uh to us to create CI/CD
pipeline. We can create logs and
troubleshoot any uh any problem in the
Azure function. So where you create uh
Azure function uh we you will select uh
hosting plan. So this is some u uh
consumption based flex uh information
about the hosting plan like consumption
uh it's work for event driven workload
it autoscale and cost effective. So in
this course we will uh uh use mostly the
consumption because it's a true
serverless
you can also use if you have
containerized application you can use a
container app hosting plan it's up to
your requirement. So this is some uh
information about the uh hosting uh
benefit no infrastructure management it
autoscaled and pay as you go model and
fast already mentioned in the beginning
okay uh you can uh use any language uh
for uh to write the function it's
reached ecosystem and secure deop also
supported so uh for this uh um video you
will first create as your account you
will set up aure account or you can
write as function uh any of your
favorite id like visual studio OBS code
and uh you will first initially if you
go in OBS code you will uh initially set
up Azure core tools. So uh we will
create uh Azure function uh uh using the
Azure portal you will see how we create
the Azure function.
So uh first you will create uh Azure
account uh uh in the portal.gu.com
com once your recovery is approved and
created. So you will see you will create
uh these resources from uh Azure uh for
example uh in the in this video we focus
on the Azure function. So uh to create
Azure function you will uh you will uh
search for Azure function uh function.
So you can see here Azure app. So first
you will create Azure app like uh this
is the Azure app. So um we will create
on the create. So you can see here uh
this is the hosting plan as I mentioned
in the uh slides.
We have flex consumption. It's for fast
event driven uh consumption based uh
it's for uh event driven workload uh
functions premium also for event driven.
Uh this is the some like matrix. So it's
up to you which uh um hosted plan suits
your requirement. If you you have a
containerized app so you can use
container app environment. So this is a
different workload different hosting
plan for uh we will select the
consumption because it's a true server
less and we will use consumption in this
video. So you will select this
and uh the pay model is a pay as you go
model reward for the consumption. So
select. So uh you will in the basic
setting you will uh you will select the
subscription uh you will create any
subscription. Uh accord I have one
subscription and uh you then you will
create uh resource group. Uh resource
group is important. Uh for example for
this course uh I created one um resource
group. Uh this is the resource group.
You can see here serverless and uh
microser. So each and everything we will
keep in this resource group. We will
check their billing. We can match uh
each component of resource in any
resource group. So how you can create
how you can create uh if you are new to
Azure and then uh how you will create
the Azure uh resource group. So this is
a is your uh services. Uh so you can see
here you can also search resource group
here resource group. So this is the
resource group I have created for other
application. Uh uh you will create uh
this create button and you will select
this your subscription. Then you will
give any uh name to this uh
subscription. Uh it will be for example
project A B C. So uh you will give this
name. Uh you can select uh nearest u uh
region to your uh uh project or
application user. Uh for example, I will
select South Southeast Asia. And then
next uh you will um review and create.
So uh when you create uh click on this
create button, you the resource will be
created. So I will not uh create again
because I already have resource group.
So once you create this resource group
then um when you come to this basic
setting you will see uh the newly
created resource group in your list. So
I have already uh serverless and micros
group. So I will select this. Now next
you will give uh one globally globally
unique identifier for uh this uh
function app name. So uh you can uh your
first uh function app demo
demo do for example. So this will be a
unique identifier uh for your app name.
After that uh you will select the
operating system you will select uh uh
window. If you are using uh Linux you
will use Linux. Next you will uh click
uh on this uh runtime stack. You will
select runtime stack. Uh I will use uh
net. You can also go here but Java.
Yeah, you are using other uh in
technology. So it's up to you. Uh this
is the version of net. I will go with
net and this is the region. Again you
will select region which is closest to
your targeted user. Okay. So next is the
storage. So you can see here basic one
is completed uh storage. So uh is your
function need uh uh storage account by
default. So you will if you don't have
any storage account then you will create
storage account. Okay. If you are new to
zu function then uh how you will create
your uh storage account you will search
uh z storage.
Okay. Storage account. You can see here
storage accounts. So um uh you will
create storage account. You click on the
create.
Okay. So this is basically while basic
setting you will select the
uh the resource group and you will give
name uh
let's
Michael
is
it should be 24 character and
for example.
So yes, so this is available or again
you will select a region. Um this is the
uh primary service. Uh if you use the
storage for Azure file, you will select
z file. If you want to use Azure blob
storage, you will go with this. So uh
this is optional. So next you will
create the performance
like uh I will go with standard uh if
you need uh other scenario low latency
for example you will select the premium
but it will charge you but so I will go
with the standard uh and this is the
redundancy uh for you will uh I will go
local redundant uh storage if you need
copies of your data uh to not cross your
data so you can use uh this option like
zone redundant geio redundant and this
is different type of uh redundant
account uh types. So uh for to use the
low cost option I will go with low
redundant storage. Okay. So uh this is
used for non-critical scenarios. Um if
you look on for example backup so you
can use uh geo redundant and uh if you
want to use highly availability scenario
so you go zone redundant and if you have
a very critical case critical data
scenario so you will go jeez red. So
this is different top of redundancy tag.
So I will go for with the redundancy.
Next uh is uh uh another one is
optional. We will keep it as default. Uh
so uh you will at the end you will
review uh the storage account and you
will uh click on the create button. So I
already created so I will not create
again. So I already have uh storage
account but uh this is uh is I show uh
how to create um storage account. So for
SU function you will need a storage
account. I already created one storage
account.
Okay. So this is uh the storage account
I created. Uh the next option is the
networking. This is for uh like security
purposes. Uh to enable public access you
will click on you will click on or
otherwise you can uh you can off this.
And uh next is the monitoring. uh you if
you want to add logging to uh
troubleshooting uh something like that
so you will uh use this monitoring uh
feature as well but uh it will cost you
for example uh enable application inside
if you want application inside you can
select yes otherwise no uh so currently
uh I don't need uh inside for this uh
demo so I will uh select no because it
will cost you next is the development so
uh you can create the CI/CD like for
your Azure function app. You can uh uh
attach GitHub account. Um but you will
select this option continuous dep once
this is enabled you can uh you can
select uh your organization the
repository you have in the GitHub
account. So you can uh you can select
your repository. Okay. So for example
this is the uh repository I created. So
you can also create the branches there.
So I will go with our deployment. In the
other demo demos we will uh create CI/CD
blind. For this we demo we will keep it
simple. Uh next is the authentication
uh we will uh use the default options
and next the take take uh it's
categorize the resource for you. So
currently we don't need uh this is
optional. And then next you will review
the application. So you will review uh
subscription they are using resource
group uh name of the function uh secure
default hosting. So this is the
information uh the option the detail of
the isue function if you make any
mistake you can go back to the specific
tab and you can correct. So once you
review and everything is fine you will
create on create button. So this is how
you will create the uh Azure uh function
app. So I will not uh uh click on the
create button because I already have uh
one is your function. You can see here
Azure function function is created. You
will see in the list here.
Okay. So uh if you open this uh you can
check the detail of your Azure app. Uh
please note this point that uh in one is
uh Azure function app we can create
multiple Azure function. So Azure
function is different and Azure app is
different. In one Azure app we can uh we
can react different applications uh
different type of uh Azure function. So
once uh you open you you will see the
overview how the function which resource
group it is targeting uh what is the
status currently it's running and the
location is southern uh Asia
subscription and the subscription ID and
this is the domain name of this uh SQL
function. You can also open this. You
can see here uh this is up and running
and this uh uh uniquely globally
uniquely identifier. So uh or or as your
app is uh running and uh the operating
system is window server plan the service
plan uh service plan and runtime. Okay.
So you can also check uh activity uh we
will cover this in upcoming uh sessions.
Uh you can uh uh deployment. This is for
deployment as you this is the uh
functions
uh environment configuration in setting
them you will you can see environment
variable configuration authentication we
will encode this uh in upcoming lessons.
Okay. So uh but this is the basic the
overview of the uh Azure function where
you create. Okay. So uh and below in
this section in the function section you
will see all the function inside this
Azure A. So this is uh for demo purposes
I created this just for practice. So uh
this is the already created uh Azure
function inside this is your app. You
can see the matrix uh you can also see
the properties of the Azure functions uh
in this is the notifications. So uh next
we will jump into the uh visual studio.
You can write a z function in VS code
and visual studio. But for this uh demo
we will use uh is uh visual studio
because it have uh rich in features. So
like debugging and other they provide a
lot of things. So we will uh open the
visual studio. Uh so you will select get
new app like we will create one health
check uh Azure function and we will
deploy it to the Azure app we created.
So you can see here uh you will search I
recently open the Azure function that's
why it's coming in my list. If you don't
see this function you will uh you will
search here as your function. You will
select Azure function. Next you will
give a name to Azure function uh like a
function app
uh two or something else according to
your requirement function
and p for example. uh you will select
the location you will go next. Uh okay.
So uh here is some important things like
uh this is the worker the function we
will select the net uh 8.0 and this is
important uh thing like the function uh
trigger each uh your function will have
one trigger. So we will discuss this uh
in detail like we we have uh is your
provide different type of trigger. If
you say here um is your function
triggers. So the basic concept is that
each function need a one trigger to
start. Without trigger we cannot start
the Azure function. So we will
definitely need one trigger to start the
execution of the uh function. Uh where
as your function can run in parallel uh
or and the other concept is that exe
execution rank is very uh by the hosting
client we selected. So this is the uh
the the first trigger which is mostly
used is the HTTP trigger. uh it exe uh
HTTP it uh execute uh uh via HTTP
request. It perfectly suitable for the
APIs and uh web hooks. If you have uh to
do something uh on our API uh or web
hook then uh you can use HTTP trigger uh
like it support HTTP methods. The other
one is the timer. Uh if you want to do
any schedule b execution so this will
suit you the timer it supports encode uh
tabs model. Uh so you can uh create your
expression and it will uh done in a
specific schedule.
And next is the blob uh storage trigger.
Uh whenever u a blob is created or
updated so based on that you can uh
trigger any function.
uh perfect for file pro processing and
uh like logging image login something
like that pre requirement but it work
for uh is your block. Uh next the qu uh
qu uh q storage uh trigger if you have
messages Q and b b b b b b b b b b b b b
b b b b b b b b b b b b b b b b b b b b
b b b b b b based on that you want to
trigger you will use this que
storage trigger and um the other one is
the uh even trigger. uh it will work
based on the Azure uh service event or
it will it's using the event
architecture. The other type is the
service bus trigger. Uh complex workflow
and uh inter application uh
communication we can use and uh the last
one is the if you want to work on the
cosmos data uh DB so you can use this
trigger Cosmos DB trigger. So you can
select but for this demo we'll select uh
the HTTP uh so you will select this uh
uh next is the authorization level.
authorization level. Uh if you want to
add any uh authorization to your Azure
function, you will select uh you can
select this options. Uh but for now, we
will uh go without uh authorization. So
we will go anonymous. Uh so once you
create here you can see the function the
actual function uh that will uh deploy
to the uh function app. So there is also
some uh settings and this is the program
uh to so we will write some code here in
this uh uh function app one. This is we
are passing the uh is your logger to log
the information. So here we will write
one uh uh h check uh function for
example it will return iction result and
u we'll use the run method uh it will
run uh the is your uh function and uh
there you can use the uh we will use the
http uh trigger http trigger uh http
trigger and we will give uh a
subparameter like uh authorization level
will be anonymous anonymous
authorization level and uh it will be a
at
like that and the next variable we will
pass the route. So we'll uh define the
route for this function. So this will be
the health and for example this is the
route.
Next uh we will http
request
http requester and get this we will use
this variable. So I will remove this
autogenerated code.
Uh
so
uh run regard anonymous
start.
>> Okay. So
so uh this is parameter is completed. Uh
okay. So next uh we will do some uh
logger. We will log out information.
for example auto log info
and we write here some message
for example
and the
check
proceeding
okay so it's some logger information
next I will create a response object
that will need dot ls
function and I return
that uh response. So function uh
function uh attribute this uh l check
this will be the can check this is the
function attribute needed to make this
as a function. So we can also download
this topic.
Okay. So you can see here uh it's
running and this is the path uh local
path. If I open this in browser
you will see that this app is you can
you can see that it's return the status
and the type. So uh it's uh running
locally. uh in next we deploy this to uh
Azure for example uh to uh publish this
to Azure uh we will click on right click
and publish.
Okay. So you will select the Azure. Next
you will uh select the Azure function
app. Uh once you select you will see the
Azure uh app uh resource in your Azure
function. You can see here this is our
Azure function uh demo which I created
initially. So you'll select this and uh
once you select uh it will create the
publishing profile.
Now publishing profile is uh completed
created. Uh now you can see here this is
the publishing profile and uh once I
publish for example if you go to publish
it will uh publish the application uh in
the Azure app and the publishing uh
succeeded and uh we can uh now uh access
this on Azure. So it's deployed to vure.
So you can see here uh the function is
now available in the azure function app.
So uh if you click on this it will give
you the the detail of that function.
This is the function to get a function
file. You can copy and check.
So you can see here this is deployed to
Azure and it's give you the same result
here.
Okay. So I have a few question for you
like which trigger fit for your use
case. You will write this in your in the
comment of this video on YouTube and uh
how you will organize your function and
what hosting plan meets your need. So
this is the question for you. So uh see
you in the next video. Thank you. Hello
everyone. Welcome back to practical
serverless and microservices with C#. My
name is Mouhammad Abdullah and I am
Microsoft solution architect and uh uh
I'm creating this course for net
developer engineer who want to learn
about serverless and microservices with
in this video we will understand
microservices architecture. So uh what
is microser architecture in
microservices architecture is nothing
but a software deployed on a computer
network. Okay and different
microservices communicate via uh network
protocol and uh there is some constraint
and rule that microservices must follow.
We will see those constraint and rule
later in this video. We have monolith
application. Monolith application have
large single system, one single
codebase, one deployment unit. They have
like different problem. The first
problem is everything is in one package.
So one deployment unit and the second
thing is different technology in
different department. And the third is
they are difficult. They are hard to
connect with other uh software. And the
fourth problem is uh poor software
communication like they cannot talk with
other system. So this is the main
problem in the monolith application. So
to fix this problem uh service oriented
architecture is introduced. So they have
some key feature like uh they have a
network of communicating processes like
each service talk together. So this is
the key feature of the service oriented
solution. The second is modules are
separated uh separated. So each module
have a separate process like uh they are
packed uh in different pieces. And the
third point is standard protocol
communication. So they have some common
uh language to their to that language
they communicate. So and the fourth
point is first step toward this is the
serviceoriented architecture is the
first step towards microser. So this is
the first step. So uh you can see here
this is the timeline timeline like in
the first phase uh we have some SOAP
protocol uh this is the first
implementmentation which is the XML
based the second is the JSON based web
API light data format and uh the second
phase the third phase rest web API like
we already familiar with the uh ASP.NET
web services and this is the foundation
for the microser. This is some timeline
for the uh serviceoriented
implementation. So uh here you can see
service oriented uh architecture fix
four problem. The first one cross
technology communication like different
tech uh work together like windows
apache. So this is the uh they fixed
this first key problem and the second
problem uh which is fixed by the service
oriented uh architecture uh is
independent version like each module
evolved separately and uh public service
offering like they communicate with
Google API MS API something they
communicate with external um a access.
Okay. And the fourth problem is they
have some uh protocol standards like
they talk with some protocol HTTP rest
and gpc uh they can communicate with the
text binary communication. So this is
some problem which is fixed by the
service oriented architecture and when
this service oriented architecture
evolved uh they face some issues like
the the scaling issue is the main major
issue. So like uh traffic increased user
increased dramatically they need a
performance optimization like speed
become critical. So and the third point
is uh like hardware resourcing uh uh
resource allocation was the other key
issue in the scaling and the fourth one
is the requirement like in software we
uh we u have to satisfy the new coming
requirement in any feature like to add
or extend any feature. So this is the uh
u main scaling issue which is faced by
the service oriented architecture. Okay.
So you can see here u as a system
involve we have four uh new critical
requirements. So the first requirement a
is a um system need to independent
scaling each module scale separately. So
this is the first requirement. Second uh
zero downtime like on video operation uh
sometime need zero downtime like
Facebook Twitter need a zero down time
uh other system like a banking system.
Um and the uh requirement number three
is independent uh team independence like
different uh separate team per service.
So this is the third new requirement and
the fourth requirement is like a
maintenance isolation. For example, if
we fix one thing in one uh service it do
not affect other. So this is the four
key u new uh requirement. Okay. So to
fix this we have microservices solution
like they each module have separate
service like u we uh every every every
service have their own um each service
work like independently and uh we can
replicate so uh service as many as node.
So this is the scalability and uh they
also uh offer orchestrator like u uh
intelligent service management uh we can
create many copy of uh any node and the
fourth point they fix is the hardware uh
dedicated resources. So this is the
problem that Microsoft fix microservices
fix and uh you can see here orchestrator
role uh we have orchestrator that can
scale u uh like uh decide service copy g
scaling decision they take the scaling
decision resource allocation and uh they
uh we can also multiply orchestrator
possible or manual management. So this
is the uh microservices uh solution.
Next uh when we work when we work with
microservices we have some constraint uh
to follow some rule to follow. So the
first constraint is uh we must follow
the domain driven boundaries. We define
we must define uh domain boundaries
clearly module boundaries by expert
domain. Okay. So the second constraint
is replicable service every uh for
example dual scaling if we need to uh
scale up the scale up the service it
should be scalable. So this is the
second constraint and uh the third
constraint is uh independent team like
different team work on different uh
modules different services. Fourth
constraint is we must define a well-
definfined interfaces. And uh fifth
point is uh standard they have some
standard communication protocol. And the
uh sixth constraint uh is interface only
dependency like services depend only on
the interfaces not on the actual
implementation. So it will decouple the
system. It will not depend module A on
module B. So this is the constraint we
must follow on the in the Microsoft when
we decide uh or design in Microsoft
environment. So um Eric Evan
theory each expert domain use typical
language. If we go with the domain
driven design, it checks by domain use
typical language and uh when language
change it detect the boundary and when
there is a common language it uh it show
boundary context and if there is any
loose interaction uh it show no common
language. Okay, this is something about
the domain design like application split
into when we design microservices u
every module or some module have their
own boundary context in one boundary
context to we have one uh module as well
as we can we have multiple module as
well. So we will see this in detail.
Okay. Okay. So you can see here uh this
is the second uh two level uh is one
terminology which have the two level
service decision. For example, if we
have you have a uh e-commerce u
microservices for example uh then you
have two approaches. The first one is we
divide logically uh we divide divide
logical microservices like we divide the
team in three uh parts like for example
shopping cart team handle all the
shopping related uh feature and we also
the business also divide into team like
handle all the related feature and the
third is like shipping team handle all
the shipping related feature and in the
second level in the second level like
physical microservices we divide uh uh
deeper like technical we split the teams
more uh more uh technical split like uh
shopping teams split into product
shopping uh and uh recommendation
service, payment teams play into payment
processing history and refund service
and uh uh the shipping teams played into
like more deeper like shipping,
packaging, delivery. So uh the main uh
purpose of this is to team independency
uh and clear boundaries. We find clear
boundaries. So this is main feature uh
two levels uh two-le approach we two
level service two-le approach. So and uh
you can see here um when we design
microservices architecture we uh have
like team to manage team we have like uh
we use some tools like context map us it
um in context map we have usual team
interaction like we usually define that
shopping team will interact with the uh
gamma team and inventory will interact
with the shipping team. So it's like a
neighborhood neighborhood map it show
like a team relationship. So we use this
in team management. This is one of the
tool and the second tool is the uh
service catalog or we also set a service
directory where we define the services
owner of this service uh cost status
requirement and we use uh tool like
swagger data do postman for
documentation. So this is why this tool
matter uh because uh it will prevent the
confusion it will reduce the meeting uh
it will track the cost it will use for
to blend better and uh when any uh new
team join it will be easy for that new
team. So this is some tool when we work
with microser so we will manage a team
through these tools. So um when we
design microservices for example each
microser have their own uh database
because u if we don't have a separate
data database um then for example if
service A and B share table T for
example and service A modify the
structure of the table then this
migration changes uh the service B will
also uh apply these changes. So this
approach will break the independence
principle of the module. So u the rule
is only same service instance can share
database. Okay. So this is the some
principle you will follow uh during uh
during designing microser. So uh there
is some challenges when you work with
microservices like uh you will face data
duplication challenge like uh the
problem is uh like in microservices uh
some m for example service A and B
service B need some data from service A.
So we must uh duplicate the data inside
service B. Okay. So uh this is some uh
like initable uh change we must
replicate the data but uh we will follow
some uh like rule like we will minimize
data duplication and the other point is
we will duplicate the data in uh few
microser we will try to uh there will be
a few microser that will duplicate the
data and only duplicate those data which
is needed for that microser. Okay. So uh
in this slide we will discuss the uh
communication between the different
microservices like for example if we
have one microser that uh need to call
the service B and service B call the C.
So it create a sequential dependency and
it will add up the response time like
the service remain block it will wait
and uh we invest the computation time
and uh the response time will be higher.
So uh this is about the synchronous
communication for example if we uh call
one service and the other service call
the third service. So it will create the
sequential dependency. So uh to the
solution for this uh problem is um
synchronous data sharing like this is
the proactive approach where uh service
uh send data when uh data change and
it's a pre-process data u like
pre-process data sent to the potential
uh color and there is no wait uh time
for that request and data is available
when it's needed and uh this is the
synchronous communication between the
microservices like the other one is the
request response which is the
traditional one and like client send
request server process and it respond Uh
this is something like synchronous like
it weight is uh involved in this case
and in this case response time is up and
about the message based communication uh
like it's a datadriven communications uh
synchronous messages and uh it it's
acknowledge versus response in the
message based we have acknowledged but
uh in the traditional one we have
response we wait for the response and uh
some benefit of the synchronous
communication like uh we completely
avoid the wait time the message send in
queue like we can use Rabbit MQ and the
mass transient if we have in inmemory
calls if we have like the microservices
is deployed in different u machines. So
we can use the uh like rabbit MQ um yeah
acknowledge mark completion uh we have
retry policy for example if any message
fail we have retry mechanism and there
is a timeout handling and uh error
recovery mechanism is also available in
this case. So uh this is all about the
section number two. Uh in the next video
we will discuss uh microservices type
and uh some patterns related to the
microservices. Hello everyone welcome
back to practical serverless and
microservices with C#. My name is
Muhammad Abdullah and uh I created this
uh course for net developer and engineer
who want to learn about serverless and
microservices in C#. So this is section
number three and uh if you missed the
section one and two please uh check out
the playlist. Uh so I will share the
link in description. So please check the
link and uh first watch the uh section
one and two. So uh in this section we
will uh uh we will cover um
microservices types and uh some
different patterns. So you can see here
um we have uh some front end
microservices and the worker
microservices. So there is uh difference
between these two microservices. So the
front end microser
face the client like the user it's the
uh userf facing uh microservices that
perform the uh client request and
response and it can also handle multiple
parallel requests from different user as
well and uh th request pattern. it
follow thread per uh pattern request. So
you can see here in the diagram we have
uh different microservices. Some of them
is front end microser like first query
microser and the at the bottom we can
see update microservices. This is the
front end microservices like it
facilitate the user or the client and
the next is the worker. You can see in
the diagram we have in the middle we
have worker microservices that only deal
the inner communication like no direct
user interaction only process the data.
uh it work in the assembly line
operation and uh it also work on a Q-
based processing it perform Q-based
processing. So this is two uh types one
is the front end the other one is the so
here is the example of car sharing
application. So in the whole course we
will focus on this uh car sharing
application we will create uh services
for it. Uh in this example you can see
here we have like six microservices uh
routing location at the top we have like
u routing allocation best uh matches and
the at the bottom we have car sharing uh
that fulfills request uh car seeking and
uh uh route choosing and in the middle
we have the route planner which is the
worker microservices the the top three
and the bottom three are the uh the
other type of front uh front end
microser and the the route planner is
the worker microser. So this is example
of the front end worker microser. So
next you can see here car sharing
application when we do analysis we find
two domain. The first domain uh
represent car sharer language which
represent six microservices and the
second domain is the routing language
like the meter uh route planner. So this
is the two domain we will uh cover in
this course. And um if you check the
architecture layout um the top
microservices are the query uh it only
give data to the user and the top at the
top microser they are update microser
update microser and the micro uh worker
microservices in the middle you can say
the gray shaded. So this is the worker
microser mostly query and update
microservices are the front end
microservices. So this is some
indication. So you can see here um this
is the car holding journey. car holding
journey. Uh it uh handle all the crude
operation for the car owner and manage
the creation and availability. Uh and it
also integrate uh integrate with route
planner. If you check in the diagram car
holder, it also interact with the route
location and route planner. Uh and uh uh
route choosing update microser. You can
see here uh car holding except car
seeker uh select route matching uh
resource destination remove incompatible
option after match. So this is some u
information about the microservices we
will uh cover in the course we will
actually uh practically do this all
microservices. Okay. So the um car
seeker request if we go if you check the
car seeker request it will uh handle all
the uh passenger request uh it will also
do the crude operation like the car
holder and it will also submit the
request uh after date selection they
will select the date and it will uh send
the request. So this is some about u uh
something about the car holding uh
holding holders and the okay. So uh in
this slide we will cover we will check
the complexity uh reality check like uh
some tooth about the microservices like
if in microservices we have like a
different services on different machine.
So the testing become complex. So this
is a truth about microservices and the
maintainability is uh time consuming and
complex data uh driven communication
pattern we will follow complex
datadriven communication pattern and uh
it will also increase the cost. So uh
this is some u truth about the
microservices. So while choosing the
microservices you will also keep this in
mind. Okay. So here many people ask
question like when to add up
microservices. So here is the some use
cases. So uh if uh your team is more
than five then you can go with the
microservices and if your application
have flight traffic and uh time
consuming modules are there in the
system so you can go uh with
microservices and um if you need module
level optimization so you can also
choose the microservices and uh uh in
microservices we must split the train
because it will uh define clear
boundaries and it will avoid
ambiguities. So uh when not to adopt the
microservices if your application is low
low traffic and uh you have small team
and uh the requirement is
straightforward. So you can not uh adopt
microservices. So uh next we will uh
like cover some patterns in
microservices like why we use patterns.
So uh in microservices we face different
problem. So uh developer and u
programmer create solution for that uh
particular problems. So that um solution
became a patterns. So whenever we face
the same problem in our micros service
so we can use the existing uh pattern
instead of creating uh our own pattern
because the already created pattern is
tested and it work for other users as
well. So uh we will cover some
microservices uh pattern uh mostly
mostly concerned with the resilient task
execution which is the one of the
microservices pattern. Okay. So what is
resilient task execution? So u in
microservices uh microservices can be uh
moved between machines uh for load
balancing. Okay. So uh load balancing
like distribute traffic between uh
nodes. So uh so also the microservices
can be restarted to recover from memory
leakage and performance issues and some
microservices can also miss some
messages uh during any operation and u
it also fail due to software bug or
hardware bug. So uh to uh to to deal
with this we have some strategy like
failure detection strategy. So in this
case we have two methods. One is
unexpected uh uh exception and the
second one is the timeout exception.
Okay. So uh we will see uh that how we
can cover this uh failure. So u we have
some reliability patterns reliability
patterns. So they have three major
standard pattern. The first one is the
exponential retry and the other one is
the circuit breaker and bulkhead
isolation. So we will check this one by
one. So the first is the exponential
retry. So what is the purpose? Uh to
overcome temporary failure. Okay. So
like when service restart during that
pattern uh we implement the retry and
the retry increase uh um exponential
number of retry and uh when the when the
maximum number reach we throw exception.
So uh we use we can use the exponential
retry u pin uh to get reliability in the
microservices. The second one is the
circuit breaker. Okay. uh like when uh
exponentially try reach maximum. Uh in
this in that scenario we use the circuit
breaker uh like block we can block
access to resource for fixed amount of
time and return immediate exception
allow time for human intervention or
manual fixes uh and it also circuit
breaker also prevent cascading failure
and the the third one is bulk ahead
isolation and it prevent failure and
cognition propagation. So here is one
point what is cognition propagation. So
uh cognition propagation happen when one
service become slow or overloaded. So it
start consuming all share resources of
other microservices like thread CPU and
DB connection. Other service that share
those resources also slow down. So uh
this is called cognition propagation. So
bulk ahead isolation prevent cognition
propagation. Okay. Uh and um replica use
same database this example um and one
replica open to many connection
cognition database for all the replica.
And what is the solution for this is
partition connection like we will define
maximum number of u replica per uh per
per service. So this is the solution for
it. So uh you can see we have some
strategies uh like uh we can limit
active request like only five database
request at a time per service instance
is allowed. This is one strategy. Second
one uh extra request keep the extra
request uh in the line. Uh so uh and the
third one is if the line is too big the
queue of the message the request is too
long stop accepting more uh request. So
this is the third strategy and the four
uh when um return error is instead of
crashing. So um if there is any uh bug
or crash scenario in our microser so
instead of crashing uh return the error.
So this is some strategy to uh bulk
ahead implementation. Okay. So next is
the uh communication pattern like um we
have output input message skew which is
needed for reliability and uh publisher
subscriber u we can get the loosely
coupling and the other one is the event
based messaging messaging which is uh we
will cover this in uh coming slides. So
when we u implement the sequence
communication we face some few uh
challenges like u message copy until
acknowledgement. Uh so we will keep
until the consumer of the message is
does not implement or accept or receive
that message. Uh we can enable retry
operation uh handle communication
failure and um the second is the message
duplication. uh in many scenario uh this
is a challenge in the synchronous
communication uh like message
duplication and the other one is the uh
like um message order like message can
arrive arrive out of order and wrong
order can cause uh consistency. So this
is a few things few challenges we we
will fix uh in as synchronous
communication. So what is output message
Q solution? What is the solution for uh
output message Q solution? So here you
can see in the diagram Q's all output
messages we will keep all the messages
in the queue and uh when any consumer
acknowledge the message just remove uh
the message from the queue and um if uh
any message fail fail or time out occur.
So just uh review the message and uh
include attempt number of exponential
exponential retry. So we can add the
retry mechanism and uh store minimum
retry time per message. So we can also
define u minimum retrive time for each
message. Okay. So this is output message
Q. The other one is the input message Q
solution. Okay. So here you can see u
recognize and discard duplicates. So in
this pattern uh in this uh communication
pattern uh we will uh avoid to
duplicate. We will recognize and discard
the duplicate uh messages and uh we will
reconstruct the correct message order.
And uh what is the processing rule? Read
message only after filling sequence
call. You can see here in the diagram
and uh what will be the result order um
we will uh we will keep the messages in
order and the duplicate free message
processing. This is what we can get from
the input messages cube. It will u uh it
will remove the duplication. It will
give order.
Okay. Uh in event based communication we
face some challenges like uh want to add
a stat uh statistic. If you want to add
a new microservices like uh that will
give you statistics to the car sharing
example I shared in the in the previous
slide. So what will be the problem? Uh
this statistic microser need like some
data from the existing microservices. So
what you will do? you will modify the
source microservices. So if you do this
you will violate the u modification
should not propagate principle. So what
is the solution for this problem? The
solution for this problem is the
publisher subscriber pattern which is
the another communication pattern. Okay,
you can see that diagram sender send
message to the publisher endpoint. Uh
there is no direct communication
reception and uh interesting
microservices like recipient one to uh
subscribe to that publisher endpoint and
uh publisher automatically forward
message to all the uh subscriber and new
microservices. what will the uh
statistic microser will do? It will just
uh subscribe to that uh pattern to
receive the messages. So in this pattern
we can uh like get the microservices
depend uh independency maintain uh so we
can get the each module will be
dependent. Okay. So for this we can to
also use the message broker like uh uh
handle by application called message
broker. We can use the message broker
like in market we have debit M2 and
Azure service bus uh which offer
publishing message and um it will
deliver to all the subscriber. So this
is a message broker and uh here is the
summary like u to get the resilience uh
in the micros service we will follow
some patterns like exponential retry uh
it will use for temporary faults circle
back long-term failure uh and bulkhead
isolation failure contentment and uh
some other uh communication patterns
like like out output input messages cues
we can get reliability publisher
subscriber uh we will we can get the
loosely coupling in the event best
messages uh so this is all about the
okay in the next uh section and we will
uh cover the doer and onion architecture
and uh uh if you are new to this channel
please subscribe and uh if you have any
questions so you can ask in the comment
thanks for watching. Hello everyone
welcome back to practical serless and
microservices with C#. My name is
Mohammed Tdula and I'm Microsoft Azure
solution architect and uh for this
course uh this is my edge agenda. So
first we will understand the how ser
serverless application work then we will
look the microservices then own
architecture then how to work with
docker we will understand different type
of function uh their pipes like trigger
background functions IoT function and
microservices how to organize
microservices with kubernetes uh then we
will see the container containerization
in microservices uh with help of
kubernetes and user container lab then
we will check the security observability
inside serverless microservices and uh
in the whole this course we'll consider
in one uh car sharing app. So we will
see this in action and lastly we will
understand how net spine work. So if you
miss the other sec session like one to
three. So please check out the first
second and third then continue the for
this session and um I will share the
link of the playlist in the uh
description of this video. So uh in this
uh session number four we will uh
understand the onion architecture. First
we will see some of the theory. Uh after
that I will show you this in practice.
uh uh there I create one template and I
will show each part in the template and
I will also share the template link in
the video description. So u so what is
onion architecture? So uh there is one
principle like u uh the onion
architecture software design first of
all this is a software design pattern
that organize the code in layer. So
onion architecture have different layers
but the key principle is the dependency
direction. So uh like the outer layer uh
can reference inner layer but the inner
layer doesn't have reference to the
outer layer. So this is the key
principle uh of the onion architecture.
So this is a basic diagram. We have the
first layer the infrastructure layer.
Then in the middle we have application
uh specific code. So infrastructure we
have a basis file uh cloud services. So
this is the basic version of the onion
architecture. So the core um concept uh
you will think like onion the inner uh
innermost layer contain your core
business logic but what your application
actually do uh it's in the um it's in in
layer while the outer layer handle
technical concern like database web APIs
web interfaces and the external system.
So this is the core concept of the uh
onion architecture. So in onion
architecture we have as we mentioned in
the slide we have some different type of
layers. So outermost layer is called
infrastructure layer. It contain all the
technical drivers like uh database
connections, web frameworks, uh UI
components and it handle the dependency
injection and reference all the inner
layer but uh never reference by them. So
this is the outer most we will see this
in diagram as well as in code as well.
So uh second we have uh if we go check
this is the uh complete on as you said
as I mentioned that in the outermost
layer we have uh infrastructure
infrastructure have database uh
databases files cloud services and web
APIs. So this is the first layer the
infrastructure layer uh handle
dependency injection reference all the
inner uh layers but never reference by
uh by other. So uh second we have
application layer uh uh define uh what
operation your application can perform.
So in this layer we have all the use
cases all the uh like command handler
queries inside this uh this layer uh
like it contains use cases application
specific workflows and uh it have only
uh reference by the domain layer. So
this is the second layer we have
application layer and this is the heart
of the on architecture like the domain
layer which is also called the core
layer. Uh uh it contain all the business
entities and rule uh and uh it's the
heart of the application like what
application fundamentally do it's inside
the domain layer and it has no
dependency on other. So uh we will see
this in action. It has no uh reference
by other project and uh
domain layer sometime like split into uh
model like entities and domain services.
We will see this in action like we will
cover this course uh with respect to uh
domain driven design. So in domain
driven design we have entities aggregate
root aggregate. We will see this in uh
in practice. So this is a complete uh
diagram of the model architecture. First
we have the infrastructure and uh then
we have application services and then we
have domain layer. So uh infrastructure
have database web APIs and other UI
component and uh application services
have all the use cases what the
application can actually perform and the
the heart of the application domain
layer all the business rules and models
is inside in the domain layer. So this
is the complete uh onion architecture.
Okay as you can see that uh this is the
onion architecture uh template. So as
you can see here we have uh basically
four layer the the first one uh we will
go one by one. So first we'll uh discuss
the infrastructure layers. So
infrastructure layer we have a DB
driver. So there is one uh C# class
library that contain the database
related things and uh we can also call
the web API layer as a infrastructure
layer. So these two layer web and DB
driver is the part of the
infrastructure. The DB driver uh handle
the DB connection. You can see here this
is DB context. Uh it has all the
required model and all the migration
related things. U and if we go to the
repository it has all the repository
implementation like uh in this template
I'm considering the car booking
application. So it have uh it has
booking repository, core repository and
uh uh and user repositories. So also we
have extension to connect this model to
other. So we have this extension we will
call this in uh like program the main uh
dependency injection. So this is the
infrastructure layer and second uh if I
go back to the you can see here um the
first layer infrastructure then we have
application layer. So if you go back to
the code you can say here application
layer this application layer and uh
first I will also show you the
references. So the key principle of the
the key principle of the uh onion
architecture uh this is the uh infra
infrastructure layer and it has only uh
reference uh of the automain layer. So
nothing else um command handlers like it
will create booking it will create car
create user. So this is like the command
uh command handlers use cases you can
see if I open uh so this will create a
booking for us. So this is uh one of the
use case and uh in this folder the
command folder we created the command
like if we if you are going to create a
booking so this will be the command this
is like a request okay first we will
pass the user id par ID start time and
end time. So this is like uh the command
for this command handler um you can see
here I'm passing it the I request
handler. So this is the command we
created in the command folder. So next
we have uh event some uh like inside
microservices or domain do design um
entity when any entity uh change and
when any change happen in entity so it
will u create some event. So when
booking happens this event will be uh
this will uh raised. So we will see this
uh in uh like detail uh in the event we
have extension uh like data uh uh
dependency injection handler and this
service collection to connect with other
part of the application. Then we have
queries. Uh so we have uh in the secure
we have some command and queries. So um
for this we um get all the available
cards we have this query get user
booking. So we uh we define all the
queries here. So this is my main point
is to show you the application layer. It
has all the use cases. Uh the domain
layer the domain layer have like all the
domain entities all the business logic.
So uh this is model booking model for
example. Uh this is the booking model uh
username car and uh this is the all for
the car. This is user. So we have all
the all the core model inside this
domain layer. So let's come back to the
slides.
Okay. So I practically show you how the
domain uh
domain look like like first we have the
infrastructure then we have application
then we have the domain layer. So the
dependency is going inward to towards
the domain layer. The domain layer will
be independent. It will not contain any
reference. the application service will
have reference only to the domain layer
and the infrastructure layer only
reference the application layer not the
uh domain layer. So this is the
dependency principle for the uh onion
architecture. Now next we will cover the
domain layer of the onion architecture.
So core concept is contain domain
entities like classes with behavior
encoded uh with public u methods and
entity can only be defined through the
method presenting actual domain
operation. uh you can think like as a
domain driven design we will uh focus
mainly on the domain driven design so it
use also use a unambiguous uh language
in the business uh business name will
follow the same language so this it's
called unbu language okay so then we
have the domain layer domain layer uh
domain entities we have state can only
be changed through the method that
encoded actual domain operation and uh
we have aggregate aggregate must be
completely covered from the persistence
code and we have like Each domain layer
have one repository interface as well
and uh in the domain layer we have like
one is called the domain entity and we
other have the core ef core yeah entity
framework core entity domain entity have
method enforce business rule and tree of
nested object like aggregate aggregate
is the group of related object it's
called the aggregate in in domain driven
design and um uh efropy
value pair with no methods and it have
uh only sing it only save data inside
the database uh so there is some
difference between these two. Okay. So
please understand this uh domain entity
have method in for business rule and uh
it also have the aggregate like group of
related entities and but the entity
framework entities just uh property
value pair and u then we have aggregate.
What is aggregate uh complex uh tree of
object related order. For example, if we
have order and uh the other um object is
purchase order. One is called purchase
order and the other one is purchase
order item. So this is the example uh of
the aggregate. So we will also check the
aggregate route as well. uh for example
if we have purchase order purchase order
and and one is purchase order item. So
uh purchase order item cannot exist
without the purchase item. So here the
aggregate root is purchase order and uh
child object cannot exist independently
like purchase order item cannot exist
without purchase item. So uh manipulate
it as a single unit. So we can treat
this as a single unit also called it
aggregate. So here is the example of the
aggregate. We have purchase order where
we have uh purchase order items. Okay.
And u only thing uh that can change uh
or manipulate the purchase order item is
the purchase order. We cannot access
purchase order item outside the purchase
order. So this is the rule of the
aggregate indent design. So uh you can
see here in this purchase order we have
we can add we can remove uh item to the
purchase order type list. So this is the
uh we have the aggregate inside domain
design. Then uh there is one another
concept inside domain domain design is
the value object object with no
identity. So in in identity in domain uh
identities we have one unique
identifier. So HD can be identified
uniquely but in case of value object we
have no u ID there is no uh identity
inside this object like for example if
we have address uh in address we have
phase number uh street number house
number. So these three connect value
object related to the address. Same for
the money as well and it's mutable once
it's created cannot be changed and uh
implemented as C# also provide the uh
record. So we can implement this as a
record when we do actual coding and
there is no repository needed repository
interface needed for value object. So
here is the example of the value object.
Uh in do design for example if we have
address like country on street so
combined we create one credit card which
is called the value object. So uh next
we'll cover the repository pattern um
like uh since aggregate and value object
differ from ORM entity like RM actually
save the data inside the database but
aggregate and value objects are
different we we checked in the previous
slides when using ORM to interact with
database you must translate OM entity
into aggregate and value object and vice
versa each time exchange data with ORM
to the ORM we exchange data from the ORM
entity to the aggregate value object and
uh aggregate and value object to the
ORM. So in this case the domain layer
interact with actual OM implementation
through the interfaces. So in this case
we need the repository pattern. So this
is the core uh concept why we need inter
um repository pattern in the domain
design. So this is the reason and there
is one rule each aggregate root I mean
we said that aggregate root main entity
in a group of related entity like in
aggregate we have related entities and
there is one thing uh entity uh which is
called the aggregate root all the child
entity aggregate depend on the aggregate
root uh should have its own repository
interface. aggregate root one interface.
This is the rule and the repository work
with the whole aggregate but not with
the individual entity. So this is the
another rule. This keep your code clean.
What is the purpose code clean modeler
and align with DDD domain driven design.
So we will follow the domain driven
design principle. So that's why we need
this repository. We must follow this
repository rules. Uh example of this uh
repository is purchase order. For
example, in previous uh slide we see
this is a purchase order. This is one
aggregate and uh what is the uh root
aggregate? Purchase order is a root
aggregate and that what is the child u
child entity purchase order item is a
child entity. So this aggregate it will
need one repository one repository and
here is the rule this example of the
repository. Okay and we have new geting
delete as deleted as with the purchase
order and get release. So this is all
the functionality it will provide. So it
will need the interface. So next we have
uh domain services layer. In complex
application, it's best practice to split
the domain layer into model layer and
domain service layer. Okay. So domain
layer uh contain only aggregate like
entities and value object entity have
unique identity and value object without
key. So this keep key difference and the
domain service layer. What is the
purpose of domain service layer? Contain
repository interface and domain
operation that does not belong inside
the aggregate. So contain repository
interface stis and domain operation that
does not belong inside aggregate. So
this is each like model have domain
layer have domain service layer as well.
So domain service layer have the
repository work only uh with aggregate
like order customer and but sometime you
do not want to the whole aggregate you
just need a read model like u uh get
data from any database. domain service
you want to show a dashboard. For
example, if you want to show dashboard
with customer name, order total order
and last order date. So this is the
requirement. So this will not come from
the single aggregate but it will come
from uh like customer and order. So here
we will use the domain service that will
interact with different domain or
different entities like um customer and
order. Uh now we will go deeper into the
application service. Uh it's a layer
about the domain layer. So uh you can
see here uh this is our application
layer. So it's uh have only one
reference domain layer. So it's the
layer above the domain layer. So the
second layer application layer. We'll
discuss this in detail. Uh it does
contain domain rule. Domain rule itself.
For example, order cannot be uh less
than zero. Something like when we uh add
item to the c or when we place order.
Order must be credit. This is one of the
simple uh business rule domain rules and
um not leave in aggregate and domain
service. But it accept command uh it
accept what what is the accept command
uh and query. They accept command and
query. You can see here um it have the
command and the queries
all the right repository or uh query
service public domain event uh is
something important. Uh we can see here
these are events. So when booking
created uh we will raise this event. So
service also raise the uh domain events
as well. Okay. We have a command
operation query operation uh command
modify aggregate through repository. Um
repository can raise domain event as
well like placeholder event and query
use query service it return duple uh no
domain events just return data. So this
is about the application layer uh when
you go to the query it does not have the
event no domain events just return the
data but uh this uh uh like we have
create event handler uh command uh when
booking is created if this event will be
generated. You can see here uh when we
create the booking so when we create the
booking at the last point publish to
main event. So this is called plishing
domain event. Uh command ar raise the
domain events. So this is I explain this
in the code. Um application uh service
operation operation in the application
service layer are split into two types
like queries command already discussed
command exe execution can trigger
events. Uh also application service must
uh also handle domain events already
explained and we have queries. This is
the some deeper look of the queries. a
query object one or several similar
queries usually have one or several
method that take input and return the
queries query result. If you go to the
query for example get available car
queries. So uh it will uh get it will
get this query uh we are only passing
the location this is the query and this
is the query handler most query method
call single repository method reming the
needed query to some some execute serial
repository. So query can also get data
from single repository and it also can
get data from multiple repositories as
well. Uh next we have uh query testing.
During system testing actual query
implementation are placed by fake
implementation. So each query has a
associated interface coupled with the
actual implementation in the dependency
in engine. This enable the UI to require
only the interface to infrastructure.
Allow testing with fake implementation.
If we don't use the interfaces here so
the system will be unable to test. So to
test that uh we must like you can see
here this query is inherit from the I
request handler. If we don't do this
like u inherit from the any I request
handler the system cannot be testable.
So uh to make the system testable is
most important thing. So we must follow
this uh testing principle. So like this
is the example we have one query that
contain and uh that will return the
basic uh information video and it will
take the start and uh you can see here
um uh we are inheriting this purchase
order by started from the I purchase
order by start query. So this is
important thing and uh and I purchase
order by started query is inheriting
from the IQ query interface. So I query
interface is necessary for to make this
system testable. Next we have command.
We will see command in detail. command
work differently because each command
represent a single application operation
for better code readability. Uh like
command we follow the single repository
principle like each command will have
only single operation. So each uh IU of
work like IU of what is unit of work in
DDD or Q plus CQS we are following
momentum design and CQS uh like in DDD
and CQS each command uh load aggregate
each command load aggregate via
repository uh call method on them to
apply changes uh command apply changes
and uh then we have save changes. So uh
first we load then call the method to
apply the changes then we uh have
another uh save changes in database but
in reality you may touch multiple in a
single command you want them to uh you
want them all to save or rotate together
like for the transaction if we deal with
multiple uh command so make uh to make
this atomic transaction this uh we will
use the uh unit of work the job of the
to make all the repository operation to
make it in the same transaction. If I go
back to the uh here to the code uh
you can see here I have I unit of work.
So it will make uh the atomic
transaction to make the transaction
atomic we use the I unit of work. We
save entities we have start as commit
async and roll back asing. So example uh
unit of work example uh save starting
commiting and roll back. So uh unit of
work uh breakdown um we have uh sing uh
save entity as save all the update in a
single transaction return true if store
actually change otherwise false and
start we use to start the transaction
commit and roll back uh respectively
commit or roll back and open uh
transaction. So this is needed to make
the uh data uh consistent. Okay. So unit
of work transaction control method are
useful for this is the uh save start
commit and roll back. So this method
transaction are useful for uh in closing
get operation and final save entity as
save in the same transaction. So example
of uh this is um we start the
transaction uh we get all the flight
async uh if flight um there is no seed
uh we roll back if the seat is less than
zero we roll back the system otherwise
we commit. Okay, next is the domain
events. For example, uh let's go back to
the code. Uh let's go back to the code.
Uh you can see here when booking is
created. This uh is raised. The booking
u booking create event is raised. So
this is called domain event. This is
called domain event. Uh raised inside
the same microser when something
happened to aggregate. When something
change in the aggregate, the command
runs the uh domain event. For example,
order placed event in our example uh
booking created event. uh when a new
record is created handle single
handle inside the same microservices. So
uh it will be inside the microservices
boundary and it follow the publish
subscriber pattern. So for this is the
publisher and uh where is the subscriber
it will be inside the handler. So this
is the um the actual event handler. So
uh whenever anything any uh anyone
subscribe it will subscribe to this one
and it will be notified the event
notification. Okay there is uh two
things like one is the domain uh and
other one the integration event. So
anything uh happen inside the microser
it is called domain event. But if one
microser communicate with other microser
uh then we use the integration event
array to communicate across the
microservices. Example order place
integration event send to a payment
microservices are notification microser.
So here two microservices are
communicating to the integration event.
So this is two type like one is domain
event other one is the integration
event.
So why use why we use the event like
inside microservices boundary. So why
why this important? Aggregate arrays
event about important side change.
Instead of writing uh side effect inside
we uh we capture the event we capture
the event. This event can uh collected
and then the command handler publish
them before calling in work. This ensure
that event are only published if the
transaction succeeded. So uh if we go to
the code here uh you can see here uh we
publish the event after saving making
changes to the car repository then we
raise the event. So this ensure the
event are only published if the
transaction succeeded otherwise we will
not uh so we will also take help of the
event of work uh save changes async as
well that's why we u we define um events
inside the uh microservices boundaries
so this is the structure of the event we
have interface and then we have a
handler if I go back to this this is the
publishing event uh code and uh go
inside uh this is a domain event and
this is a definition of the event and uh
we are inheriting from the uh from uh I
notification handler.
So this is the example uh notification
event and uh associated each event with
all its handler. So here is example how
to associate event with all its handler.
So we have event event triggers and uh
inside that we have one uh structure
then we have one trigger. We will uh
learn how uh domain object work with
database because uh the business logic
of the domain should not know about the
databases because uh if they do uh they
will violate the user. It will use the
infrastructure layer. So this is not uh
uh recommended. So database store data
in table and rows and uh the business
object like aggregate uh work
differently than the database table like
uh we cannot pass domain object directly
to the entity framework entity framework
but model is different than the
aggregates or the business domain and
the ef
domain objects. So this is one of the
problem. So how we will uh fix this
problem? So there is one solution called
a state object pattern. State object
pattern. So the core idea of the state
object pattern is that it separate the
state from the behavior. So uh state
mean like data like object has some data
and the behavior mean the logic. So uh
with the help of state object pattern we
separate the state and behavior. So how
we'll do this we will see in the next uh
slides. So uh first of all uh for
example uh uh first step we create the
state interface where we define all the
properties like uh creation date u
delivery time and the list of item. For
example if we use the purchase order
state. So this is the first step. The
second step will be u business object
use this interface. So we will pass this
uh state uh interface inside uh the
constructor. We will use the a get
constructor and we will pass that state
uh with the help of our constructor. And
uh then uh we will save this uh state
inside privately uh private variable
private state variable. As you can see
in this example we have one private
variable I purchase order state uh and
uh I'm passing the state inside the
constructor and we are saving the state
inside the private state v variable. So
this is the second step and the third uh
step you will modify the entity like
database entity you will inherit it from
the uh the interface purchase state. So
as you can see here uh in first step we
created the state interface and we are
inheriting u the database entity from
that state interface. So this is the
last step. So uh how I will show you
this in the code like if I go to the
code. So you can see here uh I modified
the code uh to uh fulfill the
requirement of the state uh pattern.
Okay. So you can see here uh now this is
the aggregate and uh I created one state
interface called IICR state. You can if
I go to this you can see here uh this is
all the properties needed uh for this
aggregate and uh this is the first step
and uh second we are the aggregate is
the uh we are passing this state
interface using this aggregate
constructor and we are saving the state
inside uh this private variable. So this
is the um example of the uh uh state
object pattern and uh you can see here
we have in this uh class we have
behaviors uh and uh we have behaviors
and uh the state is uh located in this
state interface and the second part this
is the database model. Uh so uh we are
inheriting this from the I state call.
So this is the last step. So you will
follow this pattern to uh make this u
like u state object to follow the state
object pattern. So that's uh this uh in
this way we can uh communicate with
database and uh database and uh so this
is the example of the state object
pattern. So here uh is the so this is
the life cycle of the uh aggregate
aggregate life cycle. So first uh for
example u we are uh we need some
customer data. So we'll use the uh
repository interface. Uh so uh it will
get all the data from the repository
using the DB driver and uh the DB driver
will contact with the ORM. ORM will map
the data and it will get the data from
the database. So this is the first uh uh
flow u it will give the data. Uh so you
can see here uh from the right side the
the database will provide you the tool
and uh then the ORM the ORM will start
tracking that entity. ORM will start
tracking the the entity and u the
aggregate the constructor aggregate will
create uh will accept the state state of
the uh entity state of the entity by
using the aggregate constructor and it
will create new aggregate. You can see
here uh in the dashed uh line the
aggregate is communicated uh the
accepting the state through the uh
aggregate constructor. So uh once the
aggregate uh ex uh receive the state uh
then uh we will make changes to the data
then uh unit of work in the last flow
you can see here from the left to right
uh we have unit of work after making
changes we will call the save entities
and uh implementation of unit of work
will uh pass that changes to the ORM
engine. Basically the OM engine will
follow the tracking. So the magic here
is the tracking. So you can see here the
test arrows uh it will track all the
changes and it will update the data
inside our database. So this is how the
uh state object pattern work. Okay. So
this is the last topic. So I hope you
will find this helpful. If you have any
question you can ask and if you are new
to this channel please subscribe and uh
like this. Hello everyone. Welcome back
to practical serverless and
microservices with shar. My name is
Muhammad Abdullah and I am Microsoft
certified as a as your solution
architect. For this course, our agenda
is uh to understand the ser serverless
application, how serverless application
work, how to deploy serverless
application. We will understand the
microservices on onion architecture,
different type of onion architecture
like domain layer, application layer and
uh uh infrastructure layer. Then we will
learn docker and then Azure function,
there are different uh triggers,
background function, IoT function,
microservices in detail. And uh we will
u u uh we will see microser organization
with uh kubernetes how to uh use
kubernetes with microservices. And then
we will uh learn practically container
and kubernetes and uh is your container
app and uh how to apply security
observability for serverless and
microservices application. And for this
course we will see the uh the car
sharing application uh microservices
practically and uh lastly we will see
microservices uh with netswire. So this
is our agenda. If you missed the
previous lectures, please check out and
uh you can continue. Session number five
is related to docker. So we will learn
about docker. So microservices need to
be independent of their host environment
to enable easy load balancing and to use
the hardware uh utilization. So uh we
are facing one challenge like if you are
working with legacy application with new
newer modules they use the different uh
development stack that create dependency
in the environment. Each uh stack has
own requisite. So this is our challenge
to to fix this challenge. Uh the older
solution is to uh deploy each
microservices on the separate virtual
machine. So this is the old uh solution.
But here is one problem with this
solution like virtual machine are slow
to start and stop. So this is the
problem with mic virtual machine because
they have their own copy of the
operating system which is insufficient
for microservices that need to be quick
like microservices need to be quickly
start. So uh this is the one problem
with virtual machine. So uh we can uh
fix with this problem with the container
solution like container is lightweight
and efficient uh form of the
virtualization. So uh in this uh video
we will see the uh container. So uh the
container solution how they work like u
virtual machine virtual machine that
utilize the entire um machine including
the operating system uh container
utilized at the file level and uh they
sit on the top of the operating system
kernel and they use the host operating
system like kernel DL driver but use it
native feature to isolate processes and
resources. This create an isolated
environment for the application they
run. So uh this is the um how the
container work like we will see in this
in diagram like uh we are comparing u
docker container and virtual machine and
you can see here virtual machine have
infrastructure layer then host operating
system then uh the virtualization uh
tool hyper visor and uh it have like
three virtual machine virtual machine 1
2 3 and they each virtual machine have
their own guest operating system but in
case of docker they are using the host
environment like host operating system
and they use the docker engine and they
create a container to run a different
application. So this is the main
difference between container and the
virtual machine. So a container have u
the what is container like uh the
running instance of application is
called container. Uh they are created
from the images. So first we have images
then uh from images we create multi
containers uh multiple instance of
container and what is images already uh
read only template that contain all the
necessary code instruction library
dependencies to run the application. The
same image can be used to create
multiple identical container. We can
create multiple uh container from single
um image. And uh what is image directory
where images are uh stored and managed
uh like you can think like uh docker hub
we can store images uh in docker hub we
can also use ACR as your uh container
registry. So this is a type of
repositories where we can uh save the
images and um this is the naming u image
naming and tagging. Uh for example u uh
first of all we have this is the example
ncr.microsoft.com. So this is the part
of the uh domain registry and net is the
name space and sp.net is the repository
name and 9.0 is the tag number. So this
is the URL of the image. Whenever you
need any image from any repository you
will just uh uh place this URL. You will
get this URL and you can uh you can use
that image. So this is the URL of the
image and um now we will discuss the
container and image life cycle like
process any runtime that need to create
container download the required image
from our registry. So for example if you
need a net runtime so you will use uh
that uh you will get this from registry
and you can use it. Uh some some uh
images required u credential as well
like there is two type of one is public
one and the private one. Private one
need the credential as well. image to
container. Once the image is downloaded
when you download the image the runtime
use it to create one or more container.
So then we can uh create container as
many as we want. So here you can see
here the u the instruction doer file uh
is nothing but instruction then we
create image from that and we save ACR
or docker hub. So we can uh use that and
then you can we can pull and we can use
and we can create containers. So uh here
we will see the docker what is docker
like u a defacto standard for working
with containers. So we have docker file
any runtime that need to create
container download the require image
from the registry it may need cred to
access the registry building images. Uh
docker images are generated by applying
change.
Okay. So now we'll discuss the form
instruction like when you create docker
file uh they have the form instruction u
like docker file usually start from a
form uh it specify the base you need to
modify for example uh form mcr
microsoft.com/net/sp.net
and uh 9.0. So this is the form
instruction you will see uh this uh when
we do a practical uh docker file u uh
creation and uh this uh set the build
stage or the starting environment. So,
so this is the like your computer pull
the image and uh it's built and they uh
after building they run the container
and you can see the application. So,
this is building we will see this uh in
pract practice as well. We will build
the image and uh registry URL the full
URL specify the registry domain we
already discussed the how the URL look
like they have the uh registry domain
name space repository and the tag we
already discussed this is the example
the uh you can use any type of registry
like docker hub registry and is uh is
your container registry. So this is a
view where you can store the image and
uh we have the images in a hierarchy um
like the images and container in the
hierarchy first like image one two and
uh you can number of images and uh each
num uh each image have n number of
containers. So this is some of the
hierarchy of the images and container.
Uh okay so uh key docker file
instruction. So uh when we see the
docker file we have working directory
instruction like we discussed the form
now we are discussing the working
directory uh so working directory define
the current working directory inside the
image file system if the direct does not
exist it's create so below is the
command instruction the full instruction
so uh yeah so next is the copy
instruction copy file or folder from
your computer into image file system can
copy a single file a whole uh folder and
multiple file as well here is command
instruction. So we will see this uh all
the command in our docker uh file. Uh
and next is the run execute a shell
command inside the image. For example uh
run uh net restore. So this is the type
of the uh run instruction. Next is the
cmd and entry point. Specify the command
and argument to be executed when a
container is run for the image. So below
is the cmd and entry point instruction.
And next is the expose. declare the
network port that container support it
act as a documentation. It does not
block traffic to other uh ports but uh
signal which port are intended for use.
So this is for port uh we can use the
expose and uh uh if next we'll discuss
the stages uh like um um docker have
docker file have sometime multi-stage
build as well. So so what is the concept
behind this multi-stage build? A docker
file can build intermediary image to
create the final uh smaller image. For
example, create an image that contain
the entire net SDK using form
instruction. This is the build stage for
example and then uh use run to compile
your entire net solution and create a
separate final image using a different
form instruction like uh just the net
runtime. So once your application is
built then you will run uh you will need
the runtime uh to run the application.
So uh this is multi-stage build. We'll
see this as well. Um and use copy from
build to copy only the compile binary
from the build image into the final
stage. So what is the benefit of this?
The final image is much smaller in size
because it does not include unnecessary
build tool or source code which is more
secure and efficient for production
level. So if you uh want to do a
production level setup so you you can
use the multiple stage build is highly
recommended.
So uh for example I will go back to the
my visual studio. Uh so you can see here
this is a multi-build stage. Uh this is
the the base it's create the base. So
this is stage number one the base stage
and this is the build stage. Uh this is
the build stage and this is the publish
and this is the final stage. So this is
uh like multi-stage docker file. So only
the only purpose is to u u get the
smaller image. So I will go back to the
slides. So
okay next is uh get in the docker file.
Uh if you are new to docker file you can
use the you can install the docker file
on your um Mac machine and window
machine. I already have um downloaded
for example uh what u docker file I
require for using docker on your
personal computer. It include like a
docker runtime to create and run the
container from images a docker client to
build images. We can build uh we can run
and uh we can also store the images in
local um uh local registry as well. And
u this course assume you are using the
Windows machine with WSL
windows subsignite for Linux install and
configure for the Linux container. So I
already install uh WSL uh so for Linux
so I will show you for example if I go
to the this is the docker desktop
application. uh you can see here
where you can find uh all the running
containers. So this is the uh one
container which is currently running.
When you click you can go to the log.
You can see the application is currently
running. You can inspect and uh this is
bind mouse execute all the execute uh
files and this is the file system of the
image and this is the states. So uh and
you can also see the images all the
images uh volume uh to get the
persistent state we can use the we will
see this in detail and all the builds
and this is the basic version if you are
you didn't you can install this docker
to run the uh application. So let's go
back to the slides.
Okay. Okay. So uh here we will create
one simple application de to demonstrate
how docker can build and run uh a
program without needing the language
runtime or SDK install on your host
machine. So this is the uh simple open
the visual studio. So I already created
one simple uh console application. So
there is nothing just uh one main
program and uh how you will add the
docker support to this project. So uh
here you have two option whenever you
create new project you will create new
project you can uh go with the name you
will uh give name then here you can see
uh that u framework and uh you can see
here enable container support when you
uh check this it will add the docker
support and here this is the container
OS so you can choose the Linux and the
windows so I will go with Linux and
here's the container build type so uh I
have docker file I will select the
docker file and you will create click on
the create. So when you create uh the
project will uh the visual studio will
be uh add the docker file in default. So
um but if you don't have uh docker file
uh in start you can also add it to
existing project as well. So you will
click on the u right click and you will
add click on the add and you will see
here u docker support. So when you click
on the docker support it will ask for
few options like I need I will go with
the Linux uh I will go with the docker
file not the net SDK and here is the
container image uh dro this is the
engine type so I will go with the
default one and uh this is the build
context this is the path where this is
root path of my project so I will create
the docker file in the same root level
so I will click on the okay
so you can see here one file is added
which is the docker file And here is the
detail of the toker. So it's a
multi-stage. I already uh discussed the
multi-stage. Why we need a multi-stage?
It's for production to get the image
smaller uh and production ready and
secure. And you can see here um first it
will uh get the net runtime for us and
the working is uh uh the current this is
the working the image image working
directory and this is the second build
stage. Uh it will get the net SDK 9.0.
zero and uh this is working direct for
this is uh CRS and it will copy from u
this is my current project uh path and
uh from my current project path it will
copy the file into a simple uh example
this it will create the new folder and
it will put here and next is the publish
part it will publish in final stage it
will uh run the uh delay net run you can
see here enterpoint net run so this is
the example if I uh run this you can see
here you can see this option uh dot uh
container docker file when I run it will
run the application currently. Uh you
can see here uh if I run this
application
so uh it's uh run.
You can see here when I open the
uh the container tab you can see here
the the application is running inside
the container. So this is how you can
add uh docker file to your u visual
studio project.
So uh you can see here uh this is the
same uh same code uh docker file I
created in the window studio. So now we
will discuss the different stage in
detail like uh this is the multi-stage
uh build. So the first stage is the best
the stage one is the best. You can see
here it's a runtime. Uh what is the
purpose? provide lightweight runtime
environment only the net runtime not the
SDK. So you can see here we separate the
net runtime from the SDK. So it's a
multi-build. So first is the base um and
uh image usage we are using the runtime
image contain just the end of that uh
for the net application and uh what is
the user app uid you can see here the
second instruction uh run the container
as a nonroot user security UID is
expected to be uh set externally. So you
can set this uh extendingly the UID uh
for the security purpose and the other
is the working the directory
instruction. Uh so we have set working
directory where the app will run. So it
will set the uh directory. So this is
the first the second is the build stage.
You can see here the build stage is u
compile application image usage. We are
using the SDK image uh that restore
build and publish. And uh this is the
steps uh like copy project from separate
optimized doer cache uh restore copy and
build command. And the third one is the
publish uh prepare the app for
deployment like net publish and uh uh
other uh instruction like use host uh a
host false. So you can see here if I go
to the publish uh the publish is using
the build config configuration to
release and they have some uh
instruction as well like uh use app host
equal to false and uh it will use the
net publish and the final stage uh the
final stage is the actual runtime uh
runtime container based on the base
stage runtime only not no SDK it will
only use the runtime copy publish file
from the publish stage into app and set
the setup command like net uh uh run net
simple uh simple example DL. You can see
the final one working directory app and
it will copy the file and it will uh run
the uh DL. So this is the multi-stage uh
build. I explain each uh stage in detail
and um next why we need multi-stage. If
you build everything in one image, the
final image could be uh include the SDK
and build file make it huge. The size
will be huge. So, which is not uh useful
for production. So, um uh this approach
uh like a multi-stage approach use the
SDK image only for building and use the
runtime image for production which is
lightweight, faster and safer. So, this
is the purpose. So, I will recommend to
use the multi-stage uh uh docker file.
Okay. So, next is the building how you
can build the docker file. So this is
the build command docker build uh / dot
slash uh then uh t sample app. So um
here is the explanation. The docker file
the command to build an image for from a
docker file uh and the dot forward slash
specify the the docker file is in the
current directory and and uh comma t
simple example take the new image to the
name. we specify the name uh and take
are used to name and version image. So
this is the uh type of u build command.
We'll write docker build dash forward
slash
um t uh and then you will give name. So
when you click uh enter it will start uh
building. So you can see here the image
is built. So this is how you can uh
build your image. So this is the build
command. And the next uh is all image
name must be in lower case. So you will
uh follow this rule like lower case. Uh
you can also uh run this command like
docker images that will give you all the
images list. For example, if I go here
and run the docker images. It will give
all the images. You can see here uh this
is the simple example is the latest
image I built. So uh this is about the
images list. You can also uh check uh
from the docker desktop UI as well.
Next.
Okay. Next is how you can run after
building you will run the docker image.
So this is the command docker run uh d
name. You will give name to your
container first container. Uh and this
is the image name uh simple example. So
you can see this create and run a new
container named first from the uh simple
image. Uh the program output this
program running in auto container will
be displayed. So this is the uh run
command. So how we will run like uh
docker
uh run d you will give name my first
container
and this will be the image name example
uh container. So next we will see a list
container. Uh there there is two command
like docker ps uh it will list my uh
running containers uh and u docker ps d
all list all the container including
those that have stopped. So let's uh see
these two command.
Okay. So uh first I will uh check the
docker
ps. So you can see here this is the
current running uh uh there is nothing.
So let's uh write the other command
a docker
docker ps uh d- all. So you can see here
one uh image is running one container is
running. This is the container id the
image name command and created date and
uh this is the other information. So
next we'll see uh restart and stop
container. You can also restart local
restart uh container name. This is the
restart command. And uh here is other um
removing and stopping container. You can
run docker stop my container. You can
run this command in powershell. And uh
the other one is remove a container,
remove an image from your local
registry. So this is some um other
command you can use in your uh this is
the text source take uh and uh target
image. This command attach a new tech to
an existing image. Take or native name
for an image. Taking is the first step
to prepare an image for the pushing to.
So this is when you want to push the
image to registry. So uh you can use
this uh by taking name.
Okay. So uh this is how you can like
push and pull uh images like u uh you
will need uh to know the registry like
docker. You must uh use the tag like
docker take my image you will push to
that repository. It will attach take to
that image when uh you visit the um
repository. Uh so you can see here uh
the tag. So you can also push to the
first you will take and then you can
push to the uh like Azure if you you
have Azure C ECR you can push to that.
So you can see first take and then push
pulling an image you can use this
command do pull uh regist uh URL u of
the uh image. So this is how you can
pull the image logging into registry. U
if you are using u Azure you can use uh
a login uh it will require username
if you uh su successfully login you can
uh use the login name you can uh mention
the registry name. So uh uh another
thing is that like visual if you are
using visual studio um you can uh use uh
visual studio provide the docker support
for for your application like I already
mentioned this if you are in the visual
studio and uh you if you click on the
solution add then you can see here
docker support. So uh this is how visual
studio provide you the facility to uh
add a docker file and if you are
creating new application you can alsoh
if add the docker support as well. Um
this is u new project. You will select
type of your project. Then u you will
give name to your project and uh in this
step they are asking enable docker
support. So this is this slide is about
this information real support for
docker. You see uh the important command
um practically. So this is my simple uh
console application uh that will run it
will show me the message uh service is
running at port 8080 and uh it will run
after 10 second delay. So let's see uh
the output in the locally. You can see
here uh it will uh check um it will run
after 10 second delay. So it's running
locally. So now we will run the same
application inside docker. So how we
will do this? Uh we already have our
docker file. Uh you can see here this is
our docker file. So how we will run? We
will open the powershell. So the first
command uh which is uh to build. So how
to build uh this image first? So first I
will u uh run the build command. Here
you can see docker
build t my my uh
net
app and this is will be uh from the
local directory. So I will add the dot.
So you can see here it will start
building.
So uh it's completed. So now I will go
to the uh docker uh desktop application.
So here is no container. Uh I will just
check the image. So you can see here the
image is uh created. But you can see
here the tag is latest. So um if you
want customized name so how you will
give the uh customize tag. So for this
uh you will use this command docker
builder dash e my net app and uh here
you will use the t for example v1-0 and
the current directory. So it will use
the customized uh tag. So now I will go
back to the images uh local uh images in
my uh docker desktop. You can see here
uh they use the customized tag names tag
name. So you can use uh this uh command
to give the customize name. So uh you
can see here uh image is created but
it's not uh running. So next we will run
the container. So before this uh there's
one uh also one other command. If you
want to check the progress during uh
building the image. So you can also use
this command docker build t then name my
net app and then you will use this
progress
progress equal to plane and current
directory. So it will show you the
progress beside building. So this is the
three way you can use. And now uh how we
will run uh how we will run the
container. If I go there is no running
container. So I have image but did not
run any container created any container.
So how I will do this? So
so uh I will uh use this command docker
uh run and uh the image name. So my
image name is my.NET
app. Okay. So it will run. So you can
see here the application is running
inside docker container. So for
verification I will go to the app. So
you can see here the the container is
running. So you can see here the
container is running but uh it's a
random uh with random name. So how we
will give the customized name. So for
this you will use I will stop container.
Whenever you stop the container it will
uh change the state. You can see here it
now stopped. So how you will give the
customized name? So uh you will use
docker run d sorry uh dash name and then
you will give the name to your container
by container
my container and this is my image name
my net app. So you can see uh when I run
it will run but it will use the
customized name. So you can see here the
name is changed from uh any random to
the uh custom name. So this is how you
can run the app. So uh next uh I will
add a tag and uh then I will push the
image to the GitHub repository. So uh
first of all I will give uh tag so to
the image.
So how you will give the tag? Uh because
whenever you push the image to the
docker hub you will need uh tag to uh
attach with uh the image. So you will
use this command docker tag and then um
my uh
net app my.net app and then you will use
the username of your um uh GitHub
account. For example, if I go to the uh
this is my GitHub u dockerhub docker hub
uh repo repos list. You can see here
this is the uh repo list and this is my
username. So I will use this username
with the tech. So I will use ngrdullah
Abdullah. So this will be the and then I
will give the repository name my
docker.net
app. Then I will use the like the tag
for example I will give latest.
You can see here the tag is added. Now
the next step is to push the image to
the uh docker. So first of all uh if you
are did not log to the uh docker so you
will use this command docker login. So
you can see here it will try to login to
my account. So you if you did not create
account you will create account in the
docker hub. Once you uh uh login then u
this terminal will be automatically uh
attached.
Okay. Now I will push my image to the uh
docker hub. So this will be the command
docker push
then my username
and uh then my repository my dot
net app and then the the tag name
L a t latest. So it is trying it is
preparing and it's pushing the image to
the docker hub.
It will take some time.
Okay. So, it's completed. Now, I will
verify. Uh so, I will just refresh the
page.
So, you can see here uh the repository
is created and the image is here in this
repository. So, this is the information.
The tag name is latest, image type and
uh other information related to the tag
and uh the image management. So, this is
the image here. So this is how you can
create u and uh run and push image to
the docker hub. I hope you will u like
this video. Um thanks. See you in the
next video. Hello everyone. Welcome back
to practical serverless and
microservices with car. My name is
Mohamad Abdullah and I am Microsoft
certified Azure solution architect. So
for this course this is our agenda to
understand the serverless microservices
on your architecture docker different
type of Azure function background
function IoT function and uh
microservices practically and then
kubernetes how to work with containers
and Azure container app then we will
check the security observability for
serverless and microservices application
for the whole course we will focus on
the car sharing microservices project
and uh lastly we will uh see the
microservices with uh net aspire so this
is our agenda for this course if If you
missed the previous sess sessions, so
please check out the previous sessions.
So in this session session number six,
Azure function, we'll discuss the Azure
function and uh different trigger of
Azure function. So um before I start the
session this course is moving from the
theory to serverless and microserless to
a practical implementation of Azure
function. So uh in this course our total
focus is to beside the theory we will
totally focus on the practical work. So
to explore and implement different
triggers available in this uh Azure
function. This is our agenda for this
session. We will explore the triggers
different triggers like HTTP trigger,
SQL trigger, Casmas trigger and Azure
bus trigger. So we will see this uh
practically uh with the help of Visual
Studio and Azure portal. So the first
one is the Azure trigger. So this is the
uh what is the basic of Azure HTTP
trigger. So this is the most common
trigger in Azure function. It allow a
function to execute in response of HTTP
request. So the use cases for this uh
trigger is uh building APIs, creating
web hooks, developing integration. So
the core principle a function method is
triggered as soon as HTTP request is
received. When whenever the function
receive any HTTP uh request, it uh
respond accordingly. So this is the
simplest version of HTTP triggers. Uh so
this is the what is the advantages is
it's easy to use and it also support the
get, put and uh delete. um HTTP methods
and it's also a microservices friendly
and integration uh we can easily
integrate uh this with other is your
services and third party library and
it's also a scalable and cost effective.
So the disadvantages it has the cold
start latency uh like there will be a
delay on the first request whenever you
hit the first request there will be some
delay um and this is the the drawback of
this HTTP request and the other one is
the stateless nature this HTTP uh
trigger has a stateless like for example
if you have um um stateful long running
process uh that need the state to be
saved for this uh this is not suitable
HTTP HTTP trigger so instead of HTTP
trigger we use the Azure durable uh
functions. So we will discuss Azure
deploy function uh in the upcoming uh
lessons and uh it's a resource limited
function have limited on execution time
and memory exceeded these often point to
a design issue. So this is some uh
drawbacks of HTTP triggers and u when
you design HTTP trigger uh there is some
uh authorization level like HTTP trigger
allow for different level of security
using authorization key like we have
anonymous that need no access key and we
have functional function specific is
required and the the other one is the
admin that need a master key. For
example, if you go to Azure portal and
uh uh open the app um function uh app
keys you will see the master and the
default key. So you will need this key
to be uh work to work with HTTP HTTP
triggers. So um few question like when
to use uh HTTP trigger. So this is very
uh common question like u when you will
use the HTTP trigger. Use HTTP trigger
for lightweight stateless uh function
that respond directly on the web
request. For example, if you have a rest
API, you can use the HTP trigger web
hook uh handle real-time notification
integration uh like driving a connection
between the different services and you
can also use in case of prototyping. Uh
so this is some use cases. Now we will
um go back to the actual uh code of uh
the HTTP. So we will focus on one
scenario like as I share this in start
that we will use the car sharing
microservices in this project. So this
is the part of uh uh the HTTP trigger a
car sharing application with a car
holding API that use a Azure function.
So we will see this uh in action. Uh
this is the uh one solution and it has
different uh trigger. So the first
trigger is the HTTP trigger. So I
created this Azure function based on
HTTP trigger. Uh so you can see here uh
this trigger uh this um app has uh
different type of function like uh
create, read and uh update. Then we have
delete. So this is for crude operations
crude functions. So this is for uh
swagger open ai setting and this is
important like the http trigger. Uh this
is the azure function based on http
trigger and this is theure I already
discussed like http trigger has three
type of authorization like admon
function and anonymous. So this is
theure function level and uh it's about
post and this is the root of this
application. Okay. So uh you it receive
the HTTP request and it process the HTTP
request based on all requirements. So
currently uh it's there is no
implementation actual implementation
just uh we are logging some information.
So this is the create similarly we have
read update and uh delete with same
authorization level and uh each have
their type delete
uh then read and create. So uh to run
this we have um some uh information uh
like uh we have configuration for this.
This is the configuration um open API
configuration file and we have a app
setting um app setting.json file and uh
we have a services dependency.json JSON
and uh apart from that there is no other
uh and uh similarly we we can also have
this uh we can also configure the
connected services to uh this
configuration uh this uh isue function
as well but there is no currently there
is no such thing. So just uh to show you
how HTTP trigger uh works. So uh I will
run this in front of you. So um I I
select car holding as a startup project.
So it will run.
So you can see here uh it's now running
and you can see here we have create,
delete, read uh operations and this is
the link of the swagger. So this is the
swagger UI and uh uh we have different
post uh get put and delete method. For
example, if I want to uh test this read
one, it's simply returning the uh
information. So this is about how
practically look like the HTTP trigger.
Okay. So okay so uh implementation I
already shared the implementation with
you. The simple code provide four HTTP
trigger function to handle standard code
operations and uh you can also I will
share the source code uh with this
course as well. So you can check out the
code as well. So the next trigger is
Azure SQL trigger. So the best of the
basic of Azure SQL trigger is a function
trigger that is invoked as soon as any
change happen in the SQL database. For
example, insert, update, delete. So the
key requirement this trigger uh requires
SQL server change tracking to be enabled
on the database and specifically on the
table side. So I will show you this uh
so you will on the tracking for the
database and uh table uh side to u get
advantage of the SQL uh trigger. So uh
the purpose uh enable realtime data
processing and uh integration based on
database changes whenever uh anything
happens in the database any table uh so
you can um uh you can trigger this um
this as your function. So uh it used for
real-time processing, scalability and
cost uh efficient provide the same
benefit as the Azure function by scaling
only when needed and uh it being cost
effective and integration great for
integrating differentation based on the
database events. So it advantage is uh a
little bit complex uh in setup and
latency you must consider potential
latency issue and uh design you need to
decide if this trigger is a better
solution than a simpler uh option like a
timer trigger. So uh this is um about
your requirement. So you you will decide
based on your requirement uh it's
suitable um SQL trigger is suitable or
timer trigger is suitable for you and um
example audit changes. So I now I will
show you uh the SQL trigger based um
example in Visual Studio. But for this
you will need to create uh one SQL
server database. uh for example in my
case I will go with car sharing DB and
you will create one table car holding in
my case and you will enable I will also
give you a script uh to enable uh uh
tracking and uh you will also allow IP
if you have a SQL server uh so you will
also allow the IP as well or uh Azure
portal and uh I created one specific uh
resource for this for this course uh so
you can see here I already created one
uh SQL server uh SQL server and the
other one is the database. So these two
thing is needed. First you will create a
SQL server and um uh how you will create
uh I will show you like for example you
will go to uh to the homepage you will
search SQL server and uh you will click
on the create and you will uh you will
uh select the subscription then uh
resource group you are using and you
will give any name to the server and you
will careful while while selecting the
package like um uh for example I will
select with U
SQL uh server authentication will give
password and uh confirm password and uh
you will go to the network security you
will keep it default and uh you can see
here uh I will go with the I will not
use Microsoft defender for now and you
will give the name and uh you will
create so this is how you can create
after creating a SQL server uh you will
add the database uh SQL database so you
will go to the database section you will
create the database and uh here you will
select the database name you will select
Select the server you create it and uh
uh and you will select the if you are
using SQL elastic pool so I will
currently I'm not using so uh click no
and uh it's for development purpose and
you will select the local redundance
just for uh p uh this is just for the
learning purpose so I will go with local
redundant and this is the storage and
compute so I will go you will select the
basic version just for the development
purpose so you will careful here uh
while selecting the workload for your
application. So once you are create uh
you create the SQL server, you can open
this in the uh SQL management SQL server
management. So you can see here uh I
already created and I I'm connected with
the uh this server. So how you will get
the connection string. So um you uh you
will go to the your resource group and
uh this is our server and uh you can see
here
um you will go to the this is the name
of the server and now I will go to the
database. So this is my database
and uh this uh this is the connection.
So you can get connection string from
from this part. So uh this is done uh
now you will connect uh with the server
and uh you will change the database. So
this is our database uh share car share
DB. So uh the I'm what I'm doing uh with
this script uh I'm just set change
tracking on. So you will need this to be
on for this database and you will also
create a table. Okay. So this is two
thing you will uh enable tracking on the
table side as well as on the DB side. So
you will need this script to be run. So
uh once you run this uh script the uh
tracking will be enabled for this. Now I
will go back to the visual studio. Now I
will run the other part like for example
uh this is the this is this uh isue
function is based on uh like SQL
triggers. You can see here this is your
function and it's based on the SQL
trigger and it's accepting this is the
car holder. Uh this is the table name
and this is the connection string and it
will uh it will accept the list of uh
list of the read only SQL changes. So
this is the the changes list and you can
process the changes uh according to your
uh requirements. Uh and for this you
will add to add this local setting.json
and uh you will you will need this
property to be added websites uh website
name which is the audit app and apart
from that if you go to the connected
connected services you can see here you
will connect the SQL database here. So
you can see here um uh you can connect I
already created the car shar DB. So you
can also edit for example uh you can
select your uh database from uh your
subscription as well. For example this
is our database I created on the Azure
portal. So you will select this and you
will go next and then it will be
connected to your u uh visual studio and
uh this uh Azure function Azure service
app and you can also manage the secret
uh in this secret.json file as well. So
if I go to the manage se session, this
is the connection screen I will get from
the uh portal. I already show this. You
can see this database and uh you will
get this.
Okay. So this is about the SQL u trigger
as your function and uh I will go back
to the audit. So you can see here um I
will keep I will run this application.
Uh let's go select as a startup project.
So now I will run this application.
Okay. So, uh now it's running. Uh the
application is running. Now I will add
one data. Uh for example, I will insert
one record to the table. For example,
ID4 and this is the Abdullah. So I will
add one record. As soon as I uh add the
record,
I will select the the database to run
the query.
Okay. So, record is inserted and you can
see here uh it's uh triggered and uh I
will run and you can see here uh this is
uh the record is added the new record is
added. So you can see here this is how
the isure function you can see here the
this is the SQL changes uh operation
zero item this item is added. So this is
how the SQL trigger Azure function work.
I give the full whole changes how you
will do the all changes required for
this uh function.
Okay next next we will see the Cosmos DB
trigger. So it's a powerful feature that
allow serverless function to be executed
in uh response of uh to any change in uh
your cosmos DB changes. Whenever you add
any data to the cosmos DB you can
trigger this as your function and the
trigger is invoke automatically when
item is added updated or deleted in DB
uh collection and uh it's also enable
realtime data processing and integration
similar to a SQL trigger and uh the it's
data flexibility as well. Cosmos DB
offer more flexible for handling
nonstructured data. So uh SQL uh trigger
is used for the relational data and this
one is nonstructured data make it ideal
for type of data that would be difficult
to handle in a relational database such
as C elementary telemetry uh data. So uh
this is some advantages uh realtime
processing uh it data flexibility uh so
it's like for the nonstructured data we
can use this so it will be a flexible
option and uh the one of the
disadvantages is that it's a very
expensive so it's crucial to carefully
consider the cost of the solution when
you are developing so uh this is example
car telemetry so there is one scenario a
car sharing app use coosmos DB for high
performance globally distributed storage
of realtime car telemetry data such such
as uh user activity log location. So for
this um for development and testing you
we can create a new cosmos tree trigger
in visual studio and uh we we can also
set up the local setting as well for
debugging and testing like we can use
the Cosmos DB uh emulator as well. This
is the example of the Cosmos DB trigger.
So this is the code. Uh there is one
function uh Cosmos trigger uh DB trigger
and this is the database name uh
container name uh DB connection and uh
the uh lease container uh name. It's
also used for a container used to manage
lees for processing uh changes. So this
is uh the simple uh trigger customdb
trigger. So first you have like a few
options. So you can use the uh emulator.
So you will install the emulator from
for example if you go to this uh link u
microsoft.com you can see here what is
the Azure Cosmos DB emulator. So you can
download when you go down you can
download the emulator as uh from this
page. So uh I already installed another
option is you can also use the docker
option as well. So you can install you
can get the image of this uh emulator DB
emulator and for both Linux and window.
So it will provide you image as well.
And uh if you go to with the emulator
desktop application. So you can see here
I already installed the Azure Cosmos DB
emulator. So uh when you launch the
emulator you will see uh something like
that. So you can see here it will run on
this local host 8081 and uh it's the
explorer and this is the index page. So
uh to save this cost I will suggest uh
use this uh emulator and um it will
perfectly work as like a real uh as your
Cosmos DB. So uh first of all uh
according to the code this is the code.
So first uh uh I will need uh some
connection string. So you will need the
Cosmos DB connection string. So from
where you will get this connection
string once you run the emulator uh when
you go to the quick quick start page you
will see this primary connection string.
So you will copy and you will paste this
in your uh code. So this will be the
connection string and the second thing
uh is the uh is the code actually the
code. So this is the the database name.
Uh so uh you will create one database
and one container with the same name you
mentioned in the in your function. So u
when you go to the explorer you will
create you will click on the container
and you will give the name. uh for
example in my case I have uh this this
is will be the database and uh the
container id will be this is the
container ID
and uh this is the partition key so uh
the partition key will be like u uh like
the car/ ID in our case so uh I already
created you will click on the okay and
uh once you create it you will see this
item in the list so I'm container and
this is the all the list we have in the
uh in in the this uh uh item list. So
this is the item list. Okay. So this is
the program and uh now I will run this
application. You will make this as a
startup project. And uh now I will run
and this container uh it will uh if this
is exist it will not create it. If this
is not there it will uh uh create the
list. For example, this you can see here
this is the extra container uh created
if this is not there and it will also
accept the input um like what changes uh
made in the in the cosmos DB it will
accept all as a input and this is the
object like we have car ID we will
receive the car id and we will print out
the car id and this is the modification
count input count so you will bind this
uh read only list with this uh class so
now I will run.
So now the function is uh now running.
Now it will track all the changes
happening in the emulator. So uh now I
will go to the emulator and I will add
one other item but for the before this
uh I this is our data sample data and
this is for example car number five.
Okay. So this is the same uh we will
keep this date and data same as it is.
So uh I will save. Now I will go back to
the emulator. Now you will click on the
item uh new item.
So this is the item list. Uh now I will
go to the item upload. Now I will select
the data file. I already have a data
file and uh I select it. Now I will
upload. So it's uploaded. Now I will
check the log.
So you can see it's uh the trigger is uh
now hitting and uh uh if I go one you
can see here the input count is one and
it's getting one item. Okay, you can see
here this is the same data we are
passing from the uh file.
So now you can see here this is the data
changed in the uh one item is uh first
item car. You can see here this is the
same car id passing from the file. So
this is how the cosmos DB work
and there is one uh important
distinction uh like uh a critical
difference from the SQL trigger is that
the Cosmos DB trigger only monitor
insert and update not the other case
like the delete no deletion case. Uh
this trigger does not monitor deletion
event. If you need to track deletion you
will need to implement alternative
solution. So this is one of the uh
important distinction. So now we'll
discuss the Azure service uh bus
trigger. Uh a serverless function is
triggered by messages in Azure service
bus. A powerful message broker. It's a
message broker for microservices
communication. So we will see this in
detail. So um Azure service bus have two
model. One is quiz and other one is the
topic. Uh the quiz is one to one
relationship where a single receiver
process each request. They ensure
reliable order delivery and are great
for decoupling application and buffering
messages. So we will see this uh in
practically and the other one is the
topics uh which work on the published
subscriber pattern. Uh it handle one to
many relationship where a single message
can be delivered by multiple subscriber.
They are used for complex workflow where
different services need to be react to
uh same event and the topic can also be
filtered to route specific messages to
for the specific uh subscriber. So uh if
I go to the u portal you can see here
this is our uh resource and uh I created
this resource for this course and here
you can see here I have one is service
bus name space uh called car share
service bus when I open uh this uh
service bus you can see here this we
have entity we have quiz and topic so
this is the two model I'm talking in
this slide so we have two one to one
relationship and the other one is work
on the one to many publisher subscriber
uh patterns
And uh this is the next slide is is your
service bus trigger benefit. It's a
reliable, secure and uh uh we can uh do
integration easily. Uh it's very
flexible and uh uh the about the cost Q
are generally cheaper uh than the topic.
Uh so we will see this as well. The
topic uh if you want to use the qu only
so you can use the basic with basic
subscription as well. But if you want to
do both docu and the topic you you will
need extended and premium package. Uh
you cannot do a topic with the free
version. So choose the model that fit
for your need uh to manage the cost. And
uh next is the uh apart from the Azure
service we can use the other option as
well like Kafka and Rabbit MQ. If you
are working uh inside the Azure
environment, so the Azure server bus is
a great choice. And uh if you have like
uh massive amount of data like data
streams, so you can use the Kafka and uh
you can also use the rabbit MQ and it's
uh easy to use and highly flexible and
uh it's a lightweight uh and uh uh it
make very versatile options as well. So
this is some other option you can
consider while uh working with the
messages bus message broker. And uh in
our uh code example we have one scenario
when a user search for a car a message
is sent to a service bus topic named car
seeker request like the purpose of the
topic a topic used because multiple
microservices like the root planner the
notification service need to react to uh
the same car uh the same event. So u for
this we have we will create the
dedicated Azure function uh to monitor.
So uh if I go to the code u visual
studio uh there is one uh as your
function uh route planner and uh we have
one u carse seeking u class and we have
one uh function and you can see here
that this is a SQL based trigger and uh
this is the the topic this is the topic
and uh the other one is the subscription
name. you will see this topic and
subscription name uh from where you you
can get this and uh this is the messages
uh we you can receive the messages for
example I'm receiving this message and
I'm getting the message and id message
body and me message content and the
other one is the service bus message
action whenever um it mark the message
uh like when uh it successfully process
it remove from the queue so this is
needed uh to complete the message
and uh so uh you will go to the uh you
will go to the Azure portal. So uh this
is our resource and uh here I created
one you will create one Azure uh bus and
you will create once you create uh you
will have these two uh model. So for
this example I will use the topic
because uh the subscriber will be
multiple in our case. So I will use
this. So the first thing uh you will
create one topic uh so you will give
name to the topic. So uh for in our case
we have a car seeking request according
to our code. Uh you can see here this is
the topic name. So I will use the same
topic name in uh the portal as well.
While creating the topic you will just
give name and hit the create. So you
will see this uh in the list. Once you
have this in the list you will click and
uh you will create one subscriber. You
can see here on the left side I have one
entity subscription and uh you can see
here I have only a uh route
subscription. And uh if I go to the code
I have a route and uh you will uh create
subscription you will give name and you
will see uh you will just give name and
create.
So but I already have created and the
subscription so I will not create it
again. So uh now u you uh the other
thing you will use uh the explorer to
send a message. Uh for example uh this
is the this is our function here is the
local app setting. uh this is the uh
connection string of the car share bus.
So uh you can get this from the from the
uh you will go to the when you create
the the bus you will get this
connection.
So you can see here in the left side I
have setting under setting we have share
uh access policy and this is the default
route uh and when you click you will get
the uh the the connection string. So you
will get this primary connection string
uh this is needed and you will put it in
this uh uh in this setting. So once you
get uh you can now run the application.
Uh now I will run the root planner.
So now uh it's uh run uh carseker is
run. Now I will send one message from
the portal. You can use other uh tool as
well to send a message like you can
create the C uh uh console application.
And for now I will use the same u
uh portal.
So now I will go to the topic and uh I
will click on the topic.
Now you can see on the left side you can
see the service bus explorer. Now I will
click the explorer
and now you can see here have the send
message option here. So I will click and
uh here I will write some message
service bus explore
error. So now I will send
sending completed. Now you can see here
it's automatically triggered and uh we
can get all the information inside the
message. So this is the message ID, the
message body, hello from the service bus
explorer and uh this is all about the
how you can receive the information.
So uh this is all about you can see here
this is all the information the message
ID body and the this is the and this is
the end of this uh session. Uh I have
one question for you uh what are the
benefit and concern assoc associated
with uh using Cosmos DB trigger in the
Azure function. So this is your
assignment. So I see you in the next
video. Thank you. Welcome back to
practical serverless and microservices
with C#. My name is Mouhammad Abdullah
and I am Microsoft certified Azure
solution architect. Today in session
number seven we will discuss uh
background function in Azure. So uh what
is uh background function in Azure? We
are facing one problem and the problem
is cloud computing solution in cloud
computing solution especially pass uh
often require background task to run
independent of user request. So to fix
this issue we use serverless computing
from Azure function that provide us uh
function that run bin background. So we
have u different type of background
task. For example we have timer trigger,
blob storage trigger and use storage
trigger. In this session we will uh
discuss these three background task. So
the first one is the timer trigger. So
why we need uh the timer trigger? The
timer trigger help us execute a function
at regular schedule interval. Uh for
what is scheduling? The scheduling is
defined by um chronic tab expression
which is a time based format similar to
chron expression. You can see the
example below. We have seconds, hours,
days, months and day of week. So we will
discuss this in detail. Here is the
table that define the uh chronic t
expression. So you can say we have
second, minutes, hours, days and month.
And for example, if you want to run any
task uh every second, so you will use
the star. So there is six star. So six
star mean every second and if you want
to run any job every minute so you will
uh you will add zero at start and then
five uh star. So this is the um this job
will run every minute. You can see uh
the other type as well like if you want
to run uh every five second if you have
run any uh any job on any specific time.
So this is very flexible. So uh it will
you will create expression based on your
requirement. We have one um u web
application that uh create or generate
us the chronic expression for you. So
you can visit this website to create
chronic expression. So uh when to use uh
timer trigger? So u if you want to take
a backup routine maintenance periodic
data processing or generate schedule
report. So in this scenario we can use
the timer uh trigger. So uh there is few
advantages and advantages as well. So
we'll discuss this um here this as well
very shortly like we have it's a simple
form of is your function uh when you
want to do a big round task this is the
simplest one and execute function at
regular intervals. So this is a
flexibility and the disadvantages is if
no work is needed at the schedule time
the function will run still run leading
to unnecessary cost. So this is one of
the disadvantage of uh timer uh trigger.
So uh how to create uh timer uh timer
trigger inside visual studio. I will
show you uh step by step.
So here you can see um you will go to
the uh new project you will create new
project and uh inside this uh template
window you will see u Azure function. If
you don't see the uh template in the
recent section, you can search Azure
function in the search bar and you can
see here this is the Azure function. So
you will give name uh
so this is the name of the Azure
function. Uh you can see here you can
select uh solution solution name
location then click select. In the next
video u in the window you will see the
function framework you will select any
framework based on your requirement. I
will go with net 9 and here is the
function type. So uh you can see here
you will select the timer trigger and
here you will use this expression. So
you will uh define the expression based
on your requirement. After uh creating
expression then you will create uh you
will click on the uh create button. So
once you create you will see something
like this. For example I already have a
function so I will not create it again.
So you can see here uh this is the class
a simple function. This is the
constructor and this is the function the
timer uh timer trigger function. You can
see here this is the expression and this
expression show the trigger will run
every 5 seconds. After every 5 seconds
this logic will be executed and uh
inside this uh function I'm just logging
the information. So this is how you can
define the timer trigger. So if you run
this uh timer trigger function you will
see something like this. For example,
you can see the function is run in run
mode. And you can see here this is the
information. Uh it will run every 5
seconds. So you can see here it will run
every 5 second. You can see here the
information is again printed. You can
see here this is again printed. So this
is a schedule. It will run in background
and the based on the expression.
Okay. Next we'll discuss how you can
monitor your uh as you once you define
the Azure function how you will monitor.
So Azure portal provide you different
type of monitoring tab like you can use
the Azure monitoring you can monitor
your function in the Azure portal uh in
to ensure that it's running properly
there is one tab uh this step provide a
quick overview and basic detail on the
number of time your function has been
executed uh number of it will show you
number of time and some other
information and Azure uh monitoring log
for detail information you can use Azure
monitoring log so for example here I
attach one screenshot this is the Azure
monitoring log where you can run your
query based on your requirement and it
will show you the result against that
query. And here is one inocation tab.
When you open the any of your function
and you will go to the inocation tab,
you will see uh the count number of uh
execution. You can see here success
count is five and error count is zero.
So this is uh the inocation window and
this is the monitoring application
inside performance. This is the
performance view where you can check the
performance of your is your function. So
uh it provide like a different uh two
views. One is performance view help
analyze performance issues and error and
other one is the uh live metric view
provide a real-time view of your
function performance of for debugging.
If you want to debug you can this is
very useful information you can use. So
uh you uh so uh
so uh in our car um sharing scenario we
can also use uh timer trigger. For
example, a cash sharing service need to
process billing once an hour but not on
Sunday. So to uh to cover this um
requirement, we can use the timer
trigger and timer trigger the best
solution in this case to automate this
process. So here is the example how we
will get this. For example, you will
decide do you will create one as your
function of time timer time trigger and
here is the expression uh that will be
uh run uh once uh in an hour but not on
Sunday. So this is expression used for
uh to cover this uh requirements. Uh
here is the detail of this expression
like uh in start we have zero at the
start of the second then we have at the
zero at the start of the minute and this
is the time between uh 8 a.m. to 5 uh 59
p.m. and um the the star is represent
every day of the month and the second
star is representing every month and 1 -
6 um it will represent from Monday to
Saturday. Now we'll see blob storage uh
when to use a blob storage and what is
the advantages disadvantages. So first
the basic of the blob storage is that
it's a zure function that is executed uh
in response of uh any change in u blob
storage. If you upload if you update or
if you uh delete any item from the blob
storage uh this trigger will be hit. So
um what is your blob storage? It's a
highly scalable and cost effective
services for storing unstructured data
like images, logs, backup file. Uh uh
and the blob stand for binary large
object. So uh it um scalable um it
handle large amount of data. We can
access via HTTPS and HTTP and we can
also do a seamless integration with
Azure function uh to automate any
process. uh if you uh using local
development then you can use the Azure
uh emulator and you can also use Azure
storage explorer for local development.
So uh when to use uh if you want to do
image processing we can use the blob
storage uh blob trigger and uh for data
analysis we can use and real time and
batch processing we can we we use the
blob storage and here is some advantages
disadvantages uh advantages of data
handle uh a large amount of data handled
by the blobs uh trigger and it's
scalable and disadvantages it's like uh
the pricing margin is based on execution
count and data processes. So you will be
careful while uh selecting the the
pricing plan and uh there is a latency
problem as well depending on the app
service plan. There can be a delay
between a file being uploaded and the
function being triggered. So this is
some uh dis advantages. Uh so now uh I
will go to the Azure uh so here you will
create um Azure storage. Uh for example
uh I created uh this Azure storage for
this course. If you you didn't have to
create the storage uh yet. So you can
create by uh clicking this create
button. Uh once you create u you will
fill the form you will uh you will
select the resource group and uh for
example I created this resource group to
keep all my resources in a single group.
So uh it's very useful. You will give
name uh then um you will select the blob
and z data. So I will go with this. If
you want only file so you can select the
other option as well. Uh you can select
the standard instead of premium. uh and
this is uh you will select the local
retented storage. After that you will
create uh and review. So this is some uh
thing about how you will create the
storage. Once you create the storage you
will see the storage uh in your list and
u here you will see some information
like uh you can see uh the data storage.
So this is important. Here you will see
the container file share cues and table.
So uh after this the important thing is
the access key. So here you will find
the connection string to this uh uh
storage. So you can use the key1 and
key2 it's up to you. And after that uh
you will create one container one
container. So for example uh in our car
sharing application whenever any uh
driver upload their driving lens license
after uploading uh we will trigger the
Azure Q uh trigger and we will process
the uh the document like we will extract
the information. And then when we will
analyze we will do hashing. After
hashing we will delete the uh document.
So for this purpose the Q trigger is
suitable. So that's why we will use the
Q trigger in our example. So I created
one container for this purpose. Uh
driver license license upload. Whenever
uh any user upload any document the u
the Q trigger will be hit. So this is uh
uh about how you will create the
container as your storage. Now uh you
will create one application. Uh you will
click on the visual studio.
You can see here I will create new
project. Uh you will search is your
function. Here is the function and then
you will give name to your u uh
application. For example, driver
process you will select the location. I
will select in
in our location.
So I will select the location. This is
the name and it it will solution name
will be the same. I will click next.
After that you will select the
framework. I will go with net add. And
here you will select uh the trigger that
you want. I will go with the the Q
trigger
sorry the I will I will select the blob
I will I will select the blob trigger.
Uh after that uh here you can see u uh
you can select the use is unity for
runtime storage. Uh so uh you can give
name uh to uh connection string uh that
you want and u uh you can also select
the the path uh the the storage the
container path. So uh here um what I
will do uh I will uh I will uh I will
add the connection name. For example, I
will uh use this connection.
So this will be the connection name and
uh this will be the uh path name. Uh so
so the there are two uh things this is
the container name uh as I created in
the you can say this is our container
name and the other one is the
placeholder. So we will receive uh the
file to process. Uh after that I will
select um I will select uh next.
So uh you will select u one option out
of three. Uh so I'm using the actual
storage. So I will select the storage
account. If you are doing local
development, you can use the other
option as well. The Azure uncontainer as
emulator. So I am doing the actual
development. So I will select this. Uh
then you will select the storage uh
account. It will um you will need to log
into your Azure portal. Uh and uh here
is all the storage account. So I already
created one storage account. So this is
my storage account for this course. So
and I will create next. And then um then
you can see here uh this is our uh this
is our connection string name and this
is the endpoint name. Uh so uh now I
will go next and uh it will set up the
secret for us and the other uh packages
and once you finish it will create the
uh is your function. Now we'll see uh qu
storage uh trigger a function that is
executed in response to message placed
in Azure Q storage when any new message
is uh added to the queue uh it will
trigger. So this is the basic of Q
storage trigger and it work on the FIFO
best principle u first in uh last out uh
and it ensure messages are processed in
the order they are received. So uh it's
used to make the system uh decouple and
uh here's some benefit like we can use
this for uh asynchros uh processing like
uh it's enable decoupling synchronous
communication between uh two different
or more services and u it can handle
large amount of messages uh with
automatic scaling and u it also reliable
and fall tolerance uh like it guarantee
each message will be properly processed.
So this is some benefit of using Q
storage uh trigger and u uh using a Q
storage trigger allow developer to focus
on the core business logic as the
underlying infrastructure is handled by
the cloud provider such as Azure. So you
will only focus on the core business
logic. So this is the uh benefit uh of
using Q storage trigger and advantages
uh efficient messages handling and
decouple architecture I already covered
in the previous slide and some
advantages you should be careful while
uh selecting the plan. The pricing model
is based on the number of execution and
data process which lead to unexpected
cost and other um is error handling.
developer must implement mechanisms such
as retry and error handling to manage uh
high lead load or transient failure. So
you should be careful while uh
implementing mechanism u like retry and
error handling. So u when to use a Q
trigger? Uh best scenario a storage a Q
storage trigger is reliable and
efficient uh choice for the processing a
Q uh task. Whenever you want or need a Q
task in your application, then this is
the best um tool to use uh such as order
processing uh in e-commerce we have
order processing in that case scenario
we can use the Q trigger and uh if you
want to do a background job scheduling
so uh it's u best choice and uh if you
want uh eventing notifications so uh Q's
uh trigger will help you in this and uh
for a practical uh we will see one
example Example uh in our car sharing uh
example for example in in case of best
match scenario microservices uh there is
one scenario that is the car sharing
application has a best match
microservices they need to be triggered
whenever a route manage occur. Whenever
any route manage is occur then we will
use this u my best matches uh solution
for this uh route messages are sent as a
JSON message to Azure Q storage and
named new message. So this will be the
the Q name of the Q the new message uh
will be the Q a Q storage trigger
function is used to process these
messages. So uh to to do this example uh
I will show you uh step by step how you
will do this. So let's go to the visual
studio.
So uh first of all u for a local
development for a local development you
will use uh the you will install the
this one tool Azure storage explorer. uh
we will this use this uh storage um for
emulator purpose. Okay. And uh you will
also need one security as well. For
example uh if you go to uh see your
control command prompt here you can see
uh this is the command prompt and you
will use the azure. We will use azure
plus the solution uh the storage
explorer. If you don't have any um azure
installed so you can use this command to
install the azure. So I already have
Azure so I will not run the command
again. So u to run the azure uh you will
use this command azure silent mode uh
location this is the location of the
azure and when once you run you will see
uh this messages like for the blob uh
when anything happen in the blog it will
listen at this port when anything addict
or updated in the queue it will listen
to this one and if anything uh happen in
the tables related um um
so you can use this so uh this is all
the three uh blob up Q and table uh it
will listen on the respective U ports.
So this is the first step you will
install and you will run this command.
Once you install then you will open the
explorer. Once you open the explorer you
will see something like this. U if you
uh are new to this uh uh storage
explorer how you will connect? How you
will connect? You will click on this uh
switch button and here is few options.
So you can see here u I will select
local storage emulator. So I am not
using the actual Azure services now
because this is uh in development stage.
So to save the cost I will use the local
storage emulator. So you will uh click
on this and here you will give name u
account name account key all the
information then you will click next.
After uh uh this review you will review
and you will click connect. So it will
connect. Once you connect you will see
uh the this option emulator attached. So
I already attach connect the emulator.
So I already have uh and inside this
emulator attach you will see the account
storage. So we have different accounts.
So I will use this emulator account. So
you can see here we have options for
blob storage, Q storage and table. So
this is the the three options we have uh
in the uh Azure storage explorer. So
this is something about Azure and uh
Azure uh storage explorer. You will need
this in application. Now it Azure is
running and this is connected to the
Azure storage. So next I will uh go to
visual studio and uh I will create one u
application Azure functions. So I will
create new and then you will search for
Azure uh Azure functions. You will
select next and you will give name to
your uh function. So based on our
scenario I will give uh respective name
my best matches and here is the location
of your code and the solution name and
click next.
Okay. So uh now you will select the u
the framework you want. I will go with
net uh 9. And here is the Q trigger. So
we will uh see how Q trigger work in
reality. So you will select the Q
trigger. And here is some option uh as I
already mentioned that we will use the
emulator. So you will you must check
this check box uh for to use the isurity
and uh for runtime storage account. So I
will keep this uh selected and uh this
is not needed. Uh so I will not use the
container support. Here is the
connection string name. So you can give
any connection string name. So in my
case I will give the car storage uh car
sharing storage as a connection string
name. The second thing is the Q name. So
Q name is important. Uh you will uh I
will give name like uh new mage. So this
is needed. So I will select the new mage
and here is the configure. You will
select the dependency. Uh I will explain
you. So you can see here uh in the in
the in the explorer I will create the
same uh queue with the same name I give
to this Q name. So next so uh in the
next screen you will select the connect
the dependency. So I will use the
asunity emulator. I already explained in
the start how you will connect the isure
emulator as emulator. So I will select
the the the second option. If you want
the actual uh Azure storage so you will
select the third option. Okay. So now I
will use the um Azure emulator locally
uh not on the if you have a
containerized uh uh emulator. So you can
use the first option. So I will use the
second option. So next uh next you will
use the connection string and uh this
will be the connection string. So uh
remember that uh if you are working with
the emulator uh the local development so
this will be the connection string for
your u application. Okay. So uh this
will be the name and this is the
connection string. So next uh here I
will unselect all the packages because I
don't need. So I will not select this uh
and uh yes. So uh let's back one thing
uh I will not uh save the anything
locally. Uh
so so you will click the third option uh
next and I will not I don't need any
other package. So I will unselect and uh
finish. So once you finish uh the
application uh uh the function will be
created. Uh let's close. And uh now you
can see here if I scroll
if I scroll uh it will u add all the
packages. Here you can see uh this is
the function we created and uh you can
see here uh this is the Q storage. This
will be the Q name uh new match. So uh I
will u copy this and I will create uh
the queue with same name in the uh in
the Azure storage. Uh so you can see I
will come to this point and uh I will
open the uh emulator default port with
key and I will create click on key
create new and I will give the same new
image. So uh this is uh about how you
will create the Q in the storage
explorer. Now I will go back to the
code. So here you can see the connection
this is the connection string uh name.
So I will copy and I will go to the
local setting uh uh JSON file. So you
can see here uh there is no connection
uh string name. So I will give the name
and uh uh then I will use this
connection string. This is will be the
default connection string for local
development. So uh you can see all is
done. So uh all the step is done. Now I
will add trigger here and uh I will run
this application.
So now you can see here the this is
running uh the AU function uh is
running. Now I will send one message to
the queue. So I already note down the
message to save the time. So you can see
here uh I will open the explorer. Here
is the uh the explorer Q and uh I will
click on the add and here you will add
the message and uh you will keep all the
thing as default and I will click okay.
As you can see here the uh tri the break
point is triggered. So uh we can analyze
the message we are uh uh sending from
the explorer. So you can see here you
can process the message. Uh so this is
how the uh Q trigger works. Welcome back
to practical serverless and
microservices with CR. My name is
Mohammed Abdullah and I'm Microsoft
certified as your solution architect. In
session number eight, we will discuss
IoT function. So what is IoT? The
internet of things is changing how we
interact with world by connecting
physical devices to the internet. So IoT
is about connecting physical device like
for example um temperature sensor to the
internet. So uh the challenges uh of IoT
is scalability. Delivering scalable IoT
solution can be challenging. So that's
why we use IoT hub as you provide IoT
hub. So in this session we will discuss
practically how IoT hub work. So uh IoT
hub um fix uh scalability uh issue like
uh it's highly scalable uh and uh Azure
IoT hub is a platform as a service
solution that provide a robust
environment for uh connecting monitoring
and managing a large number of IoT
devices. So uh we will see uh IoT
practically in Azure. So I will go to
Azure and uh if you are new to Azure you
will search for IoT in the search bar
and uh as you can see IoT uh hub
hub. So this is IoT. So uh there is no
IoT hub currently. So I will create one
IoT hub. So you will click on create. So
here you can see uh you will need a
subscription. Then you will need one
resource group. For this course, I
created this resource group RG
serverless uh course and you will give
name to your u ser u is your uh hub. For
example,
uh I will give name for example u IoT
car service. So the name should be
unique. For example, this will be IoT
car service will be my name of the IoT
hub and uh this will be the location. Um
you can select nearest location to your
uh customers. So uh I will go with the
uh South
Asia.
South Asia for example. And uh here uh
IoT have provide different type of uh
tiers. For example, it have free tier,
basic tier and uh standard tier. So for
this video, this is um u course. So I
will select the free. And if you want to
compare, here is the comparison for the
uh for the IoT hub services. So you can
see here uh there is three uh tiers free
tier, basic and standard. And uh the
main different is the messages. In free
tier it provide 8 uh,000
of messages and uh the message size is
0.5 uh kilobyte. And for the basic it
provide a large amount of messages like
3 billion and the message size is 4 KB.
And uh this is the table you can uh in
the standard you can use the device
update for IoT hub and uh defender for
IoT. I will use the free one. So I will
select the free. After you can see here
when you select the free it will show
the messages. Uh if I select the basic
the pricing and the messages limit will
be changed. And uh then you will select
uh review and create you will keep other
thing as default until you need anything
like in networking management and
add-ons.
So
uh after uh you will review uh the
pricing and the limit you want.
So it will take some time to create.
Development is started.
Okay. So finally uh IoT card service is
created. IOT hub is created. So uh when
you click on the go to this you can see
visit the resource as well. Uh this is
the overview uh of the IoT hub. You can
uh see the messages uh on daily basis.
the message kota and this is the
connected IoT IO IoT devices. So uh
after that once you create the IoT car
service I will add one device for
example uh I will create one for example
car you can also use other tool uh
devices as well for example temperature.
So uh for now uh I will create one uh
simple device for example car 001.
So this is the you will keep all the uh
other option as default. So uh once you
uh click save it will create the device
successfully.
You can see here the device is
connected. We can also add other device
like temperature device sensor. So it's
up to you.
So now you as you can see uh here in
this slide we will follow one example.
IoT device simulator. So in this
example, we will create one console
application that will pretend uh like u
temperature sensor and it will send uh
messages to the IoT hub and after every
5 seconds and then we will create one
IoT um hub u I already created and then
we will create one Azure function uh
whenever IoT hub receive any message the
Azure function will be triggered. So
this is our agenda. So this is the graph
as well. We will define we will have one
IoT hub and uh it will have one device
and then we will have one uh console
application that will that will act as a
temperature sensor. It will send data to
IoT hub. Then IoT hub will send this to
uh this data to the IoT uh IoT function
u and uh we will define IoT function as
a event hub trigger. So we will see this
uh in action.
So uh I created one console application
IOT device uh simulator. This is a
simple console application in C#. So you
can uh use the console uh application
template. There is nothing in the
application just a program. CS and it
will act as a um temperature sensor. And
you will add this library as well. This
is u Microsoft provide this library to
work with IoT uh hub uh in uh hub. So uh
first of all I will uh create one
connection string and uh from where you
will get this connection string I will
uh show you uh in the Azure portal. from
where you will get you will go to the
IoT hub then devices then select device
uh and then uh get the primary
connection string. So we will see this
uh and after that I will create one uh
function send message async that will
accept uh device client and message and
after that uh there will be another one
function that will send the message uh
to the IoT hub and uh to uh do uh to
call this we will use one main function
that will send the message for example
temperature uh every five uh 5 seconds
and this is you can see this is the
delay 5second delay. So uh this is the
application. Now I will go to the uh
Azure hub. Uh so you can see here uh
there is one device already. But if you
want to add other device for example I
will add temperature
temp device 001 for example you will
keep all the thing as it is. So uh you
can see here I edit the other device as
well. So uh you will get the connection
strings. So you will click on this. Um
so you can see here we have different uh
connection keys like primary key, device
ID, primary key, secondary key and
primary connection string. So we will
copy this primary connection string and
uh we will paste in the in the console
application device connection. So uh you
will paste it here. So this is done. The
first part is done. Uh now if I run this
application, you will see it will send a
data uh message after every 5 seconds.
So you can see here uh this is the
message number one. It's sent to the IoT
hub uh by using the connection string.
This is the second message. So uh our
console application is perfectly
working. Now I will go to the the second
part. So this is the second part of our
uh application. So you can see here it's
a IoT function. Uh so here is the
function one. Uh we will define one
Azure function. First of all uh I will
define logger to log any logs and then I
will define one Azure function of type
event hub trigger and this is the u Q
name the events name and this will be
the uh connection string name so uh and
we they will uh they will u process each
event for example they will accept the
events array and it will process each
event for now just for the demo purpose
I am printing the body and the content
of the uh event. So uh
uh this is all about the function. So uh
you will do one other thing. You will uh
copy this connection string and you will
go to the local setting uh JSON file and
you will add one other connection
string. So uh from where you will get
this connection string.
So uh you will go to the event hub. Uh
for example uh I will go to the event
hub.
So this is the event hub. No, this is
our event hub and uh this is the first
event hub I created. And uh you can see
here uh you will go to the uh hub
setting. Uh so you can see here this is
the hub setting. Then you will click on
the building endpoint building endpoint
and here you can see uh this event hub
computable endpoint. So this will be the
connection string for the uh function.
So you will copy and you will paste in
the in this part. So this is done.
So uh now the function is also
completed. Uh now I will add one trigger
here and the other trigger in the
console application.
So now first I will run this uh console
application. Then I will run the Azure
function as well.
Okay. So, Azure function is up and
running and uh the first message they
received. For example, if I go to the uh
this is the function and uh you can see
here this is the simulator. It will send
one message. So you can see here it will
send one message and then uh it will hit
the function. So uh you can see here
this is the function error and it will
receive the the event. For example, if I
go to this part
so the body is you can see here this is
the body of the uh body of the event and
this is the content. So this is how you
can uh communicate um your uh device the
IoT hub and the Azure function. I show
this practically go to the uh if I go to
the portal. You can see here I will go
to the portal to check the result.
I will go to the devices.
This is the temperature device01 and uh
you will see the IoT hub messages
information in the overview section. So
you can see here uh today uh we used
nine messages from the uh different uh
devices and two devices is connected. So
this is how you can use the IoT uh hub
in. Now we'll see one uh another example
from our car sharing application. This
will be IoT solution connected to a
microservices solution. For example, as
you can see in this figure uh we will
send vehicle data to is your IoT hub. I
already show how you will create IO IoT
hub and how you will add devices. How
you will extract the connection string
and use in your application. And uh this
IoT hub will use uh Azure event hub
Azure function. Okay. And the the Azure
function the Azure function will send
data to Cosmos DB either it will send
the data to the uh rabbit MQ and the
Rabbit MQ will send this data to the uh
other microservices. For example, here
the planning root uh microservices. it's
subscribed to the messages on rabbit MQ
and the Rabbit MQ will send the data to
that micros service. So this is the
example we will see. So uh uh as you can
see here I created uh one example in C
uh using Visual Studio. We have one
simulator uh that will simulate the car
similar to my previous example. You can
see you will get this connection string
and here you can see it will uh send the
data to the uh Azure hub. Okay. So you
can see here I am sending vehicle ID,
their location uh and speed, car status,
battery level, fuel level, tire
pressure. So this is all the information
we will send and uh after sending this
data, simulating this data to the IoT
hub, the IoT hub will uh then send this
data to this vehicle tracking function.
So uh what is inside this tracking? You
can see here uh this is the event hub
trigger and it's using the car sharing
uh IoT hub connection string and this is
the default endpoint messages uh event.
This is the default event um for u uh
IoT hub and uh once it's received the
data the first thing it's doing it's uh
the save data to the database. So this
is the simulation. This is just a
simulation for now. We will just print
out the uh message console write line.
Uh we will uh we will print the speed
and the ID. And the the the second point
it's sending the alert message to the
rabbit MQ. You can see here it's a
rabbit MQ simulation. And uh this is it
will send the data to Rabbit MQ. And uh
you can see here this is the all
application uh and this is the required
types needed for this application. Um uh
this is the message stamp. This is the
dual location data and this is the start
and end uh date and time. And this is
the basic uh information message and uh
this is the vehicle related data like
ID, speed, status, car, battery level,
fuel level and tire pressure. So um you
will I will run this uh like when you go
to the uh profile for example uh I'm uh
I'm running for example um the two
application uh you can see here
configured startup project multiple
startup and uh I'm only uh the car
simulator and the vehicle tracking uh
scenario will start. So I apply okay and
you will run.
So you can see here the Azure function
is running and this is the simulation
vehicle simulation uh vehicle tracking
simulation. So uh it's sending one
vehicle IoT uh explorer. So uh you will
install this uh you can install this
from the um any um Azure IoT explorer
download. You will search this and uh
here is the link uh install and uh use
Izzure IoT explorer. So you will go to
the install section. Click on this open
in new tab and you can see here uh just
install and uh download and install. So
once you install you will see this uh
like the uh you will see the explorer.
So how you will connect? You will click
on the connection string. After here you
will need connection string. So from
where you will get this connection
string. Uh go back to the IoT hub and
click on the IoT uh your IoT and uh now
you will go to the uh security setting
and share access policy and and you will
click on the IoT hub owner and uh you
will get the uh connection string the
primary connection string. You can see
this is the primary connection string I
will get and uh I will connect the the
IoT explorer. So you can see host name,
share access policy and share access
policy key. Uh now I will click on save.
So you can see here uh it's added and u
uh we already added two devices. Uh so
this is the device ID. You can see here
you can uh check uh uh device to
telemetry data and uh the cloud
messages. So you can also manage uh IoT
devices from this explorer as well.
Welcome back to practical ser
microservices. This is shop. My name is
Muhammad Abdullah and I am Azure
certified solution architect and uh this
is session number nine microservices in
practice. So uh in this session we will
create micro uh route planning micros
service for our car sharing application.
So this will be our key uh topic. Um we
will see the detailing the functionality
of the route planning microser. Uh then
we will see the input output uh
communication and request uh ser serving
logic. uh then we will use the u poly
labifier for resilient communication and
we will see also the onion architecture
on onion architecture will be be our uh
main architecture for this u micros
service. So um the route planning
microser um will uh store and match user
request to travel between uh two towns
or two routes and u the matching logic
about the matching logic when a route is
created it match it compatible request
based on the town proximity and that
constant and new request is created it's
match with existing when uh someone uh
try to reach to other town or uh their
destination uh they will create uh
request and u the route will be created
by the driver. So the the request and
route will be matched. When it's
matched, it will uh uh show the uh the
the result to the user. And uh this will
be the life cycle management like route
removal. A route is removed from match
when it's close or abort. And um the
other thing is route extension. A route
a route is extended when it merge with a
request. And a requ uh request removal
when it merge with the route. When any
request merge merged with the route then
it will be removed and uh request become
available again if merge requested a
board. If any u route is removed then uh
we will again u uh the request will be
again enabled and uh for the cleanup
purpose uh both request and route are
deleted and after maximum traveling uh
so we will also delete the uh request
and routes from our system and for
communication uh we'll use the publisher
subscriber pattern for this we'll use me
rabbit MQ as our message broker u rabbit
MQ will be used as a message broker It's
highly portable and it's not depend on
any cloud platform. So rabbit MQ is the
best choice. So for this uh microser
route microser we'll use route lining
microser we will use the rabbit MQ and
for message serializer we will use the
JSON and uh for our project template uh
we will use the SP.NET net core a
minimal API based project template
because uh it's it will support hosted
services uh needed for message broker
communication and u it also allow uh to
easily expose http uh endpoint for hell
check will also u define hell check
endpoints to check the health of any
micros service so this is best practice
in microservices so uh for security
perspective uh user to microservices
security u uh it will be handled by the
sp.net web API technique primary use
like JSON based uh uh tokens we'll use
the authorize attribute to uh to check
uh security of any request
and uh to service to service
communication we can use the mutual TLS
but to keep this uh simple in this
course we will not uh go deeper in this
u uh security u so uh we will uh this is
just for uh you you can also use the TLS
for service to security and uh about API
gateway. API gateway act as a public
facing entry point. So this is in
microservices we have different
microservices and uh we cannot access
the microser directly but we can only
access the microser with the help of API
gateway. So about the API gateway
security um uh they also handle the HTTP
termination uh receiving encrypted
traffic from the outside world and also
maintain uh encryption within the
private network. uh using MTL TLS or
encryption. So this is a best practice
uh for end to end uh security and uh to
do this uh to start practically uh first
we will create uh minimal API project uh
using uh visual studio. I will use
visual studio and uh we will uh give
name route planning to our solution and
we will not select the https and um we
will also add the docker support as well
to our application. So let's get start.
So first of all uh I will create one u
new solution. You can see here this is
the sol visual studio 2022 and here I
will select create new project
and here I will select the template.
You can see here uh you will select this
uh ASP.NET core uh web API template and
uh let's next and here you can see we
will give name to our application for
example route planning will be the
application and here we will select the
location.
I will create one other folder. You can
also I will also upload the code with
this course as well. So you can you can
access the code as well code and uh
select and the solution name will be um
uh route planning as well. So you can
see here uh solution name will be the
car sharing because our main project is
a car sharing car sharing.
Okay. So route planning and uh you can
see here I will click on the next. So
here you can see I will go with
latest.net 9 and uh no authorization uh
configure for I will unselect and uh I
will click the container support as
well. I need a container uh so I will
click the container and uh you can see
here uh there is two type of u
containers. So I will go with the the
Linux based container and uh I will also
enable the open API support and uh I
will go without controller to keep it
minimal. So so this is the
uh setting uh for our uh project
template. So uh
okay so I will create next
Okay. So the project is uh created
without controller. So this will be our
uh route planning application. So you
can see here there is nothing uh inside.
We will remove the extra code uh like u
this remove this command keep this
minimal and u I will remove this
uh
default code.
So you can see here I don't need this
weather default coding. So you can see
here now it's the it's clear from the
unwanted code. This is the app setting
file and uh this is the uh program.cs
file. So uh first I will create um one
uh folder. I will add one folder. Um I
will give name hosted
hosted services. I will give these two
hosted services. In this hosted services
we will have uh three main u services
that will run in background. So the
first one will be the main service.
Sorry. Uh this will be the hosted
service.
Hosted service and u this will be the
main service. This will be the consumer.
It will consume the messages from the uh
rabbit MQ. So I will create one class uh
main class. So this is our main class.
Uh then we will have a housekeeping
class that will clean up the request and
routes.
Okay. So you can see here and the third
one will be the output.
So we have added uh three hosted
services. So the first one is the main.
So we added the main uh so we will uh
it's currently not implemented. So um
this is just for introduction what we
will add this uh to the main service in
future. So this is the main overview. So
it main service will be act as a message
consumer and the purpose of this uh main
service consume messages is from rabbit
MQ when uh rabbit MQ exchange or uh
receive any new message. So this will be
triggered uh in background and it will
do their job. So uh this will be uh
subscription for this will be we will
create different type of messages like
uh route offer messages u whenever any
driver create new uh route offer. So we
will use this uh uh message to the we
will send this message to the rabbit MQ
that for example ABC driver created this
route and uh the second one will be the
for the passenger uh he will create the
request for the route and um for uh
extended uh route we'll use uh this
object uh route extension and
modification. This service will also do
the route closure or cancellation and uh
we will also this service will also
handle the real uh time vehicle tracking
and uh we will also do the error
handling um track error count for the
health monitoring. So this will this
service will be responsible for these
things and uh the this output standing
sending service uh this will be act like
a message uh publisher. So uh it will
process uh each uh out outbound message
u available in a queue and publish
messages as well. So uh the key feature
will be batch processing with the
configurable batch size. So uh we will
uh it will get the batch size from the
app setting. We will uh um configure the
size in the app setting and it will get
the size from the app setting and it
will process accordingly. And we will
also add the poly um library as well uh
for the resilient try catch and uh
circuit breaker. And u it will also
publish the route extension proposal
message to other services. And uh
configurable delay and retry strategy
will be also part of this uh uh service
background service. And the housekeeping
service uh it will be used for uh
maintenance uh like uh cleanup purposes.
Um pre it will clean up uh on per
periodically u to uh to maintain uh and
clean up the route and request and it
will also get the value from the app
setting uh intervals and uh it will uh
remove uh old or any expire um request
or routes and uh it will run in uh
background it will be also a background
service. So this is some uh some
information about um the
responsibilities of each uh background
service. Now you can see here uh we have
currently u this is our project net 9
and currently we have u two packages.
One is the open API uh and the other one
is the for containerization support we
we added this. So now we will add some
um other library for uh encryption
purpose. Uh so uh this will be part of
this um microser. So first we added the
uh easyet q. Uh so whenever we send or
receive messages from rabbit mq it will
uh do serialization uh uh d
serialization. For this purpose we will
use this library. And the third the
third um library will be the poly
library. For uh resilient try catch um
or circuit bracket we will use a poly
library. So uh this one is added. So you
can see here it's installed in our uh
solution. Next uh uh you can see here u
this is the route planning uh http. So
uh we will make some changes in this
like uh here we will use this uh to
check the livveness of this route
planning uh microser when any u anytime
any service call this microser he will
check he can also check their health
it's available or not. So next we will
add the domain layer where the core
business logic will exist in that layer
according to the onion architecture. If
you missed the previous um session so
you can uh please check out the uh onion
architecture session. So you will get
what is onion architecture and how many
layers uh is available in onion
architecture. So the core of the onion
architecture is the domain layer where
the business um exist. So u now uh in
our route planning application we will
add uh the core like the domain layer.
So for this uh we will we will add one
other class library. So I will give name
uh I will click on the solution. Click
on the add new project. Search for uh
class library. You can see a C# based
class library. Select and enter uh give
name route planning domain layer. This
will be the name of our domain layer. So
I'll click next and net.
nine support. So you can see here uh
layer is added. I will remove the extra
class. So uh now this will be the main
the core of our uh route planning. So
here we will add some um models and uh
business logic here. So let's get add
the model first. So uh first I will
create one uh folder uh with name models
where we will add all the models related
to the route planning and then I will
add one u other folder
uh called events where we will define
events in this folder and the third one
will be the tool and third one will be
the tool.
So uh all the required uh folder is now
added. So we'll add the related classes
uh in event model and tools one by one.
So let's add first the model. So here uh
we will create some subfolder to
separate each uh related uh to group
each related model. So for this I will
create other subfolders uh like uh for
the basic type I will create one other
folder
uh basic type then there will be a Q
output for input uh messages I will add
one
I will add to inside the model and uh
there will be request related model I
will keep in this folder and the route
route model will be inside this route
model. So this is like a separation of
concern. So we can easily identify um
model for related to the request route
and uh the basic type. So uh now I will
add uh the models related to the basic
type. So some basic type we will require
for example route status to check the uh
status of the route. I will add the
route. So this is the easy part. So I
will add one by one.
So um this will show the status like
open close and uh about about it. So
this will be the three status and then I
will add interval time time in
intervals.
So for this we have start and end time.
Uh it will um group the time interval
for us and uh the town basic
information. I will uh add one other
type
uh town basic information and uh this
will be the properties.
This is a short class having id name and
location. Here you can see um pointer is
coming from the system. But for this I
will add one other library. Uh I will
not use this one uh the system one. So I
have I will use the uh net topology
suit. So for this I will go to the route
planning domain layer project you can
add the library here uh you can also
click and uh go to the new package and
here I will search for
uh browse and uh search for net topology
suit. So you can see here net topology I
will add this library uh I will use this
for uh to get the geometrical
representation like for example the
pointer. So I will install this.
Okay. Uh let's apply. Accept.
Okay. This one is install. One other
library I will need. So the dependency
injection abstraction. I will add this
one as well.
Okay. These two library is now
installed. You can see here u extension
dependency injection abstraction and the
other one is the topology for the uh
geometrical representation. Uh now uh
you can see here uh
dot drawing I will remove this and uh I
will get this from the the recent you
can see net topology. So I will get this
from this. So you can see here uh this
one is done. Now the pointer will come
from the uh net topology suit. So next
uh uh I will add one other object one
other class
uh user basic information where we will
this will hold all the some basic info
for example the ID and the display name
uh just for now we can you can add other
information as well. So this one the
basic type is done. Now I will add some
uh classes to the output cues. Uh so
first uh I will add one repository uh
related to the u task and confirm new uh
so this will handle by this uh Q output
message. So here I will add one class uh
this will I out I I output I will show
the interface this will be the interface
for the repository. Uh so now I will add
this
uh yes so I will add this letter other
repository and the Q item and uh we will
add this letter and uh now I will add
the state object to the que that will
show the state
and this will again be the interface. It
will have uh it will have some
properties like ID, message code and
message content. And the other one is
the item uh Q item.
Q item Q item. And uh I will
Okay. So you can see here uh this will
be the Q item. Uh okay. So we will add
this later. The it will come from other
uh basic type. So now this is uh yes you
can see the repository will be added
later. So this is done. Next I will add
the model related to the request. First
I will add the repository interface
inside the request. It will hold all the
business related to the request. So
again it will be a request high route
request repository and uh this will have
some uh interfaces uh functionality like
uh for example it will have one
aggregate uh aggregate is like a group
of uh related entity and um this will be
a aggregate for other model. example uh
basic uh town and uh the uh this will be
the aggregate for uh the other models
and uh uh then I will add the route
status
I request route status
and uh last I will add
repository is added now state is also
added I will add the aggregate.
If you want to know more about uh
aggregate domain design, so you can uh
check the previous uh session. You will
uh find enough. But for now, uh focus is
to uh practically do the uh route
planning.
So this is the aggregate.
Okay. So this is done. Now I will shift
to the route. Uh I will add first uh
repository for route.
So uh for this um microser we are
following the domain driven design and
uh event driven uh uh design. So u again
if you uh have no knowledge about domain
driven design. So you can check the
previous session. I already done a
session on this topic. So you can find
out uh the domain driven design. So this
is the repository
and uh also we are using the repository
uh pattern and onion inside onion
architecture. Okay. So this is done.
Route interf repository is added. Now I
will add the state.
Okay. So this is done. uh we added the
route related uh repository state and uh
the aggregate uh related to the route
request uh and uh the in output view and
the I also added the basic type uh one
other thing I will add now the tools uh
that is needed for um for the above for
example the entity we need and some
other stuff which is missing so it will
come from the uh tool so now I will go
to the uh tool first of all I will add
the entity u entity class
here. This will be the entity and uh
this will be the information inside the
uh abstract entity class.
Okay. So we will add this uh later um in
the same uh tool folder. Uh now I will
add some other concurrency exception
related object.
This will be the main repository
interface uh for other uh repositories.
For example, uh if I go to the iO
repository uh so it will inherit from
this uh repository uh it will come from
the tool. So now you can see here.
Okay. So now next I will add a unit of
work. Again uh a unit of work is also
defined in the previous session. So you
can uh def find out some basic
information related to the unit of work
its role in the DDD. So you can also
find out this in the previous section.
Um you can also uh copy the onion
architecture from the previous session
like the onion architecture. I did a
specific uh session about the onion
architecture. So you can copy the code
from u that u session as well. So this
is again the unit of work now. So let's
build
some build info build issues. I will use
the tool one and uh again this is the
build issues.
Okay, this will come from the tool I did
and uh this is entity will come from the
tool as well.
Okay, let's add this class as well. Uh
okay, so I will add this class outside
all the folders. Okay. So you can see
here uh it will be uh at the root level
of this project. So I will add this
class geometry constant and uh currently
there will be one static property inside
this uh which is a default u srd. Okay.
So let's build again.
Okay. It will be interface.
Okay. It will be interface
and uh it will be public interface
build
some other issue. Now this is gone. This
is gone
tool. Uh add the package.
We will add this later uh attachment.
Okay. No. Uh so we will add this missing
thing like attachment event uh release
event. The event is missing. So uh what
we will do like uh we will add uh some
events in the events folder. So let's
add this event. So first first of all I
will add the attachment uh event request
attachment event.
So I will add one class name attachment
request event. Put this code. uh to make
this video short uh I uh created uh I
copy I just copy the code from other uh
solutions. So you will get just idea how
the microservices will be built. So it
will save all time
there. This will be the match candidate
uh event. So for this I will add this
record
and uh the release request event release
released request event.
Okay. So now this one is addit. Now
let's build.
Now I will import this from the events
folder and uh this one is uh not yet
added
and um
uh it will come from the system data. So
this is done.
Okay. So now you can see here uh now the
domain uh layer is uh successfully
completed. We added the events attached
request event match candidate event and
release request event. We will use this
uh in our other layer. It will use by
other layers. So you can see here uh
this is the basic type. We added the
basic then uh output cues messages and
uh the two main things which is the
route um route will be offered by the
driver and the request the passenger
will create the request. So this will be
the hand uh to handle the request we add
this and uh some other um common
information we keep in the tools folder
like unit of work events and the
repository and this is for the uh
geometric uh constant. So um this is
done for the uh domain layer. Now after
adding the route planning uh the web API
project then we added the domain layer
the main uh business layer of the
application and after that we add the
shared messages uh like which is the
contract basically the contract uh for
uh route and vehicle tracking and some
basic uh types. Now next we will add one
uh another layer one another uh class
library uh like the DB driver that will
act like data access layer layer and we
will use this for um entity framework
we'll use entity framework in this layer
uh that will store the data inside
database and uh whenever we need any
data we can also pull the data from the
database layer from the database as
well. So for this I will go with uh uh
one another layer. Uh so I will click on
uh solution right click on solution a
new project and again uh class library.
Uh next give name uh route planning
microser name and DB driver uh will be
uh a DB driver for our project. Next uh
and uh I will remove this default class.
So now this will be the DB driver for
our uh project. So uh to this uh we will
add some entries basically um the data
uh the persistent data that we store in
the database. So it will have some
entries. So route planning will have uh
some uh entities. So I will store in
this uh we will uh save this in entity
folder uh I created. Uh so u we will add
some basic uh entities like for example
for out box q uh messages I will use
this class this entity and u this will
be the
uh there will be some basic um field
like uh ID uh message code u message
count and uh uh ready message. So this
will be the information we will store uh
inside u Q output item. After that uh I
will add one other u entity for route
offering like the driver will offer the
routes. So to store it uh I will create
one entity route offer uh and uh there
will be some basic uh entities basic
field like for example id line string uh
and uh time basic user info the value
entity value object this will be the
value object and uh this is route status
collection of uh uh route request and
the time spam and uh after that uh I
will add one other uh route request.
This is the route offer. Then uh I will
add one uh other one for the request
route request. And uh here we will have
some uh fields for this uh ID uh town
basic info and town basic info for
destination and start uh end and some
other uh route uh related uh route for
uh route offers. So uh this is it. uh
after that
what I will do uh I will uh add a
repository for this uh each entity each
entity will have in domain name design
uh and repository pattern uh each entity
will have their own repository. So for
this uh I will create uh one repository
folder that will hold all the
repositories uh for the route planning
uh microser. So uh first thing uh first
of all I will add repository for the uh
output cues uh item. So I will add this
and uh it will use it will inherit from
the I repository we defined in the
business layer. So you can see here this
will uh this is the IUT it will come
from the from this layer uh we in this
layer if we go to the tool you can say
here we have uh IU unit of work
basically. So uh this means that the uh
DB driver will uh have access to the
domain layer. So for this I will add the
the project reference like the domain
layer. So it will use the domain layer
unit of work interface. So uh now you
can see here when I go to the repository
I will import the using uh you can see
here now it's showing the tool folder
for us. So this is uh about I will also
use the
uh repositories as well repository
interfaces in the you can say in domain
layer we have uh model uh output cues
and there is interface for this. So you
can say here uh this is the interface
and um okay so uh we will uh fix the
build issue uh because we don't have the
uh context yet. So we will add the
context and the other uh repository for
each entity. So next I will add the
route offer. For this I will add the
repository the related repository.
Okay. So let's go and add the repository
the class for this. And here will be the
detail of the repository the actual
repository.
Okay. So now this is uh added and uh
next uh one other uh repository for
route request. So for our request I will
add one other repository.
Okay.
Okay. So this is added. Now let's save.
And uh next I will add the Mand DB
context. You can see here the M DB
context is missing. And I will add this
at the root level of uh this layer the
DB layer. So what I will do uh I will
close this and uh I will right click and
add class.
This will be the main DB context for our
uh route planning. So uh here uh I will
do some u I will import this from the DB
context first. You can see here I will
import from the D and you can see here
the it's giving me compile time error.
So u for this uh I will add few u like
packages uh EF core packages. You can
see here there is nothing uh it's not
referencing to the EF core. So uh I will
add some u like packages for um entity
framework. So uh what you can do you can
directly add to this this
uh file as well as uh you can also add
this uh from the new get package as
well. Okay. So I to save the time I will
I I will directly past like what like I
will need the EF core SQL server because
we will save the data inside SQL server.
You can use any other uh database as
well like postress and any u database
relational database of your choice. And
the second thing is the
net topology suit. This is for the uh
geometrical representation. And the
third one will be the uh if code tool to
like build uh and run migration update
database something like to run this
command. So uh now the packages part is
done. Uh okay. So now you can see the
packages also added and uh if I go to
the project you can see here u this is
uh the project uh domain layer is
referenced uh in this uh DB driver. So
uh the key principle of the clean
architecture on architecture is that the
domain layer this is the domain layer it
will have no reference to the other
project. So you can see here it's
independent and uh uh this one the DB
driver is accessing the domain here and
uh you can see here u now uh I will add
the from the Q Q state and um I will
build the fix the build issues.
It will come from the net topology suit
like geometrical representation. I will
add this reference and uh this will be
the part of the basic type from the
domain layer and uh you can see here now
this is working. I will add the
interface from the domain layer. So this
is done. Now I will go to this
Okay. So,
route uh object is also uh route request
is also u done and uh now I will go to
the repository and uh I will fix the
repositories. So if I go to the main
context uh I will add the package ef
package and the unit of work will come
from the interfaces unit of work.
Okay.
Okay. So uh next uh this is not
implemented yet. So uh I will implement
this uh by adding the required code to
this. I will register each entity in
this uh main uh DB uh context. As you
can see here, I added the request route
entity uh route offer and output. This
is the three entity. We will save uh
data later to this in SQL server. And
this is the model builder like you can
configure each property uh here in this
method on model creating. Whenever the
model is created, this rule will be
applied. And this is some basic um save
entity unit of work related um methods
and uh you can add the missing
references. So now unit of work is done.
Uh next I will add one uh like uh
factory like DB factory. So for this I
will add one other class at the root
level.
So
add class
and give name library design time DB
context factory. So uh it will uh build
the DB context at runtime. So um it will
have the connection string and the other
uh DB uh context related option. So uh
for this I will add the code related to
this repository uh library factory. So
you can see here it's implementing the
uh design time uh DB context factory. Uh
I'm passing the D or main DB context and
this will be the connection string to my
local SQL server. Uh you can install any
other ser uh server as well. uh so like
postress for now for this course I will
use the u SQL server and u it's a build
taking argument and uh it's uh build uh
use the connection string basically
creating the builder for us so uh this
is done uh what left uh whenever we run
a migration it will create the the
migration file uh migration folder and
migration related uh uh file as well. So
uh next the final thing we will add one
extension to like this layer will be
registered in our uh API layer this one.
So for this uh to store this uh to
connect this uh to our routing uh
planning web layer uh we will add some
extension. So uh for this I will add uh
the extension folder where I will keep
all the extension
add folder
extension though. So the first extension
I will add for the DB uh I will add the
DB extension
where uh we will reference this in our
program CS in our API here. Uh you can
see here we will reference this uh
extension in this file. So uh for this I
will add one static class uh DB
extension uh where we will have one
extension method. You can see here
extension method that will uh return the
I service collection and it will uh it
will take uh Iervices and the connection
string. So uh add the required packages.
Okay. So next I will add one another uh
extension that will be used for the
repository extension repositories. uh it
will be our repository extension.
Okay.
Okay. So this is the repository
extension uh add all repository. It will
uh register all the repositories we have
in this uh layer in our API layer. Okay.
So it will uh scan for each uh for the
whole assembly and um if they find
anything uh inherit from the I
repository they will take it and they
will uh register in our uh in our uh
program.cs and it will uh register as a
scoped. Okay. So this is done. Uh one
will be the interface and the other one
will be the action repository. So uh
this is uh done. Uh okay. So after
adding the u DB driver layer uh next we
will add the final layer which is the
application service layer. Again it will
have it will be um another um class
library and uh I will give name route
planning application service that will
have all the uh command uh and queries.
Okay. So uh next 9 support remove the
extra class. Okay. So now it's the
application uh layer is now created. Uh
so first uh I will add the required
dependencies and uh the project that
needed to this uh application service.
So uh for uh this project I will need uh
some uh two extension Microsoft
extension method. Uh you can see here um
I will need uh extension configuration
abstraction and the configuration
builder. And the other thing uh we will
add the references to other some to
other uh project as well. Um I will add
um one new project reference and uh it
will be a domain layer. So uh our
application layer will use the domain
layer because it's a onion architecture
and uh the dependency will uh come from
the domain then um application layer
then presentation layer. So uh and uh
the other thing uh I will add the
messages uh because in uh shared
messages we have contracts. So the
queries and the command handler will use
their those contracts. So I will add
these two uh project in our uh
application service. So now this is
added. Uh next I will add one um
folder uh that will be a command
handler. Uh I will click uh add uh new
folder command handler and it will have
all the commands. For example, uh
message related um command handler. I
will create some extra one subfolder
messages that will hold all the messages
related uh command handlers. For
example, um for now if I go to add one
uh command handler for the request uh I
will add class and route request message
handler. So this will be the uh request
message handler. Okay. So uh after that
u there will be u commands because uh uh
it will trigger the command handlers.
Command handler will trigger the
commands. So uh I will need a command
folder.
So I will add one other folder commands
commands and uh inside these we will
have commands and uh for now I will add
one command
class uh message command
and uh it will be a of type record.
Okay so it will be of type record. So we
will u fix this compile time error when
we complete this uh uh application
service layer. And uh uh after that I
will add one uh other folder for um
event handlers
that will uh that will have all the
events uh event.
Okay. So I will create
folder.
Okay. So
there will be different uh event
handlers. For example, attach event
handler is one event handler for this
attach event handler.
And uh I will add code related to this
handler.
It will use the I route request uh
repository and uh uh it will inherit
from the I event handler and uh
okay. Okay. So next I will add the offer
match. Candidate uh handler. Offer
match. Candidate handler.
Okay. It will have it will prepare the
message by taking the route request
aggregator and u uh it will set the
destination. And we are setting the
destination resource and uh uh we have
the handler. This is the handler. Uh it
will take the configuration value from
the uh app setting. We will add various
uh configuration value to the app
setting later. And uh next I will add
the release event handler. A release
event handler.
Okay, release event handler is done. Now
I will add the candidate event handler.
Match candidate event handler.
Okay, so this is done.
Let's also add the other command uh as
well. Uh so to complete the application
service uh I will add the housekeeping
as we uh discussed that we will have
three uh services
one for uh housekeeping
and the housekeeping command will have
one record
and the other one will be the messages
and third one will be the output sending
command. Output sending command.
Output sending command.
Okay. So,
output sending command.
Okay. So, next uh I will add the other
messages related uh event. So, uh I will
add the
uh route closing.
and uh route closing will uh take the
route of a repository and it will also
have the unit of work as well. Um uh if
you unit of work is the part of the
domain layer. You can see here this is
the unit of work uh we will uh we have
the save changes and uh start commit and
roll back. So uh the implementation of
this is located in the in the driver DB
driver class. Uh you can see here when I
open the DB context you can see here
this is the implementation we
implemented the 70 is async uh and uh
this is the start that will begin the
transaction and it it will commit the
transaction and it will if anything uh
wrong happens uh then the roll back uh
will happen. So this is about the unit
of uh work.
Okay. So uh uh this is done. The close
uh message handler is also done. And uh
now I will add the extended message
handler.
Extended message handler.
Okay. So this is also added again. Uh
it's the it will handle the request. uh
it will call the handle async and um it
will uh start the unit of work and uh uh
it will get the the route uh by ID and
uh it will do the other uh operation and
uh next uh the final uh will be the
offer message handler. Offer message
handler.
offer message handler.
Okay, offer message handler is also uh
added. So uh now u we have repositories,
unit of work and uh uh event mediator.
So event mediator and some other stuff
is uh missing. So for this uh we will
create one other like u folder uh and we
will give we will give name to this
folder tool folder tools folder and we
will keep like uh the things like event
mediator I command I query something
like so let's add this one by one okay
so let's close the other
okay the first thing I will add to the
tool is the mediator
I will add the mediator that will
trigger the event handler
Okay. So this is the definition. Um it
will u we have one read only uh I
service provider. It will it will take
the it will inject the I service
provider and um there is one method uh
it will trigger the event.
Okay. So next is the event trigger.
Event trigger is missing. So we will add
the event trigger.
event trigger
is added.
Okay. So it will uh inherit from the IU
notification and uh you can see it will
have uh two um methods uh one method
which is the trigger it will trigger the
um event and uh some other um I event
handler is the next thing. So I will add
event uh I handler
I event handler is added
and um
okay this in handler and uh next thing I
will import.
Okay so this one is added this one is
also added. Now some other command I
command and I command handler also
needed and query as as well. So I will
add the I command uh if I go to the
command handler you can see here uh I
command uh handler is missing. So uh I
will add the command I command and um I
query.
So let's add
I command
and it will be a interface
and um the other thing is the command
handler that will implement this
okay command and it implement I command
handler. Okay. So, uh we have one
generic and the other one is the simple
one. And uh now the next uh is the IQ
query. The last thing is the I query.
Uh let's set
I query and it will simply
public
interface.
Okay. So this is done.
So now a messages part is done and um
one other handler is uh missing for
housekeeping and output uh sending uh
commands. Uh so if I go to the handler
we added the messages but for
housekeeping uh it's missing. So I will
add the housekeeping handler as well.
It will remove the request and response
from the uh database
for cleanup purposes. So
okay. So now uh it will take uh delete
the uh the offer and the request and the
other thing is the output sending uh
request handler command handler.
Command handler.
Okay. So this is command handler.
Okay. So let's build.
Okay.
And uh for extension like to register
this into our uh program uh that's in
our API. So we will add one extension
folder that will hold all the extension
extensions
extensions
and uh there will one hand hand
extension for now which is the
handler dependency injection extensions
and uh we will uh scan for the
repositories
sorry uh for the handlers and it will
register it will register uh in our
program.cs class. So this is done.
So uh now we will focus on the uh web
API project. We created uh initially the
domain layer. Uh we defined the entities
and uh aggregates and events uh andio uh
object and after that uh we u defined um
the contracts between uh the
microservices. uh will communicate with
this uh messages contract. Uh and uh
then we added the uh DB driver. Uh we
use the uh entity framework core uh as a
data access layer. It will save and
retrieve data from database and we
define the DB context. Uh we have uh if
you go to the DB context we have three
models uh route request, route offer and
output Q types. And then we created the
actual uh like the application layer.
application layer have uh command
handler, commands, events and um you can
see here uh we created the different
type of events uh that will be triggered
and uh uh this is the house keeping uh
event handler uh if I go to the this one
is the u for uh the housekeeping cleanup
type of thing u it will uh uh use the
request repository and offer repository
it will do the cleanup and uh after that
uh we u we'll register this in or
programmer.cs. This is the uh web API
layer. Uh you can see here um first of
all we uh we are using the open API um
specification. Uh then we are going to
register the application service. U as I
already mentioned that uh inside uh
application layer we have um uh uh
extension. You can see this is the
extract
uh it will connect basically the uh
application layer to the inside the API
layer. So you can see here this is the
uh extension and uh we are using we are
uh injecting um we are adding this
service in this part uh program.cs and
the other uh thing is the DB driver. If
you go to the DB driver layer, uh this
is the DB driver layer. Uh it's uh
accepting the few things like uh
services and the connection string and
we are injecting this from the u entry
point of our application like the API
layer. The routing planning you can see
here uh we are passing the connection
string and uh we are defining this in
our app setting. If you go to the app
setting development JSON, you can see
here it's the connection string of our
local uh database. Okay. uh and uh then
we have u rabbit MQ we'll discuss this
later um so uh after that we have uh
rabbit MQ connection uh and uh this will
come from the uh connection string as
well uh so uh once we run the docker
container in our docker uh docker hub so
it will create it will host on the local
host uh 5672 uh port and we will we can
access in our application and uh after
that we are uh uh adding the hosted
service, hosted services like uh uh we
have uh three uh services like main
service that will act as a subscriber uh
uh like it will do the it will create
the exchange uh it will create the qu Q
uh quiz and it will also handle the
serialization dialization with the help
of this uh easy.NET NETQ library and um
yes so here uh we define the service and
we are registering uh this in this part
and uh uh we are using in development
mode and uh here is one endpoint liess
we will check the health of the our uh
service or background services. So now
we will run the rabbit MQ for uh
communication purpose uh like uh uh as a
message broker u locally uh in uh docker
hub we will uh need to install the
docker hub in your system. So first of
all if you don't have a docker hop
installed so please install and uh you
can see here u we have uh no container
yet and uh if I go to the images this is
all the images we have some of the
images is uh needed for um kubernetes uh
so this is complete uh you can ignore
this completely and uh you can see here
this is no container so uh what I will
do uh you will open the command prompt
and you will write one command to get uh
the image of the of the rabbit mq. So
you can see this is the docker run and
this is the name of the uh it will be
the name of the image container and it
will run on this port and uh uh we we
are giving the password username and
password like the admin will be the
username and also the admin will be the
password and uh uh this is will be this
will be the default host slash uh this
is the default port and uh if you run
this uh it will u you can see here the
image is not found. So first of all it
will get the image from the uh
repository. Once it's download the image
uh then it will run the uh rabbit MQ uh
container. So let's wait for this.
Okay. So uh image is downloaded and uh
this is the status. Uh if you go to the
image uh it will show the image for you.
You can see here it's the tag number uh
4.0 management the tag number and you
can also uh change the tag number
according to your need. You can see here
uh this is the take number. So you can
uh it's up to you and u and again this
is the image ID and uh this is all about
the images. Once you get the image you
can say this is the container. If you
click on this container it uh it will
show the log inspect and the other files
and state and here is the ports. Uh it's
running on the local host um uh 15672
and this is the uh other port. So uh you
will click on the you can click on any
uh of this uh URL. So I will go with
this. Once you click you will get
something like this uh it will show you
the overview
uh and uh it's a connection. Currently
there is no connection. This is the
channel and uh this is the exchanges and
this is the Q and stream. So uh now you
can see here the rabbit MQ is up and uh
running. And let's go back to the uh to
the code. You can see here uh the
connection uh they will get the Revit MQ
u and uh after that uh let's go to the
main service. You can see here this is
the main service
uh like um it will publish uh send
message to the exchange when I run this
uh microser this uh main background it
will send the uh it will create the
exchange for us. You can see here it
will create uh different exchanges uh
one for the route offer and the other
one for close route. um aborted u
message. The other one will be for
extended route and one for the uh so it
will create a different uh exchanges for
us and u this is the API need for u
route request messages to handle uh like
uh for now we will uh publish the
message from the dashboard uh directly
so it will trigger. So uh once you run
this application, our container is
already running. Uh and u before this u
you will run the update database
command. Uh you will open the uh you
will open the updated uh the command
prompt. Um you can see here uh this is
the uh new get package manager. You will
open the command prompt or you can also
use the powershell and you will run the
um command like for uh update database.
So um you can also use the powershell as
well. PowerShell you will open the
PowerShell and uh you will write this
command like uh net ef database update.
Once u you run this application it will
directly u create the database to your
uh local storage. For example, if I go
to the uh database uh you can say this
is car sharing and uh this is the uh the
table we define in the uh DB context.
And um you will also find out the uh if
you go to the driver uh the DB driver
you can see uh the DB driver we have the
first you will run the migration command
uh if core migration command once you
create uh u if you don't have the
initial migration so first you will run
the migration command uh I already run
the command so I have I got the
migration and the file uh once you will
find this code as it is so you don't
need to run the migration command uh I
already run the migration command
command you will need just to run this
uh effor core uh update database
command. Once you run, you will find out
the related database uh in uh your uh
SQL server. Okay. So let's go back to
the hosted service
hosted service main service. We'll
discuss the housekeeping service and out
uh output shelling later. So uh this is
done. Uh so let's run uh the
application.
So you can see here the application is
uh successfully running and uh it's
running and uh let's uh test uh this by
sending one message from the uh rabbit
MQ. If I go to the rabbit you can see
here uh now when you open the connection
uh you will find out uh two connections.
Um connection number is two and uh it's
from um one is from pling uh you both is
from uh route planning and um this is
the protocol we are using and uh it's
running and this is the channels and uh
this is the exchanges uh so you can see
here the main uh if you go to the main
uh service if you open the main service
uh so uh it will create the exchanges sq
uh for uh you can see for the uh request
offer and u close extend it uh so we
will find the same thing here uh in
exchanges. So we have these exchanges
one is for the vehicle tracking um and
tracking messages vehicle tracking
messages uh the other one is for route
uh request message offer message
extended and close about message. So uh
exchange is created and Q is also
created for this. Now if I go to the u
exchange uh uh and try to create one uh
route request message. I will open uh
the this exchange and you will go to uh
to the publish message section. This is
the publish message section. So uh now
uh I will add uh the header and uh this
is the type you will use the same uh
contract and this will be the payload
for the message. So you can see here we
are defining the source uh and we are
destination uh when to start this is the
user id this is just a test message okay
so when I publish you can see the
message is published and if I go to the
to the result you can see here received
uh route request JSON so they they are
processing the uh passenger information
and they are successfully serializing
and uh you can see here um if I go to
the queue um and this is the respective
queue for the route request message. So
you can see here it is uh they are
processing the message and u if I go to
the database
uh here is the request.
So you can see here this is the request.
So uh this is uh about the how the
rabbit MQ communication will work. So
now we will text one uh test one uh uh
route of message as well. So you will
open the the specific uh route offer
message exchange. You can see here uh
you will go to the to the publish
message. So I already created one JSON
for this. So um this is the JSON uh New
York like longitude latitude and here is
uh like the when to uh this is when and
this is the user and uh this is the
driver information. Uh John is the
driver. So uh once I publish you can see
this is published and when I go to the
queue you can see here uh this I um this
is the um attach Q to the exchange. Uh
so you can see here uh this is the
exchange for it and it will handle the
offer. And if I go to the log of the
file log of the application you can see
here uh
uh this is the message uh coming from
the driver side for uh the offer. You
can see here this is the offer and u uh
you can also check in the table the
offer table as well. uh you will select
you can see here this is the offer this
is the path information path this is the
when uh the time uh and this is the
driver and uh information user ID and
this is the status so uh I already I
show the uh request and offer uh
practically how it will be handled uh by
using the rabbit MQ let's discuss the
some basic of the message broker as we
are using a rabbit MQ so if you are new
to rabbit MQ so this will be the basic
uh information related to the Rabbit MQ.
So Revit MQ is nothing but a message
broker, robust message broker actually.
uh it support the MQ AMQP protocol and
uh it offer a lot of u tools like for
scalability, redundancy and disaster uh
recovery and uh for um uh for a message
format u like um as we already saw in
the practical u microservices uh routing
microservices we saw that we installed
the easy uh net q library for messaging
like it decrypt uh serialized
decentralized messages and uh as you can
see here in the rabbit MQ this is the we
have two connection and the protocol
it's using the AMQP so it's using the MQ
protocol uh according to this um rabbit
MQ is b message protocol that natively
support the AMQP protocol and u uh you
can see here uh so uh there is two
concept one in skew and uh and exchange
as you can see here we have uh exchanges
uh whenever we uh run the application uh
or API application the routing u
microser this uh exchange is created. So
um this is the exchanges and this is the
cues and each exchange if you open for
example it's a uh route request message.
Okay. And as you can see here uh this
exchange is bind with this Q. So this is
the Q uh this is the binding between the
exchanges and the Q. If you click it
will redirect you to the queue. So u
let's back to the slide. Uh so uh Q
where messages are stored. So what is Q?
Actually the messages is stored in the
queue and what is exchange uh route
messages to one or more queue based on
their type. Okay. So each uh Q have
their own type and exchange are and MPQ
concept that help implement complex
communication protocol. So uh as you can
see here uh this is the exchange and
this is the type. We will also discuss
uh the exchange type. This is the
exchange type direct topic and fan out.
So and the other one is the header and
uh this is the Q you can see here this
is a type of the queue this is a classic
we are using a classic Q type. So uh uh
I hope you will understand like q uh
where messages are stored q messages are
stored in the queue and the exchange
basically u route messages to one or
more queue based on their type and uh so
this is the diagram message messages
come uh to the exchange and exchange u
further uh redirect the message to the
queue based on their type and basically
you can see here the basic functionality
of the exchange is the routing uh they
have the routing strategy and u this is
uh the type of exchange uh we have
default default exchange uh fan out and
topic. As you can see here when I go to
the exchange uh we have topic and uh we
have default default uh is not there and
uh we have a topic uh topic exchange
topic is uh is there. So uh default
exchange send message to a single queue
enable direct communicate uh
asynchronous call pattern and uh about
the fan out send message to all Q all
the subscriber Q uh and enable publisher
subscriber pattern. So this is the main
difference uh default exchange uh work
on based on the asynchronous scalp
patron and in the case of fan out uh
they have publisher subscriber pattern.
So you can see here uh this is the fan
out uh this is the default uh sorry uh
when you run the MQ container it will
create in default. So this is our own
this four uh 1 2 3 4 5 this five is our
own created exchanges
and uh topic uh what is topic and all of
our exchanges is of topic uh so what is
topic and advanc out exchange that allow
for message filter based on named topic
so we have u it work on the based on the
named topic welcome back to practical
serverless and microservices with car my
name is Mouhammad Abdullah and I am a
Microsoft certified Azure solution
architect. In this session uh we will
discuss Kubernetes but it uh this
session will contain uh different parts.
So uh in this first part we will uh see
uh the orchestrator. What is
orchestrator? We will understand their
services and we will also understand the
YAML. So YAML is the basic basic thing
to work with Kubernetes or orchestrator.
So u if you want to understand the files
that is needed for Kubernetes so you
should know about the YAML syntax how to
write the YAML file and their rules and
regulations. So for this uh part we have
uh this learning agenda to understand
the orchestrator what is orchestrator
and why they need it and their
fundamental services and uh the basic
sentence of YAML is orchestrator. So
architecture are system designed to
manage and coordinate containerized
application across multiple uh servers.
So um in microservices we have different
uh containerized application and uh one
service uh server have different
containers. So orchestrator is needed to
manage those containers across different
servers. So uh orchestrator provide you
load balancing and u between the
different microservices replica and
orchestrator also automatically uh scale
up and scale down um and the third point
is service discovery and networking. So
this is the main uh feature of
orchestrator. So whenever um
orchestrator come in your mind you can
think about the cubernetes like
kuberneti is orchestrator. So Kubernetes
is nothing but orchestrator that do the
load balancing, autoscaling and service
discovery and networking and also
provide you the health monitoring. Uh
you can check u health of different
microservices and based on that you can
take any decision and uh the fifth point
is they also provide you storage. So
this is the main benefit of using uh
kubernetes or orchestrator. So uh
question uh why we need orchestrator. So
uh if you don't use orchestrator or
kubernetes so you will do uh this thing
manually for example u decide where to
place each uh container replica you will
do this manually you will handle the
load balancing manually between the
replicas and uh monitor container health
you will do this thing uh manually and
whenever any container fail so u you
will start it manually and manage
network communication uh between the
services and uh uh storage allocation.
So this is all the thing uh which you
will do manually if you don't use the
orchestrator. So uh using orchestrator
this thing you can automate with
kubernetes or orchestrator. So uh the
next thing uh is yl. So uh what is yl?
It's a human readable file uh that work
with cub kubernetes or orchestrator. Uh
it commonly used for configuring file
and uh exchange data. So this will be
the definition of uh uh the container.
So now we will see uh one example. So I
will go uh to the uh VS code. So I just
open the VS code. There is nothing but
um uh empty project. So uh first thing
uh we will discuss uh I will show you
one uh JSON file. Okay. So uh JSON file
um with person definition. For example
uh I will create one JSON file
person.json
and um this will be the definition of
this JSON file.
So you can see here this is you will uh
you you already familiar with this u
JSON file uh it's well known uh so you
can see here uh it's name name is John
surname health and it's basically a key
value um combination
and uh here is the list uh you can see
here you can add also a list and another
uh property is related to the addresses
that this is the array that have two
addresses type home and the other item
is of type office. So this is uh one
example. So I will uh show you the
alternate of this JSON file which will
be the uh YAML file person file. So
first I will add one per
person
YAML. You can see here you will write
this extension YL. Okay. Uh and uh I
will show the exact key value pair but
in a YAML file. You can see here uh it's
the same JSON file but it's a YAML. So
uh this is the again it's key value
pairs uh so this is the key this is the
value surname Smith this is the spouse
uh and uh this is the name Mary surname
Smith and this is the address. So this
is basically a definition of YAML file.
Okay. So u to write this file you will
follow some rules. Okay. So the first
rule you will not use a tab but you will
use space. For example, between this key
value pair, I am using um not the tab.
If you use tab, uh it will be a mistake.
Uh so what you will use? You will use uh
spaces. Okay. So this is the first rule.
Uh the second rule uh is you can see
this is a hyphen. So uh it represent
list item. So uh this hyphen you can say
this is a hyphen. Uh you can also add
one another item. Go back and uh you can
see here this is another item. Okay. So
uh what does this represent? it
represent a list. Okay. So, uh this is
the list and this is the second second
rule and uh the third rule is for uh
this column you can see here this is the
column. This is the column. So, after
this column uh you will give one space.
If you do like this for example straight
and without there is if there is no
space it will be a mistake. Okay. So,
after uh this um uh column you will use
a space. So this is a rule and u the
other uh rule regulation is that for
command you can use this hash. So if I
write any comment you can say this is a
command and another command. So this is
the other um other syntax rule and u uh
okay so this is the basic um uh rules
for uh writing a JSON file. So um next I
will show you one um one other JSON file
uh with two spaces uh uh for consistency
purpose. So you can follow the same uh
rule for uh when you write the Yammer
file. So I will write person
person two person two. Okay. So you can
see here I added one other file or I can
also name some other service uh user
service. For example, this is my app
name. So I give user service and u this
is the definition of this YAML file. So
you can see here uh this is the API
version is V1. Uh this is upload. we
will discuss different type of uh
services. Uh it's a prototype and it's a
meta and it's a list and uh it's again
it's a name and a label and
specification. So this is the u one a
good consistent uh identation with two
spaces.
So uh the another point is that you can
also uh do the inline uh syntax. You can
also use the inline syntax as well.
Okay. So what if I add other user
service user service inline
uh inline inline okay so this will be
the inline so you can see here um it's a
inline uh so uh there is a difference
for example um if you see the meta uh it
show my pro and label so uh in this case
you can see here it's a it's a label and
uh that it has a app and version so it's
the example of the inline syntax you can
also move this to uh this file as well.
You can say here.
Okay. So this is the alternate this is
the alternate approach uh of this. You
can write this way as well and you can
also follow follow the inline as well.
So this is the two example and uh the
other point is that now I will write one
complete u example that will show a
person information. So uh I will create
one other person complete person profile
for example. EML.
Okay. Person profile EML. So um so I
what I will add I will add some basic
information related to the uh related to
the person uh and uh some uh their
skills and uh and some other information
like their what project they completed
something like that just to show you how
you can write the YML file. So you can
see here this is a person and uh it's
name age and uh is different properties
and it's a contact information uh this
is the contact information their skills
and uh it's the project information. So
this is one example. Okay. So next um
you can also uh convert like if you have
a JSON file uh you can also convert uh
that to the YAML file as well. you have
also YAML and uh
uh okay so uh one other point that you
can also define multi- multi-document
YAML file so what is the multi-document
uh YAML file so I will add one another
multi-doc
okay so multi-doc document so first I
will add one uh document the first
document so you can say application
configuration version one kind uh
configur config uh map so this is uh
database uh data related URL. So um the
other uh document for other document I
will use this. So for example this is
the um multi-doc
multi-docl
file. So this is the second document
again version uh same information
metadata and uh uh I will uh this is the
multi- document YAML file. So you can
also define different document in a
single YAML file.
Okay. So uh
okay so this is all about the YAML file.
Uh so again uh if you there is some few
mistake if you don't give any space it
will give you error and I will tell you
one other uh I will show you one other
application. So you can see here this
will you can use this application uh you
can paste your YL and when you click go
uh it will correct u your uh YL file um
it will show you the invalid data uh
invalid corrector in your EML file. So
for example if I have this CML file you
will paste if I don't
okay so you can say I remove the space
and it show the um it show the error a
key needed to be uh single line at line
two and so you can use this application
you can also use the another uh
application online parser fspot.com you
can also use this one as well so uh this
is uh the mistake uh many people make so
Okay.
Okay. Okay. Again, if you uh make any
spelling mistake, they will also not uh
compile. Uh so if you make any spelling
mistake, this will be another uh issue
for you. So uh the spelling mistake,
they also check for the spelling mistake
as well. So this is all about the YAML.
So this is done. Okay. So uh next uh in
the next part we will discuss mini cube
for local Kubernetes development. we
will learn basic Kubernetes commands and
we will write first Kubernetes board and
uh explore uh the Kubernetes cluster in
architecture. So uh this is all about
the part one. See you in the next part.
Hello everyone. Welcome back to
practical serverless and microservices
with C#. My name is Mohammad Abdullah
and I'm Microsoft certified as your
solution architect. This is session
number 10 and we are covering Kubernetes
and this is the part number two. In this
part number two, we will discuss
Kubernetes uh basics and uh we will set
up a cluster. If you missed the first
part of the Kubernetes session, so
please check out the first session then
continue this uh part number two. Okay.
Okay. So learn learning objective for
this part uh you will understand the
Kubernetes their architecture the core
component and uh after that we will
install the mini cube for Kubernetes uh
local development and u we will use the
cubed ctl to interact with kubernetes
cluster and uh we will try to understand
nodes master node and uh cluster
concepts and perform basic cluster
operation and troubleshooting. So we
will cover this uh five thing in this uh
part. So for this uh part number two uh
please check out the first part uh where
I uh show the EAML uh fundamentals. So
for this part it is uh pre uh
prerequisite to watch the part number
one and you should also install the
docker desktop and u you will also need
administrative access to install
softwares. So this is the prerequisites.
Now what is Kubernetes? So like the
orchestrator uh kuberneti is a
distributed orchestration system that
manage containerized application across
multiple machine node. So uh same thing
uh like orchestrator or orchestrator
also manage uh containerized application
across different uh server or node. So u
behind the uh scene of the kubernet
there is orchestrator. So Kubernetes is
nothing but orchestrator. Okay. So uh
they uh Kubernetes have different uh
component like API server and uh that is
the central management point that handle
uh the rest operation. Uh here is the
diagrams you can see here. Uh at the top
we have a master node uh that have three
component. Uh the first one is the API
server uh which is the central uh
management point held on all the rest
operation and the second one is the
scheduleuler uuler decide which node
should run uh each board and the other
one is the uh controller manager monitor
which monitor cluster state and make uh
correction. So this is the three main uh
component of the master node. So uh
apart from that we have the etc uh which
is the key value storage for a cluster
data. So uh next is the worker node. Uh
so u below we have a worker node and the
worker node have three uh components uh
which the first one is the cubectl and
uh that is the agent that communicate
with master and manage your container
and the other part is the container
runtime that provide runtime to the
container and uh the third uh component
is the cube proxy handle network uh
routing and load balancing. So cube uh
proxy is responsible for the routing and
uh load balancing. So uh I have one uh
diagram so I want to show you can see
here uh this is the one diagram for the
byite by go u so uh I just copy this
diagram to explain. So uh we have a
kuberneti uh that run the application
and u bind the dependency then they
provide uh docker file we write a docker
file uh and we build image from that uh
docker file and uh once we have a image
then we run uh that image as a
container. So uh you can see here this
is the part of the Kubernetes and
Kubernetes we have a master node and
master node have a API server and um uh
scheduler and controller manager and uh
worker node we have a worker node that
have a cubectl uh and uh uh container
runtime. So this is some graphical
representation of the uh of the uh
Kubernetes.
Okay.
Okay. So um okay now um Kubernetes have
different type of resources. For
example, we have a port um which is the
smallest smallest deployment unit and uh
port can have one or more containers and
the second is we have a server uh
provide stable network access to the
prod and uh the third one is the
deployment manage product replica and
updates and the fourth one is the name
spaces virtual cluster for resource
isolation. So we will see uh all these
uh codes uh uh services deployment and
name spaces we will create this
practically. So uh next is so you will
need uh u to install the mini cube first
because we will use uh mini cube for
local kubernetes environment and um you
have you will run this command choco
install mini cube and uh you can also
install mini cube from this page as
well. Um this is official mini cube
page. So this is the mini cube start you
have um they provide Linux Mac and u
Windows uh and uh I have Windows so I
will run this command choco install. So
uh if you um first you will install the
mini cube. So first of all u you will
open the uh command prompt as a uh as a
administrator. Uh so I will run this as
a administrator. So you can see here um
this is the command you can use
powershell or other terminal uh of your
choice. So this is the uh choco install.
So uh I already installed so uh that's
why uh it will give you uh this type of
information. Uh but if it's not
installed uh you can say it already say
that uh already installed. So if you uh
if you have no uh not installed the mini
cube so you will run this uh command. So
this is the first command and uh after
that
uh you will install uh cubectl. Okay,
cubectl is the primary tool for
interacting with cubernetes. So you will
also need uh this uh command to run uh
in your local system.
So uh let me correct.
Okay. So I will write this command
choo install cuber
netis cli.
So it's I already installed but I want
to show the command which what command
you will learn? So uh it's already
installed. How I will verify uh cube ctl
cubectl version. Uh if you want to check
the version you will run this command uh
cube cll
version
blind. So you can see here client
version is v1 um 31.1 and uh customized
version is v5.7.1.
So this is the install version uh of the
cubectl. So uh next uh we have some uh
command like basic mini cube command. So
you will learn this basic cubectl
command. For example, if you want to
start the mini cube uh with default
setting. So you can uh run this mini
cube uh start and if you want to run the
mini cube with specific configuration.
So you can uh you can use the the mini
cube start CPU, you can mention CPU uh
size, memory size and disk size. So this
is the specific u uh configuration. So
when you run this command, it will
create this resources for you. And if
you want to run the docker with uh um if
you want to run the mini cube with
docker so you can run the last command
mini cube start driver uh equal to
docker. So I will use this command uh
because uh I already installed a docker
hub. So it will be uh fine to run this
command in my local system. So u this is
the other if you want to check the
status you will uh you can u write mini
cube status. If you want to stop and you
can stop. Similarly, you can delete,
pause and unpause the mini cube. So, uh
let's check this um command one by one.
Okay. So, you can see here um so first
of all uh you will uh I will try the
last my mini cube. I will use the docker
one. Uh so um I will write mini uh cube
start
start driver equal to docker. So I will
run this command. Once you run this
command uh it will run the mini cube. So
I already have one instance uh one
cluster. For example, if I check a
status uh it will show I already have uh
one running instance. So uh if u you can
check the status if there is no uh um uh
cluster u then you can run the um run
the mini cube. So you can see here when
you write the mini cube status it will
show the type host uh cubectl planning
API version and cubectl config. So um uh
this is the first step you will run the
mini cube which is currently running in
my local. So uh next point is that
uh
so uh you can how you will check the
cluster information. Um you can also
check the running instance in uh in
docker hub as well. When once when you
open the docker hub you will see the
container. You can see here this is this
container is currently running in your
local uh docker. So uh it is currently
running. So uh I will show some other
cluster.
get nodes.
So you can see here uh it will show all
the node uh one currently one node is
running and u uh other command cube uh
ctl get all it will give all the uh all
the services. So with the type cluster
IP and uh this is all the information
about the node and u you can also get
information about the name spaces. If
you want to know about the name spaces
uh you can also get this information as
well. So cube ctl uh get all and here
you will mention all uh name space
sa
cs name. So you can see here this is all
the name space information. So um this
is all the name spaces we have. So um
how you will work with the name spaces?
So u cube
cubectl uh get name spaces
you can say here we have this u name
spaces cube node less public system and
dashboard. We have this uh name spaces
and uh you can also create uh name
spaces as well. For example, cube ctl uh
create name space
create name space and then you will give
name to your name space. So you can see
here uh name space is created. Uh if you
get again the name spaces you can see
here u uh your namespace is created my
name. So this is how you will create you
can create uh your name space and u you
can also uh get the current context. So
how you will get the current context? Uh
cube ctl cubectl uh config
current
context
cubectl config spelling mistake uh
cubectl current current context.
So this is the current context. We are
working in the mini cube context and you
can also uh wheel the cubectl uh
configuration. So how you will uh cube
ctl uh then config.
So this is all the information uh
regarding the config. So uh this is all
about the name spaces. Uh
next we will how you will explore your
cluster. So there is few command we will
explore those command. For example, if
you want to uh check the cluster
information cube ctl uh then uh cluster
cluster info. So here is the cluster
information. Next uh uh if you want to
list all the nodes. So cube ctl uh ctl
then uh get node nodes dash o uh then
wide. So it will give all the all the
all the nodes. So you can see here uh
mini cube uh the status is ready and
this is all the information.
Okay. So uh next uh we will uh uh you
can I already show how you can create
the name space you can get get the name
spaces and uh so here we'll discuss one
thing about the context. What is
context? Uh so context is the
combination of the cluster user and name
spaces. So these three uh combine make
the uh context. So um this is about the
cluster. Next uh uh we will create uh uh
we will create first uh Kubernetes
resource. So for this uh we I will go to
the VS code. So this is my VS code. So
here I will create one resource my first
resource uh cub first Kubernetes
resource. So how I will create? So uh
first of all I will create one file uh
with this name and that file will be the
YAML. uh if you are new to YAML you can
check out the first part of Kubernetes
uh session 10. So this is the first port
and uh here uh is the definition of uh
this file. So uh this is the definition
of this file API version one. Uh this is
the port. Uh I already discussed that uh
Kubernetes have board name spaces and
other resources. So this is the port
resource and port is the smallest
deployment deployable unit. Okay. So
here is the metadata. Hello world name
space default we are using the default
name space.
Okay. Uh and then uh we have a label uh
container uh information like um
container name and this is the image u
ingress image we are using and this is
the port number. So uh this is the first
uh um how you will create your first uh
kubernet resource and for this um how
you will like deploy and manage this
part. So for this I will open the
terminal. So this is my terminal and uh
here I will write my first command to
deploy and manage this resource. So uh
first of all uh I will apply apply the
configuration. So for this I will run
cube ctl ctl and um apply apply f and uh
then first you will give name first uh
name of the file you can say first
board. So this is my file name and
extension. So this will apply the
configuration qtl apply f and the name
of your uh board. So you can say here uh
it say that it is created. So this is
the first thing uh it is created. Okay.
Uh next uh we will check if there is any
port running. So for this I will run u
cubectl get port command. Cube ctl uh
get ports ports. It will give all the
port. You can see here it's showing
hello world which I recently uh created.
You can see here uh this is the name.
The name is the hello world and it's u
um currently uh uh showing uh ready 0-1
and uh um it is showing status the
container is created but it's not
running. So u now uh I uh how I will get
the detail information. So for this you
will uh you can use this command uh cube
ctl uh describe
describe uh describe port and hello port
name of the port hello world you can say
here uh we have one port with name hello
world and I'm giving the same name u to
get uh the detail information so this is
the detail information of my uh port. So
you can say here this is the detail uh
information uh it's showing uh reason
age from and uh the message. Okay. So uh
after that after that uh you can also
check the logs uh of your code as well.
So there is a command cube cl get logs
uh logs logs and the name of the port
hello world.
Hello world. You can see here this is
all the logs showing.
Okay. So let's check the
okay so this is all about the cubectl uh
created first we created the first port
and uh it's running and uh you can also
um there is also a command to delete the
port. So you can also delete as well. Q
ctl
ctl uh delete
delete port and name of the port hello
world.
So uh port deleted. Now if you uh check
the ports again there is nothing because
we deleted and uh
so uh this is all about uh all about the
kuberneti uh resources how you can
create the resources.
Okay. So also we have a uh you can also
check the dashboard of the mini cube as
well. Mini cube dashboard.
So you can see here when you run this
command it also it will al display the
uh dashboard. So welcome back to
practical serverless and microservices
with C#. My name is Muhammad Abdullah
and I'm Azure certified Azure solution
architect. Uh this is session number 10
uh about Kubernetes. And this is the
part number three. Uh in part number
three we will uh see the board and basic
uh configuration. If you missed the part
number one and two uh of session 10
kubernetes. So please check out the
first and second uh part. So for this uh
we will uh understand uh port anatomy
and their specification. We will create
uh port with multiple containers and uh
we will uh see uh how to implement init
container and sidecar uh pattern and uh
we will also see how you can configure
environment variable and uh uh command
or write and we will also see how you
can share um volume between uh different
uh containers and at the end we will see
how you can debug and troubleshoot uh
issues. So uh first of all uh you will
check out the first and second uh part
of this series and u uh the other
requirement uh you should be able uh to
u uh the mini cube should be running in
your uh system as well as the cubectl
also configured and working. So these
three is the requirement to continue
this part.
So uh first of all um we will understand
what is port uh what is u mean by p in
kuberneti environment. So port is a
smallest deployment unit in Kubernetes
uh that uh like share network APIs if u
two container share same API same port
and share some storage and lifestyle are
create destroy together. So this is
called port. So um uh this is the
definition of the port that when some
container u have share network storage
and life cycle like u if there is two
container that uh should be created um
at once or should created destroyed
together. So this is called port. This
is the deployment unit in kubernetes. So
uh why we uh use multiple container in
uh port um
when you have a site pattern uh like a
site partner uh pattern help container
enhance main uh container we will see
this in action. For example, if one
container try to log um information
error messages or other information for
the main uh container. So in this case
we will implement the side car pattern.
And uh the other one is the ambassador
pattern like it used for proxy container
that handle external uh connections. And
the third one is the adopter uh
container transfer data from main
container to another container. So this
is some three pattern we can use in
Kubernetes.
Okay. So uh now uh I will move to uh VS
code and I will show you the basic u
basic port uh ZAML structure. So let's
go to the VS code. So the first
requirement uh is that uh the mini cube
should running uh in uh your local
system. So I will verify mini cube
status. So this is the command uh where
you can use to check the status of the
uh mini cube. So you can see here u it's
currently up and running and you can
also check the uh cubectl as well. So
qctl is also uh installed and uh
currently configured. So this is the two
requirement uh before start. So now uh I
will show you uh the basic pod uh zaml
structure. So for this I will create one
uh uh zaml file. I will give name basic
portl.
Okay. So this is the basic portl. So
this will be the basic port for Okay. So
you can see here uh we have this is the
basic information uh about the port uh
kind is port metadata information and uh
this is the container specification. Um
this is the restart policy you can use
always on failure and never whenever any
container fail u you can um restart the
container by giving this option by using
this option and uh this is the uh image
name. So it will pull the image from any
uh image registry and uh if you have any
init container uh that will run before
uh the main container. So you can also
define and uh this is the volume uh if
two u container share uh same volume uh
same storage. So u this is called port.
So uh they have some uh main volume
storage as well. So this is the basic uh
version of the uh port. So next uh uh
next we will see uh board with
environment variable. So how it will
look like? Um I will create simple port
env. So environment variable or this is
the extension. So this is the simple
port with environment variable. So this
will be the definition of the simple
port. Okay. So you can see here uh I
will change the name uh to uh
simple port.
I will remove the env.
Okay. So this is the port and this the
definition uh some uh definition uh API
version basic definition and uh this is
the uh container um specification.
So uh there is one container and uh it
will take image from al latest Linux al
latest and they will use this B uh
command and this is the argument list
okay uh echo uh it will write this
message for us uh hello from my name and
this will show a de after 5 seconds it
will print this message and uh this is
the variable so the main thing is here
is that I'm passing environment variable
so uh this This is the list of the
environment variable I am passing. So
the first variable is uh the static
variable uh hardcoded variable. So this
one is the hard-coded variable and uh uh
this environment list have three uh
items one and this is my for one for
name the other one for namespace and the
third one for the code name. So the
first one is the hardcoded and the
second one is uh used for name space and
you can see here I'm using value from
and reference field reference. So um it
means that the cubernet automatically
inject value from port owner uh metadata
when the port start when port start
kubernetes automatically assign uh name
for this uh the default name space okay
on which the port is running and the
third point uh the third variable uh is
same uh similarly it will be
automatically assigned and this will be
the port name. So this is how you can uh
define a port with uh environment
variable. Some is the hardcoded and the
other variable is u from uh
automatically assign kuberneti
automatically assign. So uh we will uh
like first deploy this u we will apply
we will run this command cubectl apply
and uh this will u this will create the
port and u once it's u created we can
run another command to check uh the port
cube ctl get ports. So you can see here
one port is uh running currently in
running state and this is the name env.
Okay. Uh and uh next uh is um you can
also view the log as well. Uh you can
you can check the log as well. Um so I
will run cubectl get log. This is the
log. You can see here it will log the uh
it's logging the uh information.
Uh hello this is a message and this is
the date. Okay. If you want realtime log
so you can uh you can uh use this u uh
this command as well. cube ctl and again
logs and uh fenv
uh port name. So this will be the port
name. So it will show you a real time
log. So uh you can see here it's uh
printing the message. Okay. Um this is
all about the uh simple port with
environment variable. Okay. So next uh
the final step I will clean up this
board uh for the next example. So this
is deleted from the default n space.
Okay. So uh next uh we will discuss uh
the site pattern. Okay. So uh site
pattern for example we will use site
pattern that will log for example the
shipping information uh for the main uh
application container. So how I will
implement this? So first uh I will
create one file with the site portl. So
this will be the name of the port. Uh I
will create one new file. So sitecar
portl.
So this will be a new file for sidecar
pattern. So we'll see how it work. Let
me clear the terminal now.
Okay. So uh this is the definition of
the site card port. So here again this
is the um basic information version and
u board. What type of uh resource this
is? This is a board and because this
have a container and uh one container or
many container and that share the same
uh volume. So this is called board and
it's a label uh label side code example
and here you can see um this ML file
this port have two uh uh containers two
containers. So this is the list that
have two container. One is the main
application container and the other one
is the sidecar container that will log
uh the logs uh or that will collect or
process the log. Uh okay. So you can see
uh I will define I will explain uh this
is the main container name. This is uh
it will use this image alfane latest
image from the Linux. They will get that
image and this is the argument.
Okay. So you can see here u it will like
uh print uh um print this log
information uh like with time stamp
after uh every 5 seconds it will uh it
will print this message and uh uh you
can see here volume mouth. So uh uh in
the command argument uh it will create
it will log log the information to this
location. You can see here uh they will
create this directory warlock log
app.log file. So um this will log this
information and this is the mount
information. Uh again this is the share
uh name of the volume is the share log
and the path this is the path of the log
file and the sidecar container name
image same they are using the same
images and this is the definition like
it will read all the log and print it
will print the updates from this file.
So this is the path of the file. Okay
again you can see here they're using the
same uh volume. So uh this is the
definition of the sidecar and uh this uh
thing will create one empty directory
with share log and this directory will
use by both the container. So this is
the definition of the sidecard port.
Okay. So uh again uh I will uh deploy
this board uh I will deploy. So qctl
apply uh deploy and it's created. Once
it's created um I will check both
container are running or not. So how I
will get uh I will run this uh this um
uh this command uh for example cube ctl
uh and get port. It will give all the
port and uh I will uh give a name of the
port side side car dash demo. Okay. So
it will give um information. So you can
see here sidecar um running u the state
is uh running and it has two container
two out uh two out of two is uh running
and uh next uh you can check logs as
well. Okay. So this is the log
information u from main container and uh
okay. So for example I will you can also
you can see here this is the log
information uh shipper and u uh
followite core logs. This is for follow
site log logs is this is the site they
are printing uh from uh the main uh
container. You can see here whenever
entry comes to the site uh the container
it also print the same uh uh thing.
Okay. So um
so this is all about the sidecar
container.
Okay. So uh this is the information
related execute command specific
container. So uh this is the execute
command uh related to specific uh
container and uh okay. So this is all
about the uh side how you can implement
the sidecar container and u again at the
end I will delete and destroy.
Okay. So this is deleted.
Okay.
Okay. So uh next thing uh we will
discuss the init container. What is init
container? Okay. So init container run
before the main container started. When
when the it run before the main
container started. So this is the main
concept and must complete before main
container started. So this is the uh
this is the requirement for that and uh
I will show you this practically with
example. So again I will create one uh
let's close this file. I will create new
file init container board example. Um
and uh I will put some uh definition for
this ML.
Okay. So this is the definition. Now I
hope you will understand uh the ML file
definition. This is the basic
configuration basic uh definition and uh
this is the container specification. Uh
and you can see here this is init
command uh first init container uh that
will create u like a directory
structure. So uh for the for main uh
what why we need this uh like for
example uh the first container will uh
set up the directory. So this will be
responsible to set up the directory and
the second container will download the
configuration and the third one uh is
the main that will read and print uh the
created u uh information. So uh this is
all about this will create the directory
this will download and this is the our
main application and again they are
sharing the same uh volume that's why
it's old and u uh uh this is the they
are using all three is using image pull
image from the alin latest and u uh this
is the name directory the download and
uh it will make uh this argument show
that it will create a directory like for
one is uh for the config the other one
is for data uh and um uh they will also
create one file as well with setup.log.
So this container will set up uh the
main directory setup for uh the main
application. The second one is the uh
init container uh init container that
will download uh the configuration uh
for example uh this is a demo. So this
is for now this is just the uh database
URL. So uh in actual
application you can download the
configuration from any other source but
for now this is the current u u dummy a
dummy URL for database uh uh database
URL and uh yes uh it will u this will
they will use this file doconfig they
will go there and uh they will put this
uh configuration and uh they will
download and they will set up the uh log
and uh this is the main application the
main application uh will start and uh uh
they will do uh they will like print
this message eco start with a time. So
this is all about the init container. Uh
let's do some uh commands. So the first
command uh is uh for this I will apply
to create the container. Uh I will
apply. So uh demo in it created port is
created. Now you can see uh there is one
new command uh to watch the port uh uh
information. So you can see here you can
write this you can when you uh put this
watch it will show the uh information.
So they are showing that application
started directory uh created and uh
configuration also downloaded and and
this is the information they are
configuring. Okay. So um this is all
about the init uh init container.
Welcome to practical serverless in
microservices with Sishar. My name is
Muhammad Abdullah and I am a Microsoft
certified Azure solution architect. This
is session number 10 Kubernetes and this
is the part number four uh where we will
discuss the storage and volume. So if
you miss the first, second, third part
of Kubernetes session. So please first
check out uh the first, second and third
then you can continue this uh part. So
for a learning objectives for this part
is to understand the difference uh
different type of kubernetes volume and
uh we will discuss the empty diir uh
which is is the temporary storage then
we will create the persistent volume and
persistent volume claim and we will also
work with the storage classes for
dynamic versioning and share data
between container and board and uh at
the last we'll implement data persisting
strategy. Okay. So, uh for prerequisites
you can watch the session number one to
three uh of this uh Kubernetes series.
Then u you have enough space in your
local machine as well as you should also
install the mini cube and um some basic
uh understanding of the port. Okay. So
let's get started.
Okay. So uh first uh we will discuss uh
uh do why do we need persistent storage
in Kubernetes? Why we need uh storage?
So the first reason is that the
container are imperil like data is lost
when container is start. So uh container
does not hold or save the data. So it's
like a volatile when it's restart uh the
data is lost. So uh port can be uh moved
between the from one node to another and
uh the third point is that application
need to persist data beyond container
life cycle and multiple container may
need to share uh data that's why we need
the persistent storage and uh we have uh
like uh different type of uh persistent
storage uh for example we have we have
different type of storage in kubernetes
like we have a temporary storage uh like
we use the mtir um for a single node
then we have a persistent storage which
uh is a persistent volume with various
various uh back ends and u if you have
to store the secrets we use the
configuration secret and projected
storage combine multiple sources so uh
the first storage we'll discuss is uh
the mtdir volume which is the temporary
storage and u some information about the
diir um when you create a board uh it
automatically assign to a node this
volume is automatically assigned when
you create a port. When you delete it,
remove from the board and share between
all the container in if you have a many
container in a port this storage will be
shared for all the containers and u
storage uh stored on a node local uh uh
store. So it when it's restart or u
deleted the data will be gone. So uh for
a first uh exercise practical exercise
uh we will create uh empty diir uh demo
uh where we will see how uh this work.
Okay. So first of all uh I will create
one file uh mtdir demo. So this is the
file and uh next I will add some code
related to uh mtd. So you can see here
this is the code and uh again this is
the basic information basic um uh
configuration um basic um like we have a
port metadata name uh label application
and this the uh application uh container
specification. So uh as you can see here
we have two containers. So it's a
multicontainer port. Okay. And u we have
one share volume. You can see here name
of this volume is shared storage and you
can see here this is the property we use
to create empty storage. Okay. So the
first container is using a lightweight
Linux image uh linen latest and the name
of the container is write and uh what
does this container do? Uh it's um it
print message. Okay. After uh 10 second
uh delay they print one message and uh
to which file uh they are using this
file. Uh this file you can see here in
this file they will uh print this
message and uh uh this is the storage
and this container will act as a writer
like it will uh it's like a producer
producer and the second container which
is a writer uh that will act as a
consumer. This is a consumer. Again the
name is writer uh and reader uh is the
name and this is the image. Again uh it
will read all the data from this file.
So the one is writing the other one is
reading. So uh nothing else and uh you
can say here uh this is the volume and
uh this is the main point we are
focusing in this video is the empty
diir. So about the MTGIR
um when the board is started this um
temporary share directory is
automatically created and um it's exist
on the node file system and um it shared
by all the container in the same port.
For example u we have a
okay so you can see here we have two
container and this volume is shared by
these two reader and writer uh
container. So this is all about uh the
all about the uh intir and uh when the
port is deleted or restarted the data is
lost. So this is the characteristic of
this uh uh of this port. So now I will
uh deploy I will create by applying this
command. First of all I will write cube
ctl uh then apply
f and uh empty diir-
demo. demo. Uh remember that mtdir demo
is the name of this uh pod and uh when I
run uh it will create the pod and uh if
you want to get the running container so
you can use this command again cube ctl
and get ports and uh again the name of
the
board demo. So you can see here in this
uh uh board we have two container and
two out of two is running and status is
running and uh the restart time is zero
and the age is 26 seconds. Okay. So uh
this is how you can check the container
status and u u now I will write one
command u I will check the logs uh
inside uh the writer ctl
logs
empty diir demo and um reader
okay so you can see here this is the
logs of the reader and uh it's reading
data from the file okay so next you can
also uh specify the size for the empty
directory as well like for example uh
you can for example if you want to one
gabyte of space. So how you will do this
uh you will just change uh this empty uh
to this one for example a name is a
limited uh storage and again d mtdir and
the size limit is 1 gabyte. So you can
also uh do like this as well. And uh
similarly
uh you can also uh specify that I need
space in RAM not in the not in the disk.
So for this you can uh do like this uh
you can um again you can use the MTDIR
and medium and now the the memory will
be uh in memory like it will uh store uh
data in RAM instead of the disk. So this
is all about the NPDIR. So uh now we
will discuss the uh we will discuss the
persistent volume and persistent volume
claims. So this is the two model uh we
will discuss. So uh first we will
understand the PV and PVC model. Uh like
first one is the persistent volume
cluster wide storage uh resources. It
used for cluster wide storage resources
and persistent volume. Claim a request
for storage by a port and storage class
define a type of storage available and
uh this is different type of access mode
like a single node can mount read write
in case of uh RW and the other one is
the rox read only write um read write
many and read write once board started.
So this is the excess mood and uh now uh
we will create one other uh practical
example uh with manual PV persistent
volume and persistent volume uh claim.
So first here I will create one uh new
file with manual PV- PVC persistent
volume claim.l. So this is the file. Now
I will uh put some code.
Okay. So, so this is the definition of
the file. First one is the persistent
volume. U basic information and the
specification capacity uh 1 GB and uh
access mode. You can see here uh I
recently um discussed this access mode.
You can see here this is the list of the
access mode and uh this is the read
write once mode and uh this is the uh
policy for uh retention persistent
volume uh reclaim policy and uh name of
the class is manual. This is the path
and this is the persistent volume and
the second one is the persistent volume
uh clay and it's removed of the read
write once and uh this is the storage
class of this uh persistent volume clay
and uh this is the pod and u first we
created the you can see here you can uh
you can identify any resource by this uh
kinda like it's a persistent volume and
this one is the persistent volume claim
and the next we Uh we created one pod
that have u one container. You can see
here one container and uh it used the ln
latest um image and um it will print
this message persistent data to this
file after 30 uh seconds.
Okay. So um you can see this volume. So
the main difference between MT1 is that
uh you will use the empty TIR keyword
and here you will see uh you can see
here this is the name of the storage and
um this is the persistent volume uh
claim. So where which I I defined in
this u in the second u part. So uh and
this is the manual PVC. So now um we
will deploy this PV and PVC to end board
together. So I will use this command
qctl apply manual p-pvc.
So you can see here now three things is
created. The first one is the persistent
volume and persistent volume claim and
the other one is the port uh which is
with name pv- demo. So uh you can get
how you will get the resistant volume.
There is a command cube ctl get like
port uh this will be resistant volume.
So here is the information. Uh we have
uh one manual uh resistant volume which
is created. This is the capacity. Uh we
already define the capacity and uh uh
this is the access mode read uh mode.
You can see here read write once we have
uh this is the mode of the uh the
persistent volume and uh reclaim policy
status. This is all the information you
uh about the persistent volume. Now uh
how you will check the board status for
so I'm getting board U PVC uh so in the
third uh you can see have three things
created so first I check the manual PC
um and um PVC then uh I check the pod
which is the PVC uh demo and it it has
one container which is up and running
and uh now um uh I will view I want to
view the persistent data so uh I will
run this command
So you can see here this is this command
will use to view the persistent data and
uh this is all about the persistent
volume. So now I will delete uh this uh
uh we will delete the port but we will
keep the u uh keep the uh keep the PVC.
So uh this will delete the uh pod. Okay.
Now we'll uh see the dynamic uh
provisioning with the storage classes.
We will understand the dynamic uh uh
provisioning. Um so dynamic provisioning
uh storage classes like define uh
storage type and PVCs request storage
from our storage classes and uh u
persistent volume storage are created
automatically. So we will see this in
action like first I will create one uh
new file to see this example dynamic
PVCL.
Uh so here uh I will add some code.
Okay. So this is the code. The first one
is the PVC with dynamic provisioning. Uh
again we have a basic information, basic
configuration, then specification um
access mode and uh this is the resource
requested uh and uh the storage is about
2 GB and uh this one will use the
default storage class because the
storage class is not specified. So it
will use the default one and the default
storage class is a standard. So this uh
will PVC will use the uh standard uh
storage class and uh uh you can see here
in the dynamic PVC
persistent volume claim uh again this is
the basic information specification uh
there is one container and uh this is uh
the volume they are using name and this
location and uh again you can see here
this is the persistent volume uh claim.
So I will apply uh I will create this.
So uh it's created PVC and um uh board
dynamic storage demo is created and uh
now uh I will get the PVC cube cdtl get
get PV. So this is the PVC information.
This is the PVC information.
Okay. So um you can see here this one uh
is uh we did not specify the uh storage
class. So it is created with default one
and the other one is the manual because
we uh uh specified. So this is the main
uh difference between these two uh um PV
uh persistent volume and persistent
volume uh claim and u
uh this is uh how you can get the
dynamic one as well. Uh like I will run
this command to check the PVC status to
check the PVC status.
Okay. So this is the status of the
dynamic PVC. Uh the capacity is 2 GB uh
read mode and uh storage class is
standard. So uh this is all about the uh
dynamic provisioning. We will uh do a
practical example uh in which we will
see multiple node will share the uh
share the storage. So for this example I
will create one new file uh one new yl
share storage uh yl. Uh so this is the
storage I will add code.
Okay. So so you can see uh this is the
code for the shared one. Uh basic
information shared PVC. We created a
shared PVC.
Uh resource uh kind is the persistent
volume. And uh mood this the mood and uh
the size. And this is a producer pod
that have the container and uh this
container uh will uh print this message
print this message and uh it's using the
shared data volume persistent volume.
Okay. So this is using the persistent
volume we created in the first step and
this is the consumer port again uh this
is also using the same uh persistent
volume claim. So this will be shared
between this uh two uh ports and uh
let's run uh let's deploy. So first uh
uh I will uh create this board. Uh this
is created. Two port is created. One is
producer, other one is the consumer.
And uh uh I can also check port and PVC
together as so this is the information
related to PBC and port which is created
and uh pod is um all the container
inside the port is currently running. So
uh you can also get logs for the
producer and consumer as well. Okay,
there is no log yet.
Let's check for the consumer. So
consumer have uh messages, latest
messages. This is the producer messages
and uh uh this command will check both
uh port are in the same node. So you can
see here uh both uh both port are on the
same node due to uh rwo the access mode
about the access mode and uh the
consumer and the producer and the
storage. Back to practical serverless
and microservices. My name is Abdullah
and uh I'm Azure solution architect.
This is session number 10 Kubernetes and
uh this is part number five of
Kubernetes and uh if you missed the
previous uh parts 1 2 3 4 you can uh
first check out those part then you can
continue this part number five and part
number five is about deployments and
replica set. What is deployment and what
is replica set? We will discuss this in
this uh session. So uh agenda for this
to understand the relationship between
the deployment replica set and pods and
we will create and manage deployment for
scalable uh application and uh we will
uh do uh rolling uh back updates and
scale application manually understand
scaling uh strategies and uh we will
also use uh label and selector uh
selectors effectively. So uh for uh
prerequisites you first check out the
four parts and uh the mini cube should
be running in your local system and uh
you should also know about some basic
information about the pod and the basic
kubernetes concept. Uh now I'm in VS
code so there is some basic definition
uh so we have a three layer
architecture. The first one is the
deployment that manage the replica set
and the second we have a replica set
that manage the port and at the third
the third uh layer we have a port. So u
we will create deployment uh practically
in this uh in this part. So why we use
deployments instead of the port? So
there is some benefits like the
scalability uh you can manage multiple
replicas easily and there will be a
highly availability as well
automatically replacement of failed uh
port and u roll back updates there will
be like a zero downtime deployments and
you can also easily roll back and uh
there is a declarative management
describe uh desired state kubernet u
rest of this. So uh first of all uh we
will create one basic deployment uh
structure. So for this uh I will create
one file for example basic
d
uh
file this file and I will copy the code.
Okay. So, so now this is the new thing
uh which is uh we uh check the pod we
use the storage persistent volume and
persistent volume uh claim. Now in this
session we are um practicing uh
deployment. So this is deployment kind
of resource uh name is my deployment and
replica we need three replicas. So I
mentioned three and this is the selector
label. We will discuss label later and
this is the template and this is the
specification for the container. It's
will it will pull the address um in the
next uh image and that will run on this
board. Okay. So this is this is a basic
version of um deployment and uh now I
will create one another uh simple web
server deployment. So I will create one
new file
that will use the engineext
u image. So this the name file name of
the file is engineext deployment and uh
this will be the definition of this yl
file. Uh we have again deployment name
enginex uh deployment version and
replica. We need three replicas and um
template specification container
information and uh this is the request
and limit. Okay. Uh res resource request
and limit. Okay. Uh now I will uh create
I will deploy this application. Uh first
clear let's clear this terminal. Uh
created deployment is uh created. So how
you'll check like pod PVC you can also
use the same command uh qdl get
deployment d
mens so there is one deployment okay and
uh it's up to date available too okay uh
let's try again now you can see here uh
all the instances now working we have
three things and all three thing is up
and running so uh this is about the
deployment uh now uh how I will check
the replica set. Uh how I will check the
replica set. You can um write this
command qestl get replica set. It will
give you all the replica set. Uh it will
give the information uh desired three
current three and ready is three because
I mentioned we need three replicas. So
all three is up and running. This is the
name of the replicas development. And uh
again uh if I want to check the port uh
get ports ubectl get ports. So uh this
is all information um okay so uh this is
all about the get ports. Okay. So uh
this is all about the uh
simple web server deployment.
Okay. So if you want to get the detailed
deployment information you can run this
command. This is the detail information.
Detailed information about the engineext
deployment name default. This is all the
information the detailed information of
the uh you can also uh do the you can
also check the deployment uh roll back
status by using this command roll out
status. So um successfully rolled
deployment is successfully rol rolled
out. So you can use this command and uh
you can also u view uh deployment in um
in a yl file as well by using this
command. So it will give you all the y
information in yl file. So u this is all
about the simple uh deployment. Now I
will back to the information some
information I wrote down. Uh now there
is we will understand the labels and
selectors. So how selector work like
deployment or selector to find replica.
So with the help of deployment u label
selector we can find out the replica set
and replica set uh use selector to find
the broads and label must be select
selector exactly. So this some
information about the selectors. So um
there is one exercise working with
labels where you can use for example
view port with label. So you can use
this command get port show label. So it
will show all the labels. So you can see
here uh it showing all the label for you
and uh you can also filter port by
labels. So there is again this command
that will um filter port by a label. So
uh you can see here I add the filter uh
indress. So they will get all the
ingress um ports uh by this label and uh
you can also use you can also add a
label to a pod um label to a port like
for example if you want to label any
port you can use this command. You will
just specify the port name and um which
environment you want to use.
Master modern application architecture by building real-world serverless and microservices solutions using C# and Azure. This hands-on course from @codewithmuhammadabdullah takes you from fundamental concepts to production-ready implementations, covering everything from Azure Functions and Docker containers to advanced orchestration with Kubernetes and .NET Aspire. - 0:00:00 What is Serverless? - 0:25:30 Understanding Serverless Architecture - 0:36:11 Microservices Types & Patterns Explained - 0:49:05 Onion Architecture Explained - 1:16:05 Docker Demystified - 1:45:58 Azure Functions and Triggers - 2:15:09 Background Functions in Azure - 2:39:26 IoT Functions - 2:55:39 Microservices in Practice - Route Planning - 4:06:10 Introduction to Kubernetes ā¤ļø Support for this channel comes from our friends at Scrimba ā the coding platform that's reinvented interactive learning: https://scrimba.com/freecodecamp š Thanks to our Champion and Sponsor supporters: š¾ Drake Milly š¾ Ulises Moralez š¾ Goddard Tan š¾ David MG š¾ Matthew Springman š¾ Claudio š¾ Oscar R. š¾ jedi-or-sith š¾ Nattira Maneerat š¾ Justin Hual -- Learn to code for free and get a developer job: https://www.freecodecamp.org Read hundreds of articles on programming: https://freecodecamp.org/news