Fork me on GitHub

Kubernetes 基于 Metrics Server 与 HPA 的使用

image

在 Kubernetes 中可以手动通过 kubectl scale 命令或通过修改 replicas 数量,可以实现 Pod 的扩容或缩容。Kubernetes 中还提供了 HPA(Horizontal Pod Autoscaling) 功能,可以根据当前负载的变化情况自动触发水平扩展或缩容的行为,从而合理的使用资源。从 Kubernetes  v1.8 开始,资源使用情况的度量(如容器的 CPU 和内存使用)可以通过 Metrics API 获取,HPA 使用这些 metics 信息来实现动态伸缩。

拉取镜像

$ touch pull_k8s_images.sh

#!/bin/bash
images=(metrics-server-amd64:v0.3.1)
for imageName in ${images[@]} ; do
docker pull anjia0532/google-containers.$imageName
docker tag anjia0532/google-containers.$imageName k8s.gcr.io/$imageName
docker rmi anjia0532/google-containers.$imageName
done

$ sh touch pull_k8s_images.sh

部署 metrics-server

$ git clone https://github.com/kubernetes-incubator/metrics-server.git
$ cd metrics-server
$ kubectl create -f deploy/1.8+/

clusterrole.rbac.authorization.k8s.io/system:aggregated-metrics-reader created
clusterrolebinding.rbac.authorization.k8s.io/metrics-server:system:auth-delegator created
rolebinding.rbac.authorization.k8s.io/metrics-server-auth-reader created
apiservice.apiregistration.k8s.io/v1beta1.metrics.k8s.io created
serviceaccount/metrics-server created
deployment.extensions/metrics-server created
service/metrics-server created
clusterrole.rbac.authorization.k8s.io/system:metrics-server created
clusterrolebinding.rbac.authorization.k8s.io/system:metrics-server created

上述可能还会提示拉取不到镜像,由于配置了 imagePullPolicy: Always,可以注释掉

vi metrics-server-deployment.yaml

---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: metrics-server
  namespace: kube-system
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: metrics-server
  namespace: kube-system
  labels:
    k8s-app: metrics-server
spec:
  selector:
    matchLabels:
      k8s-app: metrics-server
  template:
    metadata:
      name: metrics-server
      labels:
        k8s-app: metrics-server
    spec:
      serviceAccountName: metrics-server
      volumes:
      # mount in tmp so we can safely use from-scratch images and/or read-only containers
      - name: tmp-dir
        emptyDir: {}
      containers:
      - name: metrics-server
        image: k8s.gcr.io/metrics-server-amd64:v0.3.1
       # imagePullPolicy: Always
        volumeMounts:
        - name: tmp-dir
          mountPath: /tmp

执行查看

$ kubectl apply -f metrics-server-deployment.yaml
$ kubectl get pod,svc -n kube-system
NAME                                            READY   STATUS    RESTARTS   AGE
pod/coredns-576cbf47c7-d6tm2                    1/1     Running   0          14d
pod/coredns-576cbf47c7-zgdsx                    1/1     Running   0          14d
pod/etcd-kubernetes-master                      1/1     Running   0          14d
pod/kube-apiserver-kubernetes-master            1/1     Running   0          14d
pod/kube-controller-manager-kubernetes-master   1/1     Running   1          14d
pod/kube-proxy-dz4fh                            1/1     Running   0          14d
pod/kube-proxy-qh9b5                            1/1     Running   0          14d
pod/kube-proxy-x8clc                            1/1     Running   0          14d
pod/kube-scheduler-kubernetes-master            1/1     Running   1          14d
pod/kubernetes-dashboard-77fd78f978-qp626       1/1     Running   0          14d
pod/metrics-server-79f8f467b5-6l5wh             1/1     Running   0          10m
pod/tiller-deploy-7788856dfb-7kkw7              1/1     Running   0          11d
pod/traefik-ingress-controller-qjnc6            1/1     Running   0          12d
pod/traefik-ingress-controller-rwxr6            1/1     Running   0          12d
pod/weave-net-j9s27                             2/2     Running   0          6d11h
pod/weave-net-p22s2                             2/2     Running   0          6d11h
pod/weave-net-vnq7p                             2/2     Running   0          6d11h

NAME                              TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)           AGE
service/kube-dns                  ClusterIP   10.96.0.10       <none>        53/UDP,53/TCP     14d
service/kubernetes-dashboard      NodePort    10.103.60.159    <none>        443:32151/TCP     14d
service/metrics-server            ClusterIP   10.110.180.222   <none>        443/TCP           42m
service/tiller-deploy             ClusterIP   10.103.123.198   <none>        44134/TCP         11d
service/traefik-ingress-service   ClusterIP   10.105.18.62     <none>        80/TCP,8080/TCP   12d
service/traefik-web-ui            ClusterIP   10.102.207.196   <none>        80/TCP            12d

配置 HPA

vi vi nginx-deployment-hpa.yaml

apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: nginx-deployment-hpa
  namespace: default
spec:
  maxReplicas: 10
  minReplicas: 4
  scaleTargetRef:
    kind: Deployment
    name: nginx-deployment
  targetCPUUtilizationPercentage: 50 # CPUUtilizationPercentage 是一个平均值,即 Pod 所有副本自身的 CPU 利用率的平均值。

备注:Kubernetes v1.2 版本中 HPA 升级为稳定版本(apiVersion: autoscaling/v1),等同于 kubectl autoscale deployment nginx-deployment--cpu-percent=50 --min=4 --max=10

执行查看

kubectl apply -f nginx-deployment-hpa.yaml
$ kubectl get hpa
NAME                   REFERENCE                     TARGETS         MINPODS   MAXPODS   REPLICAS   AGE
nginx-deployment-hpa   Deployment/nginx-deployment   <unknown>/50%   4         10        0          11s

REFER:
https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#autoscale
https://github.com/kubernetes-incubator/metrics-server
https://github.com/stefanprodan/k8s-prom-hpa
http://blog.51cto.com/ylw6006/2114338
https://www.kubernetes.org.cn/4664.html

posted @ 2018-11-13 21:13  花儿笑弯了腰  阅读(952)  评论(0编辑  收藏  举报