<p>I'm deploying Prometheus-operator to my cluster by Helm chart but I implement a custom service to monitor my application, I need to add my service to Prometheus-operator to see my metrics data.</p>
<p>How I can do that?</p>

Waseem Awashra
<p>At first, you need to deploy Prometheus-operator by Helm or manually:</p>
<pre class="lang-sh prettyprint-override"><code># By Helm:
$ helm install stable/prometheus-operator --generate-name
 
# By manual: for release `release-0.41`
kubectl apply -f  https://raw.githubusercontent.com/prometheus-operator/prometheus-operator/release-0.41/bundle.yaml
</code></pre>
<p>If your cluster is RBAC enabled then you need to install RBAC stuff for <code>Prometheus</code> object:</p>
<pre class="lang-sh prettyprint-override"><code>apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
metadata:
  name: prometheus
rules:
- apiGroups: [&quot;&quot;]
  resources:
  - nodes
  - nodes/metrics
  - services
  - endpoints
  - pods
  verbs: [&quot;get&quot;, &quot;list&quot;, &quot;watch&quot;]
- apiGroups: [&quot;&quot;]
  resources:
  - configmaps
  verbs: [&quot;get&quot;]
- nonResourceURLs: [&quot;/metrics&quot;]
  verbs: [&quot;get&quot;]
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: prometheus
  namespace: default
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
  name: prometheus
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: prometheus
subjects:
- kind: ServiceAccount
  name: prometheus
  namespace: default
</code></pre>
<p>Then you need to deploy <code>Promethues</code> object:</p>
<pre class="lang-sh prettyprint-override"><code>apiVersion: monitoring.coreos.com/v1
kind: Prometheus
metadata:
  name: prometheus
  labels:
    prometheus: prometheus
spec:
  replicas: 1
  serviceAccountName: prometheus
  serviceMonitorSelector:
    matchLabels:
      k8s-app: prometheus
  serviceMonitorNamespaceSelector:
    matchLabels:
      prometheus: prometheus
  resources:
    requests:
      memory: 400Mi
</code></pre>
<p>Here, <code>Prometheus</code> object will select all <code>ServiceMonitor</code> that meet up the below conditions:</p>
<ul>
<li><code>ServiceMonitor</code> will have the <code>k8s-app: prometheus</code> label.</li>
<li><code>ServiceMonitor</code> will be created in that namespaces which have <code>prometheus: prometheus</code> label.</li>
</ul>
<p>The ServiceMonitor has a label selector to select Services and their underlying Endpoint objects. The Service object for the example application selects the Pods by the <code>app</code> label having the <code>example-app</code> value. The Service object also specifies the port on which the metrics are exposed.</p>
<pre class="lang-sh prettyprint-override"><code>kind: Service
apiVersion: v1
metadata:
  name: example-app
  labels:
    app: example-app
spec:
  selector:
    app: example-app
  ports:
  - name: web
    port: 8080
</code></pre>
<p>This Service object is discovered by a ServiceMonitor, which selects in the same way. The <code>app</code> label must have the value <code>example-app</code>.</p>
<pre class="lang-sh prettyprint-override"><code>apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: example-app
  labels:
    k8s-app: prometheus
spec:
  selector:
    matchLabels:
      app: example-app
  namespaceSelector:
    # matchNames:
    # - demo
    any: true
  endpoints:
  - port: web
</code></pre>
<p>Here, <code>namespaceSelector</code> is used to select all-namespaces where the service is created. you can specify specific any namespace using <code>matchNames</code>.</p>
<p>You can also create a <code>ServiceMonitor</code> in any namespace as you want. But you need to specify it in <code>Prometheus</code> cr's <code>spec</code>, like:</p>
<pre class="lang-sh prettyprint-override"><code>  serviceMonitorNamespaceSelector:
    matchLabels:
      prometheus: prometheus
</code></pre>
<p>The above <code>serviceMonitorNamespaceSelector</code> is used in <code>Prometheus</code> operator to select that namespace which has the label <code>prometheus: prometheus</code>. Suppose you have a namespace <code>demo</code> and in this <code>demo</code> namespace you have created a <code>Prometheus</code> then you need to add label <code>prometheus: prometheus</code> in <code>demo</code> namespace using patch:</p>
<pre class="lang-sh prettyprint-override"><code>$ kubectl patch namespace demo -p '{&quot;metadata&quot;:{&quot;labels&quot;: {&quot;prometheus&quot;:&quot;prometheus&quot;}}}'
</code></pre>
<p>You can find more details here:</p>
<ul>
<li><p>Helm: <a href="https://github.com/helm/charts/tree/master/stable/prometheus-operator" rel="nofollow noreferrer">https://github.com/helm/charts/tree/master/stable/prometheus-operator</a></p>
</li>
<li><p>Manual: <a href="https://github.com/prometheus-operator/prometheus-operator/blob/release-0.41/Documentation/user-guides/getting-started.md" rel="nofollow noreferrer">https://github.com/prometheus-operator/prometheus-operator/blob/release-0.41/Documentation/user-guides/getting-started.md</a></p>
</li>
<li><p>namespaceSelector: <a href="https://github.com/prometheus-operator/prometheus-operator/blob/master/Documentation/design.md" rel="nofollow noreferrer">https://github.com/prometheus-operator/prometheus-operator/blob/master/Documentation/design.md</a></p>
</li>
</ul>
