K8S集群基于heapster的HPA测试

本文将介绍基于heapster获取metric的HPA配置。在开始之前,有必要先了解一下K8S的HPA特性。

1、HPA全称Horizontal Pod Autoscaling,即pod的水平自动扩展。
自动扩展主要分为两种,其一为水平扩展,针对于实例数目的增减;其二为垂直扩展,即单个实例可以使用的资源的增减。HPA属于前者。

2、HPA是Kubernetes中实现POD水平自动伸缩的功能。
云计算具有水平弹性的特性,这个是云计算区别于传统IT技术架构的主要特性。对于Kubernetes中的POD集群来说,HPA可以实现很多自动化功能,比如当POD中业务负载上升的时候,可以创建新的POD来保证业务系统稳定运行,当POD中业务负载下降的时候,可以销毁POD来提高资源利用率。

3、HPA控制器默认每隔30秒就会运行一次。
如果要修改间隔时间,可以设置horizontal-pod-autoscaler-sync-period参数。

4、HPA的操作对象是RC、RS或Deployment对应的Pod
根据观察到的CPU等实际使用量与用户的期望值进行比对,做出是否需要增减实例数量的决策。

5、hpa的发展历程
在Kubernetes v1.1中首次引入了hpa特性。hpa第一个版本基于观察到的CPU利用率,后续版本支持基于内存使用。
在Kubernetes 1.6中引入了一个新的API自定义指标API,它允许HPA访问任意指标。
Kubernetes 1.7引入了聚合层,允许第三方应用程序通过注册为API附加组件来扩展Kubernetes API。自定义指标API以及聚合层使得像Prometheus这样的监控系统可以向HPA控制器公开特定于应用程序的指标。

一、准备工作

因为pod的metrics信息来源与heapster,所以在开始之前要保证heapster运行正常。heapster的配置可参考前文。我们可以通过运行kubectl top node来验证heapster是否运行正常。

二、针对CPU的HPA演示

1、直接通过kubectl工具来创建hpa

# docker pull siriuszg/hpa-example
# kubectl get pod,deployment,svc
# kubectl run php-apache --image=siriuszg/hpa-example --requests=cpu=50m --expose --port=80
# kubectl autoscale deployment php-apache --cpu-percent=10 --min=1 --max=3

# kubectl get deployment,svc,hpa


2、运行一个deployment来制造压力

# kubectl run -i --tty load-generator --image=registry.59iedu.com/busybox /bin/sh
# nslookup php-apache
# while true; do wget -q -O- http://php-apache; done


从输入的日志上看,可以看到自动扩展的过程中有出现“connection refused”,最后我们将制造压力的deployment删除

# kubectl delete deployment load-generator


3、在整个过程中可以新开一个终端来观察hpa自动扩展和收缩的过程

4、排错

Warning FailedGetResourceMetric 12s (x41 over 20m) horizontal-pod-autoscaler unable to get metrics for resource cpu: unable to fetch metrics from API: the server could not find the requested resource (get pods.metrics.k8s.io)


出现上述错误,需要修改kube-controller-manager的配置文件

# grep ‘autoscaler‘ /usr/lib/systemd/system/kube-controller-manager.service
  --horizontal-pod-autoscaler-use-rest-clients=false
# systemctl daemon-reload
systemctl restart kube-controller-manager

三、针对内存的HPA演示

1、通过yaml文件创建hpa

# cat hpa-memory.yaml
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
  name: tomcat-shopxx-hpa
  namespace: default
spec:
  scaleTargetRef:
    apiVersion: apps/v1beta1
    kind: Deployment
    name: tomcat-shopxx
  minReplicas: 1
  maxReplicas: 3
  metrics:
  - type: Resource
    resource:
      name: memory
      targetAverageUtilization: 30

# kubectl create -f hpa-memory.yaml 

2、修改deployment的yaml文件,添加资源的requests和limit限制

如果没有相应的资源限制,则describe查看hpa会有报错信息“missing request for memory on container xxxx”

# cat tomcat.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: tomcat-shopxx
  namespace: default
spec:
  replicas: 1
  template:
    metadata:
      labels:
        k8s-app: tomcat-shopxx
    spec:
      containers:
      - image: registry.59iedu.com/filebeat:v5.4.0
        imagePullPolicy: Always
        resources:
          requests:
            cpu: "50m"
            memory: "20Mi"
          limits:
            cpu: "100m"
            memory: "50Mi"
        name: filebeat
        volumeMounts:
        - name: app-logs
          mountPath: /log
        - name: filebeat-config
          mountPath: /etc/filebeat/
      - image: registry.59iedu.com/tomcat_shopxx:v1
        name : tomcat-shopxx
        imagePullPolicy: Always
        resources:
          requests:
            cpu: "50m"
            memory: "200Mi"
          limits:
            cpu: "100m"
            memory: "250Mi"
        env:
        - name: JAVA_OPTS
          value: "-Xmx128m -Xms128m"
        ports:
        - containerPort: 8080
        volumeMounts:
        - name: app-logs
          mountPath: /home/tomcat/logs
      volumes:
      - name: app-logs
        emptyDir: {}
      - name: filebeat-config
        configMap:
          name: filebeat-config

