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.
You can easily change the number of pods a particular ReplicaSet manages in one of two ways: Edit the controllers configuration by using kubectl edit rs ReplicaSet_name and change the replicas count up or down as you desire. Use kubectl directly. For example, kubectl scale –replicas=2 rs/web.
Number of replicas. You need at least two replicas for the application to be considered minimally available.
A ReplicaSet ensures that a number of Pods is created in a cluster. The pods are called replicas and are the mechanism of availability in Kubernetes. But changing the ReplicaSet will not take effect on existing Pods, so it is not possible easily change, for example, the image version.
Scaling Multiple ResourcesThe kubectl scale command can scale several resources at once when you supply more than one name as arguments. Each of the resources will be scaled to the same replica count set by the --replicas flag. This command scales the app and database deployments to five replicas each.
Today, Kubernetes advises using Deployments to represent your workloads. Your Deployments will run and scale ReplicaSets automatically; ReplicaSets will in turn manage your Pods. You can perform a rolling update of a Deployment by updating the replicas field in its manifest.
Most of the time deployments are preferred over pod as they provide more control over failures of a pod. Deployments API is the part of Kubernetes API which allows user to run CRUD operations on deployments. We can list the deployments using GET API call to /apis/apps/v1/namespaces/ {namespace}/deployments.
After the rollout succeeds, you can view the Deployment by running kubectl get deployments . The output is similar to this: Run kubectl get rs to see that the Deployment updated the Pods by creating a new ReplicaSet and scaling it up to 3 replicas, as well as scaling down the old ReplicaSet to 0 replicas.
Kubernetes marks a Deployment as progressing when one of the following tasks is performed: The Deployment creates a new ReplicaSet. The Deployment is scaling up its newest ReplicaSet. The Deployment is scaling down its older ReplicaSet(s). New Pods become ready or available (ready for at least MinReadySeconds).
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 [email protected] -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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With