前言
定时伸缩(cronhpa)是很多开发者在解决负载周期性时最常用的方法,通过类似crontab的语法可以在一个时间点定时触发伸缩活动。crontab的语义表达是很强大的,但是也存在语法复杂,执行计划容易被打断等问题。为了解决上述的问题,定时伸缩(cronhpa)提供了运维模式,支持通过运维页面来查看底层排队的定时任务,同时也提供了API接口用于开发者自研的平台接入与集成。
定时伸缩组件运维模式介绍
当通过ACK的应用目录或者开源社区的模板下发最新版本定时伸缩组件(cronhpa)后,默认在8000端口就已经开启了运维模式。我们先给定时伸缩组件(cronhpa)创建一个svc用来访问运维页面。
apiVersion: v1
kind: Service
metadata:
name: kubernetes-cronhpa-controller
namespace: kube-system
spec:
ports:
- port: 80
protocol: TCP
targetPort: 8000
selector:
app: ack-kubernetes-cronhpa-controller
sessionAffinity: None
type: ClusterIP
此时,在本地我们可以通过执行kubectl proxy --port=8080
启动proxy模式,再在本地浏览器中输入http://localhost:8080/api/v1/namespaces/kube-system/services/kubernetes-cronhpa-controller/proxy/ 即可访问运维管理页面。
让我们来解析下这个页面中展现的字段和使用的方式。
- CronHPA:定时伸缩CRD的名字。
- Namespace:定时伸缩CRD的命名空间。
- Id:伸缩任务的唯一Id,这个Id的作用是唯一决定一个任务的信息。
当CRD中出现了JobId与底层CronEngine中的Id不匹配的情况,说明CRD中的任务没有及时被下发到底层的定时执行引擎中,此时就需要特别关注下定时伸缩(cronhpa)组件稳定性的问题了。
- Pre:上一次任务执行的时间,如果这个数值是初始时间(0001-01-01 00:00:00 +0000 UTC)说明任务还没有被执行过。
- Next:下一次任务执行的时间,用来确认定时引擎中是否已经有正确排队的任务。
通常情况下,我们只需要通过页面大盘确认下发的作业已经存在,并且Pre/Next这两个字段是正常的状态,基本就可以确认定时任务可以正常执行了。如果开发者有自己的运维管理系统,可以使用API的方式进行接入。接口地址:http://localhost:8080/api/v1/namespaces/kube-system/services/kubernetes-cronhpa-controller/proxy/api.json
[
{
"Schedule": {
"Second": 1073741824,
"Minute": 10376293541461622783,
"Hour": 9223372036871553023,
"Dom": 9223372041149743102,
"Month": 9223372036854783998,
"Dow": 9223372036854775935
},
"Next": "2021-03-15T18:53:30+08:00",
"Prev": "2021-03-15T18:52:30+08:00",
"Job": {
"TargetRef": {
"RefName": "nginx-deployment-basic",
"RefNamespace": "default",
"RefKind": "Deployment",
"RefGroup": "apps",
"RefVersion": "v1"
},
"HPARef": {
"kind": "CronHorizontalPodAutoscaler",
"apiVersion": "autoscaling.alibabacloud.com/v1beta1",
"metadata": {
"name": "cronhpa-sample",
"namespace": "default",
"selfLink": "/apis/autoscaling.alibabacloud.com/v1beta1/namespaces/default/cronhorizontalpodautoscalers/cronhpa-sample",
"uid": "54dc98fe-b311-4763-8d99-25a93bdf5c74",
"resourceVersion": "770255389",
"generation": 2,
"creationTimestamp": "2021-03-15T10:49:46Z",
"labels": {
"controller-tools.k8s.io": "1.0"
},
"annotations": {
"kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"autoscaling.alibabacloud.com/v1beta1\",\"kind\":\"CronHorizontalPodAutoscaler\",\"metadata\":{\"annotations\":{},\"labels\":{\"controller-tools.k8s.io\":\"1.0\"},\"name\":\"cronhpa-sample\",\"namespace\":\"default\"},\"spec\":{\"jobs\":[{\"name\":\"scale-down\",\"schedule\":\"30 */1 * * * *\",\"targetSize\":1},{\"name\":\"scale-up\",\"schedule\":\"01 */1 * * * *\",\"targetSize\":3}],\"scaleTargetRef\":{\"apiVersion\":\"apps/v1\",\"kind\":\"Deployment\",\"name\":\"nginx-deployment-basic\"}}}\n"
}
},
"spec": {
"scaleTargetRef": {
"apiVersion": "apps/v1",
"kind": "Deployment",
"name": "nginx-deployment-basic"
},
"jobs": [
{
"name": "scale-down",
"schedule": "30 */1 * * * *",
"targetSize": 1
},
{
"name": "scale-up",
"schedule": "01 */1 * * * *",
"targetSize": 3
}
]
},
"status": {
"scaleTargetRef": {
"apiVersion": "apps/v1",
"kind": "Deployment",
"name": "nginx-deployment-basic"
},
"conditions": [
{
"name": "scale-down",
"jobId": "f68e5d16-f92b-4456-89d0-1da3ca36efa4",
"schedule": "30 */1 * * * *",
"targetSize": 1,
"runOnce": false,
"state": "Submitted",
"lastProbeTime": "2021-03-15T10:49:46Z",
"message": ""
},
{
"name": "scale-up",
"jobId": "8f611afa-ebde-4903-8c23-b323951b8757",
"schedule": "01 */1 * * * *",
"targetSize": 3,
"runOnce": false,
"state": "Submitted",
"lastProbeTime": "2021-03-15T10:49:46Z",
"message": ""
}
]
}
},
"DesiredSize": 1,
"Plan": "30 */1 * * * *",
"RunOnce": false
}
},
{
"Schedule": {
"Second": 2,
"Minute": 10376293541461622783,
"Hour": 9223372036871553023,
"Dom": 9223372041149743102,
"Month": 9223372036854783998,
"Dow": 9223372036854775935
},
"Next": "2021-03-15T18:54:01+08:00",
"Prev": "2021-03-15T18:53:01+08:00",
"Job": {
"TargetRef": {
"RefName": "nginx-deployment-basic",
"RefNamespace": "default",
"RefKind": "Deployment",
"RefGroup": "apps",
"RefVersion": "v1"
},
"HPARef": {
"kind": "CronHorizontalPodAutoscaler",
"apiVersion": "autoscaling.alibabacloud.com/v1beta1",
"metadata": {
"name": "cronhpa-sample",
"namespace": "default",
"selfLink": "/apis/autoscaling.alibabacloud.com/v1beta1/namespaces/default/cronhorizontalpodautoscalers/cronhpa-sample",
"uid": "54dc98fe-b311-4763-8d99-25a93bdf5c74",
"resourceVersion": "770255389",
"generation": 2,
"creationTimestamp": "2021-03-15T10:49:46Z",
"labels": {
"controller-tools.k8s.io": "1.0"
},
"annotations": {
"kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"autoscaling.alibabacloud.com/v1beta1\",\"kind\":\"CronHorizontalPodAutoscaler\",\"metadata\":{\"annotations\":{},\"labels\":{\"controller-tools.k8s.io\":\"1.0\"},\"name\":\"cronhpa-sample\",\"namespace\":\"default\"},\"spec\":{\"jobs\":[{\"name\":\"scale-down\",\"schedule\":\"30 */1 * * * *\",\"targetSize\":1},{\"name\":\"scale-up\",\"schedule\":\"01 */1 * * * *\",\"targetSize\":3}],\"scaleTargetRef\":{\"apiVersion\":\"apps/v1\",\"kind\":\"Deployment\",\"name\":\"nginx-deployment-basic\"}}}\n"
}
},
"spec": {
"scaleTargetRef": {
"apiVersion": "apps/v1",
"kind": "Deployment",
"name": "nginx-deployment-basic"
},
"jobs": [
{
"name": "scale-down",
"schedule": "30 */1 * * * *",
"targetSize": 1
},
{
"name": "scale-up",
"schedule": "01 */1 * * * *",
"targetSize": 3
}
]
},
"status": {
"scaleTargetRef": {
"apiVersion": "apps/v1",
"kind": "Deployment",
"name": "nginx-deployment-basic"
},
"conditions": [
{
"name": "scale-down",
"jobId": "f68e5d16-f92b-4456-89d0-1da3ca36efa4",
"schedule": "30 */1 * * * *",
"targetSize": 1,
"runOnce": false,
"state": "Submitted",
"lastProbeTime": "2021-03-15T10:49:46Z",
"message": ""
},
{
"name": "scale-up",
"jobId": "8f611afa-ebde-4903-8c23-b323951b8757",
"schedule": "01 */1 * * * *",
"targetSize": 3,
"runOnce": false,
"state": "Submitted",
"lastProbeTime": "2021-03-15T10:49:46Z",
"message": ""
}
]
}
},
"DesiredSize": 3,
"Plan": "01 */1 * * * *",
"RunOnce": false
}
}
]
最后
通过运维页面和API,开发者可以很好地洞悉定时伸缩(cronhpa)组件的底层状态,提供更确切的伸缩保障。如果对于运维模式有更多的建议,请提交到社区。