前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >原 荐 Kubernetes HPA Con

原 荐 Kubernetes HPA Con

作者头像
Walton
发布于 2018-04-13 08:53:45
发布于 2018-04-13 08:53:45
2K00
代码可运行
举报
文章被收录于专栏:KubernetesKubernetes
运行总次数:0
代码可运行

Author: xidianwangtao@gmail.com

更多关于kubernetes的深入文章,请看我csdn或者oschina的博客主页。

关于kubernetes HPA Controller的工作原理,请参考我这篇博文

源码目录结构分析

HorizontalPodAutoscaler(以下简称HPA)的主要代码如下,主要涉及的文件不多。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
cmd/kube-controller-manager/app/autoscaling.go    // HPA Controller的启动代码


/pkg/controller/podautoscaler
.
├── BUILD
├── OWNERS
├── doc.go
├── horizontal.go    // podautoscaler的核心代码,包括其创建和运行的代码
├── horizontal_test.go
├── metrics
│   ├── BUILD
│   ├── metrics_client.go
│   ├── metrics_client_test.go
│   ├── metrics_client_test.go.orig
│   ├── metrics_client_test.go.rej
│   └── utilization.go
├── replica_calculator.go   // ReplicaCaculator的创建,以及根据cpu/metrics计算replicas的方法
└── replica_calculator_test.go

其中,horizontal.go和replica_calculator.go是最核心的文件,他们对应的Structure如下:

  • horizontal.go
  • replica_calculator.go

源码分析

HPA Controller同其他Controller一样,都是在kube-controller-manager启动时完成初始化并启动的,如下代码所示。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
cmd/kube-controller-manager/app/controllermanager.go:224

func newControllerInitializers() map[string]InitFunc {
	controllers := map[string]InitFunc{}
	
	...
	
	controllers["horizontalpodautoscaling"] = startHPAController
	
	...

	return controllers
}

kube-controller-manager启动时会initial一堆的controllers,对于HPA controller,它的启动就交给startHPAController了。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
cmd/kube-controller-manager/app/autoscaling.go:29

func startHPAController(ctx ControllerContext) (bool, error) {
	
	...
	
	// HPA Controller需要集群已经部署Heapster,由Heapster提供监控数据,来进行replicas的计算。
	metricsClient := metrics.NewHeapsterMetricsClient(
		hpaClient,
		metrics.DefaultHeapsterNamespace,
		metrics.DefaultHeapsterScheme,
		metrics.DefaultHeapsterService,
		metrics.DefaultHeapsterPort,
	)
	
	// 创建ReplicaCaculator,后面会用它来计算desired replicas。
	replicaCalc := podautoscaler.NewReplicaCalculator(metricsClient, hpaClient.Core())
	
	// 创建HPA Controller,并启动goroutine执行其Run方法,开始工作。
	go podautoscaler.NewHorizontalController(
		hpaClient.Core(),
		hpaClient.Extensions(),
		hpaClient.Autoscaling(),
		replicaCalc,
		ctx.Options.HorizontalPodAutoscalerSyncPeriod.Duration,
	).Run(ctx.Stop)
	
	return true, nil
}

首先我们来看看NewHorizontalController创建HPA Controller的代码。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
pkg/controller/podautoscaler/horizontal.go:112

func NewHorizontalController(evtNamespacer v1core.EventsGetter, scaleNamespacer unversionedextensions.ScalesGetter, hpaNamespacer unversionedautoscaling.HorizontalPodAutoscalersGetter, replicaCalc *ReplicaCalculator, resyncPeriod time.Duration) *HorizontalController {
	
	...

	// 构建HPA Controller
	controller := &HorizontalController{
		replicaCalc:     replicaCalc,
		eventRecorder:   recorder,
		scaleNamespacer: scaleNamespacer,
		hpaNamespacer:   hpaNamespacer,
	}
	
	// 创建Informer,配置对应的ListWatch Func,及其对应的EventHandler,用来监控HPA Resource的Add和Update事件。newInformer是HPA的核心代码入口。
	store, frameworkController := newInformer(controller, resyncPeriod)
	controller.store = store
	controller.controller = frameworkController

	return controller
}

我们有必要来看看HPA Controller struct的定义:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
pkg/controller/podautoscaler/horizontal.go:59

type HorizontalController struct {
	scaleNamespacer unversionedextensions.ScalesGetter
	hpaNamespacer   unversionedautoscaling.HorizontalPodAutoscalersGetter

	replicaCalc   *ReplicaCalculator
	eventRecorder record.EventRecorder

	// A store of HPA objects, populated by the controller.
	store cache.Store
	// Watches changes to all HPA objects.
	controller *cache.Controller
}
  • scaleNamespacer其实是一个ScaleInterface,包括Scale subresource的Get和Update接口。
  • hpaNamespacer是HorizontalPodAutoscalerInterface,包括HorizontalPodAutoscaler的Create, Update, UpdateStatus, Delete, Get, List, Watch等接口。
  • replicaCalc根据Heapster提供的监控数据,计算对应desired replicas。 pkg/controller/podautoscaler/replica_calculator.go:31 type ReplicaCalculator struct { metricsClient metricsclient.MetricsClient podsGetter v1core.PodsGetter }
  • store和controller:controller用来watch HPA objects,并更新到store这个cache中。

