Fork me on GitHub

k8s之资源指标API部署metrics-server

1.部署metrics-server

  从v1.8开始,引入了新的功能,即把资源指标引入api,资源指标:metrics-server,自定义指标:prometheus,k8s-prometheus-adapter(把prometheus采集的数据转化成k8s可理解的数据).

新一代架构:

a.核心指标流水线:由kubelet、metrics-server以及由API server提供的api组成;

cpu累计利用率、内存实时利用率、pod的资源占用率及容器的磁盘占用率;

b.监控流水线:用于从系统收集各种指标数据并提供终端用户、存储系统以及HPA,包含核心指标以及许多非核心指标,非核心指标不能被k8s所解析.

metrics-server是个api server,仅仅收集cpu利用率、内存利用率等.

# 访问https://github.com/kubernetes/kubernetes/tree/master/cluster/addons/metrics-server 
for i in auth-delegator.yaml metrics-apiservice.yaml metrics-server-service.yaml \
auth-reader.yaml metrics-server-deployment.yaml resource-reader.yaml; \
do wget https://raw.githubusercontent.com/kubernetes/kubernetes/master/cluster/addons/metrics-server/$i;done
# 手动在node1上下载镜像,有googlecontainer、mirrorgooglecontainers这两个可以用
docker pull mirrorgooglecontainers/metrics-server-amd64:v0.3.3
docker pull mirrorgooglecontainers/addon-resizer:1.8.4
docker tag mirrorgooglecontainers/metrics-server-amd64:v0.3.3 k8s.gcr.io/metrics-server-amd64:v0.3.3
docker tag mirrorgooglecontainers/addon-resizer:1.8.4 k8s.gcr.io/addon-resizer:1.8.4

前方高能,做了那么多始终报错,只有回退版本,修改配置文件

vim resource-reader.yaml # 添加一行nodes/stats
rules:
- apiGroups:
  - ""
  resources:
  - pods
  - nodes
  - namespaces
  - nodes/stats
# 第一个镜像的command只留这三行
vim metrics-server-deployment.yaml
containers:
- name: metrics-server
  image: k8s.gcr.io/metrics-server-amd64:v0.3.3
  imagePullPolicy: IfNotPresent
  command:
  - /metrics-server
  - --kubelet-insecure-tls
  - --kubelet-preferred-address-types=InternalIP
# 第二个镜像的command改成需要的参数
volumeMounts:
- name: metrics-server-config-volume
  mountPath: /etc/config
command:
  - /pod_nanny
  - --config-dir=/etc/config
  - --cpu=20m
  - --extra-cpu=0.5m
  - --memory=200Mi
  - --extra-memory=50Mi
  - --threshold=5
  - --deployment=metrics-server-v0.3.2
  - --container=metrics-server
  - --poll-period=300000
  - --estimator=exponential
  - --minClusterSize=10

kubectl logs metrics-server-v0.3.2-7c686bc879-trs8q -c metrics-server-nanny -n kube-system
ERROR: Invoked by [/pod_nanny --config-dir=/etc/config --cpu=40m --extra-cpu=0.5m --memory=200Mi
ERROR: Watching namespace: kube-system, pod: metrics-server-v0.3.2-7c686bc879-trs8q, container: metrics-server.
ERROR: storage: MISSING, extra_storage: 0Gi
ERROR: cpu: 40m, extra_cpu: 0.5m, memory: 200Mi, extra_memory: 50Mi
ERROR: Resources: [{Base:{i:{value:40 scale:-3} d:{Dec:<nil>} s:40m Format:DecimalSI} 

kubectl logs metrics-server-v0.3.2-79c585b88c-h74pl -c metrics-server -n kube-system
I0511 15:59:50.515057       1 serving.go:312] Generated self-signed cert (apiserver.local.config/certificates/apiserver.crt, 
apiserver.local.config/certificates/apiserver.key)
I0511 16:00:25.615475       1 secure_serving.go:116] Serving securely on [::]:443

wget https://github.com/kubernetes-incubator/metrics-server/archive/v0.3.1.zip
unzip v0.3.1.zip 
cd metrics-server-0.3.1/deploy/1.8+/
# 修改deployment.yaml文件
--kubelet-preferred-address-types=InternalIP # 直接使用节点IP地址获取数据
--kubelet-insecure-tls # 不验证客户端证书
image: mirrorgooglecontainers/metrics-server-amd64:v0.3.1 # 更换镜像源
cat metrics-server-deployment.yaml 
---
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:
      # 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: mirrorgooglecontainers/metrics-server-amd64:v0.3.1
        imagePullPolicy: IfNotPresent
        command:
          - /metrics-server
          - --metric-resolution=30s
          - --kubelet-insecure-tls
          - --kubelet-preferred-address-types=InternalIP,Hostname,InternalDNS,ExternalDNS,ExternalIP
        volumeMounts:
        - name: tmp-dir
          mountPath: /tmp

kubectl api-versions
kubectl proxy --port=8080
curl http://localhost:8080/apis/metrics.k8s.io/v1beta1
curl http://localhost:8080/apis/metrics.k8s.io/v1beta1/pods
kubectl top nodes
NAME         CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%   
k8s-master   325m         16%    1322Mi          70%       
k8s-node1    157m         7%     765Mi           40%       
k8s-node2    144m         7%     592Mi           31%

 

参考博客:https://blog.csdn.net/oyym_mv/article/details/87166639

参考博客:http://blog.itpub.net/28916011/viewspace-2216340/

 

posted @ 2019-08-03 22:06  法外狂徒  阅读(3235)  评论(0编辑  收藏  举报