K8S-Demo集群实践14:部署集群监控服务Metrics Server

K8S-Demo集群实践14:部署集群监控服务Metrics Server

  • 一、下载并解压 Metrics-Server
  • 二、修改Metrics-Server配置文件
  • 三、部署Metrics-Server服务
  • 四、验证和测试
  • 五、遇到的问题
  • 参考
  • 附专栏链接

  • K8S资源使用情况的度量(如容器的 CPU 和内存使用)可以通过Metrics API获取
  • metrics-server通过调用Kubelet Summary API获取数据
  • 把master节点也加入集群进行调度,即master节点部署kubelet和kube-proxy,mertics-server组件才能够正常部署

一、下载并解压 Metrics-Server

[root@master1 ~]# cd /opt/install && mkdir -p metrics-server
[root@master1 install]# cd metrics-server
[root@master1 metrics-server]# wget https://github.com/kubernetes-sigs/metrics-server/archive/v0.3.6.tar.gz

二、修改Metrics-Server配置文件

[root@master1 ~]# cd /opt/install/metrics-server/metrics-server-0.3.6/deploy/1.8+/
[root@master1 1.8+]# cp metrics-server-deployment.yaml metrics-server-deployment.yaml.backup
[root@master1 1.8+]# vi metrics-server-deployment.yaml
[root@master1 1.8+]# diff metrics-server-deployment.yaml metrics-server-deployment.yaml.backup
32,38c32,33
<         image: harbor.demo/middleware/metrics-server-amd64:v0.3.6
<         imagePullPolicy: IfNotPresent
<         command:
<         - /metrics-server
<         - --requestheader-allowed-names=k8s-demo-aggregator
<         - --kubelet-insecure-tls
<         - --kubelet-preferred-address-types=InternalIP,Hostname,InternalDNS,ExternalDNS,ExternalIP
---
>         image: k8s.gcr.io/metrics-server-amd64:v0.3.6
>         imagePullPolicy: Always
42,48c37
<         resources:
<           limits:
<             cpu: 500m
<             memory: 500Mi
<           requests:
<             cpu: 200m
<             memory: 200Mi
---
> 
  • 容器镜像地址改为私有仓库地址
  • 拉取容器镜像的策略改为IfNotPresent(一般开发环境用Always配合latest)
  • 添加命令行参数,k8s-demo-aggregator是在aggregator-client-csr.json中配置的CN字段值
  • 添加了CPU、内存资源约束

三、部署Metrics-Server服务

  • 部署Metrics-Server
[root@master1 ~]# cd /opt/install/metrics-server/
[root@master1 metrics-server]# kubectl apply -f metrics-server-0.3.6/deploy/1.8+/
serviceaccount/metrics-server created
deployment.apps/metrics-server created
  • 查看 Metrics api服务
[root@master1 ~]# kubectl get apiservices.apiregistration.k8s.io | grep metrics
v1beta1.metrics.k8s.io                  kube-system/metrics-server   True        1h
[root@master1 ~]# kubectl get apiservice v1beta1.metrics.k8s.io -o yaml
apiVersion: apiregistration.k8s.io/v1
kind: APIService
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"apiregistration.k8s.io/v1beta1","kind":"APIService","metadata":{"annotations":{},"name":"v1beta1.metrics.k8s.io"},"spec":{"group":"metrics.k8s.io","groupPriorityMinimum":100,"insecureSkipTLSVerify":true,"service":{"name":"metrics-server","namespace":"kube-system"},"version":"v1beta1","versionPriority":100}}
  creationTimestamp: "2020-12-30T01:42:45Z"
  name: v1beta1.metrics.k8s.io
  resourceVersion: "6762803"
  selfLink: /apis/apiregistration.k8s.io/v1/apiservices/v1beta1.metrics.k8s.io
  uid: 437ded8b-3a2c-40ed-8503-4c1450f61635
spec:
  group: metrics.k8s.io
  groupPriorityMinimum: 100
  insecureSkipTLSVerify: true
  service:
    name: metrics-server
    namespace: kube-system
    port: 443
  version: v1beta1
  versionPriority: 100
status:
  conditions:
  - lastTransitionTime: "2021-01-27T02:16:16Z"
    message: all checks passed
    reason: Passed
    status: "True"
    type: Available

四、验证和测试

  • 查看节点资源利用信息
