04-Metrics-Server 插件和top命令
2021-07-05 本文已影响0人
玄德公笔记
前言:
使用top命令查看需k8s使用资源信息,需要安装Metrics-Server 插件。
注意:这个kube-state-metrics
是不一样的。
1. Metrics Server 插件
参考文档:https://github.com/kubernetes-sigs/metrics-server
注意:官网有坑不要按上边的来
1.2 yaml 文件
-
下载地址:
https://github.com/kubernetes-sigs/metrics-server/releases/download/v0.3.6/components.yaml -
镜像准备
k8s.gcr.io的镜像下载不了,修改成阿里云的镜像如下:
registry.aliyuncs.com/google_containers/metrics-server-amd64:v0.3.6
- 修改yaml文件
添加command(必要)
添加limit(非必要)
修改镜像(非必要)
components.yaml 文件修改如下:
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
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", "nodes"]
verbs: ["get", "list", "watch"]
---
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: 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: 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: v1
kind: ServiceAccount
metadata:
name: metrics-server
namespace: kube-system
---
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.6
image: registry.aliyuncs.com/google_containers/metrics-server-amd64:v0.3.6
imagePullPolicy: IfNotPresent
args:
- --cert-dir=/tmp
- --secure-port=4443
command:
- /metrics-server
- --kubelet-insecure-tls
- --kubelet-preferred-address-types=InternalDNS,InternalIP,ExternalDNS,ExternalIP,Hostname
ports:
- name: main-port
containerPort: 4443
protocol: TCP
securityContext:
readOnlyRootFilesystem: true
runAsNonRoot: true
runAsUser: 1000
volumeMounts:
- name: tmp-dir
mountPath: /tmp
resources:
limits:
cpu: 300m
memory: 200Mi
requests:
cpu: 200m
memory: 100Mi
nodeSelector:
kubernetes.io/os: linux
kubernetes.io/arch: "amd64"
---
apiVersion: v1
kind: Service
metadata:
name: metrics-server
namespace: kube-system
labels:
kubernetes.io/name: "Metrics-server"
kubernetes.io/cluster-service: "true"
spec:
selector:
k8s-app: metrics-server
ports:
- port: 443
protocol: TCP
targetPort: main-port
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: system:metrics-server
rules:
- apiGroups:
- ""
resources:
- pods
- nodes
- nodes/stats
- namespaces
- configmaps
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
1.3 安装
- 安装
kubectl create -f ./components.yaml
- 查看
pod/metrics-server-6cbcc95697-xckv6 1/1 Running 0 6h26m
service/metrics-server ClusterIP 10.1.113.17 <none> 443/TCP 6h26m
deployment.apps/metrics-server 1/1 1 1 6h26m
replicaset.apps/metrics-server-6cbcc95697 1 1 1 6h26m
对比一下我们这钱安装的kube-state-metrics
[root@IoT-M01 yml]# kubectl get all -n kube-system |grep metrics
pod/kube-state-metrics-6497487656-4p9w6 1/1 Running 1 13d
pod/metrics-server-6cbcc95697-xckv6 1/1 Running 0 13m
service/kube-state-metrics ClusterIP None <none> 8080/TCP,8081/TCP 13d
service/metrics-server ClusterIP 10.1.113.17 <none> 443/TCP 13m
deployment.apps/kube-state-metrics 1/1 1 1 13d
deployment.apps/metrics-server 1/1 1 1 13m
replicaset.apps/kube-state-metrics-6497487656 1 1 1 13d
replicaset.apps/kube-state-metrics-74b87488f 0 0 0 13d
replicaset.apps/metrics-server-6cbcc95697 1 1 1 13m
可以看到,之前监控启动的
kube-state-metrics
和本次安装的metrics-server
并不是一套服务 。
一个使用了8080端口,一个使用了安全端口443。
虽然镜像不一样,但都是收集k8s资源信息的,不知道是不是用一个就可以,没有测试。
2.top命令使用示例
- 查看pod资源
[root@devops-master ~]# kubectl top pod -n iot-test-03
NAME CPU(cores) MEMORY(bytes)
apigw-iot-85584cf578-ffr9d 1m 35Mi
apigw-iot-85584cf578-j5bg9 1m 43Mi
apigw-iot-85584cf578-vs4f6 1m 45Mi
emqx-iot-0 2m 173Mi
emqx-iot-1 3m 143Mi
emqx-iot-2 3m 139Mi
monitor-iot-67857b5d65-5gwxd 1m 40Mi
monitor-iot-67857b5d65-p2gjx 1m 47Mi
monitor-iot-67857b5d65-vgl92 1m 33Mi
msgroute-iot-6c4d676c5d-6n6jp 1m 11Mi
msgroute-iot-6c4d676c5d-7grgx 1m 17Mi
msgroute-iot-6c4d676c5d-q4k87 1m 14Mi
mysql-iot-8dflr 1m 251Mi
redis-iot-58498b5dc8-j5w8g 2m 13Mi
remotectl-iot-74d59ff47-6r9fd 1m 23Mi
remotectl-iot-74d59ff47-r42gn 1m 27Mi
remotectl-iot-74d59ff47-s2c7k 1m 33Mi
remoteops-iot-679465fd48-kn2l8 1m 28Mi
remoteops-iot-679465fd48-sz8nx 1m 16Mi
remoteops-iot-679465fd48-vsj8v 1m 23Mi
web-iot-ffc9b5fff-4ls9k 0m 9Mi
web-iot-ffc9b5fff-5d457 0m 10Mi
web-iot-ffc9b5fff-g54rx 0m 5Mi
- 查看node资源
NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
b9-node02 118m 5% 1205Mi 32%
b9-node3 80m 2% 2321Mi 14%
devopsnode01 186m 2% 6569Mi 41%
es-node32cuogf 325m 2% 6866Mi 21%
master01 244m 3% 17915Mi 56%
- 简单的监控页面
写一个脚本将将数据写入nginx页面
#!/bin/bash
for ((i=0;i<=58;i++))
do
kubectl top -n $1 pod > /usr/local/nginx/build/$1.txt
echo "----------date-------------" >> /usr/local/nginx/build/$1.txt
date "+%Y-%m-%d %H:%M:%S" >> /usr/local/nginx/build/$1.txt
sleep 1
done
设置定时任务
* * * * * bash /usr/local/script/nginx/kube-top.sh crust-framework
image.png查看如下