I want to change the number of replications (pods) for a Deployment using the Kubernetes API (v1beta1).
For now I'm able to increase the replicas from CLI using the command:
kubectl scale --replicas=3 deployment my-deployment
In the Kubernetes API documentation it's mention that there is a PUT request to do the same
PUT /apis/extensions/v1beta1/namespaces/{namespace}/deployments/{name}/scale
but there is no example of how to do it.
I'm not sure what to send in the request body in order to perform the update.
The previous solution didn't work for me on kubernetes 1.14. I had to use a different API endpoint. Here's the full example:
#!/bin/sh
set -e
NUMBER_OF_REPLICAS="$1"
CURRENT_NAMESPACE="$2"
DEPLOYMENT_NAME="$3"
KUBE_TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)
KUBE_CACRT_PATH="/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"
PAYLOAD="{\"spec\":{\"replicas\":$NUMBER_OF_REPLICAS}}"
curl --cacert $KUBE_CACRT_PATH \
-X PATCH \
-H "Content-Type: application/strategic-merge-patch+json" \
-H "Authorization: Bearer $KUBE_TOKEN" \
--data "$PAYLOAD" \
https://$KUBERNETES_SERVICE_HOST/apis/apps/v1/namespaces/$CURRENT_NAMESPACE/deployments/$DEPLOYMENT_NAME
Note that the $KUBERNETES_SERVICE_HOST
is automatically set by kubernetes inside the pods.
the easiest way is to retrieve the actual data first with:
GET /apis/extensions/v1beta1/namespaces/{namespace}/deployments/{name}/scale
This will give you an yaml or json object which you can modify and send back with the PUT
request.
With curl the roundtrip look like this:
API_URL="http://kubernetes:8080/apis/extensions/v1beta1/namespaces/{namespace}/deployments/{name}/scale"
curl -H 'Accept: application/json' $API_URL > scale.json
# edit scale.json
curl -X PUT -d@scale.json -H 'Content-Type: application/json' $API_URL
Alternatively you could just use a PATCH
call:
PAYLOAD='[{"op":"replace","path":"/spec/replicas","value":"3"}]'
curl -X PATCH -d$PAYLOAD -H 'Content-Type: application/json-patch+json' $API_URL