본문 바로가기

elk

Docker swarm elastic apm 올리기

728x90
반응형

swarm init, network생성

docker swarm init
docker network create -d overlay elk-network

 

docker-compose.yml

version: '3.8'
services:
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.11.1 
    environment:
      ES_JAVA_OPTS: "-Xmx256m -Xms256m"
      discovery.type: single-node 
    ports:
      - 9200:9200
    networks:
      - elk-network
  
  kibana:    
    image: docker.elastic.co/kibana/kibana:7.11.2
    ports:
      - 5601:5601
    networks:
      - elk-network
    depends_on:
      - elasticsearch 
      
  apm-server:
    image: docker.elastic.co/apm/apm-server:7.11.1
    depends_on:
      - elasticsearch
      - kibana      
    cap_add: ["CHOWN", "DAC_OVERRIDE", "SETGID", "SETUID"]
    cap_drop: ["ALL"]
    ports:
    - 8200:8200
    networks:
    - elk-network
    command: >
       apm-server -e
         -E apm-server.rum.enabled=true
         -E setup.kibana.host=kibana:5601
         -E setup.template.settings.index.number_of_replicas=0
         -E apm-server.kibana.enabled=true
         -E apm-server.kibana.host=kibana:5601
         -E output.elasticsearch.hosts=["elasticsearch:9200"]
    healthcheck:
      interval: 10s
      retries: 12
      test: curl --write-out 'HTTP %{http_code}' --fail --silent --output /dev/null http://localhost:8200/
    
networks: 
    elk-network:
        external: true

docker compose를 올린다. 

docker stack deploy -c elastic-docker-compose.yml elastic

local에서 kibana화면을 볼 수 있다.

 

Test API Controller

package com.example.demo.test;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @RequestMapping("/test")
    public String test() {
        System.out.println("test");
        return "testAPI Call";
    }
}

 

만든 api는 jar로 말아서 올린다. 

 

mvnrepository.com/artifact/co.elastic.apm/apm-agent-api/1.21.0

apm 적용을 위해 elastic-apm-agent.jar를 다운로드받는다.

 

jar 파일을 실행한다. 

java -javaagent:elastic-apm-agent-1.21.0.jar -Delastic.apm.service_name=test -Delastic.apm.application_packages=scgapi -Delastic.apm.server_urls=http://localhost:8200/ -jar demo-0.0.1-SNAPSHOT.jar

 

http://localhost:8080/test API를 호출한다. 

 

kibana > Observability > APM 메뉴로 들어간다. 

 

 

jar 실행할 때 설정한 name이 보인다. 

 

 

API 호출한 내역을 확인할 수 있다. 

 

눌러서 들어가면 세부 항목을 확인할 수 있다.

 

 

transaction 세부내역도 확인이 가능하다. 

 

 

elasticsearch 로그로도 확인이 가능하다. 

 

 

api -> 다른 서버 api 호출 테스트 

기존코드에서 restTemplate을 이용해서 호출한다. 

@RequestMapping("/test")
    public String test() {

        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> result = restTemplate.getForEntity("http://localhost:8081/routeapi1/hello", String.class);

        return result.getBody();
    }
@GetMapping("/routeapi1/hello")
public String hello(HttpServletRequest request) throws Exception{
	StringBuffer returnValue= new StringBuffer();
	Enumeration headerNames = request.getHeaderNames();
	while(headerNames.hasMoreElements()) {
		String name = (String) headerNames.nextElement();
		returnValue.append(name + " ");
	}
	return returnValue.toString();
}

 

http://localhost:8080/test를 호출하면  trace를 확인할 수 있다.


API를 Docker Swarm으로 올리면서 elastic apm 추가 

FROM docker.autoever.com/openjdk:11-jre-slim

WORKDIR /application

ARG DEPENDENCY=./build/libs/scgapi-0.0.1-SNAPSHOT

COPY ${DEPENDENCY}/BOOT-INF/lib ./BOOT-INF/lib
COPY ${DEPENDENCY}/org ./org
COPY ${DEPENDENCY}/META-INF ./META-INF
COPY ${DEPENDENCY}/BOOT-INF/classpath.idx ./BOOT-INF/classpath.idx
COPY ${DEPENDENCY}/BOOT-INF/classes ./BOOT-INF/classes

COPY ./elastic-apm-agent-1.21.0.jar /apm-agent.jar

CMD ["java","-javaagent:/apm-agent.jar", "-Delastic.apm.service_name=scgapi", "-Delastic.apm.application_packages=me.scgapi.scgapi", "-Delastic.apm.server_urls=http://127.0.0.1:8200/", "org.springframework.boot.loader.JarLauncher"]

agent를 복사한다.

system property를 세팅해준다. 

version: '3.1'

services: 
    scgapi:   
        image: localhost:5000/scgapi
        ports: 
            - "8081:8081"
        networks: 
            - elk-network
        environment: 
            TZ: UTC

networks: 
    elk-network:
        external: true

api docker compose 파일을 작성한다. 

docker stack deploy -c docker-compose.yml api

올리면 apm에서 확인 가능하다. 

728x90
반응형

'elk' 카테고리의 다른 글

logstash input rabbitmq 설정 docker swarm  (0) 2021.08.12