k8s Metrics Server 获取资源指标与 hpa 部署

Posted lixinliang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了k8s Metrics Server 获取资源指标与 hpa 部署相关的知识,希望对你有一定的参考价值。

使用 helm 部署 Metrics Server

helm repo add bitnami https://charts.bitnami.com/bitnami
helm install bitnami/metrics-server   会有报错,执行以下命令
helm upgrade loopy-saola bitnami/metrics-server     --set apiService.create=true

$  kubectl get pod   查看节点
loopy-saola-metrics-server-58796b4bc7-4mv4t      1/1     Running   0          16m

$ kubectl top nodes  不能获取资源指标,需要修改 deployment

$  kubectl get   deployment   loopy-saola-metrics-server       -o yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "3"
  creationTimestamp: "2019-12-06T10:18:00Z"
  generation: 3
  labels:
    app.kubernetes.io/instance: loopy-saola
    app.kubernetes.io/managed-by: Tiller
    app.kubernetes.io/name: metrics-server
    helm.sh/chart: metrics-server-4.1.0
  name: loopy-saola-metrics-server
  namespace: default
  resourceVersion: "77963814"
  selfLink: /apis/extensions/v1beta1/namespaces/default/deployments/loopy-saola-metrics-server
  uid: ae24b7b2-1811-11ea-a9a8-b8ca3a614e64
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app.kubernetes.io/instance: loopy-saola
      app.kubernetes.io/name: metrics-server
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app.kubernetes.io/instance: loopy-saola
        app.kubernetes.io/managed-by: Tiller
        app.kubernetes.io/name: metrics-server
        helm.sh/chart: metrics-server-4.1.0
    spec:
      containers:
      - command:
        - metrics-server
        - --secure-port=8443
        - --v=8                                         以下三行为修改的内容
        - --kubelet-insecure-tls
        - --kubelet-preferred-address-types=InternalIP
        image: docker.io/bitnami/metrics-server:0.3.6-debian-9-r27
        imagePullPolicy: IfNotPresent
        name: metrics-server
        ports:
        - containerPort: 8443
          hostPort: 8443                                   增加 host port
          name: https
          protocol: TCP
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      serviceAccount: loopy-saola-metrics-server
      serviceAccountName: loopy-saola-metrics-server
      terminationGracePeriodSeconds: 30
status:
  availableReplicas: 1
  conditions:
  - lastTransitionTime: "2019-12-06T10:17:05Z"
    lastUpdateTime: "2019-12-06T10:17:05Z"
    message: Deployment has minimum availability.
    reason: MinimumReplicasAvailable
    status: "True"
    type: Available
  - lastTransitionTime: "2019-12-06T10:16:54Z"
    lastUpdateTime: "2019-12-06T10:40:41Z"
    message: ReplicaSet "loopy-saola-metrics-server-58796b4bc7" has successfully progressed.
    reason: NewReplicaSetAvailable
    status: "True"
    type: Progressing
  observedGeneration: 3
  readyReplicas: 1
  replicas: 1
  updatedReplicas: 1


$ kubectl top node
NAME                          CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%   
k8s-master01.gdfsxxds.rjyun   423m         1%     28532Mi         44%       
k8s-master02.gdfsxxds.rjyun   334m         1%     5932Mi          9%        
k8s-master03.gdfsxxds.rjyun   361m         1%     4107Mi          6%        
k8s-node01                    242m         1%     10696Mi         8%        
k8s-node02                    560m         1%     12201Mi         18%       
k8s-node03                    1811m        5%     28904Mi         45%       

$ kubectl top pod
NAME                                             CPU(cores)   MEMORY(bytes)   
a9vg-project-v2-deployment-7bc4968b44-mzp5n      2m           273Mi           
a9vg-static-deployment-7bb7576cd8-hcmgm          1m           26Mi            
cm-acme-http-solver-x8tkd                        1m           7Mi     
成功获取资源指标数据

 

部署 hpa
$ kubectl get pod  |grep ssr             部署前查看是一个pod 
tgbus-ssr-deployment-85df9c9c59-2s4lw            1/1     Running   0          23h
$ cat hpa.yaml 
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: tgbus-ssr-deployment
  namespace: default
spec:
  maxReplicas: 15
  minReplicas: 2
  scaleTargetRef:
    apiVersion: extensions/v1beta1
    kind: Deployment
    name: tgbus-ssr-deployment
  targetCPUUtilizationPercentage: 50


$ kubectl create -f hpa.yaml 
$ kubectl get pod  |grep ssr
tgbus-ssr-deployment-85df9c9c59-2s4lw            1/1     Running   0          23h
tgbus-ssr-deployment-85df9c9c59-k75xh            1/1     Running   0          42s
发现已创建出新的pod    后续会根据负载情况动态增加减少pod 数量

  

以上是关于k8s Metrics Server 获取资源指标与 hpa 部署的主要内容,如果未能解决你的问题,请参考以下文章

K8s api server 无法连接 metrics server 问题解决方案

十七,k8s集群指标API及自定义API

Kubernetes 集群部署 Metrics Server 获取集群 Metric 数据

Kubernetes 集群部署 Metrics Server 获取集群 Metric 数据

十五. 资源指标API以及自定义指标API

K8SK8S部署Metrics-Server服务