Manage scaling of Statefulsets using Kubernetes API

8/21/2020
  • I want scale up my statefulset and want to initiate this scale up from inside the pod(container) using the Kubernetes API(http request) to the kubernetes rest server.
  • I tried scaling up statefulset pods using

    PUT /apis/apps/v1/namespaces/{namespace}/statefulsets/{name}/scale

  • but it didn't work for me.

Even tried fetching the scale data of specified statefulset using

"GET /apis/apps/v1/namespaces/{namespace}/statefulsets/{name}/scale"

REQUEST : curl -s -k -H "Authorization: Bearer $TOKEN" -X GET https://kubernetes.default.svc:443/apis/apps/v1/namespaces/$Namespace/$Kind/$PodNamePrefix/scale

Gives an error :

Can somebody help me with this?

-- himanshu
containers
kubernetes
kubernetes-apiserver
kubernetes-statefulset
microservices

1 Answer

8/21/2020

You need to define RBAC using Role and RoleBinding to authorize the service account to perform the required operations

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  namespace: default
  name: ss-role
rules:
- apiGroups: ["apps"]
  resources: ["statefulsets/scale" ]
  verbs: ["get", "list", "create"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  namespace: default
  name: ss-rolebinding
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: ss-role
subjects:
- kind: ServiceAccount
  name: default
  namespace: default

Above Role and RoleBinding is based on the assumption that you are using default service account of default namespace to scale statefulsets in default namespace.

-- Arghya Sadhu
Source: StackOverflow