上面提到了Scale subresource,那是个什么东西?好吧,我们得看看Scale的定义。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
pkg/apis/extensions/v1beta1/types.go:56

// represents a scaling request for a resource.
type Scale struct {
	metav1.TypeMeta `json:",inline"`
	// Standard object metadata; More info: http://releases.k8s.io/HEAD/docs/devel/api-conventions.md#metadata.
	// +optional
	v1.ObjectMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`

	// defines the behavior of the scale. More info: http://releases.k8s.io/HEAD/docs/devel/api-conventions.md#spec-and-status.
	// +optional
	Spec ScaleSpec `json:"spec,omitempty" protobuf:"bytes,2,opt,name=spec"`

	// current status of the scale. More info: http://releases.k8s.io/HEAD/docs/devel/api-conventions.md#spec-and-status. Read-only.
	// +optional
	Status ScaleStatus `json:"status,omitempty" protobuf:"bytes,3,opt,name=status"`
}

// describes the attributes of a scale subresource
type ScaleSpec struct {
	// desired number of instances for the scaled object.
	Replicas int `json:"replicas,omitempty"`
}

// represents the current status of a scale subresource.
type ScaleStatus struct {
	// actual number of observed instances of the scaled object.
	Replicas int `json:"replicas"`

	// label query over pods that should match the replicas count.
	Selector map[string]string `json:"selector,omitempty"`
}
  • Scale struct作为一次scale动作的请求数据。
  • 其中Spec定义的是desired replicas number。
  • ScaleStatus定义了current replicas number。

看完了HorizontalController的结构后,接着看看NewHorizontalController中调用的newInformer。在上面的注释中,我提到newInformer是整个HPA的核心代码入口。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
pkg/controller/podautoscaler/horizontal.go:75

func newInformer(controller *HorizontalController, resyncPeriod time.Duration) (cache.Store, *cache.Controller) {
	return cache.NewInformer(
	
		// 配置ListFucn和WatchFunc,用来定期List和watch HPA resource。
		&cache.ListWatch{
			ListFunc: func(options v1.ListOptions) (runtime.Object, error) {
				return controller.hpaNamespacer.HorizontalPodAutoscalers(v1.NamespaceAll).List(options)
			},
			WatchFunc: func(options v1.ListOptions) (watch.Interface, error) {
				return controller.hpaNamespacer.HorizontalPodAutoscalers(v1.NamespaceAll).Watch(options)
			},
		},
		
		// 定义期望收到的object为HorizontalPodAutoscaler
		&autoscaling.HorizontalPodAutoscaler{},
		
		// 定义定期List的周期
		resyncPeriod,
		
		// 配置HPA resource event的Handler(AddFunc, UpdateFunc)
		cache.ResourceEventHandlerFuncs{
			AddFunc: func(obj interface{}) {
				hpa := obj.(*autoscaling.HorizontalPodAutoscaler)
				hasCPUPolicy := hpa.Spec.TargetCPUUtilizationPercentage != nil
				_, hasCustomMetricsPolicy := hpa.Annotations[HpaCustomMetricsTargetAnnotationName]
				if !hasCPUPolicy && !hasCustomMetricsPolicy {
					controller.eventRecorder.Event(hpa, v1.EventTypeNormal, "DefaultPolicy", "No scaling policy specified - will use default one. See documentation for details")
				}
				
				// 根据监控调整hpa的数据
				err := controller.reconcileAutoscaler(hpa)
				if err != nil {
					glog.Warningf("Failed to reconcile %s: %v", hpa.Name, err)
				}
			},
			UpdateFunc: func(old, cur interface{}) {
				hpa := cur.(*autoscaling.HorizontalPodAutoscaler)
				
				// 根据监控调整hpa的数据
				err := controller.reconcileAutoscaler(hpa)
				if err != nil {
					glog.Warningf("Failed to reconcile %s: %v", hpa.Name, err)
				}
			},
			// We are not interested in deletions.
		},
	)
}

newInformer的代码也不长嘛,简单说来,就是配置了HPA resource的ListWatch的Func,注册HPA resource 的Add和Update Event的handler Func。

最终通过调用reconcileAutoscaler来矫正hpa的数据。

上面代码中,将HPA resource的ListWatch Func注册为HorizontalPodAutoscaler Interface定义的List和Watch接口。

等等,说了这么多,怎么还没看到HorizontalPodAutoscaler struct的定义呢!好吧,下面就来看看,正好HorizontalPodAutoscaler Interface中出现了。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
pkg/apis/autoscaling/v1/types.go:76

// configuration of a horizontal pod autoscaler.
type HorizontalPodAutoscaler struct {
	metav1.TypeMeta `json:",inline"`
	// Standard object metadata. More info: http://releases.k8s.io/HEAD/docs/devel/api-conventions.md#metadata
	// +optional
	v1.ObjectMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`

	// behaviour of autoscaler. More info: http://releases.k8s.io/HEAD/docs/devel/api-conventions.md#spec-and-status.
	// +optional
	Spec HorizontalPodAutoscalerSpec `json:"spec,omitempty" protobuf:"bytes,2,opt,name=spec"`

	// current information about the autoscaler.
	// +optional
	Status HorizontalPodAutoscalerStatus `json:"status,omitempty" protobuf:"bytes,3,opt,name=status"`
}
  • Spec HorizontalPodAutoscalerSpec存的是hpa的描述信息,是可以通过kube-controller-manager配置对应flag的信息。包括最小副本数MinReplicas,最大副本数MaxReplicas,hpa对应的所有pods的平均的百分比形式的目标CPU利用率TargetCPUUtilizationPercentage。 pkg/apis/autoscaling/v1/types.go:36 // specification of a horizontal pod autoscaler. type HorizontalPodAutoscalerSpec struct { // reference to scaled resource; horizontal pod autoscaler will learn the current resource consumption // and will set the desired number of pods by using its Scale subresource. ScaleTargetRef CrossVersionObjectReference json:"scaleTargetRef" protobuf:"bytes,1,opt,name=scaleTargetRef" // lower limit for the number of pods that can be set by the autoscaler, default 1. // +optional MinReplicas *int32 json:"minReplicas,omitempty" protobuf:"varint,2,opt,name=minReplicas" // upper limit for the number of pods that can be set by the autoscaler; cannot be smaller than MinReplicas. MaxReplicas int32 json:"maxReplicas" protobuf:"varint,3,opt,name=maxReplicas" // target average CPU utilization (represented as a percentage of requested CPU) over all the pods; // if not specified the default autoscaling policy will be used. // +optional TargetCPUUtilizationPercentage *int32 json:"targetCPUUtilizationPercentage,omitempty" protobuf:"varint,4,opt,name=targetCPUUtilizationPercentage" }
  • Status HorizontalPodAutoscalerStatu存的是HPA的当前状态数据,包括前后两次scale的时间间隔ObservedGeneration,上一次scale的时间戳LastScaleTime,当前副本数CurrentReplicas,期望副本数DesiredReplicas,hpa对应的所有pods的平均的百分比形式的当前CPU利用率。 pkg/apis/autoscaling/v1/types.go:52 // current status of a horizontal pod autoscaler type HorizontalPodAutoscalerStatus struct { // most recent generation observed by this autoscaler. // +optional ObservedGeneration *int64 json:"observedGeneration,omitempty" protobuf:"varint,1,opt,name=observedGeneration" // last time the HorizontalPodAutoscaler scaled the number of pods; // used by the autoscaler to control how often the number of pods is changed. // +optional LastScaleTime *metav1.Time json:"lastScaleTime,omitempty" protobuf:"bytes,2,opt,name=lastScaleTime" // current number of replicas of pods managed by this autoscaler. CurrentReplicas int32 json:"currentReplicas" protobuf:"varint,3,opt,name=currentReplicas" // desired number of replicas of pods managed by this autoscaler. DesiredReplicas int32 json:"desiredReplicas" protobuf:"varint,4,opt,name=desiredReplicas" // current average CPU utilization over all pods, represented as a percentage of requested CPU, // e.g. 70 means that an average pod is using now 70% of its requested CPU. // +optional CurrentCPUUtilizationPercentage *int32 json:"currentCPUUtilizationPercentage,omitempty" protobuf:"varint,5,opt,name=currentCPUUtilizationPercentage" }