[root@master1 ~]# kubectl top node
NAME      CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%   
master1   175m         4%     1681Mi          58%       
master2   226m         5%     1844Mi          63%       
master3   195m         4%     1772Mi          61%       
node1     230m         5%     1212Mi          64%       
node2     118m         2%     962Mi           51%       
node3     238m         5%     1315Mi          69%     
  • 查看Pod资源利用信息
[root@master1 ~]# kubectl top pod -A
NAMESPACE              NAME                                         CPU(cores)   MEMORY(bytes)   
default                busybox                                      0m           1Mi             
kube-system            calico-kube-controllers-6bbbcf9cb7-mp2qg     1m           23Mi            
kube-system            calico-node-2mmrn                            33m          108Mi           
kube-system            calico-node-4gnz4                            28m          111Mi           
kube-system            calico-node-7lb2j                            36m          112Mi           
kube-system            calico-node-kwksx                            33m          111Mi           
kube-system            calico-node-lnwj2                            33m          114Mi           
kube-system            calico-node-tfj98                            35m          114Mi           
kube-system            coredns-7f44d46d8d-q4sd2                     4m           22Mi            
kube-system            metrics-server-59fc8d556f-tdbhd              1m           33Mi      
  • 通过命令行获取节点CPU、内存资源利用信息
[root@master1 ~]# kubectl get --raw "/apis/metrics.k8s.io/v1beta1/nodes/node2" | jq
{
  "kind": "NodeMetrics",
  "apiVersion": "metrics.k8s.io/v1beta1",
  "metadata": {
    "name": "node2",
    "selfLink": "/apis/metrics.k8s.io/v1beta1/nodes/node2",
    "creationTimestamp": "2021-01-28T02:41:22Z"
  },
  "timestamp": "2021-01-28T02:40:59Z",
  "window": "30s",
  "usage": {
    "cpu": "114340963n",
    "memory": "990204Ki"
  }
}

五、遇到的问题

[root@master1 ~]# kubectl top node
error: metrics not available yet
[root@master1 ~]# kubectl top node
Error from server (ServiceUnavailable): the server is currently unable to handle the request (get nodes.metrics.k8s.io)
  • 检查第二步中的配置信息是否和证书中的一致

参考

  • https://github.com/kubernetes-sigs/metrics-server/issues/157
  • https://blog.csdn.net/weixin_43384009/article/details/106011429
  • https://www.cnblogs.com/tchua/p/10844013.html
  • http://blog.ljmict.com/?p=98

附专栏链接

K8S-Demo集群实践00:搭建镜像仓库Harbor+安全扫描
K8S-Demo集群实践01:准备VMware虚拟机模板
K8S-Demo集群实践02:准备VMware虚拟机3台Master+3台Node
K8S-Demo集群实践03:准备集群各组件间HTTPS通讯需要的x509证书
K8S-Demo集群实践04:部署etcd三节点高可用集群
K8S-Demo集群实践05:安装kubectl并配置集群管理员账户
K8S-Demo集群实践06:部署kube-apiserver到master节点(3个无状态实例)
K8S-Demo集群实践07:kube-apiserver高可用方案
K8S-Demo集群实践08:部署高可用kube-controller-manager集群
K8S-Demo集群实践09:部署高可用kube-scheduler集群
K8S-Demo集群实践10:部署ipvs模式的kube-proxy组件
K8S-Demo集群实践11:部署ipvs模式的kube-kubelet组件
K8S-Demo集群实践12:部署Calico网络
K8S-Demo集群实践13:部署集群CoreDNS
K8S-Demo集群实践14:部署集群监控服务Metrics Server
K8S-Demo集群实践15:部署Kubernetes Dashboard
K8S-Demo集群实践16:部署Kube-Prometheus
K8S-Demo集群实践17:部署私有云盘owncloud(10.6版本)
K8S-Demo集群实践18:构建宇宙中第一个基础容器镜像


  • 先用起来,通过操作实践认识k8s,积累多了自然就理解了
  • 把理解的知识分享出来,自造福田,自得福缘
  • 追求简单,容易使人理解,知识的上下文也是知识的一部分,例如版本,时间等
  • 欢迎留言交流,也可以提出问题,一般在周末回复和完善文档
  • [email protected] 2021-1-28

你可能感兴趣的:(k8s-demo,kubernetes,k8s)