参考地址:https://github.com/kubernetes-incubator/metrics-server/
metrics-server: 它是一种API Server,提供了核心的Metrics API,就像k8s组件kube-apiserver提供了很多API群组一样,但它不是k8s组成部分,而是托管运行在k8s之上的Pod。为了让用户无缝的使用metrics-server当中的API,还需要把这类自定义的API,通过聚合器聚合到核心API组里,然后可以把此API当作是核心API的一部分,通过kubectl api-versions可直接查看。metrics-server收集指标数据的方式是从各节点上kubelet提供的Summary API 即10250端口收集数据,收集Node和Pod核心资源指标数据,主要是内存和cpu方面的使用情况,并将收集的信息存储在内存中。
原理图如下:
Metrics Server安装
准备metrics-server部署文件:
https://github.com/kubernetes-incubator/metrics-server/tree/master/deploy/1.8%2B
其中metrics-server-deployment.yaml需要使用k8s.gcr.io/metrics-server-amd64:v0.3.1镜像,该镜像无法直接下载,到docker hub中寻找替代镜像rancher/metrics-server-amd64
下载rancher/metrics-server-amd64基础镜像
修改metrics-server-deployment.yaml镜像名称:docker.io/rancher/metrics-server-amd64:v0.3.1
部署文件详解如下:
声明一个ClusterRole:
[root@Ac-private-1 metrics]# cat aggregated-metrics-reader.yaml
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: system:aggregated-metrics-reader
labels:
rbac.authorization.k8s.io/aggregate-to-view: "true"
rbac.authorization.k8s.io/aggregate-to-edit: "true"
rbac.authorization.k8s.io/aggregate-to-admin: "true"
rules:
- apiGroups: ["metrics.k8s.io"]
resources: ["pods"]
verbs: ["get", "list", "watch"]
声明RoleBinding:
root@Ac-private-1 metrics]# cat auth-reader.yaml
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: RoleBinding
metadata:
name: metrics-server-auth-reader
namespace: kube-system
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: extension-apiserver-authentication-reader
subjects:
- kind: ServiceAccount
name: metrics-server
namespace: kube-system
进行角色绑定:
[root@Ac-private-1 metrics]# cat auth-delegator.yaml
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: metrics-server:system:auth-delegator
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: system:auth-delegator
subjects:
- kind: ServiceAccount
name: metrics-server
namespace: kube-system
声明读取资源的角色:ClusterRole ClusterRoleBinding
root@Ac-private-1 metrics]# cat resource-reader.yaml
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: system:metrics-server
rules:
- apiGroups:
- ""
resources:
- pods
- nodes
- nodes/stats
- namespaces
verbs:
- get
- list
- watch
- apiGroups:
- "extensions"
resources:
- deployments
verbs:
- get
- list
- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: system:metrics-server
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: system:metrics-server
subjects:
- kind: ServiceAccount
name: metrics-server
namespace: kube-system
声明APIService
[root@Ac-private-1 metrics]# cat metrics-apiservice.yaml
---
apiVersion: apiregistration.k8s.io/v1beta1
kind: APIService
metadata:
name: v1beta1.metrics.k8s.io
spec:
service:
name: metrics-server
namespace: kube-system
group: metrics.k8s.io
version: v1beta1
insecureSkipTLSVerify: true
groupPriorityMinimum: 100
versionPriority: 100
进行Deployment 注意修改image以及添加启动命令,禁止https验证
---
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:
- name: tmp-dir
emptyDir: {}
containers:
- name: metrics-server
image: docker.io/rancher/metrics-server-amd64:v0.3.1
imagePullPolicy: Always
command:
- /metrics-server
- --metric-resolution=30s
- --kubelet-insecure-tls
- --kubelet-preferred-address-types=InternalIP,Hostname,InternalDNS,ExternalDNS,ExternalIP
volumeMounts:
- name: tmp-dir
mountPath: /tmp
声明Service
[root@Ac-private-1 metrics]# cat metrics-server-service.yaml
---
apiVersion: v1
kind: Service
metadata:
name: metrics-server
namespace: kube-system
labels:
kubernetes.io/name: "Metrics-server"
spec:
selector:
k8s-app: metrics-server
ports:
- port: 443
protocol: TCP
targetPort: 443
执行部署文件:
访问路径:
获取所有Node资源状态
curl -k https://192.168.1.15:6443/apis/metrics.k8s.io/v1beta1/nodes
{"kind":"NodeMetricsList","apiVersion":"metrics.k8s.io/v1beta1","metadata":{"selfLink":"/apis/metrics.k8s.io/v1beta1/nodes"},"items":[{"metadata":{"name":"ac-private-1","selfLink":"/apis/metrics.k8s.io/v1beta1/nodes/ac-private-1","creationTimestamp":"2018-12-06T02:09:50Z"},"timestamp":"2018-12-06T02:09:15Z","window":"30s","usage":{"cpu":"139173580n","memory":"14264488Ki"}},{"metadata":{"name":"ac-private-2","selfLink":"/apis/metrics.k8s.io/v1beta1/nodes/ac-private-2","creationTimestamp":"2018-12-06T02:09:50Z"},"timestamp":"2018-12-06T02:09:15Z","window":"30s","usage":{"cpu":"21085203n","memory":"3846036Ki"}},{"metadata":{"name":"ac-private-0","selfLink":"/apis/metrics.k8s.io/v1beta1/nodes/ac-private-0","creationTimestamp":"2018-12-06T02:09:50Z"},"timestamp":"2018-12-06T02:09:18Z","window":"30s","usage":{"cpu":"73255790n","memory":"12064132Ki"}},{"metadata":{"name":"xjs-dn02","selfLink":"/apis/metrics.k8s.io/v1beta1/nodes/xjs-dn02","creationTimestamp":"2018-12-06T02:09:50Z"},"timestamp":"2018-12-06T02:09:18Z","window":"30s","usage":{"cpu":"93832180n","memory":"10894344Ki"}},{"metadata":{"name":"xjs-dn04","selfLink":"/apis/metrics.k8s.io/v1beta1/nodes/xjs-dn04","creationTimestamp":"2018-12-06T02:09:50Z"},"timestamp":"2018-12-06T02:09:20Z","window":"30s","usage":{"cpu":"329854983n","memory":"10906516Ki"}}]}
获取所有pods资源状态:
https://192.168.1.15:6443/apis/metrics.k8s.io/v1beta1/namespaces/default/pods
{"kind":"PodMetricsList","apiVersion":"metrics.k8s.io/v1beta1","metadata":{"selfLink":"/apis/metrics.k8s.io/v1beta1/namespaces/default/pods"},"items":[{"metadata":{"name":"tomcat-t6q74","namespace":"default","selfLink":"/apis/metrics.k8s.io/v1beta1/namespaces/default/pods/tomcat-t6q74","creationTimestamp":"2018-12-06T02:21:56Z"},"timestamp":"2018-12-06T02:21:43Z","window":"30s","containers":[{"name":"tomcat","usage":{"cpu":"259106n","memory":"650528Ki"}}]},{"metadata":{"name":"centos-ssh","namespace":"default","selfLink":"/apis/metrics.k8s.io/v1beta1/namespaces/default/pods/centos-ssh","creationTimestamp":"2018-12-06T02:21:56Z"},"timestamp":"2018-12-06T02:21:23Z","window":"30s","containers":[{"name":"centos-ssh","usage":{"cpu":"0","memory":"1032Ki"}}]},{"metadata":{"name":"busybox-68f48b9c57-887mw","namespace":"default","selfLink":"/apis/metrics.k8s.io/v1beta1/namespaces/default/pods/busybox-68f48b9c57-887mw","creationTimestamp":"2018-12-06T02:21:56Z"},"timestamp":"2018-12-06T02:21:42Z","window":"30s","containers":[{"name":"busybox","usage":{"cpu":"0","memory":"60Ki"}}]}]}
获取某个Node节点资源:
https://192.168.1.15:6443/apis/metrics.k8s.io/v1beta1/nodes/ac-private-2
{"kind":"NodeMetrics","apiVersion":"metrics.k8s.io/v1beta1","metadata":{"name":"ac-private-2","selfLink":"/apis/metrics.k8s.io/v1beta1/nodes/ac-private-2","creationTimestamp":"2018-12-06T02:26:04Z"},"timestamp":"2018-12-06T02:25:46Z","window":"30s","usage":{"cpu":"22436949n","memory":"3846476Ki"}}
获取某个Pod资源:
https://192.168.1.15:6443/apis/metrics.k8s.io/v1beta1/namespaces/default/pods/tomcat-t6q74
{"kind":"PodMetrics","apiVersion":"metrics.k8s.io/v1beta1","metadata":{"name":"tomcat-t6q74","namespace":"default","selfLink":"/apis/metrics.k8s.io/v1beta1/namespaces/default/pods/tomcat-t6q74","creationTimestamp":"2018-12-06T02:24:30Z"},"timestamp":"2018-12-06T02:24:15Z","window":"30s","containers":[{"name":"tomcat","usage":{"cpu":"222623n","memory":"650528Ki"}}]}
metric可以获取Node以及Pod的资源情况 这里只包含cpu以及内存使用。