newInformer的代码可见,不管hpa resource的event为Add或者update,最终都是调用reconcileAutoscaler来触发HorizontalPodAutoscaler数据的更新。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
pkg/controller/podautoscaler/horizontal.go:272

func (a *HorizontalController) reconcileAutoscaler(hpa *autoscaling.HorizontalPodAutoscaler) error {
	...

	// 获取对应resource的scale subresource数据。
	scale, err := a.scaleNamespacer.Scales(hpa.Namespace).Get(hpa.Spec.ScaleTargetRef.Kind, hpa.Spec.ScaleTargetRef.Name)
	
	...
	
	// 得到当前副本数
	currentReplicas := scale.Status.Replicas

	cpuDesiredReplicas := int32(0)
	cpuCurrentUtilization := new(int32)
	cpuTimestamp := time.Time{}

	cmDesiredReplicas := int32(0)
	cmMetric := ""
	cmStatus := ""
	cmTimestamp := time.Time{}

	desiredReplicas := int32(0)
	rescaleReason := ""
	timestamp := time.Now()

	rescale := true

	// 如果期望副本数为0,这不进行scale操作。
	if scale.Spec.Replicas == 0 {
		// Autoscaling is disabled for this resource
		desiredReplicas = 0
		rescale = false
	} 
	
	// 期望副本数不能超过hpa中配置的最大副本数
	else if currentReplicas > hpa.Spec.MaxReplicas {
		rescaleReason = "Current number of replicas above Spec.MaxReplicas"
		desiredReplicas = hpa.Spec.MaxReplicas
	} 
	
	// 期望副本数不能低于配置的最小副本数
	else if hpa.Spec.MinReplicas != nil && currentReplicas < *hpa.Spec.MinReplicas {
		rescaleReason = "Current number of replicas below Spec.MinReplicas"
		desiredReplicas = *hpa.Spec.MinReplicas
	} 
	
	// 期望副本数最少为1
	else if currentReplicas == 0 {
		rescaleReason = "Current number of replicas must be greater than 0"
		desiredReplicas = 1
	} 
	// 如果当前副本数在Min和Max之间,则需要根据cpu或者custom metrics(如果加了对应的Annotation)数据进行算法计算得到期望副本数。
	else {
		// All basic scenarios covered, the state should be sane, lets use metrics.
		cmAnnotation, cmAnnotationFound := hpa.Annotations[HpaCustomMetricsTargetAnnotationName]

		if hpa.Spec.TargetCPUUtilizationPercentage != nil || !cmAnnotationFound {
		
			// 根据cpu利用率计算期望副本数
			cpuDesiredReplicas, cpuCurrentUtilization, cpuTimestamp, err = a.computeReplicasForCPUUtilization(hpa, scale)
			if err != nil {
			
				// 更新hpa的当前副本数
				a.updateCurrentReplicasInStatus(hpa, currentReplicas)
				return fmt.Errorf("failed to compute desired number of replicas based on CPU utilization for %s: %v", reference, err)
			}
		}

		if cmAnnotationFound {
		
			// 根据custom metrics数据计算期望副本数
			cmDesiredReplicas, cmMetric, cmStatus, cmTimestamp, err = a.computeReplicasForCustomMetrics(hpa, scale, cmAnnotation)
			if err != nil {
			
				// 更新hpa的当前副本数
				a.updateCurrentReplicasInStatus(hpa, currentReplicas)
				return fmt.Errorf("failed to compute desired number of replicas based on Custom Metrics for %s: %v", reference, err)
			}
		}

		// 取cpu和custom metric得到的期望副本数的最大值作为最终的desired replicas,并且要在min和max范围内。
		rescaleMetric := ""
		if cpuDesiredReplicas > desiredReplicas {
			desiredReplicas = cpuDesiredReplicas
			timestamp = cpuTimestamp
			rescaleMetric = "CPU utilization"
		}
		if cmDesiredReplicas > desiredReplicas {
			desiredReplicas = cmDesiredReplicas
			timestamp = cmTimestamp
			rescaleMetric = cmMetric
		}
		if desiredReplicas > currentReplicas {
			rescaleReason = fmt.Sprintf("%s above target", rescaleMetric)
		}
		if desiredReplicas < currentReplicas {
			rescaleReason = "All metrics below target"
		}

		if hpa.Spec.MinReplicas != nil && desiredReplicas < *hpa.Spec.MinReplicas {
			desiredReplicas = *hpa.Spec.MinReplicas
		}

		//  never scale down to 0, reserved for disabling autoscaling
		if desiredReplicas == 0 {
			desiredReplicas = 1
		}

		if desiredReplicas > hpa.Spec.MaxReplicas {
			desiredReplicas = hpa.Spec.MaxReplicas
		}

		// Do not upscale too much to prevent incorrect rapid increase of the number of master replicas caused by
		// bogus CPU usage report from heapster/kubelet (like in issue #32304).
		if desiredReplicas > calculateScaleUpLimit(currentReplicas) {
			desiredReplicas = calculateScaleUpLimit(currentReplicas)
		}

		// 根据currentReplicas和desiredReplicas的对比,以及scale时间是否满足配置间隔要求,决定是否此时需要rescale
		rescale = shouldScale(hpa, currentReplicas, desiredReplicas, timestamp)
	}

	if rescale {
		scale.Spec.Replicas = desiredReplicas
		// 执行ScaleInterface的Update接口,触发调用API Server的对应resource的scale subresource的数据更新。其实最终会去修改对应rc或者deployment的replicas,然后由rc或deployment Controller去最终扩容或者缩容,使得副本数达到新的期望值。
		_, err = a.scaleNamespacer.Scales(hpa.Namespace).Update(hpa.Spec.ScaleTargetRef.Kind, scale)
		if err != nil {
			a.eventRecorder.Eventf(hpa, v1.EventTypeWarning, "FailedRescale", "New size: %d; reason: %s; error: %v", desiredReplicas, rescaleReason, err.Error())
			return fmt.Errorf("failed to rescale %s: %v", reference, err)
		}
		a.eventRecorder.Eventf(hpa, v1.EventTypeNormal, "SuccessfulRescale", "New size: %d; reason: %s", desiredReplicas, rescaleReason)
		glog.Infof("Successfull rescale of %s, old size: %d, new size: %d, reason: %s",
			hpa.Name, currentReplicas, desiredReplicas, rescaleReason)
	} else {
		desiredReplicas = currentReplicas
	}

	// 更新hpa resource的status数据
	return a.updateStatus(hpa, currentReplicas, desiredReplicas, cpuCurrentUtilization, cmStatus, rescale)
}

