k8s-vpa深入剖析

Posted DevOperater

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了k8s-vpa深入剖析相关的知识,希望对你有一定的参考价值。

1.介绍

1.1vpa组件

vpa主要由三个组件组成,分别为recommendupdateradmission-controller

    recommend:根据metric-server api获取到的容器指标,计算推荐指标。

    updater:根据pod的request中设置的指标和recommend计算的推荐指标,在一定条件下驱逐pod。

    admission-controller:这是一个webhook组件,监听pod的创建,把recommend计算出的指标设置给pod的request和limit

1.2自定义资源

涉及到的自定义资源主要有两个:VerticalPodAutoscaler 、VerticalPodAutoscalerCheckpoint

VerticalPodAutoscaler

该资源由用户创建,用于设置纵向扩容的目标对象和存储recommend组件计算出的推荐指标。

apiVersion: autoscaling.k8s.io/v1beta2
kind: VerticalPodAutoscaler
metadata:
name: nginx-vpa
namespace: default
spec:
targetRef:
apiVersion: "apps/v1"
kind: Deployment
name: nginx
updatePolicy:
updateMode: "Off"
resourcePolicy:
containerPolicies:
- containerName: "nginx"
minAllowed:
cpu: "90m"
memory: "10Mi"
maxAllowed:
cpu: "1000m"
memory: "1Gi"
status:
conditions:
- lastTransitionTime: "2021-11-24T08:54:41Z"
status: "True"
type: RecommendationProvided
recommendation:
containerRecommendations:
- containerName: nginx
lowerBound:
cpu: 90m
memory: 131072k
target:
cpu: 90m
memory: 131072k
uncappedTarget:
cpu: 12m
memory: 131072k
upperBound:
cpu: "1"
memory: 131072k
- containerName: yellow-pod-container
lowerBound:
cpu: 12m
memory: 131072k
target:
cpu: 12m
memory: 131072k
uncappedTarget:
cpu: 12m
memory: 131072k
upperBound:
cpu: 407m
memory: 425500k

VerticalPodAutoscalerCheckpoint

该资源由recommend组件创建和维护,用于存储指标相关信息。

一个vpa对应的多个容器,每个容器创建一个该资源。

apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscalerCheckpoint
metadata:
creationTimestamp: "2021-11-25T08:00:20Z"
name: nginx-vpa-nginx
namespace: default
spec:
containerName: nginx
vpaObjectName: nginx-vpa
status:
cpuHistogram:
bucketWeights:
"0": 10000
"9": 4
"11": 2
"12": 16
"13": 2
"15": 1
"16": 11
"17": 5
"31": 40
"34": 2
"44": 2
"60": 13
"75": 744
referenceTimestamp: "2021-11-28T00:00:00Z"
totalWeight: 560.4882004282056
firstSampleStart: null
lastSampleStart: "2021-11-28T14:13:59Z"
lastUpdateTime: null
memoryHistogram:
bucketWeights:
"0": 10000
"1": 67
"34": 141
referenceTimestamp: "2021-11-29T00:00:00Z"
totalWeight: 23.976566526407623
totalSamplesCount: 9371
version: v3

2.recommend组件

2.1yaml介绍

2.1.1版本细节

0.7版本

0.7版本
apiVersion: autoscaling.k8s.io/v1beta2
kind: VerticalPodAutoscaler
metadata:
name: nginx-vpa
namespace: default
spec:
targetRef: ----设置目标资源,也可以是scaledClient能获取信息的自定义资源,如cloneSet这种能作为hpa target的自定义资源
apiVersion: "apps/v1"
kind: Deployment
name: nginx
updatePolicy:  ---- 可设置Auto、Recreate或Off,设置Off,只会计算推荐值,updater组件不会驱逐pod。设置Auto:updater组建会驱逐pod
updateMode: "Auto"
resourcePolicy:
containerPolicies: ---- 指定设置推荐值的容器
- containerName: "nginx"
minAllowed: ---- 设置了minAllowed和maxAllowed,那么recommend计算推荐值的最后,会把推荐值控制在这个范围内。
cpu: "90m"
memory: "10Mi"
maxAllowed:
cpu: "1000m"
memory: "1Gi"

0.8版本

多了一个controlledResources

可以只设置cpu、或memory  

该属性在recommend组件中有使用

该属性可设置的值如下:enum: ["cpu", "memory"] 

// Specifies the type of recommendations that will be computed
// (and possibly applied) by VPA.
// If not specified, the default of [ResourceCPU, ResourceMemory]will be used.
ControlledResources *[]v1.ResourceName `json:"controlledResources,omitempty" patchStrategy:"merge" protobuf:"bytes,5,rep,name=controlledResources"`

0.9版本

又多了一个controlledValues

该属性在recommend组件中没有使用,在admission组件中使用了

该属性可设置的值如下:enum: ["RequestsAndLimits", "RequestsOnly"]

// Specifies which resource values should be controlled.
// The default is "RequestsAndLimits".
// +optional
ControlledValues *ContainerControlledValues `json:"controlledValues,omitempty" protobuf:"bytes,6,rep,name=controlledValues"`

 2.1.2targetRef

设置目标资源

可以是如下资源daemonSet、deployment、replicaSet、statefulSet、replicationController、job、cronJob

也可以是scaledClient能获取信息的自定义资源,如cloneSet这种能作为hpa target的自定义资源


注意:只有是deployment、replicaSet、replicationController、statefulSet、job时,updater驱逐pod的时候,updater设置的可驱逐pod比例(evictionToleranceFraction)才生效。当是其他资源时,会把所有pod全部驱逐,因为计算的tolerance=0。

targetRef:  
apiVersion: "apps/v1"
kind: Deployment
name: nginx

源码查看

//根据vpa获取target 资源的selector。。例如:deployment的selector
func (f *vpaTargetSelectorFetcher) Fetch(vpa *vpa_types.VerticalPodAutoscaler) (labels.Selector, error)
if vpa.Spec.TargetRef == nil
return nil, fmt深入剖析:oracle 的并行机制

万字长文深入剖析volatile(Java)

Kafka集群内复制功能深入剖析

深入剖析linq的联接

IP地址深入剖析

python小数据池,代码块深入剖析