目录

一:通过daemonset运行cadvisor

二:通过daemonset运行node

三:通过deployment运行kube-state-metrics

四:prometheus联邦节点添加监控信息

五:prometheus-server节点添加match规则


一:通过daemonset运行cadvisor

kubectl apply -f cadvisor-export.yaml 

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: cadvisor
  namespace: monitoring
spec:
  selector:
    matchLabels:
      app: cAdvisor
  template:
    metadata:
      labels:
        app: cAdvisor
    spec:
      tolerations:    #污点容忍,忽略master的NoSchedule
        - effect: NoSchedule
          key: node-role.kubernetes.io/master
      hostNetwork: true #用node节点的网络
      restartPolicy: Always   #重启策略
      containers:
      - name: cadvisor
        image: opsharbor.****.net/prometheus/cadvisor:v0.39.2  #自己harbor仓库里的镜像
        imagePullPolicy: IfNotPresent  # 镜像策略
        ports:
        - containerPort: 8080
        volumeMounts:
          - name: root
            mountPath: /rootfs
          - name: run
            mountPath: /var/run
          - name: sys
            mountPath: /sys
          - name: docker
            mountPath: /var/lib/docker
      volumes:
      - name: root
        hostPath:
          path: /
      - name: run
        hostPath:
          path: /var/run
      - name: sys
        hostPath:
          path: /sys
      - name: docker
        hostPath:
          path: /var/lib/docker

查看信息cadvisor-export pod信息

[root@k8smaster prometheus]# kubectl get pod  -n monitoring  -o wide
NAME                  READY   STATUS    RESTARTS   AGE   IP            NODE        NOMINATED NODE   READINESS GATES
cadvisor-kwttc        1/1     Running   0          33s   10.19.14.21   k8snode2    <none>           <none>
cadvisor-n4wrn        1/1     Running   0          33s   10.19.14.9    k8smaster   <none>           <none>
cadvisor-zj4p9        1/1     Running   0          33s   10.19.14.22   k8snode1    <none>           <none>

二:通过daemonset运行node

kubectl apply -f node-export.yaml 

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: node-exporter
  namespace: monitoring 
  labels:
    k8s-app: node-exporter
spec:
  selector:
    matchLabels:
        k8s-app: node-exporter
  template:
    metadata:
      labels:
        k8s-app: node-exporter
    spec:
      hostNetwork: true
      hostPID: true
      tolerations:
        - effect: NoSchedule
          key: node-role.kubernetes.io/master
      containers:
      - image: prom/node-exporter:v1.3.1 
        imagePullPolicy: IfNotPresent
        name: prometheus-node-exporter
        ports:
        - containerPort: 9100
          hostPort: 9100
          protocol: TCP
          name: metrics
        volumeMounts:
        - mountPath: /host/proc
          name: proc
        - mountPath: /host/sys
          name: sys
        - mountPath: /host
          name: rootfs
        args:
        - --path.procfs=/host/proc
        - --path.sysfs=/host/sys
        - --path.rootfs=/host
      volumes:
        - name: proc
          hostPath:
            path: /proc
        - name: sys
          hostPath:
            path: /sys
        - name: rootfs
          hostPath:
            path: /
      

查看node-export pod信息

[root@k8smaster prometheus]# kubectl get pod -n monitoring -o wide
NAME                  READY   STATUS    RESTARTS   AGE    IP            NODE        NOMINATED NODE   READINESS GATES
cadvisor-kwttc        1/1     Running   1          72m    10.19.14.21   k8snode2    <none>           <none>
cadvisor-n4wrn        1/1     Running   0          72m    10.19.14.9    k8smaster   <none>           <none>
cadvisor-zj4p9        1/1     Running   1          72m    10.19.14.22   k8snode1    <none>           <none>
node-exporter-kc7c6   1/1     Running   0          110s   10.19.14.9    k8smaster   <none>           <none>
node-exporter-m7pg5   1/1     Running   0          110s   10.19.14.22   k8snode1    <none>           <none>
node-exporter-xhbkd   1/1     Running   0          110s   10.19.14.21   k8snode2    <none>           <none>

三:通过deployment运行kube-state-metrics

kubectl apply -f kube-state-metrics.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: kube-state-metrics
  namespace: kube-system
spec:
  replicas: 1
  selector:
    matchLabels:
      app: kube-state-metrics
  template:
    metadata:
      labels:
        app: kube-state-metrics
    spec:
      serviceAccountName: kube-state-metrics
      containers:
      - name: kube-state-metrics
        image: opsharbor.***.net/prometheus/kube-state-metrics:2.2.4 
        ports:
        - containerPort: 8080
---
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: kube-state-metrics
  namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: kube-state-metrics
rules:
- apiGroups: [""]
  resources: ["nodes", "pods", "services", "resourcequotas", "replicationcontrollers", "limitranges", "persistentvolumeclaims", "persistentvolumes", "namespaces", "endpoints"]
  verbs: ["list", "watch"]
- apiGroups: ["extensions"]
  resources: ["daemonsets", "deployments", "replicasets"]
  verbs: ["list", "watch"]
- apiGroups: ["apps"]
  resources: ["statefulsets"]
  verbs: ["list", "watch"]
- apiGroups: ["batch"]
  resources: ["cronjobs", "jobs"]
  verbs: ["list", "watch"]
- apiGroups: ["autoscaling"]
  resources: ["horizontalpodautoscalers"]
  verbs: ["list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: kube-state-metrics
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: kube-state-metrics
subjects:
- kind: ServiceAccount
  name: kube-state-metrics
  namespace: kube-system
---
apiVersion: v1
kind: Service
metadata:
  annotations:
    prometheus.io/scrape: 'true'
  name: kube-state-metrics
  namespace: kube-system
  labels:
    app: kube-state-metrics
spec:
  type: NodePort
  ports:
  - name: kube-state-metrics
    port: 8080
    targetPort: 8080
    nodePort: 30100
    protocol: TCP
  selector:
    app: kube-state-metrics

四:prometheus联邦节点添加监控信息

- job_name: "prometheus-node2-3"
    static_configs:
      - targets: ["10.19.14.21:9100", "10.19.14.22:9100", "10.19.14.22:8080", "10.19.14.21:8080", "10.19.14.21:30100"]

五:prometheus-server节点添加match规则

     params:
      'match[]':
       - '{job="prometheus"}'
       - '{__name__=~"job:.*"}'
       - '{__name__=~"node.*"}'
       - '{__name__=~"container.*"}'
       - '{__name__=~"cadvisor.*"}'
       - '{__name__=~"kube.*"}'

查看prometheus监控信息

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