Monitor ubuntu servers with Prometheus Operator

9/20/2018

I need to monitor several ubuntu servers using Prometheus Operator. These servers are outside the kubernetes cluster where my prometheus operator is running. The manifests below work to monitor one of those servers with IP aaa.bbb.ccc.ddd. How can I add multiple IPs as endpoints to monitor? Or do I need to create one endpoint/service for each of those servers?

apiVersion: v1
kind: Service
metadata:
  name: external-server
  namespace: monitoring
  labels:
    k8s-app: external-server
spec:
  type: ExternalName
  externalName: aaa.bbb.ccc.ddd
  clusterIP: ""
  ports:
  - name: metrics
    port: 9100
    protocol: TCP
    targetPort: 9100

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: external-server
  labels:
    k8s-app: external-server
spec:
  endpoints:
  - port: metrics
    interval: 30s
    honorLabels: true
    path: /metrics
  selector:
    matchLabels:
      k8s-app: external-server
  namespaceSelector:
    matchNames:
    - monitoring

apiVersion: v1
kind: Endpoints
metadata:
  name: external-server
  labels:
    k8s-app: external-server
subsets:
- addresses:
  - ip: aaa.bbb.ccc.ddd
  ports:
  - name: metrics
    port: 9100
    protocol: TCP
-- Preeti V
kubernetes
monitoring
prometheus
prometheus-operator

2 Answers

9/21/2018

Just use

apiVersion: v1
kind: Endpoints
metadata:
  name: external-server
subsets:
- addresses:
  - ip: aaa.bbb.ccc.ddd
  - ip: aaa.bbb.ccc.eee
  - ip: aaa.bbb.ccc.fff
  ports:
  - name: metrics
    port: 9100
    protocol: TCP

and a new target with several endpoints will be added to Prometheus /targets. enter image description here

-- Arslanbekov
Source: StackOverflow

9/21/2018

This is the complete solution that worked for me. Make note of the fact that I had to change the Service type to ClusterIP.

apiVersion: v1
kind: Service
metadata:
  name: external-server
  namespace: monitoring
  labels:
    k8s-app: external-server
spec:
  type: ClusterIP
  clusterIP: None
  ports:
  - name: metrics
    port: 9100
    protocol: TCP

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: external-server
  labels:
    k8s-app: external-server
spec:
  endpoints:
  - port: metrics
    interval: 30s
    scheme: http
  selector:
    matchLabels:
      k8s-app: external-server
  namespaceSelector:
    matchNames:
    - monitoring

apiVersion: v1
kind: Endpoints
metadata:
  name: external-server
  labels:
    k8s-app: external-server
subsets:
- addresses:
  - ip: aaa.bbb.ccc.ddd
  - ip: aaa.bbb.ccc.eee
  ports:
  - name: metrics
    port: 9100
    protocol: TCP

Once deployed, the two IPs will show up as endpoints under Targets under external-server.

-- Preeti V
Source: StackOverflow