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     
成功获取资源指标数据


第二种方式:
helm fetch stable/metrics-server
tar -xvf metrics-server-2.7.1.tgz
cd metrics-server
helm install --name metric --namespace kube-system --set image.repository=gcr.azk8s.cn/google_containers/metrics-server-amd64 .
(报错的话可能是镜像下载不下来,docker pull   mirrorgooglecontainers/metrics-server-amd64:v0.3.5,然后 docker tag 成报错的镜像名称)
 kubectl get pods -n kube-system -l release=metric


$ kubectl get deployments.apps   -n kube-system    metric-metrics-server    -o yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "3"
  creationTimestamp: "2020-04-06T07:48:50Z"
  generation: 3
  labels:
    app: metrics-server
    chart: metrics-server-2.8.8
    heritage: Tiller
    release: metric
  name: metric-metrics-server
  namespace: kube-system
  resourceVersion: "227096"
  selfLink: /apis/apps/v1/namespaces/kube-system/deployments/metric-metrics-server
  uid: 3562767f-934f-4f37-a84f-eedd9765444f
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: metrics-server
      release: metric
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: metrics-server
        release: metric
    spec:
      affinity: {}
      containers:
      - command:
        - /metrics-server
        - --cert-dir=/tmp
        - --logtostderr
        - --secure-port=8443
        - --kubelet-insecure-tls                                   新增加两行: 不启用证书以及解析ip地址
        - --kubelet-preferred-address-types=InternalIP
        image: gcr.azk8s.cn/google_containers/metrics-server-amd64:v0.3.5
        imagePullPolicy: IfNotPresent
        livenessProbe:
          failureThreshold: 3
          httpGet:
            path: /healthz
            port: https
            scheme: HTTPS
          initialDelaySeconds: 20
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 1
        name: metrics-server
        ports:
        - containerPort: 8443
          name: https
          protocol: TCP
        readinessProbe:
          failureThreshold: 3
          httpGet:
            path: /healthz
            port: https
            scheme: HTTPS
          initialDelaySeconds: 20
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 1
        resources: {}
        securityContext:
          allowPrivilegeEscalation: false
          capabilities:
            drop:
            - all
          readOnlyRootFilesystem: true
          runAsGroup: 10001
          runAsNonRoot: true
          runAsUser: 10001
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        volumeMounts:
        - mountPath: /tmp
          name: tmp
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      serviceAccount: metric-metrics-server
      serviceAccountName: metric-metrics-server
      terminationGracePeriodSeconds: 30
      volumes:
      - emptyDir: {}
        name: tmp
status:
  availableReplicas: 1
  conditions:
  - lastTransitionTime: "2020-04-06T08:04:03Z"
    lastUpdateTime: "2020-04-06T08:04:03Z"
    message: Deployment has minimum availability.
    reason: MinimumReplicasAvailable
    status: "True"
    type: Available
  - lastTransitionTime: "2020-04-06T07:49:04Z"
    lastUpdateTime: "2020-04-06T08:04:03Z"
    message: ReplicaSet "metric-metrics-server-69c486c888" has successfully progressed.
    reason: NewReplicaSetAvailable
    status: "True"
    type: Progressing
  observedGeneration: 3
  readyReplicas: 1
  replicas: 1
  updatedReplicas: 1




部署 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 数量


报错:
[root@yzsjhl-suanfan-master01 ~]# kubectl top node
Error from server (NotFound): the server could not find the requested resource (get services http:heapster:)


解决:
替换镜像:k8s.gcr.io/metrics-server-amd64:v0.3.1  换为 :  registry.aliyuncs.com/google_containers/metrics-server-amd64:v0.3.1
git clone https://github.com/kodekloudhub/kubernetes-metrics-server.git
kubectl create -f kubernetes-metrics-server/




Kubernetes Metrics Server第三种在二进制K8s上部署

  • helm 安装
helm repo add metrics-server https://kubernetes-sigs.github.io/metrics-server/
helm upgrade --install metrics-server metrics-server/metrics-server

  • 修改 deployments 关闭证书配置 --kubelet-insecure-tls
    spec:
      containers:
      - args:
        - --secure-port=4443
        - --cert-dir=/tmp
        - --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname
        - --kubelet-use-node-status-port
        - --metric-resolution=15s
        - --kubelet-insecure-tls
        image: yz.xxxxxcom/base/metrics-server:v0.6.1
  • 查看 metrics-server 日志
# kubectl  logs metrics-server-75fff855cd-flcbl  -f
I0712 07:51:20.127701       1 serving.go:342] Generated self-signed cert (/tmp/apiserver.crt, /tmp/apiserver.key)
I0712 07:51:20.524337       1 requestheader_controller.go:169] Starting RequestHeaderAuthRequestController
I0712 07:51:20.524362       1 configmap_cafile_content.go:201] "Starting controller" name="client-ca::kube-system::extension-apiserver-authentication::client-ca-file"
I0712 07:51:20.524401       1 shared_informer.go:240] Waiting for caches to sync for client-ca::kube-system::extension-apiserver-authentication::client-ca-file
I0712 07:51:20.524380       1 shared_informer.go:240] Waiting for caches to sync for RequestHeaderAuthRequestController
I0712 07:51:20.524470       1 configmap_cafile_content.go:201] "Starting controller" name="client-ca::kube-system::extension-apiserver-authentication::requestheader-client-ca-file"
I0712 07:51:20.524525       1 shared_informer.go:240] Waiting for caches to sync for client-ca::kube-system::extension-apiserver-authentication::requestheader-client-ca-file
I0712 07:51:20.525014       1 dynamic_serving_content.go:131] "Starting controller" name="serving-cert::/tmp/apiserver.crt::/tmp/apiserver.key"
I0712 07:51:20.525500       1 secure_serving.go:266] Serving securely on [::]:4443
I0712 07:51:20.525615       1 tlsconfig.go:240] "Starting DynamicServingCertificateController"
W0712 07:51:20.528374       1 shared_informer.go:372] The sharedIndexInformer has started, run more than once is not allowed
I0712 07:51:20.625632       1 shared_informer.go:247] Caches are synced for client-ca::kube-system::extension-apiserver-authentication::requestheader-client-ca-file 
I0712 07:51:20.625833       1 shared_informer.go:247] Caches are synced for RequestHeaderAuthRequestController 
I0712 07:51:20.625830       1 shared_informer.go:247] Caches are synced for client-ca::kube-system::extension-apiserver-authentication::client-ca-file 

  • 查看是否获取指标
# kubectl  top node
NAME                    CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%   
yz-devk8s-master10011   153m         3%     4096Mi          52%       
yz-devk8s-master10012   129m         3%     2478Mi          31%       
yz-devk8s-master10013   129m         3%     2647Mi          33%       
yz-devk8s-node10015     1730m        2%     15299Mi         5%        
yz-devk8s-node10016     750m         0%     12179Mi         4%  

posted @ 2021-03-11 16:55  lixinliang  阅读(550)  评论(0编辑  收藏  举报