How to produce a 503 service unavailable on Google cloud run?

Viewed 422

I am working on a disaster recovery strategy and would like to automate the cloud run infrastructure deployment on the Google Cloud platform if the service is unavailable for more than 10 minutes.

To do the above automation, I want to check if my cloud run's get public API's are responding or not and send an alert that will trigger the automation and provision the resource

Problem
I am not able to produce a 503 error on the demo container (hello-world) of cloud run, I tried sending 10000 requests using python multiprocessing with 1 concurrency on Cloud run keeping max instances at 1 and minimum instances at 0, Memory- 128MB, CPU-1

How can I produce a 503 error on the cloud run demo container hello-world and see the what is the status of the get public API?

What I don't want to do
I don't want to add a healthcheck to the container application and use that healthcheck to determine the availability of the container cloud run service.

2 Answers

Update I misread your question and assumed you were looking to use Cloud Storage.

Here's two options:

Mocking

from googleapiclient.discovery import build
from googleapiclient.http import HttpMockSequence
from google.api_core.client_options import ClientOptions

http = HttpMockSequence([
    ({"status":"503"},"")
])

service = build("run","v1",http=http)

namespace = "foo"
service = "bar"

name = "namespaces/{namespace}/services/{service}".format(
  namespace=namespace,
  service=service,
)

rqst = service.namespaces().services().get(name=name)
resp = rqst.execute(http=http)

print(resp)

service.close()

And then running it:

googleapiclient.errors.HttpError: <HttpError 503 when requesting https://run.googleapis.com/apis/serving.knative.dev/v1/namespaces/foo/services/bar?alt=json returned "Ok">

ClientOptions

You can point the client to your own "implementation" of the API using ClientOptions that always returns 503s:

from googleapiclient.discovery import build
from google.api_core.client_options import ClientOptions

client_options = ClientOptions(api_endpoint="http://{host}:{port}".format(
  host="localhost",
  port="8080",
)

service = build("run","v1",client_options=client_options)

namespace = "foo"
service = "bar"

name = "namespaces/{namespace}/services/{service}".format(
  namespace=namespace,
  service=service,
)

rqst = service.namespaces().services().get(name=name)
resp = rqst.execute(http=http)

print(resp)

service.close()

And then running it:

googleapiclient.errors.HttpError: <HttpError 503 when requesting http://localhost:8080/apis/serving.knative.dev/v1/namespaces/foo/services/bar?alt=json returned "Ok">

NOTE This time, I've an HTTP server with a / handler that always returns 503 (ServiceUnavailable). The error comes from localhost:8080

Original

from google.api_core.client_options import ClientOptions
from google.cloud import storage

client_options = ClientOptions(
    api_endpoint="[[ENDPOINT]]"
)

client = storage.Client(client_options=client_options)

NOTE You may want to migrate from gcloud to google.cloud

Here's an idea: try uploading a large file. On an instance with limited amount of memory that presumably will naturally result in 503.

from gcloud import storage
from oauth2client.service_account import ServiceAccountCredentials
import os


credentials_dict = {
    'type': 'service_account',
    'client_id': os.environ['BACKUP_CLIENT_ID'],
    'client_email': os.environ['BACKUP_CLIENT_EMAIL'],
    'private_key_id': os.environ['BACKUP_PRIVATE_KEY_ID'],
    'private_key': os.environ['BACKUP_PRIVATE_KEY'],
}
credentials = ServiceAccountCredentials.from_json_keyfile_dict(
    credentials_dict
)
client = storage.Client(credentials=credentials, project='myproject')
bucket = client.get_bucket('mybucket')
blob = bucket.blob('myfile')
blob.upload_from_filename('myfile')
Related