k8s-prometheus

prometheus operator

prometheus operator๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด k8s์—์„œ ์‰ฝ๊ฒŒ prometheus๋ฅผ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

node exporter์™€ grafana๊นŒ์ง€ ๋ชจ๋‘ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ ๊ธฐ๋ณธ์ ์œผ๋กœ prometheus operator๋Š” k8s cluster๋งŒ ๋ชจ๋‹ˆํ„ฐ๋ง์„ ํ•œ๋‹ค.

์™ธ๋ถ€์— ์žˆ๋Š” server๋ฅผ ๋ชจ๋‹ˆํ„ฐ๋ง์„ ํ•˜์ง€ ๋ชปํ•œ๋‹ค.

์•„๋ž˜์—์„œ ์™ธ๋ถ€ server๋ฅผ prometheus operator๋กœ ๋ชจ๋‹ˆํ„ฐ๋ง ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด์ž.

prometheus operator ์—์„œ additional Scrape Configuration ์‚ฌ์šฉํ•˜๊ธฐ

https://github.com/prometheus-operator/prometheus-operator/blob/main/Documentation/additional-scrape-config.md

์—ฌ๊ธฐ๋ฅผ ์ฐธ๊ณ ํ•˜๋ฉด ๋œ๋‹ค.

์ผ๋‹จ prometheus-additional.yaml๋ฅผ ๋งŒ๋“ ๋‹ค.

- job_name: 'node_exporter_vm'
  static_configs:
    - targets: ['aaa:9100']

k8s Prometheus๋Š” ์„ค์ •์„ ๋ชจ๋‘ secret์œผ๋กœ ์ €์žฅํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์ด ์„ค์ •์„ secret์œผ๋กœ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค.

kubectl create secret generic additional-scrape-configs --from-file=prometheus-additional.yaml --dry-run=client -o yaml > additional-scrape-configs.yaml

์ด์ œ additional-scrape-configs.yaml์ด ์ƒ๊ฒป๋‹ค.

k8s์— ์ ์šฉํ•ด์ฃผ์ž.

kubectl apply -f additional-scrape-configs.yaml -n monitoring

์ž˜ ๋ฌ๋‹ค. ์ด์ œ ๊ธฐ์กด์— ์„ค์น˜๋˜์žˆ๋˜ prometheus operator์— ์„ค์ •์— ๋‹ค์Œ์„ ์ถ”๊ฐ€ํ•ด์ค˜์•ผํ•œ๋‹ค.

apiVersion: monitoring.coreos.com/v1
kind: Prometheus
metadata:
  name: prometheus
  labels:
    prometheus: prometheus
spec:
  ...
  additionalScrapeConfigs:
    name: additional-scrape-configs
    key: prometheus-additional.yaml

์—…๋ฐ์ดํŠธ๋ฅผ ํ•˜๋ฉด ์ ์šฉ๋œ๋‹ค.

์ด์ œ prometheus์— ui์— ๋“ค์–ด๊ฐ€์„œ Status >> target์„ ํ™•์ธํ•ด๋ณด๋ฉด ์ถ”๊ฐ€๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

kube-prometheus

์ €๋Š” prometheus operator๋ฅผ ์ข€๋” ํŽธํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์–ด ๋‹ค์Œ ํ”„๋กœ์ ํŠธ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

https://github.com/prometheus-operator/kube-prometheus

์ด๊ฑธ ์‚ฌ์šฉํ•˜์‹œ๋Š”๋ถ„๋“ค์€ ๋‹ค์Œ์ฒ˜๋Ÿผ ์ง„ํ–‰ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

secret ์ƒ์„ฑ

์•ž์— ๋ฐฉ์‹๊ณผ ๊ฐ™์ด ์„ค์ •ํŒŒ์ผ์„ yaml๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  secret์œผ๋กœ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

- job_name: 'node_exporter_vm'
  static_configs:
    - targets: ['aaa:9100']
kubectl create secret generic additional-scrape-configs --from-file=prometheus-additional.yaml --dry-run=client -o yaml > additional-scrape-configs.yaml

์ด๊ฑธ k8s์— ์ ์šฉํ•ฉ๋‹ˆ๋‹ค.

์ด์ œ cluster.jsonํŒŒ์ผ์„ ์—…๋ฐ์ดํŠธํ•ด์ค๋‹ˆ๋‹ค.

prometheus+:: {
  prometheus+: {
    spec+: {
      ...

      // ์ถ”๊ฐ€
      additionalScrapeConfigs:{
        name: 'additional-scrape-configs',
        key: 'prometheus-additional.yaml'
      }
    },
  },
},

์ด์ œ manifest๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

brew install jsonnet-bundler

jb install # create vendor
jb update

docker run --rm -v $(pwd):$(pwd) --workdir $(pwd) quay.io/coreos/jsonnet-ci ./build.sh cluster.jsonnet

cat build.sh

#!/usr/bin/env bash

# This script uses arg $1 (name of *.jsonnet file to use) to generate the manifests/*.yaml files.

set -e
set -x
# only exit with zero if all commands of the pipeline exit successfully
set -o pipefail

# Make sure to use project tooling
PATH="$(pwd)/tmp/bin:${PATH}"

# Make sure to start with a clean 'manifests' dir
rm -rf manifests
mkdir -p manifests/setup

# Calling gojsontoyaml is optional, but we would like to generate yaml, not json
jsonnet -J vendor -m manifests "${1-example.jsonnet}" | xargs -I{} sh -c 'cat {} | gojsontoyaml > {}.yaml' -- {}

# Make sure to remove json files
find manifests -type f ! -name '*.yaml' -delete
rm -f kustomization

์ด์ œ ์ƒ์„ฑ๋œ manifest๋ฅผ ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

cat prometheus-prometheus.yaml

apiVersion: monitoring.coreos.com/v1
kind: Prometheus
metadata:
  name: k8s
  namespace: monitoring
spec:
  // ์ถ”๊ฐ€๋˜์ž‡๋Š”๊ฑฐ ํ™•์ธํ•˜์ž.
  additionalScrapeConfigs:
    key: prometheus-additional.yaml
    name: additional-scrape-configs

์ด์ œ ์ ์šฉํ•˜๋ฉด ๋œ๋‹ค.

Last updated