Annotated and kubez-autoscaler-controller will maintain the HPA automatically for kubernetes resources.

Overview

Kubez-autoscaler Overview

kubez-autoscaler 通过为 deployment / statefulset 添加 annotations 的方式,自动维护对应 HorizontalPodAutoscaler 的生命周期.

Prerequisites

kubernetes 集群中, 需要先完成 Metrics Server 组件的安装,请参考 Metrics Server

kubectl top node/pod 验证 Metrics Server 已成功安装

# kubectl top node
NAME          CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%
kubez         333m         16%    1225Mi          65%

# kubectl top pod
NAME                     CPU(cores)   MEMORY(bytes)
test1-54cd855b77-q67h6   1m           3Mi

Installing

kubez-autoscaler 控制器的安装非常简单,通过 kubectl 执行 apply 如下文件即可完成安装,真正做到猩猩都能使用.

apiVersion: v1
kind: ServiceAccount
metadata:
  name: kubez
  namespace: default
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: kubez
rules:
  - apiGroups:
      - "*"
    resources:
      - horizontalpodautoscalers
      - deployments
      - statefulsets
      - endpoints
      - leases
    verbs:
      - get
      - list
      - watch
      - create
      - delete
      - update
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: kubez
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: kubez
subjects:
- kind: ServiceAccount
  name: kubez
  namespace: default
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    kubez.hpa.controller: kubez-autoscaler
  name: kubez-autoscaler-controller
  namespace: default
spec:
  replicas: 2
  selector:
    matchLabels:
      kubez.hpa.controller: kubez-autoscaler
  template:
    metadata:
      labels:
        kubez.hpa.controller: kubez-autoscaler
    spec:
      containers:
        - image: jacky06/kubez-autoscaler-controller:v0.0.1
          command:
            - kubez-autoscaler-controller
            - --leader-elect=true
          imagePullPolicy: IfNotPresent
          livenessProbe:
            failureThreshold: 8
            httpGet:
              host: 127.0.0.1
              path: /healthz
              port: 10256
              scheme: HTTP
            initialDelaySeconds: 15
            periodSeconds: 10
            successThreshold: 1
            timeoutSeconds: 15
          resources:
            requests:
              cpu: 100m
              memory: 90Mi
          name: kubez-autoscaler-controller
      serviceAccountName: kubez

然后通过 kubectl get pod -l kubez.hpa.controller=kubez-autoscaler 能看到 kubez-autoscaler 已经启动成功.

NAME                                          READY   STATUS    RESTARTS   AGE
kubez-autoscaler-controller-dbc4bc4d8-hwpqz   1/1     Running   0          20s
kubez-autoscaler-controller-dbc4bc4d8-tqxrl   1/1     Running   0          20s

Getting Started

deployment / statefulsetannotations 中添加所需注释即可自动创建对应的 HPA

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    ...
    # 可选,默认 minReplicas 为 1
    hpa.caoyingjunz.io/minReplicas: "2"  # MINPODS
    # 必填
    hpa.caoyingjunz.io/maxReplicas: "6"  # MAXPODS
    ...
    # 支持多种 TARGETS 类型,若开启,至少选择一种,可同时支持多个 TARGETS
    # CPU, in cores. (500m = .5 cores)
    # Memory, in bytes. (500Gi = 500GiB = 500 * 1024 * 1024 * 1024)

    # 使用率 examples
    cpu.hpa.caoyingjunz.io/targetAverageUtilization: "80"
    memory.hpa.caoyingjunz.io/targetAverageUtilization: "60"

    # 使用值 examples
    cpu.hpa.caoyingjunz.io/targetAverageValue: 600m
    memory.hpa.caoyingjunz.io/targetAverageValue: 60Mi

    # TODO: prometheus 暂不支持
    ...
  name: test1
  namespace: default
  ...

kubez-autoscaler-controller 会根据注释的变化,自动同步 HPA 的生命周期.

通过 kubectl get hpa test1 命令,可以看到 deployment / statefulset 关联的 HPA 被自动创建

NAME    REFERENCE          TARGETS            MINPODS   MAXPODS   REPLICAS   AGE
test1   Deployment/test1   6% / 60%           1         2         1          5h29m
test2   Deployment/test2   152%/50%, 0%/60%   2         3         3          34m
Issues
  • 非常有意思的项目!不过我有点好奇使用场景?🤔🤔

    非常有意思的项目!不过我有点好奇使用场景?🤔🤔

    想请教一下: 为什么要把 HPA 的配置放在 Annotation 里面? 如果为了同一配置管理,一个 YAML 里面本身可以放多个 YAML,这样感觉更清晰一点。

    恕我能力有限,目前看到的好处是:不同自己再设置引用对象,减少了这个操作。

    但是这样的实现方式带来了一个问题:用户如果自己在 HPA 里修改,是不是会被 Annotation 直接再覆盖回去?还是说下次 Annotation 更新时,才会更新 HPA 里的配置?但是后面这种方式会导致有段时间 Annotation 里的配置和 HPA 里的配置不同,可能会给用户困惑。

    help wanted 
    opened by willemswang 2
  • Compare RV to avoid unnecessary process.

    Compare RV to avoid unnecessary process.

    opened by crazytaxii 1
  • 可使用官方库去处理 Annotation

    可使用官方库去处理 Annotation

    // SetMetaDataAnnotation sets the annotation and value func SetMetaDataAnnotation(obj *ObjectMeta, ann string, value string) { if obj.Annotations == nil { obj.Annotations = make(map[string]string) } obj.Annotations[ann] = value }

    opened by caoyingjunz 1
  • Compare RV to avoid unnecessary process.

    Compare RV to avoid unnecessary process.

    Periodic resync will send update events for all known Deployments. Two different versions of the same Deployment will always have different RVs.

    reference: https://github.com/kubernetes/sample-controller/blob/v0.19.2/controller.go#L134

    opened by crazytaxii 0
