如何使503服务在Google云上不可用?



我正在研究一种灾难恢复策略,如果服务不可用超过10分钟,我希望在Google cloud平台上自动执行云运行基础设施部署。

要执行上述自动化,我想检查我的云运行的get公共API是否响应,并发送一个警报,将触发自动化并提供资源


我无法在云运行的演示容器(hello-world)上生成503 error,我尝试使用python多处理与1 concurrency在云运行上发送10000 requests,保持最大实例在1最小实例值为0Memory- 128MB, CPU-1

如何在云运行演示容器hello-world上产生503错误并查看get public API的状态?

我不想做的
我不想在容器应用程序中添加健康检查,并使用该健康检查来确定容器云运行服务的可用性。

更新我误解了你的问题,以为你想使用云存储。

有两个选项:

  • 模拟
  • 有一个api_option在ClientOptions.

嘲笑

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()

然后运行它:

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

你可以把客户端指向你自己的"实现"使用总是返回503的ClientOptions的API:

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()

然后运行它:

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

注意这一次,我有一个HTTP服务器,其/处理程序总是返回503(ServiceUnavailable)。错误来自localhost:8080

原来

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)

注意您可能想要从gcloud迁移到google.cloud

这是一个想法:尝试上传一个大文件。在内存有限的实例上,可能会自然地产生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')

最新更新