资源类型
CPU 和 memory 都是 资源类型。资源类型具有基本单位。CPU 的单位是 core,memory 的单位是 byte。这些都统称为计算资源。
CPU含义:
CPU 资源的限制和请求以cpu为单位
Kubernetes 中的一个 cpu 等于一个core,就是逻辑CPU。1颗逻辑CPU等于1000millicores。500m=0.5颗cpu。
内存含义:
内存的限制和请求以字节为单位。您可以使用以下后缀之一作为平均整数或定点整数表示内存:E,P,T,G,M,K。您还可以使用两个字母的等效的幂数:Ei,Pi,Ti ,Gi,Mi,Ki。
POD中的资源请求和资源限制
requests 资源请求 pod最低需求
limits 资源限制 pod最大的使用资源
测试
执行下面yaml的内容:
apiVersion: v1
kind: Pod
metadata:
name: my-demo
namespace: default
labels:
name: myapp
tier: appfront
spec:
containers:
- name: myapp
image: ikubernetes/stress-ng
command: ["/usr/bin/stress-ng","-c 1","--metrics-brief"]
ports:
- name: http
containerPort:
resources:
requests:
memory: "128Mi"
cpu: "200m"
limits:
memory: "512Mi"
cpu: "500m"
查看结果:
$ kubectl exec my-demo -- top
Mem: 3914044K used, 126532K free, 205252K shrd, 2176K buff, 2650160K cached
CPU: % usr % sys % nic % idle % io % irq % sirq
Load average: 0.12 0.08 0.09 /
PID PPID USER STAT VSZ %VSZ CPU %CPU COMMAND
root R % % {stress-ng-cpu} /usr/bin/stress-ng
root R % % top
root S % % /usr/bin/stress-ng -c --metrics-
root S % % top
root S % % top
我们看到CPU占用是25%,为什么呢?因为我们的node是2个core。我们最大限制是0.5核。所以应该是1/4。
QoS(服务质量等级)
是作用在 Pod 上的一个配置,当 Kubernetes 创建一个 Pod 时,它就会给这个 Pod 分配一个 QoS 等级,可以是以下等级之一:
-
Guaranteed:同时设置了CPU和内存的
requests
和limits
而且值必须相等。(这类的pod是最高优先级) -
Burstable:pod至少有一个容器设置了cpu或内存的
requests
和limits,且不满足 Guarantee 等级的要求。即内存或CPU的值设置的不同。(中等优先级)
-
BestEffort:没有任何一个容器设置了
requests
或limits的属性。(最低优先级)
Guaranteed样例:
apiVersion: v1
kind: Pod
metadata:
name: my-demo
namespace: default
labels:
name: myapp
tier: appfront
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v2
ports:
- name: http
containerPort:
resources:
requests:
memory: "512Mi"
cpu: "500m"
limits:
memory: "512Mi"
cpu: "500m"
结果:
$ kubectl describe pod my-demo
......
QoS Class: Guaranteed
......
Burstable样例:
apiVersion: v1
kind: Pod
metadata:
name: my-demo02
namespace: default
labels:
name: myapp
tier: appfront
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v2
ports:
- name: http
containerPort:
resources:
requests:
memory: "256Mi"
cpu: "200m"
limits:
memory: "512Mi"
cpu: "500m"
结果:
$ kubectl describe pod my-demo02
....
QoS Class: Burstable
....
BestEffort样例:
apiVersion: v1
kind: Pod
metadata:
name: my-demo03
namespace: default
labels:
name: myapp
tier: appfront
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v2
ports:
- name: http
containerPort:
结果:
$ kubectl describe pod my-demo03
....
QoS Class: BestEffort
....
HeapSter部署
Heapster可以收集Node节点上的cAdvisor数据,还可以按照kubernetes的资源类型来集合资源,比如Pod、Namespace域,可以分别获取它们的CPU、内存、网络和磁盘的metric。默认的metric数据聚合时间间隔是1分钟。
部署Heapster的地址 https://github.com/kubernetes-retired/heapster/tree/master/deploy/kube-config
首先我们需要先部署一个influxdb,这里我们需要改动一下。把文件下载下来。https://raw.githubusercontent.com/kubernetes-retired/heapster/master/deploy/kube-config/influxdb/influxdb.yaml
修改完成后的:
kind: Deployment
metadata:
name: monitoring-influxdb
namespace: kube-system
spec:
replicas:
selector:
matchLabels:
task: monitoring
k8s-app: influxdb
template:
metadata:
labels:
task: monitoring
k8s-app: influxdb
spec:
containers:
- name: influxdb
image: k8s.gcr.io/heapster-influxdb-amd64:v1.5.2
volumeMounts:
- mountPath: /data
name: influxdb-storage
volumes:
- name: influxdb-storage
emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
labels:
task: monitoring
# For use as a Cluster add-on (https://github.com/kubernetes/kubernetes/tree/master/cluster/addons)
# If you are NOT using this as an addon, you should comment out this line.
kubernetes.io/cluster-service: 'true'
kubernetes.io/name: monitoring-influxdb
name: monitoring-influxdb
namespace: kube-system
spec:
ports:
- port:
targetPort:
selector:
k8s-app: influxdb $ kubectl apply -f influxdb.yaml
deployment.apps/monitoring-influxdb created
service/monitoring-influxdb created
$ kubectl get pod -n kube-system
monitoring-influxdb-848b9b66f6-rplh8 1/1 Running 0 3
创建角色权限
kubectl apply -f https://raw.githubusercontent.com/kubernetes-retired/heapster/master/deploy/kube-config/rbac/heapster-rbac.yaml
部署heapster
kubectl apply -f https://raw.githubusercontent.com/kubernetes-retired/heapster/master/deploy/kube-config/influxdb/heapster.yaml
部署 grafana
https://raw.githubusercontent.com/kubernetes-retired/heapster/master/deploy/kube-config/influxdb/grafana.yaml