Answers: <p>No changes need to be made to your service deployment. As for Prometheus, if you are deploying a <a href="https://github.com/prometheus-operator/prometheus-operator" rel="nofollow noreferrer">Prometheus Operator</a>, A simple <a href="https://github.com/prometheus-operator/prometheus-operator/blob/master/Documentation/user-guides/running-exporters.md" rel="nofollow noreferrer">ServiceMonitor</a> will work for you. ServiceMonitors select the services to monitor based on selectors and then identify Endpoints to monitor under the Service.
  Use the following to <a href="https://sysdig.com/blog/kubernetes-monitoring-prometheus-operator-part3/" rel="nofollow noreferrer">setup</a> a ServiceMonitor</p>
  
Answers: CC BY-SA 4.0
Answers: 2021-08-15T10:00:52.200Z
Answers: 2021-08-15T10:00:52.200Z
Answers: rohatgisanat
Body: <p>I have created a deployment with 2 replica sets. But the Service exposes the 8080 and the management endpoint using NodePort. How should I set up the pods deployment if Prometheus deployment should get 2 targets instead of one (the k8s service port mapping)</p>
  <p>I have a deployment.yaml creating 2 replicas:</p>
  <pre><code>apiVersion: apps/v1
  kind: Deployment
  metadata:
    labels:
      app: rps-app # Deployment manages the pods with these labels
    name: rps-app-deployment
  spec:
    replicas: 2
    selector:
      matchLabels: # This replicaSet manages the pods with these labels
        app: rps-app
    minReadySeconds: 10
    strategy:
      rollingUpdate:
        maxSurge: 1
        maxUnavailable: 0
      type: RollingUpdate
    template:
      metadata:
        labels:
          app: rps-app
        name: rps-app-deployment
      spec:
        hostname: rps-app
        restartPolicy: Always
        containers:
          - image: edekargaurav88/rps2:1.4
            name: rps-app
            imagePullPolicy: Always
            resources:
              limits:
                cpu: &quot;1&quot;
                memory: 512M
              requests:
                cpu: &quot;0.5&quot;
                memory: 256M
            ports:
              - containerPort: 8080
                #hostPort: 8080
                name: http
              - containerPort: 8091
                #hostPort: 8091
                name: metrics
            env:
              - name: POSTGRES_USER
                value: postgres
              - name: POSTGRES_PASSWORD
                value: postgres
              - name: POSTGRES_URL
                value: jdbc:postgresql://192.168.10.220:5432/postgres
              - name: SPRING_PROFILES_ACTIVE
                value: prod,liquibase
            livenessProbe:
              httpGet:
                path: /actuator/health
                port: 8091
                scheme: HTTP
              initialDelaySeconds: 60
              periodSeconds: 10
              timeoutSeconds: 5
              failureThreshold: 6
            readinessProbe:
              httpGet:
                path: /actuator/health
                port: 8091
                scheme: HTTP
              initialDelaySeconds: 30
              periodSeconds: 10
              timeoutSeconds: 5
  </code></pre>
  <p>My Service yaml:</p>
  <pre><code>apiVersion: v1
  kind: Service
  metadata:
    name: rps-app
  spec:
    allocateLoadBalancerNodePorts: true # default
    selector:
      app: rps-app
    type: NodePort
    ports:
      - protocol: TCP
        port: 8080
        targetPort: 8080
        name: app
      - protocol: TCP
        port: 8091
        targetPort: 8091
        name: health
  </code></pre>
  
Tags: <kubernetes><prometheus>
Title: How prometheus scrapes individual pods actuator endpoints when they are exposed via a single service in kubernetes?
UserDisplayName: GauravEdekar
ThreadText: # How prometheus scrapes individual pods actuator endpoints when they are exposed via a single service in kubernetes?
  
  (Asked by: username_0 on 2021-08-14)
  
  I have created a deployment with 2 replica sets. But the Service exposes the 8080 and the management endpoint using NodePort. How should I set up the pods deployment if Prometheus deployment should get 2 targets instead of one (the k8s service port mapping)
  
  I have a deployment.yaml creating 2 replicas:
  
  ```
  apiVersion: apps/v1
  kind: Deployment
  metadata:
    labels:
      app: rps-app # Deployment manages the pods with these labels
    name: rps-app-deployment
  spec:
    replicas: 2
    selector:
      matchLabels: # This replicaSet manages the pods with these labels
        app: rps-app
    minReadySeconds: 10
    strategy:
      rollingUpdate:
        maxSurge: 1
        maxUnavailable: 0
      type: RollingUpdate
    template:
      metadata:
        labels:
          app: rps-app
        name: rps-app-deployment
      spec:
        hostname: rps-app
        restartPolicy: Always
        containers:
          - image: edekargaurav88/rps2:1.4
            name: rps-app
            imagePullPolicy: Always
            resources:
              limits:
                cpu: "1"
                memory: 512M
              requests:
                cpu: "0.5"
                memory: 256M
            ports:
              - containerPort: 8080
                #hostPort: 8080
                name: http
              - containerPort: 8091
                #hostPort: 8091
                name: metrics
            env:
              - name: POSTGRES_USER
                value: postgres
              - name: POSTGRES_PASSWORD
                value: postgres
              - name: POSTGRES_URL
                value: jdbc:postgresql://192.168.10.220:5432/postgres
              - name: SPRING_PROFILES_ACTIVE
                value: prod,liquibase
            livenessProbe:
              httpGet:
                path: /actuator/health
                port: 8091
                scheme: HTTP
              initialDelaySeconds: 60
              periodSeconds: 10
              timeoutSeconds: 5
              failureThreshold: 6
            readinessProbe:
              httpGet:
                path: /actuator/health
                port: 8091
                scheme: HTTP
              initialDelaySeconds: 30
              periodSeconds: 10
              timeoutSeconds: 5
  
  ```
  
  My Service yaml:
  
  ```
  apiVersion: v1
  kind: Service
  metadata:
    name: rps-app
  spec:
    allocateLoadBalancerNodePorts: true # default
    selector:
      app: rps-app
    type: NodePort
    ports:
      - protocol: TCP
        port: 8080
        targetPort: 8080
        name: app
      - protocol: TCP
        port: 8091
        targetPort: 8091
        name: health
  
  ```
  
  **Tags:** kubernetes, prometheus
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-08-15. Score: 3
  
  No changes need to be made to your service deployment. As for Prometheus, if you are deploying a [Prometheus Operator](https://github.com/prometheus-operator/prometheus-operator), A simple [ServiceMonitor](https://github.com/prometheus-operator/prometheus-operator/blob/master/Documentation/user-guides/running-exporters.md) will work for you. ServiceMonitors select the services to monitor based on selectors and then identify Endpoints to monitor under the Service.
  Use the following to [setup](https://sysdig.com/blog/kubernetes-monitoring-prometheus-operator-part3/) a ServiceMonitor
  
  
  ---
  
  