雲計算

Kubernetes彈性伸縮全場景解讀(八) – 定時伸縮組件支持運維模式

前言

定時伸縮(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/ 即可訪問運維管理頁面。

image.png

讓我們來解析下這個頁面中展現的字段和使用的方式。

  • CronHPA:定時伸縮CRD的名字。
  • Namespace:定時伸縮CRD的命名空間。
  • Id:伸縮任務的唯一Id,這個Id的作用是唯一決定一個任務的信息。

image.png

當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)組件的底層狀態,提供更確切的伸縮保障。如果對於運維模式有更多的建議,請提交到社區

Leave a Reply

Your email address will not be published. Required fields are marked *