1. 程式人生 > >[叢集監控]使用cAdvisor + Heapster + InfluxDB + Grafana

[叢集監控]使用cAdvisor + Heapster + InfluxDB + Grafana

架構和元件

元件功能

  • cAdvisor:容器資料收集。
  • Heapster:叢集監控資料收集,彙總所有節點監控資料。
  • InfluxDB:時序資料庫,儲存監控資料。
  • Grafana:視覺化展示。

架構圖

這裡寫圖片描述

工作流程

cAdvisor負責收集節點的狀態資訊,然後Heapster會通過kubelet向每一個節點請求cAdvisor收集的資訊,並將資訊儲存在InfluxDB中,Grafana通過InfluxDB中的資訊進行前端的展示。

部署

cAdvisor

cAdvisor預設是安裝的,可以通過瀏覽器訪問節點的4194埠進行測試。
這裡寫圖片描述

cAdvisor會收集當前節點的資訊和容器的資訊,主要會收集網路和磁碟IO,檔案系統大小、記憶體使用情況等。

但是,cAdvisor只會收集實時的資料,並不會做持久化儲存

InfluxDB

# cat influxdb.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: monitoring-influxdb
  namespace: kube-system
spec:
  replicas: 1
  template:
    metadata:
      labels:
        task: monitoring
        k8s-app: influxdb
    spec:
      containers:
      - name: influxdb
        image: registry.cn-hangzhou.aliyuncs.com/google-containers/heapster-influxdb-amd64:v1.1.1
volumeMounts: - mountPath: /data name: influxdb-storage volumes: - name: influxdb-storage emptyDir: {} --- apiVersion: v1 kind: Service metadata: labels: task: monitoring # For use as a Cluster add-on (https://github.com/kubernetes/kubernetes/tree/master/cluster/addons)
# If you are NOT using this as an addon, you should comment out this line. kubernetes.io/cluster-service: 'true' kubernetes.io/name: monitoring-influxdb name: monitoring-influxdb namespace: kube-system spec: ports: - port: 8086 targetPort: 8086 selector: k8s-app: influxdb

建立pod和service:

kubectl create -f influxdb.yaml

檢視建立情況:
這裡寫圖片描述

Heapster

# cat heapster.yaml

apiVersion: v1
kind: ServiceAccount
metadata:
  name: heapster
  namespace: kube-system

---

kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: heapster
subjects:
  - kind: ServiceAccount
    name: heapster
    namespace: kube-system
roleRef:
  kind: ClusterRole
  name: cluster-admin
  apiGroup: rbac.authorization.k8s.io

---

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: heapster
  namespace: kube-system
spec:
  replicas: 1
  template:
    metadata:
      labels:
        task: monitoring
        k8s-app: heapster
    spec:
      serviceAccountName: heapster
      containers:
      - name: heapster
        image: registry.cn-hangzhou.aliyuncs.com/google-containers/heapster-amd64:v1.4.2
        imagePullPolicy: IfNotPresent
        command:
        - /heapster
        - --source=kubernetes:https://kubernetes.default
        - --sink=influxdb:http://monitoring-influxdb:8086

---

apiVersion: v1
kind: Service
metadata:
  labels:
    task: monitoring
    # For use as a Cluster add-on (https://github.com/kubernetes/kubernetes/tree/master/cluster/addons)
    # If you are NOT using this as an addon, you should comment out this line.
    kubernetes.io/cluster-service: 'true'
    kubernetes.io/name: Heapster
  name: heapster
  namespace: kube-system
spec:
  ports:
  - port: 80
    targetPort: 8082
  selector:
    k8s-app: heapster

建立pod和svc:

kubectl create -f heapster.yaml

檢視建立情況:
這裡寫圖片描述

Grafana

# cat grafana.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: monitoring-grafana
  namespace: kube-system
spec:
  replicas: 1
  template:
    metadata:
      labels:
        task: monitoring
        k8s-app: grafana
    spec:
      containers:
      - name: grafana
        image: registry.cn-hangzhou.aliyuncs.com/google-containers/heapster-grafana-amd64:v4.4.1
        ports:
          - containerPort: 3000
            protocol: TCP
        volumeMounts:
        - mountPath: /var
          name: grafana-storage
        env:
        - name: INFLUXDB_HOST
          value: monitoring-influxdb
        - name: GRAFANA_PORT
          value: "3000"
          # The following env variables are required to make Grafana accessible via
          # the kubernetes api-server proxy. On production clusters, we recommend
          # removing these env variables, setup auth for grafana, and expose the grafana
          # service using a LoadBalancer or a public IP.
        - name: GF_AUTH_BASIC_ENABLED
          value: "false"
        - name: GF_AUTH_ANONYMOUS_ENABLED
          value: "true"
        - name: GF_AUTH_ANONYMOUS_ORG_ROLE
          value: Admin
        - name: GF_SERVER_ROOT_URL
          # If you're only using the API Server proxy, set this value instead:
          value: /api/v1/proxy/namespaces/kube-system/services/monitoring-grafana/
          #value: /
      volumes:
      - name: grafana-storage
        emptyDir: {}

---

apiVersion: v1
kind: Service
metadata:
  labels:
    # For use as a Cluster add-on (https://github.com/kubernetes/kubernetes/tree/master/cluster/addons)
    # If you are NOT using this as an addon, you should comment out this line.
    kubernetes.io/cluster-service: 'true'
    kubernetes.io/name: monitoring-grafana
  name: monitoring-grafana
  namespace: kube-system
spec:
  # In a production setup, we recommend accessing Grafana through an external Loadbalancer
  # or through a public IP.
  # type: LoadBalancer
  type: NodePort
  ports:
  - port : 80
    targetPort: 3000
  selector:
    k8s-app: grafana

建立svc和pod:

kubectl create -f grafana.yaml

檢視建立狀態:
這裡寫圖片描述

訪問測試

想要訪問grafana有兩種方式:第一種是通過apiserver的非安全埠進行訪問,第二種是通過proxy代理的埠訪問

通過apiserver的非安全埠訪問

apiserver的非安全埠就是在配置檔案中定義的insecure
這裡寫圖片描述

首先修改一下這個地址:

# vim /opt/kubernetes/cfg/kube-apiserver

--insecure-bind-address=10.10.99.225

重啟apiserver:

systemctl restart kube-apiserver.service

通過瀏覽器訪問:

http://10.10.99.225:8080/api/v1/proxy/namespaces/kube-system/services/monitoring-grafana/