在运行时缩减 kubernetes statefulset 的 up/scale 选项有哪些?
What are the options to scale up/scale down a kubernetes statefulset during runtime?
在我的用例中,我需要根据特定逻辑扩大和缩小有状态集。
我不确定 HPA 是否会有所帮助,因为我需要每次只创建一个副本,然后发出通知以缩小它,这意味着 - 应用程序将被缩放,然后在特定的时间内不会缩放向下(HPA 将继续旋转 pods 直到满足其条件,如果满足,它将缩小 pod,这是一个问题)。
我正在使用 helm 进行部署。
我应该:
- 让 pod 访问 K8s API 并更改副本数?
- 使用 helm controller 可能 create/update 包含副本计数的 CRD?
- 运行 当条件满足时,kubectl 在 pod 内缩放?
此处的最佳做法是什么?
您可以设置 CronJob that will spawn a pod each (x minutes) and check, for example by using ConfigMap if it needs to scale up/down the StatefulSet.
此 Job
将使用 REST API, with it you can use API reference docs to Replace
or Patch
您的 StatefulSet。
您可以通过以下方式做到这一点:
$ kubectl proxy --port=8080 &
See kubectl proxy for more details.
Then you can explore the API with curl, wget, or a browser, like so:
$ curl http://localhost:8080/api/
The output is similar to this:
{
"versions": [
"v1"
],
"serverAddressByClientCIDRs": [
{
"clientCIDR": "0.0.0.0/0",
"serverAddress": "10.0.1.149:443"
}
]
}
It is possible to avoid using kubectl proxy by passing an authentication token directly to the API server, like this:
Using grep/cut
approach:
# Check all possible clusters, as you .KUBECONFIG may have multiple contexts:
kubectl config view -o jsonpath='{"Cluster name\tServer\n"}{range .clusters[*]}{.name}{"\t"}{.cluster.server}{"\n"}{end}'
# Select name of cluster you want to interact with from above output:
export CLUSTER_NAME="some_server_name"
# Point to the API server refering the cluster name
APISERVER=$(kubectl config view -o jsonpath="{.clusters[?(@.name==\"$CLUSTER_NAME\")].cluster.server}")
# Gets the token value
TOKEN=$(kubectl get secrets -o jsonpath="{.items[?(@.metadata.annotations['kubernetes\.io/service-account\.name']=='default')].data.token}"|base64 -d)
# Explore the API with TOKEN
curl -X GET $APISERVER/api --header "Authorization: Bearer $TOKEN" --insecure
The output is similar to this:
{
"kind": "APIVersions",
"versions": [
"v1"
],
"serverAddressByClientCIDRs": [
{
"clientCIDR": "0.0.0.0/0",
"serverAddress": "10.0.1.149:443"
}
]
}
或者通过编程访问 API,Kubernetes 正式支持 Go and Python 客户端库。
希望对您有所帮助,如有其他问题请追问。
在我的用例中,我需要根据特定逻辑扩大和缩小有状态集。 我不确定 HPA 是否会有所帮助,因为我需要每次只创建一个副本,然后发出通知以缩小它,这意味着 - 应用程序将被缩放,然后在特定的时间内不会缩放向下(HPA 将继续旋转 pods 直到满足其条件,如果满足,它将缩小 pod,这是一个问题)。
我正在使用 helm 进行部署。
我应该:
- 让 pod 访问 K8s API 并更改副本数?
- 使用 helm controller 可能 create/update 包含副本计数的 CRD?
- 运行 当条件满足时,kubectl 在 pod 内缩放?
此处的最佳做法是什么?
您可以设置 CronJob that will spawn a pod each (x minutes) and check, for example by using ConfigMap if it needs to scale up/down the StatefulSet.
此 Job
将使用 REST API, with it you can use API reference docs to Replace
or Patch
您的 StatefulSet。
您可以通过以下方式做到这一点:
$ kubectl proxy --port=8080 &
See kubectl proxy for more details.Then you can explore the API with curl, wget, or a browser, like so:
$ curl http://localhost:8080/api/
The output is similar to this:
{
"versions": [
"v1"
],
"serverAddressByClientCIDRs": [
{
"clientCIDR": "0.0.0.0/0",
"serverAddress": "10.0.1.149:443"
}
]
}
It is possible to avoid using kubectl proxy by passing an authentication token directly to the API server, like this:
Using
grep/cut
approach:
# Check all possible clusters, as you .KUBECONFIG may have multiple contexts:
kubectl config view -o jsonpath='{"Cluster name\tServer\n"}{range .clusters[*]}{.name}{"\t"}{.cluster.server}{"\n"}{end}'
# Select name of cluster you want to interact with from above output:
export CLUSTER_NAME="some_server_name"
# Point to the API server refering the cluster name
APISERVER=$(kubectl config view -o jsonpath="{.clusters[?(@.name==\"$CLUSTER_NAME\")].cluster.server}")
# Gets the token value
TOKEN=$(kubectl get secrets -o jsonpath="{.items[?(@.metadata.annotations['kubernetes\.io/service-account\.name']=='default')].data.token}"|base64 -d)
# Explore the API with TOKEN
curl -X GET $APISERVER/api --header "Authorization: Bearer $TOKEN" --insecure
The output is similar to this:
{
"kind": "APIVersions",
"versions": [
"v1"
],
"serverAddressByClientCIDRs": [
{
"clientCIDR": "0.0.0.0/0",
"serverAddress": "10.0.1.149:443"
}
]
}
或者通过编程访问 API,Kubernetes 正式支持 Go and Python 客户端库。
希望对您有所帮助,如有其他问题请追问。