ASP.NET Core 502 API Gateway error in Google Cloud

Today in this article, we will cover below aspects,

Issue Description

ASP.NET Core deployment gives 502 errors with the below logs after the frequent intervals.

ASP.NET Core 502 API Gateway error with application shutdown is most visible in the logs

Error

“Application is shutting down”

Resolution

There could be multiple reasons for the application shutdown. Please make sure to validate the below steps.

Solution1 – Increase Memory Allocation

  • Application shutdown could occur mainly if the application is loaded with more responsibility than it can handle.
  • Please make sure you allocate enough memory or CPU depending on the number of requests API can handle. A good load test on API should provide the request load metrics of API.

Please update the YAML file,

resources:
    cpu: 2
    memory_gb: 2
    disk_size_gb: 10

If the above properties are not provided then the cloud environment will select default settings.

Example1 CPU or 1 GB memory etc.

Note – Application default settings could be less sustainable for your API.

Specifically if API handles a large load or more requests then above settings will need to be updated.

If you have tried the above option already still see the issue surfacing then please look at the below few options.

Solution2 – Liveness and readiness Health check probe

  • Google Cloud uses liveness checks to confirm VM and the Docker container are healthy and running.
  • Instances with unhealthy status are killed and restarted.
  • In such a case please make sure the Liveness probe is successful as per the configuration.

Example,

 liveness_check:
  path: "/liveness_check"
  check_interval_sec: 30
  timeout_sec: 4
  failure_threshold: 2
  success_threshold: 2

Or

liveness_check:
  path: "/healthcheck"
  check_interval_sec: 30
  timeout_sec: 4
  failure_threshold: 2
  success_threshold: 2

The “path” variable can be defined by the API developer as required.

Please make sure you have a “path”: defined correctly. Example. “/liveness_check” or “/healthcheck” routes should return HTTP status 200 or OK response only.

For any reason, if “/healthcheck” is not defined or produces non-OK status then the google app engine initiates the container killing and restarting the container.

You can add health to ASP.NET Core in two simple easy steps which are explained in the below article.

Solution3 – Health route with empty route or controller “

If your API doesn’t specify any health route, then it is possible sometimes the Google app engine might look for the root route “/”.

If you have any custom health check then please specify the same else make sure you point to the correct health check here in the path: “/healthcheck”

Having a controller with an empty route “sometimes will resolve the issue.

Please try this too as one of the solutions to fix the issue.

References:

Did I miss anything else in these resolution steps?

Did the above steps resolve your issue? Please sound off your comments below!

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *