Prometheus 구성
이 페이지는 Calico Enterprise가 설치하는 샘플 Prometheus 알림 규칙을 수정하는 방법부터, 새 알림 규칙 만들기, 스크레이프 간격 조정, 설정 업데이트 문제 해결까지 다뤄요. 샘플 매니페스트가 만든 규칙을 고쳐 쓰는 과정은 사용자가 직접 만든 규칙을 업데이트하는 과정과 같아서, 하나를 익히면 나머지도 자연스럽게 익힐 수 있어요.
출처: 문서
본문
Denied packets 규칙 업데이트
샘플 매니페스트가 만들어 둔 규칙을 수정하는 방법의 예시예요. 규칙을 업데이트하는 절차는 아래에서 다룰 사용자 생성 규칙과 동일해요.
-
현재 알림 규칙을 저장해요:
kubectl -n tigera-prometheus get prometheusrule -o yaml > calico-prometheus-alert-rule-dp.yaml -
알림 규칙에 필요한 편집을 마친 뒤, 업데이트된 매니페스트를 적용해요.
kubectl apply -f calico-prometheus-alert-rule-dp.yaml
변경 사항은 prometheus-operator가 띄운 prometheus pod 안의 prometheus-config-reloader 컨테이너(보통 prometheus-<your-prometheus-instance-name> 이름)가 몇 초 안에 반영해 줘요.
예시로, 이 매니페스트의 range query는 10초예요.
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
name: calico
namespace: tigera-prometheus
labels:
role: tigera-prometheus-rules
prometheus: calico-node-prometheus
spec:
groups:
- name: calico.rules
rules:
- alert: DeniedPacketsRate
expr: rate(calico_denied_packets[10s]) > 50
labels:
severity: critical
annotations:
summary: 'Instance {{$labels.instance}} - Large rate of packets denied'
description: '{{$labels.instance}} with calico-node pod {{$labels.pod}} has been denying packets at a fast rate {{$labels.sourceIp}} by policy {{$labels.policy}}.'
이 알림 규칙을, 예를 들어 쿼리 range를 20초로 실행되도록 바꾸고 싶다면 매니페스트를 이렇게 고쳐요:
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
name: calico
namespace: tigera-prometheus
labels:
role: tigera-prometheus-rules
prometheus: calico-node-prometheus
spec:
groups:
- name: calico.rules
rules:
- alert: DeniedPacketsRate
expr: rate(calico_denied_packets[20s]) > 50
labels:
severity: critical
annotations:
summary: 'Instance {{$labels.instance}} - Large rate of packets denied'
description: '{{$labels.instance}} with calico-node pod {{$labels.pod}} has been denying packets at a fast rate {{$labels.sourceIp}} by policy {{$labels.policy}}.'
새 알림 규칙 만들기
규칙이 무엇을 찾도록 할지 정하기만 하면, 새 알림 규칙 만들기는 어렵지 않아요. 자세한 내용은 alerting rules와 Queries를 참고해요.
Calico node 모니터링용 새 알림 규칙
새 알림 규칙을 Prometheus 인스턴스에 추가하려면 tigera-prometheus 네임스페이스에 role: tigera-prometheus-rules, prometheus: calico-node-prometheus 라벨을 붙인 PrometheusRule 매니페스트를 정의해요. 이 라벨은 Prometheus 매니페스트의 ruleSelector 필드가 정의한 라벨과 일치해야 해요.
예를 들어 calico-node 인스턴스가 5분 이상 내려가 있을 때 알림을 띄우려면, 아래 내용을 calico-node-down-alert.yaml 같은 파일로 저장해요.
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
name: calico-prometheus-calico-node-down
namespace: tigera-prometheus
labels:
role: tigera-prometheus-rules
prometheus: calico-node-prometheus
spec:
groups:
- name: calico.rules
rules:
- alert: CalicoNodeInstanceDown
expr: up == 0
for: 5m
labels:
severity: warning
annotations:
summary: 'Instance {{$labels.instance}} Pod: {{$labels.pod}} is down'
description: '{{$labels.instance}} of job {{$labels.job}} has been down for more than 5 minutes'
그다음 이 매니페스트를 쿠버네티스에서 생성/적용해요.
kubectl apply -f calico-node-down-alert.yaml
변경 사항은 prometheus-operator가 띄운 prometheus pod 안의 prometheus-config-reloader 컨테이너(보통 prometheus-<your-prometheus-instance-name> 이름)가 몇 초 안에 반영해 줘요.
BGP 피어 모니터링용 새 알림 규칙
이번에는 BGP 피어링 상태를 모니터링하는 관점에서, 새 알림 규칙의 예시를 볼게요. tigera-prometheus 네임스페이스에 role: tigera-prometheus-rules, prometheus: calico-node-prometheus 라벨을 붙인 PrometheusRule 매니페스트를 정의해요. 이 라벨은 Prometheus 매니페스트의 ruleSelector 필드가 정의한 라벨과 일치해야 해요.
예를 들어, "Established"가 아닌 상태의 피어링 연결 수가 클러스터에서 최근 5분 동안 0이 아닌 속도로 늘어날 때 알림을 띄우려면, 아래 내용을 calico-peer-status-not-established.yaml 같은 파일로 저장해요.
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
labels:
prometheus: calico-node-prometheus
role: tigera-prometheus-rules
name: calico-prometheus-peer-status-not-established
namespace: tigera-prometheus
spec:
groups:
- name: calico.rules
rules:
- alert: CalicoNodePeerStatusNotEstablished
annotations:
description: '{{$labels.instance}} has at least one peer connection that is
no longer up.'
summary: Instance {{$labels.instance}} has peer connection that is no longer
up
expr: rate(bgp_peers{status!~"Established"}[5m]) > 0
labels:
severity: critical
그다음 이 매니페스트를 쿠버네티스에서 생성/적용해요.
kubectl apply -f calico-peer-status-not-established.yaml
변경 사항은 prometheus-operator가 띄운 prometheus pod 안의 prometheus-config-reloader 컨테이너(보통 prometheus-<your-prometheus-instance-name> 이름)가 몇 초 안에 반영해 줘요.
추가 알림 규칙
Calico Enterprise 설치 매니페스트가 설치하는 Alerting Rules는 간단한 규칙으로, 특정 Source IP에서 노드의 정책이 패킷을 거부하는 속도가 초당 패킷 수 기준 일정 임계값을 넘으면 알림을 띄워요. 여기에 쓰이는 Prometheus 쿼리(임계값 20은 무시하고 보면)는 다음과 같아요:
rate(calico_denied_packets[10s])
이 쿼리는 대략 다음과 같은 결과를 돌려줘요:
{endpoint="calico-metrics-port",instance="10.240.0.81:9081",job="calico-node-metrics",namespace="kube-system",pod="calico-node-hn0kl",policy="profile/k8s_ns.test/0/deny",service="calico-node-metrics",srcIP="192.168.167.129"} 0.6
{endpoint="calico-metrics-port",instance="10.240.0.84:9081",job="calico-node-metrics",namespace="kube-system",pod="calico-node-97m3g",policy="profile/k8s_ns.test/0/deny",service="calico-node-metrics",srcIP="192.168.167.175"} 0.2
{endpoint="calico-metrics-port",instance="10.240.0.84:9081",job="calico-node-metrics",namespace="kube-system",pod="calico-node-97m3g",policy="profile/k8s_ns.test/0/deny",service="calico-node-metrics",srcIP="192.168.252.157"} 0.4
{endpoint="calico-metrics-port",instance="10.240.0.81:9081",job="calico-node-metrics",namespace="kube-system",pod="calico-node-hn0kl",policy="profile/k8s_ns.test/0/deny",service="calico-node-metrics",srcIP="192.168.167.175"} 1
{endpoint="calico-metrics-port",instance="10.240.0.84:9081",job="calico-node-metrics",namespace="kube-system",pod="calico-node-97m3g",policy="profile/k8s_ns.test/0/deny",service="calico-node-metrics",srcIP="192.168.167.129"} 0.4
{endpoint="calico-metrics-port",instance="10.240.0.81:9081",job="calico-node-metrics",namespace="kube-system",pod="calico-node-hn0kl",policy="profile/k8s_ns.test/0/deny",service="calico-node-metrics",srcIP="192.168.167.159"} 0.4
{endpoint="calico-metrics-port",instance="10.240.0.81:9081",job="calico-node-metrics",namespace="kube-system",pod="calico-node-hn0kl",policy="profile/k8s_ns.test/0/deny",service="calico-node-metrics",srcIP="192.168.252.175"} 0.4
{endpoint="calico-metrics-port",instance="10.240.0.84:9081",job="calico-node-metrics",namespace="kube-system",pod="calico-node-97m3g",policy="profile/k8s_ns.test/0/deny",service="calico-node-metrics",srcIP="192.168.252.175"} 0.6
{endpoint="calico-metrics-port",instance="10.240.0.81:9081",job="calico-node-metrics",namespace="kube-system",pod="calico-node-hn0kl",policy="profile/k8s_ns.test/0/deny",service="calico-node-metrics",srcIP="192.168.252.157"} 0.6
{endpoint="calico-metrics-port",instance="10.240.0.84:9081",job="calico-node-metrics",namespace="kube-system",pod="calico-node-97m3g",policy="profile/k8s_ns.test/0/deny",service="calico-node-metrics",srcIP="192.168.167.159"} 0.6
이 쿼리를 고쳐서, 모든 노드에서 각기 다른 정책이 드랍한 패킷을 모두 찾아볼 수도 있어요.
(sum by (instance,policy) (rate(calico_denied_packets[10s])))
이 쿼리는 여러 Source IP의 결과를 집계하면서 policy와 instance 라벨은 그대로 남겨둬요. instance 라벨은 calico 노드의 IP 주소와 PrometheusReporterPort를 나타낸다는 점, 이 부분만 기억해두면 돼요. 이 쿼리는 다음과 같은 결과를 돌려줘요:
{instance="10.240.0.84:9081",policy="profile/k8s_ns.test/0/deny"} 2
{instance="10.240.0.81:9081",policy="profile/k8s_ns.test/0/deny"} 2.8
이 결과에 pod 이름까지 포함하려면, by 표현식에 나열된 라벨에 pod 라벨을 추가해요:
(sum by (instance,pod,policy) (rate(calico_denied_packets[10s])))
그러면 다음과 같은 결과가 나와요:
{instance="10.240.0.84:9081",pod="calico-node-97m3g",policy="profile/k8s_ns.test/0/deny"} 2
{instance="10.240.0.81:9081",pod="calico-node-hn0kl",policy="profile/k8s_ns.test/0/deny"} 2.8
재미있는 활용 사례는, 악의적인 Pod가 nmap 같은 도구로 서브넷의 열린 포트를 훑는 상황이에요. 이걸 잡아내려면 모든 인스턴스의 모든 정책에 걸쳐 집계하면서 source IP 주소는 보존하는 쿼리를 실행해야 해요. 이런 쿼리로 가능해요:
(sum by (srcIP) (rate(calico_denied_packets[10s])))
결과는 source IP 주소별로 이런 식으로 나와요:
{srcIP="192.168.167.159"} 1.0000000000000002
{srcIP="192.168.167.129"} 1.2000000000000002
{srcIP="192.168.252.175"} 1.4000000000000001
{srcIP="192.168.167.175"} 0.4
{srcIP="192.168.252.157"} 1.0000000000000002
이 쿼리들을 Alerting Rules로 쓰려면, 새 알림 규칙 만들기 섹션의 안내를 따라 적절한 쿼리를 담은 ConfigMap을 만들면 돼요.
스크레이프 간격 업데이트
스크레이프 간격(Prometheus가 각 노드를 폴링해 새 denied packet 정보를 가져오는 주기)을 바꾸고 싶을 수도 있어요. 간격을 늘리면 Prometheus 부하와 필요한 스토리지는 줄어들지만, 수집되는 메트릭의 세밀함은 떨어져요.
엔드포인트(여기서는 calico-node)의 스크레이프 간격은 ServiceMonitor 매니페스트의 일부로 정의돼요. 간격을 바꾸려면:
-
현재 ServiceMonitor 매니페스트를 저장해요:
kubectl -n tigera-prometheus get servicemonitor calico-node-monitor -o yaml > calico-node-monitor.yaml -
endpoints아래의interval필드를 원하는 값으로 고치고, 업데이트된 매니페스트를 적용해요.kubectl apply -f calico-node-monitor.yaml
변경 사항은 prometheus-operator가 띄운 prometheus pod 안의 prometheus-config-reloader 컨테이너(보통 prometheus-<your-prometheus-instance-name> 이름)가 몇 초 안에 반영해 줘요.
무엇을 고치는지 예시로 보면, 이 ServiceMonitor 매니페스트의 interval은 5초(5s)예요.
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: calico-node-monitor
namespace: tigera-prometheus
labels:
team: network-operators
spec:
selector:
matchLabels:
k8s-app: calico-node
namespaceSelector:
matchNames:
- kube-system
endpoints:
- port: calico-metrics-port
interval: 5s
Calico Enterprise Prometheus의 스크레이프 간격을 10초로 바꾸려면 매니페스트를 이렇게 고쳐요:
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: calico-node-monitor
namespace: tigera-prometheus
labels:
team: network-operators
spec:
selector:
matchLabels:
k8s-app: calico-node
namespaceSelector:
matchNames:
- kube-system
endpoints:
- port: calico-metrics-port
interval: 10s
설정 업데이트 문제 해결
config reloader 로그에서 최근 변경을 감지했는지 확인해 보세요.
-
prometheus라면 이렇게 실행해요:
kubectl -n tigera-prometheus logs prometheus-<your-prometheus-name> prometheus-config-reloader -
alertmanager라면 이렇게 실행해요:
kubectl -n tigera-prometheus logs alertmanager-<your-prometheus-name> config-reloader
config-reloader는 각 pod의 파일 시스템을 지켜보다가 ConfigMap이나 Secret에서 업데이트된 설정을 발견하면, 설정을 다시 읽는 데 필요한 단계를 수행해요.