# kubectl apply -f tomcat..yaml

3、观察hpa过程

#  journalctl -u kube-controller-manager.service -f

# kubectl describe hpa tomcat-shopxx-hpa 

# kubectl get hpa
# kubectl get pod

下文将会介绍基于metric-server的hpa,尽请关注!
参考:
https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/
https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/
https://github.com/kubernetes/kubernetes/issues/57673

原文地址:http://blog.51cto.com/ylw6006/2113848

时间: 2024-07-30 12:56:10

K8S集群基于heapster的HPA测试的相关文章

基于prometheus监控k8s集群

本文建立在你已经会安装prometheus服务的基础之上,如果你还不会安装,请参考:prometheus多维度监控容器 如果你还没有安装库k8s集群,情参考: 从零开始搭建基于calico的kubenetes 前言 kubernetes显然已成为各大公司亲睐的容器编排工具,各种私有云公有云平台基于它构建,那么,我们怎么监控集群中的所有容器呢?目前有三套方案: heapster+influxDB heapster为k8s而生,它从apiserver获取节点信息,每个节点kubelet内含了cAdv

基于k8s集群部署prometheus监控etcd

目录 基于k8s集群部署prometheus监控etcd 1.背景和环境概述 2.修改prometheus配置 3.检查是否生效 4.配置grafana图形 基于k8s集群部署prometheus监控etcd 1.背景和环境概述 本文中涉及到的环境中.prometheus监控和grafana基本环境已部署好.etcd内置了metrics接口供收集数据,在etcd集群任意一台节点上可通过ip:2379/metrics检查是否能正常收集数据. curl -L http://localhost:237

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

目录 资源指标: Metrics-Server 资源指标: Metric-Server介绍 Metric-Server部署 下载yaml文件 因为有墙, 所以提前下载image镜像, 当然也可以手动修改yaml相关文件 修改文件, 不然报错 创建Metric-Server 自定义资源指标: Prometheus k8s-prometheus-adapter 项目 Prometheus 在k8s集群中部署Prometheus github地址 需要部署的服务清单 安装部署所有服务及插件 部署kub

kubernetes生态--交付prometheus监控及grafana炫酷dashboard到k8s集群

由于docker容器的特殊性,传统的zabbix无法对k8s集群内的docker状态进行监控,所以需要使用prometheus来进行监控: 什么是Prometheus? Prometheus是由SoundCloud开发的开源监控报警系统和时序列数据库(TSDB).Prometheus使用Go语言开发,是Google BorgMon监控系统的开源版本. 2016年由Google发起Linux基金会旗下的原生云基金会(Cloud Native Computing Foundation), 将Prom

k8s集群之日志收集EFK架构

参考文档 http://tonybai.com/2017/03/03/implement-kubernetes-cluster-level-logging-with-fluentd-and-elasticsearch-stack/ https://github.com/kubernetes/kubernetes/tree/master/cluster/addons/fluentd-elasticsearch https://t.goodrain.com/t/k8s/242 http://logz

k8s集群Canal的网络控制 原

1 简介 直接上干货 public class DispatcherServlet extends HttpServlet { private Properties contextConfigProperties = new Properties(); private List<String> classNames = new ArrayList<>(); private Map<String, Object> ioc = new HashMap<String,

Prometheus+Grafan监控k8s集群详解

一,Prometheus概述 1,什么是Prometheus?Prometheus是最初在SoundCloud上构建的开源系统监视和警报工具包,自2012年成立以来,许多公司和组织都采用了Prometheus,该项目拥有非常活跃的开发人员和用户社区.现在,它是一个独立的开源项目,并且独立与任何公司维护.为了强调这一点并阐明项目的治理结构,Prometheus在2016年加入了 Cloud Native Computing Foundation(云原生计算基金会(CNCF)),这是继kuberne

k8s集群之kubernetes-dashboard和kube-dns组件部署安装

说明 最好先部署kube-dns,有些组合服务直接主机用hostname解析,例如redis主从,heapster监控组件influxdb.grafana之间等. 参考文档 https://greatbsky.github.io/KubernetesDoc/kubernetes1.5.2/cn.html 安装集群文档见: http://jerrymin.blog.51cto.com/3002256/1898243 安装PODS文档见: http://jerrymin.blog.51cto.com

Redis中sentinel集群的搭建和Jedis测试 图文教程[三]

在前两篇Redis中sentinel集群的搭建和Jedis测试 图文教程[一] 和Redis中sentinel集群的搭建和Jedis测试 图文教程[二] 中分别简述了Redis中sentinel集群的搭建和Java代码的Jedis测试. 这篇主要来简单分析一下Redis-sentinel集群的原理,根据追踪sentinel信息来完成Redis-sentinel集群测试中的详细的原理分析.包括master-slave各个中的sentinel信息的分析,failover过程,master宕机后的le