Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manage replicas count for deployment using Kubernetes API

Tags:

kubernetes

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.

like image 794
yuval simhon Avatar asked Jan 22 '17 15:01

yuval simhon


People also ask

How do I change the number of replicas in Kubernetes?

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.

How many replicas do I need Kubernetes?

Number of replicas. You need at least two replicas for the application to be considered minimally available.

What is replica in Kubernetes Deployment?

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.

How do you scale multiple Deployments in Kubernetes?

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.

How do I manage replicasets in Kubernetes?

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.

What is the difference between Kubernetes deployment and pod deployment?

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.

How do I view a deployment in Kubernetes?

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.

What does it mean when a Kubernetes deployment is progressing?

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


2 Answers

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
like image 95
pagid Avatar answered Sep 29 '22 17:09

pagid


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.

like image 30
Bruno_Ferreira Avatar answered Sep 29 '22 15:09

Bruno_Ferreira