Kubernetes : How to scale a deployment from another service/pod?

1/20/2020

I have 2 services. Service A and Service B. They correspond to deployments dA and dB.

I set up my cluster and start both services/deployments. Service A is reachable from the external world. External World --> Service A <--> Service B.

How can I scale dB (change replicaCount and run kubectl apply OR kubectl scale) from within Service A that is responding to a user request.

For example, if a user that is being served by Service A wants some extra resource in my app, I would like to provide that by adding an extra pod to dB. How do I do this programatically?

-- crossvalidator
deployment
kubernetes
pod
scaling
service

1 Answer

1/20/2020

Every Pod, unless it opts out, has a ServiceAccount token injected into it, which enables it to interact with the kubernetes API according to the Role associated with the ServiceAccount

Thus, one can use any number of kubernetes libraries -- most of which are "in cluster" aware, meaning they don't need any further configuration to know about that injected ServiceAccount token and how to use it -- to issue scale events against any resource the ServiceAccount's Role is authorized to use

You can make it as simple or as complex as you'd like, but the tl;dr is akin to:

curl --cacert /var/run/secrets/kubernetes.io/ca.crt \
    --header "Accept: application/json" \
    --header "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/token)" \
    https://${KUBERNETES_SERVICE_HOST}:${KUBERNETES_SERVICE_PORT}/api/v1/namespaces
-- mdaniel
Source: StackOverflow