Monitoring

Prometheus Operator #2 - 설치 및 사용

토마스.dev 2019. 1. 2. 11:55

여기서는 Prometheus Operator를 Helm Chart로 설치해보고 형태를 간략히 살펴보도록 하겠다.


Operator를 OLM(Operator Lifecycle Manager)으로 설치/관리할 수도 있겠으나 필자는 현재 해당 방법으로는 해본적이 없고 Harbor, Spinnaker등의 Registry, CD 플랫폼과 연계하려다보니 Helm Chart로 하는게 적합하다 싶어 해당 방법으로만 설명한다.

사전 준비조건

  • Kubernetes v1.11
  • Helm

Prometheus Operator 다운받기

Stable Helm chart를 곧바로 사용하면 좋겠으나, 그대로 사용하기에는 환경에 따라 수정을 해야할 필요가 많기 때문에(예를들어 k8s api 버전에 따라 최신 chart가 동작하지 않음) 해당 chart를 다운로드 받아 사용하기로 한다.(여기서는 직접 수정하는 것까지는 하지 않는다)


$ git clone https://github.com/helm/charts.git


다운로드가 완료되면 다음의 폴더로 이동한다.


$ cd charts/stable/prometheus-operator


Prometheus Operator 설치하기

Helm을 이용해 "po" 란 namespace를 만들어 설치한다. resource 제한 등으로 prometheus가 정상동작하지 않을 수 있으니 아래 custom value(cvalues.yaml) 을 이용한다.


먼저, subchart를 빌드한다.


