一、简介
kubernetes监控指标有核心指标和自定义指标两大类,目前使用监控指标依赖于基础组件metrics-server。
核心指标也可称为核心指标流水线,由kubelet、metrics-server和由API server提供的api组成。可以提供基础指标,如CPU累计使用率、内存实时使用率、Pod资源占用率及容器的磁盘占用率,主要由metrics-server组件提供。
自定义监控也称为监控流水线,用于从系统收集各种指标数据并提供给终端用户、存储系统以及HPA,包含了核心指标和非核心指标(非核心指标需要特殊中间控件如被kubernetes进行识别)。
二、安装部署(核心指标监控控件metrics-server)
1.安装metrics-server
GitHub官方metrics-server项目地址点击 这里 或 这里 查看,
下载yaml配置文件
[root@master1 ~]# for file in auth-delegator.yaml auth-reader.yaml metrics-apiservice.yaml metrics-server-deployment.yaml metrics-server-service.yaml resource-reader.yaml; do wget https://raw.githubusercontent.com/kubernetes/kubernetes/master/cluster/addons/metrics-server/$file; done
创建后报错,查看日志如下内容:
E1012 08:26:57.910695 1 memcache.go:134] couldn't get resource list for metrics.k8s.io/v1beta1: the server is currently unable to handle the request E1012 08:27:13.214427 1 resource_quota_controller.go:430] unable to retrieve the complete list of server APIs: metrics.k8s.io/v1beta1: the server is currently unable to handle the request W1012 08:27:17.126343 1 garbagecollector.go:647] failed to discover some groups: map[metrics.k8s.io/v1beta1:the server is currently unable to handle the request]
2.创建metrics-server报错处理方法
此yaml配置文件并非可以直接执行成功,笔者亲测此版本(metrics-server-v0.3.6)需要修改两个文件的某些参数,后续版本会做什么调整尚未可知,修改详细内容如下:(若需要相关镜像或文件可通过微信公众号“琼杰笔记”免费获取)
修改文件: metrics-server-deployment.yaml
修改1:
46 spec: 47 priorityClassName: system-cluster-critical 48 serviceAccountName: metrics-server 49 nodeSelector: 50 kubernetes.io/os: linux 51 hostNetwork: true # 新增 52 containers: 53 - name: metrics-server 54 image: k8s.gcr.io/metrics-server-amd64:v0.3.6 55 imagePullPolicy: IfNotPresent 56 command: 57 - /metrics-server 58 - --metric-resolution=30s 59 - --requestheader-allowed-names=aggregator # 新增 60 # These are needed for GKE, which doesn't support secure communication yet. 61 # Remove these lines for non-GKE clusters, and when GKE supports token-based auth. 62 # - --kubelet-port=10255 # 注释 63 # - --deprecated-kubelet-completely-insecure=true # 注释 64 - --kubelet-insecure-tls # 新增 65 - --kubelet-preferred-address-types=InternalIP,Hostname,InternalDNS,ExternalDNS,ExternalIP
修改2处:
您暂时无权查看此隐藏内容!
修改之后,重新创建即可成功,如下:
[root@master1 metrics-server]# kubectl top nodes NAME CPU(cores) CPU% MEMORY(bytes) MEMORY% master1 226m 11% 1093Mi 63% node1 91m 4% 340Mi 19% node2 127m 6% 406Mi 23%
三、安装部署(自定义控件Prometheus)
若允许prometheus获取metrics的数据,一般需要在metrics中定义annotations参数prometheus.io/scrape: “true”
四、HorizontalPodAutoscaler(简称hpa)
1.创建pod和service资源
apiVersion: v1 kind: Service metadata: name: myapp-svc namespace: default spec: selector: app: myapp release: for1 ports: - name: myapp-svc port: 80 targetPort: 80 type: ClusterIP --- apiVersion: apps/v1 kind: Deployment metadata: name: myapp-hpa namespace: default spec: selector: matchLabels: app: myapp release: for1 replicas: 1 template: metadata: labels: app: myapp release: for1 spec: containers: - name: myapp-hpa-test image: ikubernetes/myapp:v1 imagePullPolicy: IfNotPresent ports: - name: http containerPort: 80 resources: limits: cpu: 80m memory: 128Mi requests: cpu: 80m memory: 128Mi
2.创建配置hpa
[root@master1 ~]# kubectl autoscale deployment myapp-hpa --min=1 --max=6 --cpu-percent=50
3.压测
[root@node2 ~]# ab -c 1000 -n 500000000 http://192.168.222.150:32331/index.html This is ApacheBench, Version 2.3 <$Revision: 1430300
gt; Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking 192.168.222.150 (be patient) apr_socket_recv: Connection reset by peer (104) Total of 2663 requests completed
4.验证(此时发现pod从1个变为2个,然后等压测结束,又变为1个)
[root@master1 hpa]# kubectl describe hpa
评论前必须登录!
注册