github地址:https://github.com/kubernetes-incubator/metrics-server
官网介绍:https://kubernetes.io/docs/tasks/debug-application-cluster/core-metrics-pipeline/
Starting from Kubernetes 1.8, resource usage metrics, such as container CPU and memory usage, are available in Kubernetes through the Metrics API. These metrics can be either accessed directly by user, for example by using kubectl top
command, or used by a controller in the cluster, e.g. Horizontal Pod Autoscaler, to make decisions.
metrics-server.yaml
--- apiVersion: v1 kind: ServiceAccount metadata: name: metrics-server namespace: kube-system --- 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 --- apiVersion: rbac.authorization.k8s.io/v1 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 --- apiVersion: rbac.authorization.k8s.io/v1 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 --- 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 --- apiVersion: apps/v1 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.0 command: - /metrics-server - --kubelet-insecure-tls - --kubelet-preferred-address-types=InternalIP volumeMounts: - name: tmp-dir mountPath: /tmp --- 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
测试效果:
[root@node01 metrics-server]# kubectl get hpa NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE tomcat-hpa Deployment/tomcat 0%/5% 1 5 1 1h
[root@node01 metrics-server]# kubectl top node NAME CPU(cores) CPU% MEMORY(bytes) MEMORY% node01 653m 16% 3045Mi 39%
[root@node01 metrics-server]# kubectl top pod NAME CPU(cores) MEMORY(bytes) lxcfs-8tg6b 1m 2Mi mysql-85c85c5668-fr6j7 1m 462Mi tomcat-78c9778858-424gc 2m 113Mi
[root@node01 metrics-server]# kubectl top pod NAME CPU(cores) MEMORY(bytes) lxcfs-8tg6b 1m 2Mi mysql-85c85c5668-fr6j7 1m 462Mi tomcat-78c9778858-424gc 2m 113Mi [root@node01 metrics-server]# kubectl top pod --namespace=kube-system NAME CPU(cores) MEMORY(bytes) coredns-777d78ff6f-rrpx8 2m 9Mi coredns-777d78ff6f-tql47 2m 9Mi etcd-node01 16m 96Mi kube-apiserver-node01 34m 485Mi kube-controller-manager-node01 49m 73Mi kube-flannel-ds-rxrp5 3m 14Mi kube-proxy-r6bd2 2m 20Mi kube-scheduler-node01 10m 15Mi kubernetes-dashboard-d4866d978-kpz4m 1m 19Mi metrics-server-84bd4db594-fz2k2 1m 16Mi