주요 단계
- 프로 메테우스는 봄 부트 프로젝트에서 우리가 걱정하는 비즈니스 메트릭을 생성, 자바 클라이언트 패키지에 의해 제공
- 스프링 부트 프로젝트 표시된 고정 표시기 미러
- 클러스터에 배치 된 고정 표시기 고정 표시기 떼 선박
- 프로 메테우스는 해당 수정
file_sd_configs
부가 서비스의 배포에 파일을 - 비즈니스 지표 Grafana에 의해 관찰
선박은 비즈니스 메트릭을 모니터링 본 논문 거래, 빌드 컨테이너 모니터링을 참조, 환경 프로 메테우스는 도커 떼를 모니터링
프로 메테우스의 감사
데이터 모델
메테우스 시계열 값의 데이터이다. 동일한 인덱스 이름, 그리고 관찰 량과 동일한 라벨이, 메테우스이 값에 대응하는 각 시점에 대한 시계열을, 서로 다른 시점에서의 각종 데이터의 관측에 저장된 호출되어있어서, 같은 시료에 언급 밀리 초 정밀도 값과 시간 값을 float64.
각 인덱스 이름 및 서열 고유 라벨 연관된 결정 등 (등의 원하는 특성 관측 시스템 나타내는 주요 지표의 이름
http_requests_total
요청의 총 수에 의해 시스템 시간 시리즈), 키 / 값 쌍의 각각에있는 동일한 인덱스 (라벨로 지칭 )의 치수 (라고도http_requests_total{code=200}
) 성공적인 요청의 시계열을 의미한다.PromQL하여 해당 시계열 조회 외부 애플리케이션 쿼리 메테우스 제공된 다양한 기능의 함수를 이용하여 산출 된 시계열의 값을 가질 수있다.
유형 표시
클라이언트 항아리 프로 메테우스 패키지로 지표의 다음과 같은 네 가지 유형을 제공- 카운터
- 계량기
- 히스토그램
- 요약
프로 메테우스 서버 끝이 구별되지에서이 분류는, 클라이언트 사용자에 대해서만 유효합니다. 프로 메테우스의 서버 측의 경우, 클라이언트 반환 샘플 시간 순서입니다, 예를 들어
http_requests_total{code=200} 290
, 시간 표시기에서이 점을 끌어 프로 메테우스 서버를 나타내는, 성공에 대한 요청의 총 수는 우리가하지 않더라도, 일반 텍스트 데이터는, 290 배 메테우스 데이터 만족이 형식을 반환함으로써 클라이언트 제공 메테우스 서버는 일반적으로 기억하고, 외부 조회 PromQL에 의해 공급된다.카운터
서비스가 시작되면 축적 작업은 인덱스 값을 할 수있을 때 카운터 인덱스 단 하나의 값에 해당하는 값, 또한 재설정을 할 수 있습니다, 빼기 작업을 수행 할 수 없습니다, 요청 수, 작업 실행 시간, 키를 계산하는 데 사용할 수 있습니다 비즈니스 오브젝트 및 기타 작업의 수.
계량기
카운터 차이로, 단지 하나의 값에 대응하는 게이지 인덱스 값 덕 감산 값은 일반적으로 온도를 나타내는 데 사용 게이지 표시를 완료 나타내는 동작 될 수 있으며, 다른 지표 작업을 수행
히스토그램
샘플 값의 히스토그램은 히스토그램 더이상 덧셈과 뺄셈 연산을 수행 색인 간단한없는 세 개의 샘플 값 각각에 대해 다음의 동작을 수행- 히스토그램은 지정된 시간 간격을 정의 버킷 샘플에 할당 된 각각의 버킷은 버킷 각각의 상기 다수의 저장이 범위 내에
- 각 포인트 샘플링 (합계)에 대한 누적 값
- 샘플링 포인트의 수 및 총 수 (카운트)
예를 들어, 우리는 메테우스 통해 클라이언트에 의해 제공된
Histogram.build().name("job_execute_time").help("job执行时间时间分布(分)").buckets(1,5,10) .register();
, 히스토그램의 정의 잡 실행 시간에 대한 통계 분포. 네 부분을 나타내는 대응 버킷 (5, 10)이며- <= 일분
- <= 5분
- <= 10분
- <무한
히스토그램 값들은 여섯 차원의 인덱스를 생성
job_execute_time_bucket{le="1.0",} job_execute_time_bucket{le="5.0",} job_execute_time_bucket{le="10.0",} job_execute_time_bucket{le="+Inf",} job_execute_time_count job_execute_time_sum
각 차원의 가치, 우리는 대응으로, 5.6 분의 작업 실행 시간이있을 때
job_execute_time_bucket{le="1.0",} 0.0 job_execute_time_bucket{le="5.0",} 0.0 job_execute_time_bucket{le="10.0",} 1.0 job_execute_time_bucket{le="+Inf",} 1.0 job_execute_time_count 1.0 job_execute_time_sum 5.6
무한하고 있어야합니다
job_execute_time_count
일관된지표 히스토그램 유형은 각각의 샘플의 특정 값을 유지하지 않는 볼, 기록 된 샘플의 수, 각 버킷 만 카운터 될 수있다.
요약 통계 샘플링이 다른 통에 샘플 histgram 유사 비트 맵,하지만 분위수를 이용하여 점선, 특정 차이 볼 막대 그래프는 AND 요약 , 개인 나쁜 수학이 너무 고통스러운 이해.
봄 부트 프로젝트의 건설 (2.X)
메테우스은 종래의 pom.xml 따라 첨가
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-core</artifactId> </dependency> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> </dependency> <dependency> <groupId>io.prometheus</groupId> <artifactId>simpleclient</artifactId> <version>0.8.1</version> </dependency>
application.yml 프로 메테우스는 파일 통계 관련 추가 가능
spring: application: name: sbprometheus server: port: 8080 management: metrics: export: prometheus: enabled: true endpoint: metrics: enabled: true prometheus: enabled: true endpoints: web: exposure: include: ["prometheus","health"]
- 엔드 포인트 / 액츄에이터 / 메테우스 대응 기본 메테우스
지표 정의 된 비즈니스 요구
/** * */ package chengf.falcon.sb.prometheus; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import io.micrometer.prometheus.PrometheusMeterRegistry; import io.prometheus.client.Counter; import io.prometheus.client.Gauge; import io.prometheus.client.Histogram; import io.prometheus.client.Summary; /** * @author: 作者: chengaofeng * @date: 创建时间:2020-03-20 12:04:20 * @Description: TODO * @version V1.0 */ @Configuration public class MetricConfig { @Autowired PrometheusMeterRegistry registry; @Value("${spring.application.name}") String appName; @Bean public Counter operatorCount() { return Counter.build().name(appName + "_class_operator_count").help("操作总次数").labelNames("className") .register(registry.getPrometheusRegistry()); } @Bean public Gauge runningJob() { return Gauge.build().name(appName + "_running_job_count").help("正在运行的job数") .register(registry.getPrometheusRegistry()); } @Bean public Histogram executeTime() { return Histogram.build().name(appName + "_job_execute_time").help("job执行时间时间分布(分)").buckets(1,5,10) .register(registry.getPrometheusRegistry()); } @Bean public Summary timeQuantile() { return Summary.build().name(appName + "_job_execute_time_quantile").help("job执行时间时间分布(分)").quantile(0.5, 0.05).quantile(0.9, 0.01) .register(registry.getPrometheusRegistry()); } }
업데이트 비즈니스 코드 인덱스 (resturl에 의해 실제 시뮬레이션 작업)
/** * */ package chengf.falcon.sb.prometheus; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import io.prometheus.client.Counter; import io.prometheus.client.Gauge; import io.prometheus.client.Histogram; import io.prometheus.client.Summary; /** * @author: 作者: chengaofeng * @date: 创建时间:2020-03-03 19:02:43 * @Description: TODO * @version V1.0 */ @RestController public class MetricController { @Autowired Counter operatorCount; @Autowired Gauge runningJob; @Autowired Histogram executeTime; @Autowired Summary timeQuantile; @RequestMapping("/counter/{className}") public String counter(@PathVariable String className) { operatorCount.labels(className).inc(); return "COUNTER"; } @RequestMapping("/guage/{number}") public String guage(@PathVariable int number) { runningJob.set(number); return "guage"; } @RequestMapping("/histogram/{time}") public String histogram(@PathVariable double time) { executeTime.observe(time); return "histogram"; } @RequestMapping("/summary/{time}") public String summary(@PathVariable double time) { timeQuantile.observe(time); return "summary"; } }
시작스러운 곳이 섰다 부트 작품, 편안한 인터페이스 위의 여러 번, 다음 액세스 방문
/actuator/prometheus
샘플 아래에 게시보기 지표를 (우리 세대에 자동으로 표시하는 다양한 스프링을 제거하기 위해)# HELP sbprometheus_job_execute_time_quantile job执行时间时间分布(分) # TYPE sbprometheus_job_execute_time_quantile summary sbprometheus_job_execute_time_quantile{quantile="0.5",} 5.0 sbprometheus_job_execute_time_quantile{quantile="0.9",} 13.0 sbprometheus_job_execute_time_quantile_count 11.0 sbprometheus_job_execute_time_quantile_sum 120.0 # HELP sbprometheus_job_execute_time job执行时间时间分布(分) # TYPE sbprometheus_job_execute_time histogram sbprometheus_job_execute_time_bucket{le="1.0",} 2.0 sbprometheus_job_execute_time_bucket{le="5.0",} 3.0 sbprometheus_job_execute_time_bucket{le="10.0",} 3.0 sbprometheus_job_execute_time_bucket{le="+Inf",} 3.0 sbprometheus_job_execute_time_count 3.0 sbprometheus_job_execute_time_sum 6.5 # HELP sbprometheus_class_operator_count 操作总次数 # TYPE sbprometheus_class_operator_count counter sbprometheus_class_operator_count{className="transform",} 2.0 sbprometheus_class_operator_count{className="sub",} 1.0 # HELP sbprometheus_running_job_count 正在运行的job数 # TYPE sbprometheus_running_job_count gauge sbprometheus_running_job_count 10.0
생성 이미지 고정 표시기
스프링 부팅 치어 프로젝트 때문에, 우리가 추가 한 spring-boot-maven-plugin
실행이 때문에,이 플러그인을 mvn package
독립 실행 형 실행 항아리 후 생성되는 거울의 생산 있도록 만 오픈 JDK 기반 미러링을, 다음에 항아리 복사, 다음을 시작합니다 그것.
(프로젝트 디렉토리에) 구축
$ mvn clean package $ cd target $ cat > Dockerfile<<EOF #基础镜像基于openjdk,利用alpine FROM openjdk:8u212-jdk-alpine #所属团队 MAINTAINER chengf #将编译好的工程jar包copy到镜像容器中 ENV TARGET_JAR="sb-prometheus-0.0.1-SNAPSHOT.jar" COPY ${TARGET_JAR} /usr/src/${TARGET_JAR} # 工作目录 WORKDIR /usr/src #程序入口 RUN echo "java -jar \${TARGET_JAR} > start.sh \ && chmod 777 start.sh CMD ./start.sh EOF $ docker build -t sb-prometheus:0.0.1 .
부팅 이미지, 테스트 이미지가 올바른지
docker run --rm --name sb-prometheus -p 8080:8080 sb-prometheus:0.0.1
- 우리의 정의로운 인터페이스의 일부를 방문하여
/actuator/prometheus
제대로 작동하는지 확인
- 우리의 정의로운 인터페이스의 일부를 방문하여
고정 표시기 떼 클러스터에 배포
편집 스택 파일
$ cd /opt/k8s/prometheus $ cat> sb-prom-stack.yml<<EOF version: "3" services: sbprometheus: image: sb-prometheus:0.0.1 networks: - mcsas-network deploy: restart_policy: condition: on-failure networks: mcsas-network: external: true EOF
서비스 시작
$ docker stack deploy -c sb-prom-stack.yml sbprom
의 프로 메테우스 수정 file_sd_configs
구성 파일
에서 프로 메테우스가 도커 스웜을 모니터링 , 우리는 프로 메테우스 구성 파일에 다음 구성 섹션을 지정합니다 :
- job_name: 'springboot'
metrics_path: /actuator/prometheus
file_sd_configs:
- files:
- /etc/prometheus/service.yaml
따라서 만 마운트 디렉토리에 service.yaml을 만들어, 우리는 할 수있는 추가 서비스를 모니터링 할
$ cd /opt/k8s/prometheus/conf
$ cat>service.yaml<<EOF
- targets: ['sbprometheus:8080']
EOF
편집 후, 프로 메테우스가 볼 수 색인 분류 포트 서비스를 노출, 프로 메테우스 (Prometheus)에 인수 된 우리의 사용자 정의 비즈니스 측정에서 찾을 수 있습니다
우리는 컨테이너 포트 비즈니스에 노출되지 않기 때문에, 그래서 용기의 내부를 입력 증명하기 위해, 일부 지표 데이터를 생성, wget을 통해 우리의 편안한 인터페이스를 방문
$ docker ps |grep sbprometheus
8dbafd80573b sb-prometheus:0.0.1 "/bin/sh -c ./start.…" 44 minutes ago Up 44 minutes sbprom_sbprometheus.1.kuzpe4he7j2iz9i43cwrrxh3x
$ docker exec -it 8dbafd80573b sh
/usr/src # wget -q -O - http://localhost:8080/summary/66
/usr/src # wget -q -O - http://localhost:8080/counter/tranform
/usr/src # wget -q -O - http://localhost:8080/counter/sub
/usr/src # wget -q -O - http://localhost:8080/histogram/1
/usr/src # wget -q -O - http://localhost:8080/histogram/3.4
구성 대시 보드는 grafana를 입력합니다
그것은에서 grafana에서 우리의 사업의 지표로 볼 수있다 :
또한 cAdvisor 의해 컨테이너에 대응하는 서비스의 배포를 관찰 할 수있다