使用Prometheus监控Kubernetes

因为想要在k8s上监控prometheus,所以整理了一下方法。花了相当多时间来研究。下次希望能更简洁快速地查找。

参考:https://github.com/giantswarm/prometheus

扩展Kubernetes默认分配的权限。


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

只是为了使用而进行的更改。在生产环境中,绝对不能错误地进行设置。正确的做法是通过使用命名空间等方法进行隔离,然后授予权限。对于Kubernetes一窍不通。

部署 Prometheus

Prometheus的主体。创建配置文件,并通过configMap传递给Pod。


---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: prometheus
spec:
  replicas: 1
  selector:
    matchLabels:
      app: prometheus
  template:
    metadata:
      labels:
        app: prometheus
    spec:
      containers:
        - name: prometheus
          image: prom/prometheus
          args:
            - --config.file=/mnt/etc/prometheus.yml
          ports:
            - containerPort: 9090
          volumeMounts:
            - name: config-volume
              mountPath: /mnt/etc/
      volumes:
      - name: config-volume
        configMap:
          name: prometheus-config

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: prometheus-config
  labels:
    name: prometheus-config
data:
  prometheus.yml: |-
    global:
      scrape_interval:     15s
      evaluation_interval: 15s
    scrape_configs:
      - job_name: "kube-setting"
        kubernetes_sd_configs:
          - role: node
        relabel_configs:
          - source_labels: [__address__]
            regex: '(.*):10250'
            replacement: '${1}:30080'
            target_label: __address__

连接时适当地进行端口转发。

kubectl get pods
kubectl port-forward [pod_name] 9090:9090

在每个节点上部署NodeExporter。

为了将节点的信息传递给Prometheus,部署NodeExporter。顺便创建NodePort,以便从Prometheus获取数据。


---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: prometheus-ne
spec:
  selector:
    matchLabels:
      name: prometheus-ne
  template:
    metadata:
      labels:
        name: prometheus-ne
    spec:
      containers:
        - image: prom/node-exporter:v0.14.0
          name: prometheus-node-exporter
          ports:
            - containerPort: 9100

---
apiVersion: v1
kind: Service
metadata:
  name: prometheus-np
spec:
  type: NodePort
  ports:
    - name: prometheus-ne
      port: 9100
      targetPort: 9100
      protocol: TCP
      nodePort: 30080
  selector:
    name: prometheus-ne

获取其他指标的方法

稍后查询

广告
将在 10 秒后关闭
bannerAds