在Openshift用Prometheus和Grafana搭建监控应用的实践

参考地址: https://labs.consol.de/development/2018/01/19/openshift_application_monitoring.html

部署应用

 只要代码如下

@Component
@Path("/")
public class Metrics {

    private final Logger logger = LogManager.getLogger(Metrics.class);

    private final Counter promRequestsTotal = Counter.build()
                    .name("requests_total")
                    .help("Total number of requests.")
                    .register();

  {
    DefaultExports.initialize();
  }

  @GET()
  @Path("/hello-world")
  @Produces(MediaType.TEXT_PLAIN)
  public String sayHello() {
    promRequestsTotal.inc();
    return "hello, world";
  }

  @GET()
  @Path("/metrics")
  @Produces(MediaType.TEXT_PLAIN)
  public StreamingOutput metrics() {
    logger.info("Starting service for metrics");
    return output -> {
      try (Writer writer = new OutputStreamWriter(output)) {
        TextFormat.write004(writer, CollectorRegistry.defaultRegistry.metricFamilySamples());
      }
    };
  }
}

部署

oc new-project demoapplication

oc new-app -f https://raw.githubusercontent.com/ConSol/springboot-monitoring-example/master/templates/restservice_template.yaml -n demoapplication


部署Prometheus

oc new-project prometheus


oc new-app -f https://raw.githubusercontent.com/ConSol/springboot-monitoring-example/master/templates/prometheus3.7_with_clusterrole.yaml -p NAMESPACE=prometheus

 

或者

oc new-app -f https://raw.githubusercontent.com/ConSol/springboot-monitoring-example/master/templates/prometheus3.7_without_clusterrole.yaml -p NAMESPACE=prometheus

 

部署Grafana

oc new-project grafana


oc new-app -f https://raw.githubusercontent.com/ConSol/springboot-monitoring-example/master/templates/grafana.yaml -p NAMESPACE=grafana

 

oc policy add-role-to-user view system:serviceaccount:grafana:grafana-ocp -n prometheus



oc get route prometheus -n prometheus

oc sa get-token grafana-ocp

更新Prometheus的配置

scrape_configs:
- job_name: 'kubernetes-pods'

  kubernetes_sd_configs:
  - role: pod
    namespaces:
      names:
      - prometheus
      - demoapplication



  kubernetes_sd_configs:
  - role: endpoints
    namespaces:
      names:
      - prometheus
      - demoapplication


- source_labels: [__meta_kubernetes_service_annotation_prometheus_io_scrape]
    action: drop
    regex: false


oc exec prometheus-0 -c prometheus -- curl -X POST http://localhost:9090/-/reload








部署新的应用


---
apiVersion: v1
kind: Service
metadata:
name: ashe-service
annotations:
prometheus.io/scrape: "true"
prometheus.io/scheme: http
prometheus.io/port: "8080"
prometheus.io/path: /metrics
spec:
type: LoadBalancer
ports:
- port: 8080
protocol: TCP
targetPort: 8080
selector:
app: ashe
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: ashe-app
spec:
replicas: 1
selector:
matchLabels:
app: ashe
template:
metadata:
labels:
app: ashe
spec:
containers:
- name: ashe
image: docker.artron.net/zed/ashe:v1
ports:
- containerPort: 8080

oc apply -f app.yaml -n demoapplication
 
原文地址:https://www.cnblogs.com/restful/p/12160363.html