上面reconcileAutoscaler的代码很重要,把想说的都写到对应的注释了。其中computeReplicasForCPUUtilizationcomputeReplicasForCustomMetrics需要单独提出来看看,因为这两个方法是HPA算法的体现,实际上最终算法是在pkg/controller/podautoscaler/replica_calculator.go:45#GetResourceReplicaspkg/controller/podautoscaler/replica_calculator.go:153#GetMetricReplicas实现的:

  • pkg/controller/podautoscaler/replica_calculator.go:45#GetResourceReplicas负责根据heapster提供的cpu利用率数据计算得到desired replicas number。
  • pkg/controller/podautoscaler/replica_calculator.go:153#GetMetricReplicas负责根据heapster提供的custom raw metric数据计算得到desired replicas number。

具体关于HPA算法的源码分析,我后续会单独写一篇博客,有兴趣的可以关注(对于绝大部分同学来说没必要关注,除非需要定制HPA算法时,才会具体去分析)。

总而言之,根据cpu和custom metric数据分别计算得到desired replicas后,取两者最大的值,但不能超过配置的Max Replicas。

稍等稍等,计算出了desired replicas还还够,我们还要通过shouldScale看看现在距离上一次弹性伸缩的时间间隔是否满足条件:

  • 两次缩容的间隔不得小于5min。
  • 两次扩容的间隔不得小于3min。

