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

Was this helpful?