k8s-vpa深入剖析

古人已用三冬足,年少今开万卷余。这篇文章主要讲述k8s-vpa深入剖析相关的知识,希望能为你提供帮助。
1.介绍1.1vpa组件
vpa主要由三个组件组成,分别为recommend、updater、admission-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组件中使用了
【k8s-vpa深入剖析】该属性可设置的值如下: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

    推荐阅读