title: Kubernetes: Prometheus Count number of pod instances
body: <p>In <a href="https://github.com/kubernetes-client/java" rel="nofollow noreferrer">Kubernetes-client Java api</a>, I can get a count of available and total deployed pod instances for a given app using this way:</p>
  
  <pre><code>ApiClient defaultClient = Configuration.getDefaultApiClient();
  AppsV1beta1Api apiInstance = new AppsV1beta1Api();
  ...
  try {
      AppsV1beta1DeploymentList result = apiInstance.listDeploymentForAllNamespaces(_continue, fieldSelector, includeUninitialized, labelSelector, limit, pretty, resourceVersion, timeoutSeconds, watch);
      foreach(ExtensionsV1beta1Deployment extensionsDeployment : result.getItems() ) {
          Map&lt;String, String&gt; labels = extensionsDeployment.getMetadata().getLabels();
          String appName = labels.getOrDefault("app", "");
          ExtensionsV1beta1DeploymentStatus status = extensionsDeployment.getStatus();
          int availablePods = status.getAvailableReplicas();
          int deployedPods = status.getReplicas();
          if ( availablePods != deployedPods) {
               // Generate an alert
          }
      }
  } catch (ApiException e) {
      System.err.println("Exception when calling AppsV1beta1Api#listDeploymentForAllNamespaces");
      e.printStackTrace();
  }
  </code></pre>
  
  <p>In above example, I'm comparing the <code>availablePods</code> with the <code>deployedPods</code> and if they don't match, I generate an alert. </p>
  
  <p><strong>How can I replicate this logic using Prometheus using Alerting Rules and/or Alertmanager config, where it checks the number of available pod instances for a given app or job, and if it doesn't match a specified number of instances, it will trigger an alert?</strong></p>
  
  <p>The specified threshold can be total <code>deployedPods</code> or it can come from another config file or template.</p>