How To Build a Load Balancer In .NET With YARP Reverse Proxy

Поделиться
HTML-код
  • Опубликовано: 15 ноя 2024

Комментарии • 95

  • @MilanJovanovicTech
    @MilanJovanovicTech  Год назад +2

    Want to master Clean Architecture? Go here: bit.ly/3PupkOJ
    Want to unlock Modular Monoliths? Go here: bit.ly/3SXlzSt

    • @JoeRomano-s8g
      @JoeRomano-s8g 4 месяца назад

      Milan how does this applies to automatic scaling on Azure all service?

  • @ivandrofly
    @ivandrofly Год назад +2

    Just learned yarp last week, and I'm in love with it

  • @m5s10
    @m5s10 Год назад +11

    If you use yarp like load balancer like this, you can optimize this even further - yarp app does not have to be web api. Web apis today are lightweight, but if you want to push it to extremes, you can just make a console app and configure kestrel for your load balancer. Then your yarp will be available as a url, but is as lightweight as possible. If you only want to do load balancing with yarp, there's no need to have the whole machinery of web api behind yarp, configuring kestrel will be enough.

    • @MsMagician0
      @MsMagician0 Год назад +1

      very helpful thank u

    • @m5s10
      @m5s10 Год назад

      @@MsMagician0 you're welcome :)

    • @MilanJovanovicTech
      @MilanJovanovicTech  Год назад +1

      Awesome! Are there any guidelines on this in the docs? 🤔

    • @zedmagdy
      @zedmagdy Год назад

      from dotnet core and above web apis are actually a console apps with krestel configuered so there is no difference I think

    • @test124
      @test124 6 месяцев назад

      So, what's the difference if I use nginx, and config as reverse proxy and load balancer, instead of yarp?

  • @mikhailtokarev4633
    @mikhailtokarev4633 Год назад +2

    A few comments:
    - Health checks are meant for your destination boxes. To identify if they are alive and using configured logic re-route traffic to a healthy box.
    - Another benefit of having LB is to build highly available infra, but here you need to add another LB node. Not sure if YARP supports it
    - When your API will get a request thru the reverse proxy it will see the proxy IP address (not the client). I believe you need to configure additional headers
    For small projects might be that is OK to have a LB like this, but for the >=mid\prod apps I would prefer to have something that is optimized for this particular task.

    • @MilanJovanovicTech
      @MilanJovanovicTech  Год назад +1

      There are many YARP config settings I left out that cover the topics you mentioned. I will check about running YARP in a HA setup, that piqued my curiosity.

  • @alwaseem5309
    @alwaseem5309 Год назад +2

    Simple and helpful. Thank you for such a great video.

  • @douglasperez3701
    @douglasperez3701 10 месяцев назад +1

    Such a great video! 🤘

    • @MilanJovanovicTech
      @MilanJovanovicTech  10 месяцев назад +1

      Glad you think so! I've another one for API gateways coming out soon

  • @diegomelgar2696
    @diegomelgar2696 11 месяцев назад

    Hi @MilanJovanovicTech,
    Again, another insightful video, thanks for sharing it.
    I know that this is a simple intro to how to start with a load balancer using yarp. In order to let us know that you were not lying us 😅 you showed the links attribute of the products endpoint.
    Here, I kept thinking that if you want to protect your APIs through the API Load Balancer/Gateway, the real path or domain of the application will be exposed, so I wonder if it's something that it has to be configured in Yarp or in the application (eShop) in order to transform those links or any other response that involves the application URL?
    Thanks again for your videos, they are really interesting and insightful!

    • @MilanJovanovicTech
      @MilanJovanovicTech  11 месяцев назад +1

      Yeah, you'd have to forward the Load balancer address in a header - typically X-Forwarded-For - and fix the LinksService which generates the HATEOAS links to use the correct address

    • @diegomelgar2696
      @diegomelgar2696 11 месяцев назад

      @@MilanJovanovicTech thanks. I will check how to do it.

  • @dcernach
    @dcernach Год назад +1

    Awasome video.. Now bring us a Series of videos about YARP... Let me ask you something, can YARP request client certificates for authentication and forward them to apis via headers?

  • @alexmadnix
    @alexmadnix Год назад +1

    Excellent video

  • @kodindoyannick5328
    @kodindoyannick5328 8 месяцев назад

    Very interesting content. Thank you Milan.

  • @antonmartyniuk
    @antonmartyniuk Год назад +1

    Very useful video!

  • @aliwa91
    @aliwa91 Год назад +1

    Thank you

  • @danimatar7815
    @danimatar7815 Год назад +2

    Can you show us an example, in case we had 2 replicas on different servers, while loading the config from netflix eureka ? Making it more dynamic

  • @misters6451
    @misters6451 10 месяцев назад

    Very helpful, thank you!!!

  • @ewer
    @ewer 11 месяцев назад +1

    How does it compare to Ocelot?
    Seems like Ocelot supports more features than YARP, I mean, YARP seems a "pure" load balancer and Ocelot has other features (like Kubernetes integration) than a "pure" load balancer.
    Is there any specific reason to choose one over another?

    • @MilanJovanovicTech
      @MilanJovanovicTech  11 месяцев назад +1

      YARP is used in Microsoft internally, for Azure App Service for example. It can also work as a Kubernetes ingress controller. It's not just a load balancer, it's mainly a reverse proxy. It can also be an API gateway. It has auth, rate limiting, etc.

    • @ewer
      @ewer 11 месяцев назад

      @@MilanJovanovicTech which one do you prefer?

  • @diegofritz2436
    @diegofritz2436 Год назад +1

    Me encanto el video. Muy, pero muy bueno

  • @gakshay9537
    @gakshay9537 Год назад +2

    Thank you for explaining this topic. I just have a question. If the service is deployed on server1 and server2, we can use application where yarp is used to route the requests. So, ideally this has to be on some other server say server3. What if the server3 is down. How can we handle this situation.

    • @ibrahimb6791
      @ibrahimb6791 Год назад

      imo this is not a coding problem

    • @JohnOliverAtHome
      @JohnOliverAtHome Год назад

      @@ibrahimb6791 It is a Yarp problem though. Yarp would need to handle the failing health checks. The question is, how to configure/achieve this?

    • @MilanJovanovicTech
      @MilanJovanovicTech  Год назад

      You can run YARP in a HA setup, to improve availability. But if your gateway is down, so is your app

  • @DebayanDe
    @DebayanDe 2 месяца назад

    Great video.. Wanted to know, if YARP can be applied with a .NET Core Web API which is hosted in IIS over multiple VMs.

    • @MilanJovanovicTech
      @MilanJovanovicTech  2 месяца назад

      Yes

    • @DebayanDe
      @DebayanDe 2 месяца назад

      @@MilanJovanovicTech Okay. I have an issue, where I have a .NET Core web api application hosted in IIS, and also running the same application through VS 2022 Kestrel server (http), and have configured the IIS url in my appsettings.json file against the cluster destination address. So I want to call the Kestrel server api url but want YARP to reroute the request to the IIS api url.
      How do I know if YARP is forwarding the request from the Kestrel url to the IIS one correctly or not, as every time my 'HttpContext.Request.GetDisplayUrl()' is returning the Kestrel server url (using Round Robin LoadBalancingPolicy) instead of the IIS one.
      Any help will be great.
      Thanks.

    • @DebayanDe
      @DebayanDe 2 месяца назад

      ​@@MilanJovanovicTech If YARP is not routing the requests to the url defined in the cluster destination address, how to fix that?
      I am returning the value of 'HttpContext.Request.GetDisplayUrl()' in my api endpoint, which is always returning the original url, instead of the one I have mentioned in the appsettings.json cluster destination address.

  • @MaxSupercars
    @MaxSupercars Год назад +1

    Great video! 😊

  • @tonystoynev7969
    @tonystoynev7969 Год назад +1

    Great video

  • @IlyasKazi
    @IlyasKazi 4 месяца назад

    If we setup our application (web api) on 3 different servers having unique IP-addresses.. say server1, server2 and server3. And Yarp is been setup on server1 as guided in your video. So by just configuring appsettings.json with their host address it should work?

  • @jeremiahgavin9687
    @jeremiahgavin9687 Год назад +1

    I am having trouble finding performance metrics for YARP. Do you have any resources? I am interested in using this to route SignalR connections between a client and a SignalR server.

    • @MilanJovanovicTech
      @MilanJovanovicTech  Год назад +1

      Here, page 13
      msit.powerbi.com/view?r=eyJrIjoiYTZjMTk3YjEtMzQ3Yi00NTI5LTg5ZDItNmUyMGRlOTkwMGRlIiwidCI6IjcyZjk4OGJmLTg2ZjEtNDFhZi05MWFiLTJkN2NkMDExZGI0NyIsImMiOjV9

    • @jeremiahgavin9687
      @jeremiahgavin9687 Год назад

      @@MilanJovanovicTech Thanks a ton! I found this resource when I was looking on my own but I didn't know there were pages so I was very confused.

  • @Paul-uo9sv
    @Paul-uo9sv Месяц назад

    Hi Milan, So YARP can be easily implemented with Docker and Kubernetes?

    • @MilanJovanovicTech
      @MilanJovanovicTech  Месяц назад +1

      Yes, works great with Docker. Not sure about Kubernetes, but I found this: github.com/microsoft/reverse-proxy/blob/main/docs/docfx/articles/kubernetes-ingress.md

    • @Paul-uo9sv
      @Paul-uo9sv 17 дней назад

      @@MilanJovanovicTech thanks good sir! thank you I'll check it out

  • @EivindGussiasLkseth
    @EivindGussiasLkseth Год назад +1

    How would you configure a health check on the instances, and forward requests only to the healthy instances?

    • @MilanJovanovicTech
      @MilanJovanovicTech  Год назад +1

      Here's how: microsoft.github.io/reverse-proxy/articles/dests-health-checks.html

  • @pilotboba
    @pilotboba Год назад +1

    I assume YARP sends an X-Forwarded-For or something so you can adjust your HATEAOS links to point to the reverse proxy rather than directly into your API since usually you wouldn't use public ports on your API instances to force all traffic through the proxy/load balancer.

    • @MilanJovanovicTech
      @MilanJovanovicTech  Год назад

      Of course, I only left the original implementation in place to make the load balancing obvious.

  •  Год назад +1

    Has yarp any advantages over use nginx as reverse proxy?

    • @MilanJovanovicTech
      @MilanJovanovicTech  Год назад +1

      It's built in .NET and integrates with .NET Core... And it's faster than nginx

    •  Год назад

      @@MilanJovanovicTech Ok, but it is not reccomended to put kestrel directly to open world, as i know at least. But if you think that yarp hosted on kestrel can be exposed directly it make sense.

  • @alphaios7763
    @alphaios7763 Год назад +1

    It's like you read my mind. I just have a use case where I need this. Thank you very much!
    Kind of random, but one thing I'm concerned about is your eyebrow raising. I feel like you might end up with forehead like Gordon Ramsay. Not trying to be mean, but helpful :) thanks again for the awesome content!

    • @skyhappy
      @skyhappy Год назад +2

      The eyebrows just mean he is a very interesting person

    • @MilanJovanovicTech
      @MilanJovanovicTech  Год назад +3

      Can't fight it. The eyebrows have a mind of their own 🤣

  • @modernkennnern
    @modernkennnern Год назад +1

    What about Orleans? Have you tried it before?

  • @gakshay9537
    @gakshay9537 11 месяцев назад

    In your example, api is running on 3 ports and yarp is used as load balancer. Is there a way for yarp to know if any of those instances are down. So that yarp will stop sending the requests to it.

    • @MilanJovanovicTech
      @MilanJovanovicTech  11 месяцев назад +1

      You can set this up: microsoft.github.io/reverse-proxy/articles/dests-health-checks.html

  • @AbdusSalamAzad
    @AbdusSalamAzad Год назад +1

    Will YARP work, if I set my application multiple instances on the same IIS server?

    • @MilanJovanovicTech
      @MilanJovanovicTech  Год назад

      Yes, as long as they are on different ports

    • @AbdusSalamAzad
      @AbdusSalamAzad Год назад

      @@MilanJovanovicTech It is using the same CPU, and memory as the same computer. Will it work properly?

  • @marcoguevara3369
    @marcoguevara3369 3 месяца назад

    Great video but I came to find out a way where a custom implementation of ILoadBalancingPolicy actually works...? I'm trying to implement a custom fail-over policy but I don't know what I missing...

    • @MilanJovanovicTech
      @MilanJovanovicTech  3 месяца назад +1

      Did you explore health checks? microsoft.github.io/reverse-proxy/articles/dests-health-checks.html

    • @marcoguevara3369
      @marcoguevara3369 3 месяца назад

      @@MilanJovanovicTech I will investigate the matter. Thanks so much

  • @rubberuk
    @rubberuk Год назад +1

    What happens if a user has logged in against one of that API servers?

  • @elvargas1327
    @elvargas1327 Год назад +1

    using the load balancing of an apigateway like ocelot is not enough?

  • @floringhisa48
    @floringhisa48 3 месяца назад

    I don't get it how this can be used in real world scenario. In a real scenario, the instances will be dynamic, so you can't know what ports or how many instances will there be. How can I configure YARP to support dynamic scaling?

    • @MilanJovanovicTech
      @MilanJovanovicTech  3 месяца назад

      You can populate the Routes/Clusters at runtime, so you'd need a way to discover the services dynamically. In other words, you'd end up implementing service discovery. YARP config can do live-reloads at runtime, so that makes it a bit easier. I'll see if I can come up with a practical solution for this and demo it.

  • @SalmanTariq223
    @SalmanTariq223 Год назад +1

    Can it route the SignalR requests??

  • @quantranba6494
    @quantranba6494 Месяц назад

    Where I can download the source code in this video please

  • @akshayinuk
    @akshayinuk 8 месяцев назад

  • @codenetw
    @codenetw 4 месяца назад

    Hi, how to make yarp show swager of all endpoints with which it works ?

    • @MilanJovanovicTech
      @MilanJovanovicTech  4 месяца назад +2

      YARP is a proxy, it doesn't really know which endpoints are defined behind the services

    • @codenetw
      @codenetw 4 месяца назад

      @@MilanJovanovicTech thanks for the answer!

    • @wennwenn1422
      @wennwenn1422 4 месяца назад

      Curious to know, why do you want to do that?