shouldScale的代码如下:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
pkg/controller/podautoscaler/horizontal.go:387

...

var downscaleForbiddenWindow = 5 * time.Minute
var upscaleForbiddenWindow = 3 * time.Minute

...

func shouldScale(hpa *autoscaling.HorizontalPodAutoscaler, currentReplicas, desiredReplicas int32, timestamp time.Time) bool {
	if desiredReplicas == currentReplicas {
		return false
	}

	if hpa.Status.LastScaleTime == nil {
		return true
	}

	// Going down only if the usageRatio dropped significantly below the target
	// and there was no rescaling in the last downscaleForbiddenWindow.
	if desiredReplicas < currentReplicas && hpa.Status.LastScaleTime.Add(downscaleForbiddenWindow).Before(timestamp) {
		return true
	}

	// Going up only if the usage ratio increased significantly above the target
	// and there was no rescaling in the last upscaleForbiddenWindow.
	if desiredReplicas > currentReplicas && hpa.Status.LastScaleTime.Add(upscaleForbiddenWindow).Before(timestamp) {
		return true
	}
	return false
}

只有满足这个条件后,接着才会调用Scales.Update接口与API Server交互,完成Scale对应的RC的replicas的设置。以rc Controller为例(deployment Controller的雷同),API Server对应的Scales.Update接口的实现逻辑如下:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
pkg/registry/core/rest/storage_core.go:91
func (c LegacyRESTStorageProvider) NewLegacyRESTStorage(restOptionsGetter generic.RESTOptionsGetter) (LegacyRESTStorage, genericapiserver.APIGroupInfo, error) {
	...
	if autoscalingGroupVersion := (schema.GroupVersion{Group: "autoscaling", Version: "v1"}); registered.IsEnabledVersion(autoscalingGroupVersion) {
		apiGroupInfo.SubresourceGroupVersionKind["replicationcontrollers/scale"] = autoscalingGroupVersion.WithKind("Scale")
	}

	...
	restStorageMap := map[string]rest.Storage{
		...
		
		"replicationControllers":        controllerStorage.Controller,
		"replicationControllers/status": controllerStorage.Status,
		
		...
	}
	return restStorage, apiGroupInfo, nil
}



pkg/registry/core/controller/etcd/etcd.go:124

func (r *ScaleREST) Update(ctx api.Context, name string, objInfo rest.UpdatedObjectInfo) (runtime.Object, bool, error) {
	rc, err := r.registry.GetController(ctx, name, &metav1.GetOptions{})
	if err != nil {
		return nil, false, errors.NewNotFound(autoscaling.Resource("replicationcontrollers/scale"), name)
	}

	oldScale := scaleFromRC(rc)
	obj, err := objInfo.UpdatedObject(ctx, oldScale)
	if err != nil {
		return nil, false, err
	}

	if obj == nil {
		return nil, false, errors.NewBadRequest("nil update passed to Scale")
	}
	scale, ok := obj.(*autoscaling.Scale)
	if !ok {
		return nil, false, errors.NewBadRequest(fmt.Sprintf("wrong object passed to Scale update: %v", obj))
	}

	if errs := validation.ValidateScale(scale); len(errs) > 0 {
		return nil, false, errors.NewInvalid(autoscaling.Kind("Scale"), scale.Name, errs)
	}
	
	// 设置rc对应spec.replicas为Scale中的期望副本数
	rc.Spec.Replicas = scale.Spec.Replicas
	rc.ResourceVersion = scale.ResourceVersion
	
	// 更新到etcd
	rc, err = r.registry.UpdateController(ctx, rc)
	if err != nil {
		return nil, false, err
	}
	return scaleFromRC(rc), false, nil
}

了解kubernetes rc Controller的同学很清楚,修改rc的replicas后,会被rc Controller watch到,然后触发rc Controller去执行创建或者销毁对应差额数量的replicas,最终使得其副本数达到HPA计算得到的期望值。也就是说,最终由rc controller去执行具体的扩容或缩容动作。

最后,来看看HorizontalController的Run方法:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
pkg/controller/podautoscaler/horizontal.go:130

func (a *HorizontalController) Run(stopCh <-chan struct{}) {
	defer utilruntime.HandleCrash()
	glog.Infof("Starting HPA Controller")
	go a.controller.Run(stopCh)
	<-stopCh
	glog.Infof("Shutting down HPA Controller")
}