An example of Kubernetes' Horizontal Pod Autoscaler using costume metrics.

Kubernetes Autoscaling Example In this project, I try to implement Horizontal Pod AutoscalerHPA provided by Kubernetes. The Horizontal Pod Autoscaler

Jaskeerat Singh Randhawa 8 Mar 15, 2022
Image clone controller is a kubernetes controller to safe guard against the risk of container images disappearing

Image clone controller image clone controller is a kubernetes controller to safe guard against the risk of container images disappearing from public r

Jayadeep KM 0 Oct 10, 2021
A Controller written in kubernetes sample-controller style which watches a custom resource named Bookstore

bookstore-sample-controller A Controller written in kubernetes sample-controller style which watches a custom resource named Bookstore. A resource cre

Abdullah Al Shaad 0 Jan 20, 2022
Simple HPA base NGINX Ingress

Simple HPA Base Ingress Access Log Support NGINX Ingress How to Use Requirement Kubernetes NGINX Ingress Quick Start 1. Deploy Change deploy.yamlENV,

有关部门 2 Dec 27, 2021
DigitalOcean Droplets target plugin for HashiCorp Nomad Autoscaler

Nomad DigitalOcean Droplets Autoscaler The do-droplets target plugin allows for the scaling of the Nomad cluster clients via creating and destroying D

Johan Siebens 37 Aug 4, 2022
The k8s-generic-webhook is a library to simplify the implementation of webhooks for arbitrary customer resources (CR) in the operator-sdk or controller-runtime.

k8s-generic-webhook The k8s-generic-webhook is a library to simplify the implementation of webhooks for arbitrary customer resources (CR) in the opera

Norwin Schnyder 6 Jul 12, 2022
Knative Controller which emits cloud events when Knative Resources change state

Knative Sample Controller Knative sample-controller defines a few simple resources that are validated by webhook and managed by a controller to demons

salaboy 2 Oct 2, 2021
Carrier is a Kubernetes controller for running and scaling game servers on Kubernetes.

Carrier is a Kubernetes controller for running and scaling game servers on Kubernetes. This project is inspired by agones. Introduction Genera

Open Cloud-native Game-application Initiative 30 Jul 28, 2022
network-node-manager is a kubernetes controller that controls the network configuration of a node to resolve network issues of kubernetes.

Network Node Manager network-node-manager is a kubernetes controller that controls the network configuration of a node to resolve network issues of ku

kakao 97 Aug 6, 2022
A k8s vault webhook is a Kubernetes webhook that can inject secrets into Kubernetes resources by connecting to multiple secret managers

k8s-vault-webhook is a Kubernetes admission webhook which listen for the events related to Kubernetes resources for injecting secret directly from sec

Opstree Container Kit 111 Apr 28, 2022
An Ingress controller for Kubernetes using NGINX as a reverse proxy and load balancer

NGINX Ingress Controller Overview ingress-nginx is an Ingress controller for Kubernetes using NGINX as a reverse proxy and load balancer. Learn more a

null 0 Nov 15, 2021
A kubernetes controller that watches the Deployments and “caches” the images

image-cloner This is just an exercise. It's a kubernetes controller that watches

Luca Sepe 1 Dec 20, 2021
Sesame: an Ingress controller for Kubernetes that works by deploying the Envoy proxy as a reverse proxy and load balancer

Sesame Overview Sesame is an Ingress controller for Kubernetes that works by dep

Sesame 1 Dec 28, 2021
A Kubernetes Terraform Controller

Terraform Controller Terraform Controller is a Kubernetes Controller for Terraform, which can address the requirement of Using Terraform HCL as IaC mo

Open Application Model 88 Jul 11, 2022
the simplest testing framework for Kubernetes controller.

KET(Kind E2e Test framework) KET is the simplest testing framework for Kubernetes controller. KET is available as open source software, and we look fo

Riita 37 Jun 27, 2022
Kubernetes workload controller for container image deployment

kube-image-deployer kube-image-deployer는 Docker Registry의 Image:Tag를 감시하는 Kubernetes Controller입니다. Keel과 유사하지만 단일 태그만 감시하며 더 간결하게 동작합니다. Container, I

PUBG Corporation 2 Mar 8, 2022
Kubernetes Admission controller for golang

KCAdm Kubernetes Admission controller Test locally First create the required cer

Christos Kotsis 0 Dec 23, 2021
Kubernetes controller for backing up public container images to our own registry repository

image-clone-controller Kubernetes controller which watches applications (Deployment and DaemonSet) and "caches" the images (public container images) b

Sahadat Hossain 8 Aug 2, 2022
Kubernetes Admission Controller Demo: Validating Webhook for Namespace lifecycle events

Kubernetes Admission Controller Based on How to build a Kubernetes Webhook | Admission controllers Local Kuberbetes cluster # create kubernetes cluste

Marco Lehmann 2 Feb 27, 2022