zoukankan      html  css  js  c++  java
  • 在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
     
  • 相关阅读:
    记一次不好不坏的数据库优化
    洛谷 P3065 [USACO12DEC]First! G(字典树,环的判断)
    洛谷 P3879 [TJOI2010]阅读理解(trie树)
    洛谷 [USACO08DEC]Secret Message G(01字典树)
    洛谷 P5149 会议座位(归并排序,trie树)
    2019强网杯babybank writeup及浅析
    python多线程的学习
    关于信息安全的学习
    FineCMS v5.4.1 后台getshell
    二次注入的复现
  • 原文地址:https://www.cnblogs.com/restful/p/12160363.html
Copyright © 2011-2022 走看看