LoginSignup
2
3

More than 3 years have passed since last update.

PrometheusでKubernetesの監視

Last updated at Posted at 2019-09-27

prometheusでk8sの監視をしてみたくなったので、やり方をまとめる。結構調べるのに時間がかかった。次回はもっと短くささっと調べたい。

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

KubernetesにDefaultで付与される権限を拡張


---
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

あくまで使うための変更。Productionでは間違っても設定していけない。正しいやり方はnamespaceとか色々やって隔離してから権限を付与するみたい。Kubernetes何もわからん。
参考: https://github.com/fabric8io/fabric8/issues/6840

PrometheusをDeploy

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__

接続する際には適当にPortForwardingしてやる。

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

各NodeにNodeExporterを配置

Nodeの情報を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

他のメトリクス取得手段

後で調べる

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3