很简单,就是负责 HPA Resource的ListWatch,将change更新到对应的store(cache)。

HPA Resource的同步周期通过--horizontal-pod-autoscaler-sync-period设置,默认值为30s。

总结(流程图)

更多关于kubernetes的深入文章,请看我csdn或者oschina的博客主页。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
韩松、王威廉、杨迪一、方飞、张含望入选,2020 IEEE智能系统十大AI青年科学家出炉
机器之心报道 编辑:泽南、小舟 IEEE 的「AI's 10 to Watch」青年学者榜单出炉了。相比上次,华人学者的比例再次提高。 IEEE 的「十大 AI 青年科学家」榜单每两年推出一次,旨在介绍人工智能领域中年轻而有抱负的科学家。今年 4 月,《IEEE 智能系统》开始接受全球范围内的提名,要求候选人必须在 2014 年之后获得博士学位提名,最终的入选名单由杂志编委会和顾问团队提出。在超过 20 人的名单中,基于研究质量、声誉、影响力、专家认可度及多样性,选出了最后入围的十人。 在 2018 年的
机器之心
2023/03/29
6760
韩松、王威廉、杨迪一、方飞、张含望入选,2020 IEEE智能系统十大AI青年科学家出炉
业界 | 今秋开课!卡内基梅隆大学将开设美国首个人工智能本科学位
机器之心编译 机器之心编辑部 目前,很多美国大学都在计算机科学和计算机工程等学位课程内提供人工智能方向,但以「人工智能」命名的学位还未出现。近日,卡内基梅隆大学(CMU)终于宣布将开设美国首个
机器之心
2018/06/12
4330
全球高校人工智能实力排名出炉,清华力压卡内基梅隆占据第一宝座
近日,麻省理工学院马萨诸塞校区计算机与信息科学学院教授 Emery Berger 发布的全球院校计算机科学领域实力排名的开源项目 CSranking 更新。
AI科技大本营
2018/07/23
2930
全球高校人工智能实力排名出炉,清华力压卡内基梅隆占据第一宝座
2018全球大学计算机科学与人工智能排名:CMU名列第一
机器之心报道 参与:李泽南 计算机科学排名顶级学校排名(CSRankings)旨在帮助人们了解全球各家大学在计算机科学领域体系与师资方面的实力。不同于 US News 和 World Report 的方法(仅仅基于调查),该排名完全基于研究指标,其度量了绝大多数院校教员在计算机科学领域的各大顶会所发布的论文数量。 该项目由麻省大学阿默斯特分校的 Emery Berger 教授发起。由于顶会论文发表难度很高,这种统计排名的方式被认为难以进行造假。目前,卡耐基梅隆大学(CMU)、麻省理工学院(MIT)与斯坦福大
机器之心
2018/05/10
6120
卡耐基梅隆大学利用人工智能、机器人等技术变革农业
据《福布斯》网站报道,卡耐基梅隆大学的研究人员正在利用人工智能、机器人等技术变革农业。 报道表示,预计到2050年世界人口将达到97亿。中国和印度是世界上人口最多的两个国家,其人口总数都在10亿左右。预计四年后,到2022年,印度会超过中国,成为世界上人口最多的国家。这意味着,为了养活地球上的人口,避免出现全球性粮食危机,我们需要更智能的新方法来种植粮食,帮助调节我们对土地、水资源和能源的使用。 卡耐基梅隆大学(Carnegie Mellon University)机器人研究所(Robotics Insti
人工智能快报
2018/03/14
1.1K0
卡耐基梅隆大学利用人工智能、机器人等技术变革农业
2018全球大学人工智能排名发布,中国高校表现强势!
近日,麻省理工学院马萨诸塞校区计算机与信息科学学院教授EmeryBerger发布一个全球院校计算机科学领域实力排名的开源项目CSranking更新了。目前,卡耐基梅隆大学(CMU)、麻省理工学院(MIT)与斯坦福大学名列全球前三。 其中,清华大学位列第13名,北京大学第30名,上海交通大学此次排行第48名。 排名分数计算依据 不同于USNews和WorldReport的方法(仅仅基于调查),CSranking的排名主要依据各个高校在计算机领域的顶级学术会议发表的论文数量,度量了绝大多数院校教员在计算机科学领
WZEARW
2018/04/08
8350
2018全球大学人工智能排名发布,中国高校表现强势!
学界丨Yann LeCun、Jeff Dean频繁亮相普及人工智能知识,全民AI时代来临|AI科技评论周刊
前段时间,吴恩达连续给业界人士写了两篇公开信,为各行各业普及了人工智能在行业中的应用。而在最近,谷歌大脑负责人 Jeff Dean 和 Yann LeCun 也频繁在公众场合露脸,为大众解读机器学习、
AI科技评论
2018/03/09
1K0
学界丨Yann LeCun、Jeff Dean频繁亮相普及人工智能知识,全民AI时代来临|AI科技评论周刊
《安全的人工智能系统开发指南》解读
11月26日,由英国国家网络安全中心(NCSC)、美国网络安全与基础设施安全局(CISA)联合美国国家安全局、美国联邦调查局以及澳大利亚、加拿大、新西兰、德国、法国、日本等10余个国家的网络和/或信息安全部门发布了“安全的人工智能系统开发指南”(Guidelines for secure AI system development [1])。亚马逊、微软、IBM、google等10余家机构参与了指南的制定。该指南旨在为使用人工智能(AI)的任何系统的提供商提供指导原则,帮助提供商构建能够按预期工作、在需要时可用、在不泄露敏感数据的前提下工作的人工智能系统。本文对其主要内容进行解读,以供普通读者阅读了解。人工智能专家或人工智能系统开发人员可根据需要,参阅原文[1]以获取更详细、全面的信息。此外,指南的末尾注记中列出的资料可作为延伸阅读材料,感兴趣的读者可以自行参阅。
绿盟科技研究通讯
2023/12/11
6480
《安全的人工智能系统开发指南》解读
专家称人工集群智能有助于应对人工智能的潜在威胁
最近关于人工智能威胁的讨论越来越多,斯蒂芬·霍金、比尔·盖茨、伊隆·马斯克、史蒂夫·沃兹尼亚克等世界各地名人已经敲响了警钟,称人类可能会失去对人工智能技术的控制,因为人工智能技术创造的是具有自主思想的
人工智能快报
2018/03/07
6450
CMU开设美国首个人工智能本科专业
---- 新智元报道 来源:CMU 编辑:克雷格 【新智元导读】今年秋天开始,卡内基梅隆大学(CMU)计算机科学学院将开设全美首个人工智能本科专业,计划招收大约100名学生,每个班级大约30-35人。2018年秋季,二年级和大三学生可以申请该课程。 卡内基梅隆大学(CMU)计算机科学学院(School of Computer Science,SCS)将在今年秋季开始,提供一个新的人工智能本科学位,为学生们提供如何将大量数据转化为可操作决策的深入知识。 这是美国大学首次开设人工智能本科专业,目的是应对
新智元
2018/05/29
1.2K0
监管人工智能系统:风险、挑战、能力和策略
作者: Matthew U. Scherer   译者: 曹建峰 腾讯研究院法律研究中心研究员                    李金磊 腾讯研究院法律研究中心助理研究员    来源: Harvard journal of law and technology 引言   智能机器时代,人工智能正在全方位侵入我们的生活。人工智能(artificial intelligence,简称AI)正在替代那些以前只能由具有专业知识或者获得政府颁发的许可证的人员才能完成的任务。美国一些州以
腾讯研究院
2018/02/01
1K0
【1996~2016】盘点 20 年 AAAI 人工智能最佳论文
【新智元导读】新智元汇集计算机科学领域1996年到2016年人工智能领域最佳论文,附上题目及摘要的中文翻译,并对这些作品进行系统考察,从中可以看出人工智能这门学科的一些发展走势。过去20年来(空缺年份表示当年没有最佳论文),AAAI 最佳论文覆盖面完善,包括推理、知识、规划、学习、交流、感知、移动和操作物体。论文中使用最多的还是优化、逻辑推演等方法,与刚刚结束的 IJCAI'16相比,机器学习在其中光芒并没那么显著。 2012年,浙江大学EAGLE-Lab及CAD&CG 实验室合作的《基于数据重构的文档摘要
新智元
2018/03/26
1.4K0
多家IT巨头公司就人工智能建立合作组织
全球IT产业的巨头谷歌、微软、IBM、脸书和亚马逊宣布就人工智能联合建立了一个非盈利性组织,其中谷歌子公司DeepMind以独立成员身份参与。 这个新成立的非盈利组织旨在促进公众对人工智能技术(AI)的了解,并就人工智能领域的挑战和机遇制定最佳实践。该组织被命名为“人工智能惠及人类与社会合作伙伴组织”(Partnership on Artificial Intelligence to Benefit People and Society,简称“人工智能合作组织”),学术机构、非盈利组织及政治和伦理道德界的专
人工智能快报
2018/03/07
8560
清华人工智能研究院成立,张钹姚期智分别任院长和主任
今日,清华大学人工智能研究院成立仪式暨清华-谷歌 AI 学术研讨会在清华举行。据介绍,张钹院士将担任院长,图灵奖得主姚期智院士任学术委员会主任,同时,谷歌人工智能部门负责人 Jeff Dean 为清华大学计算机学科顾问委员会委员。
AI科技大本营
2018/07/23
5520
清华人工智能研究院成立,张钹姚期智分别任院长和主任
人工智能在医疗:改善药物依从性、虚拟医疗助手、智能看护、智能药物研发...
摘自:动脉网 网站:http://www.vcbeat.net/ 人工智能用来提高健康医疗服务的效率和自动化程度。人工智能技术的发展在过去备受质疑,然后如今我们发现大数据技术正在推进人工智能的进程,在医疗健康领域也是如此。 分析患者行为,制定个性化肿瘤治疗方案 例如,两位乳腺癌患者可能会得到相同的治疗方案,但其实两者的身体情况可能完全不同。 其中一个可能是马拉松长跑者,另外一个是喜欢安静的读书的人;一个可能是吸烟者,另一个也许是个注重养生的人;一个可能都60多岁了,另一个也就刚刚40。这样的情况在我们身边
大数据文摘
2018/05/21
7510
关于人工智能应该知道的十件事
来源:InfoQ 作者:张天雷 网站:http://www.infoq.com 微信号:infoqchina 人工智能(AI)已经被广泛地应用于软件和在线服务领域,由于机器学习算法和各种相关技术的不断进步,它正变得越来越普遍。但是像基于软件的知觉、人工意识等技术的流行描述却模糊了AI的真正定义以及它的实际利用方式。 本月初,卡耐基梅隆大学计算机科学学院院长Andrew Moore,接受了InformationWeek的采访,谈到了人工智能以及它在人们生活中所发挥的日益增长的重要作用。Moore表示,人工智
大数据文摘
2018/05/22
2740
以人工智能为火种,点燃下一代工业革命!2021年WAIC云帆奖获得者名单公布
机器之心报道 机器之心编辑部 2021年云帆奖获奖者名单公布:以人工智能为火种,点燃下一代工业革命! 7月10日,在2021世界人工智能大会开发者论坛上,最新一届「WAIC云帆奖」得主名单揭晓。作为全球首个面向青年华人AI开发者的奖项,云帆奖已经成为WAIC最具代表性的赛事评奖活动之一。 2021年的WAIC云帆奖共评选出璀璨明星10位和明日之星17位,他们中既有来自斯坦福、加州大学、清华、上海交大、浙大等海内外高校的青年教师及优秀博士生,也有参与创立了思谋科技、循环智能、深势科技、Whale帷幄等新一代
机器之心
2023/03/29
1.6K0
以人工智能为火种,点燃下一代工业革命!2021年WAIC云帆奖获得者名单公布
外媒预测2016人工智能七大趋势
据TechRepublic网站2015年12月15日报道,来自卡内基梅隆大学、德蒙特福德大学与路易斯维尔大学的研究人员就2016年的主要人工智能研究领域做出了以下预测: 1.深度学习 路易斯维尔大学网络安全实验室主任Yampolskiy表示,卷积神经网络的(深度学习)功能将得到显著改进,并且这一功能将与日益增多的超级计算机的大量计算资源相匹配,德蒙特福德大学高级研究员Richardson认为深度学习技术将成为2016年的一个热门研究领域。 2.人工智能取代劳动力 卡内基梅隆大学计算机科学学院院长Moore表
人工智能快报
2018/03/07
4700
【IJCAI】国际人工智能大会看AI趋势
IJCAI 由国际人工智能联合会主办,是中国计算机学会认定为 A 类的人工智能学科顶级会议,在近千种人工智能国际会议中公认排名第一。IJCAI-15 的亮点是把最新研究的问题和应用场景进行了集中展示,例如人工智能与艺术、可持续性论文等。 本届 IJCAI 大会共包括 Main Track(341篇论文)、机器学习(Machine Learning Track,114 篇论文)、知识表示(KR Track,83 篇论文)、人工智能与艺术(AI & Arts Track,16 篇论文)和可持续
新智元
2018/03/13
1.4K0
【IJCAI】国际人工智能大会看AI趋势
【深度长文】人工智能过去60年沉浮史,未来60年将彻底改变人类
导读:对于人工智能来说,前60年的人工智能历程,可以用“无穷动”来形容;后60年的人工智能发展,可以用“无穷大”来期许。
IT阅读排行榜
2018/08/14
1.6K0
【深度长文】人工智能过去60年沉浮史,未来60年将彻底改变人类
推荐阅读
韩松、王威廉、杨迪一、方飞、张含望入选,2020 IEEE智能系统十大AI青年科学家出炉
6760
业界 | 今秋开课!卡内基梅隆大学将开设美国首个人工智能本科学位
4330
全球高校人工智能实力排名出炉,清华力压卡内基梅隆占据第一宝座
2930
2018全球大学计算机科学与人工智能排名:CMU名列第一
6120
卡耐基梅隆大学利用人工智能、机器人等技术变革农业
1.1K0
2018全球大学人工智能排名发布,中国高校表现强势!
8350
学界丨Yann LeCun、Jeff Dean频繁亮相普及人工智能知识,全民AI时代来临|AI科技评论周刊
1K0
《安全的人工智能系统开发指南》解读
6480
专家称人工集群智能有助于应对人工智能的潜在威胁
6450
CMU开设美国首个人工智能本科专业
1.2K0
监管人工智能系统:风险、挑战、能力和策略
1K0
【1996~2016】盘点 20 年 AAAI 人工智能最佳论文
1.4K0
多家IT巨头公司就人工智能建立合作组织
8560
清华人工智能研究院成立,张钹姚期智分别任院长和主任
5520
人工智能在医疗:改善药物依从性、虚拟医疗助手、智能看护、智能药物研发...
7510
关于人工智能应该知道的十件事
2740
以人工智能为火种,点燃下一代工业革命!2021年WAIC云帆奖获得者名单公布
1.6K0
外媒预测2016人工智能七大趋势
4700
【IJCAI】国际人工智能大会看AI趋势
1.4K0
【深度长文】人工智能过去60年沉浮史,未来60年将彻底改变人类
1.6K0
相关推荐
韩松、王威廉、杨迪一、方飞、张含望入选,2020 IEEE智能系统十大AI青年科学家出炉
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档