kubectl top reports error when viewing container resources
Error from server (NotFound): the server could not find the requested resource (get services http:heapster:)
Reason for error
The k8s cluster is missing the metrics-server component
Install metrics-server
1. Download the yaml file required by metrics-server
Path: https://github.com/kubernetes-sigs/metrics-server/archive/v0.3.6.tar.gz
2. After decompression, enter the…/metrics-server-0.3.6/deploy/1.8+ directory
3. Modify metric-server-deployment.yaml
---
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
#修改镜像拉取地址为aliyun路径
image: registry.cn-hangzhou.aliyuncs.com/google_containers/metrics-server-amd64:v0.3.6
#镜像拉取策略改为IfNotPresent
imagePullPolicy: IfNotPresent
#增加command字段与参数:这个是k8s在1.7的新特性,如果是1.16版本的可以不用添加,1.17以后要添加。这个参数的作用是Aggregation允许在 不修改Kubernetes核心代码的同时扩展Kubernetes API
command:
- /metrics-server
- --kubelet-insecure-tls
- --kubelet-preferred-address-types=InternalDNS,InternalIP,ExternalDNS,ExternalIP,Hostname
volumeMounts:
- name: tmp-dir
mountPath: /tmp
4. Deploy metrics-server
kubectl apply -f .
5. Check pod status
kubectl get pod -n kube-system |grep metrics-server
6. Post-deployment verification
kubectl top nodes
kubectl top pods -n kube-system
After deploying metrics-server, execute kubectl top. If there is still an error: Metrics not available for pod, the error will be eliminated automatically after a while.