$ helm dep build .
Hang tight while we grab the latest from your chart repositories...
...Unable to get an update from the "local" chart repository (http://127.0.0.1:8879/charts):
	Get http://127.0.0.1:8879/charts/index.yaml: dial tcp 127.0.0.1:8879: connect: connection refused
...Successfully got an update from the "stable" chart repository
Update Complete. ⎈Happy Helming!⎈
Saving 3 charts
Downloading kube-state-metrics from repo https://kubernetes-charts.storage.googleapis.com/
Downloading prometheus-node-exporter from repo https://kubernetes-charts.storage.googleapis.com/
Downloading grafana from repo https://kubernetes-charts.storage.googleapis.com/
Deleting outdated charts



cvalues.yaml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
nameOverride: "po"
fullnameOverride: "po"
alertmanager:
  alertmanagerSpec:
    resources:
      requests:
        memory: 400Mi
        cpu: 100m
      limits:
        memory: 1Gi
        cpu: 1
    securityContext: {}
grafana:
  enabled: true
  image:
  securityContext:
    fsGroup: null
    runAsUser: null
kube-state-metrics:
  securityContext:
    fsGroup: null
    runAsUser: null
prometheusOperator:
  enabled: true
  createCustomResource: true
  resources:
    limits:
      cpu: 200m
      memory: 200Mi
    requests:
      cpu: 100m
      memory: 100Mi
prometheus:
  enabled: true
  prometheusSpec:
    resources:
      requests:
        memory: 2Gi
        cpu: 1
      limits:
        memory: 4Gi
        cpu: 2
    securityContext:
      fsGroup: 2000
      runAsNonRoot: true
      runAsUser: 1000


만약 재설치 등을 하는 경우라면 기존 CRD가 남아 있기 때문에 25번째 라인의 createCustomResource 값을 false로 해주고 한다. (삭제 명령어는 helm del --purge po)



$ helm install . --name=po --namespace=po -f ./cvalues.yaml
NAME:   po
LAST DEPLOYED: Wed Jan  2 15:16:08 2019
NAMESPACE: po
STATUS: DEPLOYED

RESOURCES:
==> v1/ServiceMonitor
NAME                        AGE
po-alertmanager             1s
po-coredns                  1s
po-apiserver                1s
po-kube-controller-manager  1s
po-kube-etcd                1s
po-kube-scheduler           1s
po-kube-state-metrics       1s
po-kubelet                  1s
po-node-exporter            1s
po-operator                 1s
po-prometheus               1s

==> v1/Secret
po-grafana                    2s
alertmanager-po-alertmanager  2s

==> v1/ClusterRole
po-grafana-clusterrole  2s
po-alertmanager         2s
po-operator             2s
po-operator-psp         2s
po-prometheus           2s
po-prometheus-psp       2s

==> v1beta1/ClusterRole
po-kube-state-metrics            2s
psp-po-prometheus-node-exporter  2s

==> v1/Role
po-prometheus-config  2s
po-prometheus         1s
po-prometheus         1s

==> v1/PrometheusRule
po                  1s
po-kube-etcd-rules  1s

==> v1/RoleBinding
po-prometheus-config  2s
po-prometheus         1s
po-prometheus         1s

==> v1beta2/Deployment
po-grafana  2s

==> v1beta1/Deployment
po-kube-state-metrics  2s

==> v1beta1/PodSecurityPolicy
po-grafana                   2s
po-prometheus-node-exporter  2s
po-alertmanager              2s
po-operator                  2s
po-prometheus                2s

==> v1/ServiceAccount
po-grafana                   2s
po-kube-state-metrics        2s
po-prometheus-node-exporter  2s
po-alertmanager              2s
po-operator                  2s
po-prometheus                2s

==> v1/ClusterRoleBinding
po-grafana-clusterrolebinding  2s
po-alertmanager                2s
po-operator                    2s
po-operator-psp                2s
po-prometheus                  2s
po-prometheus-psp              2s

==> v1beta1/ClusterRoleBinding
po-kube-state-metrics            2s
psp-po-prometheus-node-exporter  2s

==> v1beta1/RoleBinding
po-grafana  2s

==> v1/Pod(related)

NAME                                    READY  STATUS             RESTARTS  AGE
po-prometheus-node-exporter-2bdjk       0/1    ContainerCreating  0         2s
po-prometheus-node-exporter-2lc2n 0/1 ContainerCreating 0 2s
po-grafana-5c449db976-l8phh 0/3 ContainerCreating 0 2s po-kube-state-metrics-5fcd8bf5c9-rlqvj 0/1 ContainerCreating 0 2s po-operator-5b8965bf77-27xbz 0/1 ContainerCreating 0 2s ==> v1/ConfigMap NAME AGE po-grafana-config-dashboards 2s po-grafana 2s po-grafana-datasource 2s po-grafana-coredns-k8s 2s po-grafana-dashboard-etcd 2s po-grafana-dashboard-k8s-cluster-rsrc-use 2s po-grafana-dashboard-k8s-node-rsrc-use 2s po-grafana-dashboard-k8s-resources-cluster 2s po-grafana-dashboard-k8s-resources-namespace 1s po-grafana-dashboard-k8s-resources-pod 1s po-grafana-dashboard-nodes 1s po-grafana-dashboard-statefulset 1s ==> v1beta1/Role po-grafana 2s ==> v1/Service po-grafana 2s po-kube-state-metrics 2s po-prometheus-node-exporter 2s po-alertmanager 2s po-coredns 2s po-kube-controller-manager 2s po-kube-etcd 2s po-kube-scheduler 2s po-operator 2s po-prometheus 2s ==> v1/Deployment po-operator 2s ==> v1/Prometheus po-prometheus 1s ==> v1beta1/DaemonSet po-prometheus-node-exporter 2s ==> v1/Alertmanager po-alertmanager 2s NOTES: The Prometheus Operator has been installed. Check its status by running: kubectl --namespace po get pods -l "release=po" Visit https://github.com/coreos/prometheus-operator for instructions on how to create & configure Alertmanager and Prometheus instances using the Operator


생성 리소스 확인

설치 성공후 pod 상태를 보면 다음과 같다.


$ kubectl -n po get po
NAME                                     READY   STATUS    RESTARTS   AGE
alertmanager-po-alertmanager-0           2/2     Running   0          3m21s
po-grafana-5c449db976-l8phh              3/3     Running   0          3m28s
po-kube-state-metrics-5fcd8bf5c9-rlqvj   1/1     Running   0          3m28s
po-operator-5b8965bf77-27xbz             1/1     Running   0          3m28s
po-prometheus-node-exporter-2bdjk        1/1     Running   0          3m28s
po-prometheus-node-exporter-2lc2n        1/1     Running   0          3m28s
prometheus-po-prometheus-0               3/3     Running   1          3m13s


각각의 pod 에 대해서 설명하면,

  • alertmanager-po-alertmanager-0: prometheus의 alertmanager pod이다. statefulset이며, AlertManager CRD로 생성된 리소스를 바탕으로 Operator에 의해 생성되었다. (여러개 생성할 수 있다는 얘기) 해당 CRD로 생성되었기에 prefix로 alertmanager가 붙어 있다.
  • po-grafana-xxx-xxx: grafana deployment이며, operator가 다루는게 아닌 단순 subchart로서 설치가 되었다. 해당 chart에 대한 설명은 stable/grafana를 참고하면 알 수 있다.
  • po-kube-state-metrics-xxx-xxx: kube-state-metric deployment이다. 이 엮시 grafana와 동일하게 단순 subchart 이다.
  • po-operator-xxx-xxx: Prometheus operator deployment이다. 여기서 관련 CRD 리소스를 watching하면서 prometheus을 operating한다.
  • po-prometheus-node-exporter-xxx: node-exporter daemonset이다. 이것도 subchart이다.
  • prometheus-po-prometheus-0: prometheus statefulset이다. 역시 alertmanager와 동일하게 Prometheus CRD로 생성된 리소스를 바탕으로 Operator에 의해 생성되었다.
전에 나왔던 그림을 다시 보자면, 그림에서 점선안에 있는 리소스들은 CRD에 해당되며, 위에 pod과 매핑되는 부분은 Operator(po-operator-xxx-xxx)와 Prometheus Server(prometheus-po-prometheus-0) 이다.


CRD를 살펴보면 4개의 CRD가 존재한다.(설명에서 monitoring.coreos.com은 생략하여 적는다)

$ kubectl -n po get crd
alertmanagers.monitoring.coreos.com     2019-01-02T06:07:11Z
prometheuses.monitoring.coreos.com      2019-01-02T06:07:11Z
prometheusrules.monitoring.coreos.com   2019-01-02T06:07:12Z
servicemonitors.monitoring.coreos.com   2019-01-02T06:07:12Z
  • alertmanagers: AlertManager statefulset을 생성하는 CRD이다. AlertManager 리소스 리스트를 보면 현재 1개가 생성되어 있는 것을 알 수 있고, 이 리소스를 바탕으로 Operator가 AlertManager(statefulset)를 생성하는 것이다.
$ kubectl -n po get AlertManager
NAME              AGE
po-alertmanager   5m
  • prometheuses: Prometheus statefulset을 생성하는 CRD이다. Prometheus 리소스로 리스트업하면 현재 1개가 생성되어 있을 것을 알 수 있고, 자세한 내용을 보면 다음과 같다.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
apiVersion: monitoring.coreos.com/v1
kind: Prometheus
metadata:
  creationTimestamp: "2019-01-02T06:29:28Z"
  generation: 1
  labels:
    app: po-prometheus
    chart: prometheus-operator-1.1.0
    heritage: Tiller
    release: po
  name: po-prometheus
  namespace: po
  resourceVersion: "7484353"
  selfLink: /apis/monitoring.coreos.com/v1/namespaces/po/prometheuses/po-prometheus
  uid: c175e470-0e57-11e9-940a-fa163e6e7c03
spec:
  alerting:
    alertmanagers:
    - name: po-alertmanager
      namespace: po
      pathPrefix: /
      port: web
  baseImage: quay.io/prometheus/prometheus
  externalUrl: http://po-prometheus.po:9090
  listenLocal: false
  logLevel: info
  paused: false
  replicas: 1
  resources:
    limits:
      cpu: 2
      memory: 4Gi
    requests:
      cpu: 1
      memory: 2Gi
  retention: 10d
  routePrefix: /
  ruleSelector:
    matchLabels:
      app: po
      release: po
  securityContext:
    fsGroup: 2000
    runAsNonRoot: true
    runAsUser: 1000
  serviceAccountName: po-prometheus
  serviceMonitorNamespaceSelector: {}
  serviceMonitorSelector:
    matchLabels:
      release: po
  version: v2.5.0


CRD의 이름이 alertmanagers 와 prometheuses로 복수형으로 되어 있음을 알 수 있다. 필요에 따라 여러개의 AlertManager와 Prometheus 설치할 수 있다. 48번째 라인을 보면 ServiceMonitor중에 어떤것을 select하여 사용할 지를 지정할 수 있다. 

  • prometheusrules: record/alert rule에 대한 CRD이다. 위의 2개는 컨테이너에 대한 설정값이라면, prometheusrules와 sevicemonitors는 각 앱에서 사용할 설정값에 해당된다.
  • servicemonitors: scrape할 대상에 대한 CRD이다. 해당 CRD 리소스가 변경될 때마다 이를 watch하고 있는 Operator가 재조합하여 prometheus의 설정을 변경하고 reload를 처리한다.
ServiceMonitor 리소스를 보면 다음과 같다.

$ kubectl -n po get ServiceMonitor
NAME                         AGE
po-alertmanager              15m
po-apiserver                 15m
po-coredns                   15m
po-kube-controller-manager   15m
po-kube-etcd                 15m
po-kube-scheduler            15m
po-kube-state-metrics        15m
po-kubelet                   15m
po-node-exporter             15m
po-operator                  15m
po-prometheus                15m


어떠한 Service를 모니터링할지 필요에 따라 추가 삭제하면 되는데, stable chart를 바로 사용하면서 servicemonitor를 변경하고 싶다면, stable chart를 subchart로 두고 새로운 chart를 생성하여 포함시키면된다. (하지만 subchart로 두는것은 customization에 많은 불편함을 초래하기 때문에 필자의 경우 해당 chart를 다운받아 직접 수정해 사용하고 있다)


Grafana Service를 수정하여 외부에서 접속하게 되면 다음과 같이 기본적으로 설정된 대시보드들을 볼 수있다.(default password는 prom-operator이다)





이렇게 해서 Prometheus-Operator를 살펴보았다. 요약해보자면 Operator는 k8s CRD를 이용해 리소스 관리를 효율적으로 할 수 있으며 Application-specific한 operating을 가능하게 해준다.


Prometheus-Operator 외에도 etcd/valut operator등이 공식적으로 있으며, 필요에 따라 SDK를 이용해 직접 만들 수 있다.