k8s HorizontalPodAutoscaler

k8s scale其实就是调用apiserver提供的api接口,然后更新replicationcontroller,然后由rc controler进行控制pod的数量。

 

HorizontalPodAutoscaler:

客户端;

    通过kubectl创建一个horizontalPodAutoscaler对象,并存储到etcd中

服务端:

    api server:负责接受创建hpa对象,然后存入etcd

    hpa controler和其他的controler类似,每30s同步一次,将已经创建的hpa进行一次管理(从heapster获取监控数据,查看是否需要scale, controler的store中就保存着从始至终创建出来的hpa,当做一个缓存),watch hpa有变化也会运行。从heapster中获取scale数据,和hpa对比,计算cup利用率等信息,然后重新调整scale。根据hpa.Spec.ScaleTargetRef.Kind(例如Deployment,然后deployment控制器在调整pod数量),调整其值,发送到apiserver存储到etcd,然后更新hpa到etcd.

 

if rescale {
   scale.Spec.Replicas = desiredReplicas
  //调整绑定hpa的资源的值,比如deployment
   _, err = a.scaleNamespacer.Scales(hpa.Namespace).Update(hpa.Spec.ScaleTargetRef.Kind, scale)
   if err != nil {
      a.eventRecorder.Eventf(hpa, api.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, api.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的值
return a.updateStatus(hpa, currentReplicas, desiredReplicas, cpuCurrentUtilization, cmStatus, rescale)

你可能感兴趣的:(k8s)