有奖捉虫:办公协同&微信生态&物联网文档专题 HOT

简介

组件介绍

HPC(HorizontalPodCronscaler)是一种可以对 K8S workload 副本数进行定时修改的自研组件,配合 HPC CRD 使用,最小支持秒级的定时任务。

组件功能

支持设置“实例范围”(关联对象为 HPA)或“目标实例数量”(关联对象为 deployment 和 statefulset)。
支持开关“例外时间”。例外时间的最小配置粒度是日期,支持设置多条。
支持设置定时任务是否只执行一次。

部署在集群内的 Kubernetes 对象

在集群内部署 HPC , 将在集群内部署以下 Kubernetes 对象:
Kubernetes 对象名称
类型
默认占用资源
所属Namespaces
horizontalpodcronscalers.autoscaling.cloud.tencent.com
CustomResourceDefinition
-
-
hpc-leader-election-role
Role
-
kube-system
hpc-leader-election-rolebinding
RoleBinding
-
kube-system
hpc-manager-role
ClusterRole
-
-
hpc-manager-rolebinding
ClusterRoleBinding
-
-
cronhpa-controller-manager-metrics-service
Service
-
kube-system
hpc-manager
ServiceAccount
-
kube-system
tke-hpc-controller
Deployment
100mCPU/pod、100Mi/pod
kube-system

限制条件

环境要求

说明:
您在创建集群时选择1.12.4以上版本集群,无需修改任何参数,开箱可用。
仅支持1.12版本以上的 Kubernetes。
需设置 kube-apiserver 的启动参数:--feature-gates=CustomResourceSubresources=true

节点要求

HPC 组件默认挂载主机的时区将作为定时任务的参考时间,因此要求节点存在 /etc/localtime 文件。
HPC 默认安装2个 HPC Pod 在不同节点,因此节点数推荐为2个及以上。

被控资源要求

在创建 HPC 资源时,被控制的 workload(K8S 资源)需要存在于集群中。

组件权限说明

权限说明

该组件权限是当前功能实现的最小权限依赖。

权限场景

功能
涉及对象
涉及操作权限
监听 horizontalpodcronscalers 的变动
horizontalpodcronscalers
create/delete/get/list/patch/watch
需要修改 deployments/statefulsets 的 replicas
deployments/statefulsets
get/list/patch/watch
修改 horizontalpodautoscalers 的 minReplicas/maxReplicas
horizontalpodautoscalers
get/list/patch/watc
同步 HPC 定时任务执行的 events
events
create/patch

权限定义

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
creationTimestamp: null
name: hpc-manager-role
rules:
- apiGroups:
- ""
resources:
- events
verbs:
- create
- patch
- apiGroups:
- apps
resources:
- deployments
verbs:
- get
- list
- patch
- watch
- apiGroups:
- apps
resources:
- statefulsets
verbs:
- get
- list
- patch
- watch
- apiGroups:
- autoscaling
resources:
- horizontalpodautoscalers
verbs:
- get
- list
- patch
- watch
- apiGroups:
- autoscaling.cloud.tencent.com
resources:
- horizontalpodcronscalers
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- autoscaling.cloud.tencent.com
resources:
- horizontalpodcronscalers/status
verbs:
- get
- patch
- update
- apiGroups:
- apiextensions.k8s.io
resources:
- customresourcedefinitions
resourceNames:
- horizontalpodcronscalers.autoscaling.cloud.tencent.com
verbs:
- get
- list
- delete
- watch

操作步骤

安装 HPC

1. 登录 容器服务控制台,在左侧导航栏中选择集群
2. 在集群列表中,单击目标集群 ID,进入集群详情页。
3. 选择左侧菜单栏中的组件管理,在组件管理页面单击新建
4. 新建组件管理页面中勾选 HPC
5. 单击完成即可创建组件。

创建并使用 HPC 工作负载示例

创建关联 Deployment 的定时任务资源

示例如下:
apiVersion: autoscaling.cloud.tencent.com/v1
kind: HorizontalPodCronscaler
metadata:
name: hpc-deployment
namespace: default
spec:
scaleTarget:
apiVersion: apps/v1
kind: Deployment
name: nginx-deployment
namespace: default
crons:
- name: "scale-down"
excludeDates:
- "* * * 15 11 *"
- "* * * * * 5"
schedule: "30 */1 * * * *"
targetSize: 1
- name: "scale-up"
excludeDates:
- "* * * 15 11 *"
- "* * * * * 5"
schedule: "0 */1 * * * *"
targetSize: 3

创建关联 StatefulSet 的定时任务资源

示例如下:
apiVersion: autoscaling.cloud.tencent.com/v1
kind: HorizontalPodCronscaler
metadata:
name: hpc-statefulset
namespace: default
spec:
scaleTarget:
apiVersion: apps/v1
kind: Statefulset
name: nginx-statefulset
namespace: default
crons:
- name: "scale-down"
excludeDates:
- "* * * 15 11 *"
schedule: "0 */2 * * * *"
targetSize: 1
- name: "scale-up"
excludeDates:
- "* * * 15 11 *"
schedule: "30 */2 * * * *"
targetSize: 4

创建关联 HPA 的定时任务资源

示例如下:
apiVersion: autoscaling.cloud.tencent.com/v1
kind: HorizontalPodCronscaler
metadata:
labels:
controller-tools.k8s.io: "1.0"
name: hpc-hpa
spec:
scaleTarget:
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
name: nginx-hpa
namespace: default
crons:
- name: "scale-up"
schedule: "30 */1 * * * *"
minSize: 2
maxSize: 6
- name: "scale-down"
schedule: "0 */1 * * * *"
minSize: 1
maxSize: 5

定时时间设置参考

字段名称
是否必选
允许值范围
允许的特殊字符
Seconds
0 - 59
* / , -
Minutes
0 - 59
* / , -
Hours
0 - 23
* / , -
Day of month
1 - 31
* / , - ?
Month
1 - 12 或 JAN - DEC
* / , -
Day of week
0 - 6 或 SUN - SAT
* / , - ?