Home > Enterprise >  How can I scale deployment replicas in Kubernetes cluster from python client?
How can I scale deployment replicas in Kubernetes cluster from python client?

Time:01-14

I have Kubernetes cluster set up and managed by AKS, and I have access to it with the python client.

Thing is that when I'm trying to send patch scale request, I'm getting an error.

I've found information about scaling namespaced deployments from python client in the GitHub docs, but it was not clear what is the body needed in order to make the request work:

# Enter a context with an instance of the API kubernetes.client
with kubernetes.client.ApiClient(configuration) as api_client:
    # Create an instance of the API class
    api_instance = kubernetes.client.AppsV1Api(api_client)
    name = 'name_example' # str | name of the Scale
namespace = 'namespace_example' # str | object name and auth scope, such as for teams and projects
body = None # object | 
pretty = 'pretty_example' # str | If 'true', then the output is pretty printed. (optional)
dry_run = 'dry_run_example' # str | When present, indicates that modifications should not be persisted. An invalid or unrecognized dryRun directive will result in an error response and no further processing of the request. Valid values are: - All: all dry run stages will be processed (optional)
field_manager = 'field_manager_example' # str | fieldManager is a name associated with the actor or entity that is making these changes. The value must be less than or 128 characters long, and only contain printable characters, as defined by https://golang.org/pkg/unicode/#IsPrint. This field is required for apply requests (application/apply-patch) but optional for non-apply patch types (JsonPatch, MergePatch, StrategicMergePatch). (optional)
force = True # bool | Force is going to \"force\" Apply requests. It means user will re-acquire conflicting fields owned by other people. Force flag must be unset for non-apply patch requests. (optional)

    try:
        api_response = api_instance.patch_namespaced_deployment_scale(name, namespace, body, pretty=pretty, dry_run=dry_run, field_manager=field_manager, force=force)
        pprint(api_response)
    except ApiException as e:
        print("Exception when calling AppsV1Api->patch_namespaced_deployment_scale: %s\n" % e)

So when running the code I'm getting Reason: Unprocessable Entity

Does anyone have any idea in what format should the body be? For example if I want to scale the deployment to 2 replicas how can it be done?

CodePudding user response:

The body argument to the patch_namespaced_deployment_scale can be a JSONPatch document, as @RakeshGupta shows in the comment, but it can also be a partial resource manifest. For example, this works:

>>> api_response = api_instance.patch_namespaced_deployment_scale(
...   name, namespace,
...   [{'op': 'replace', 'path': '/spec/replicas', 'value': 2}])

(Note that the value needs to be an integer, not a string as in the comment.)

But this also works:

>>> api_response = api_instance.patch_namespaced_deployment_scale(
...   name, namespace,
...   {'spec': {'replicas': 2}})
  •  Tags:  
  • Related