Elastic APM 服务器在 Docker 中不可用

问题描述 投票:0回答:1

我正在尝试运行 apm-server 使用 apm-agent 从基于 java 的应用程序收集数据并将其发送到 elasticsearch。 这是我的撰写文件:

services:
  order-service:
    image: apm-java/order-service:1.0.0
    build:
      context: .
      dockerfile: Dockerfile
      args:
        - "JAR_PATH=target/order-service-0.0.1.jar"
    environment:
      - "JAR_NAME=order-service-0.0.1.jar"
      - "JVM_OPTIONS="
      - "ELASTIC_APM_SERVICE_NAME=order-service"
      - "ELASTIC_APM_APPLICATION_PACKAGES=org.elk.orderservice"
      - "ELASTIC_APM_SERVER_URLS=http://apm:8200"
    ports:
      - 8080:8080
    networks:
      - app-network
    depends_on:
      - postgres
      - redis
      - apm

  kibana:
    image: kibana:8.11.3
    environment:
      - "LOGGING_QUIET=true"
    ports:
      - 5601:5601
    networks:
      - app-network
    depends_on:
      - elasticsearch

  elasticsearch:
    image: apm-java/elasticsearch:1.0.0
    build:
      context: ./docker/elasticsearch
      dockerfile: Dockerfile
    environment:
      - cluster.name=apm-java
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms256m -Xmx256m"
      - ELASTIC_PASSWORD=$ELASTIC_PASSWORD
    ulimits:
      memlock:
        soft: -1
        hard: -1
    networks:
      - app-network
    ports:
      - 9200:9200

  apm:
    image: apm-java/apm-server:1.0.0
    build:
      context: ./docker/apm
      dockerfile: Dockerfile
    environment:
      - "ELASTICSEARCH_USERNAME=elastic"
      - "ELASTICSEARCH_PASSWORD=$ELASTIC_PASSWORD"
    ports:
      - 8200:8200
    networks:
      - app-network
    depends_on:
      - elasticsearch
      - kibana

  postgres:
    image: 'postgres:16.1'
    environment:
      - 'POSTGRES_DB=mydatabase'
      - 'POSTGRES_PASSWORD=secret'
      - 'POSTGRES_USER=myuser'
    ports:
      - '5432:5432'
    networks:
      - app-network

  redis:
    image: 'redis:7.2.3'
    ports:
      - '32780:6379'
    networks:
      - app-network

networks:
  app-network:
    driver: bridge
    name: app-network

Java 应用程序 dockerfile:

FROM eclipse-temurin:21-jdk

EXPOSE 8080
RUN mkdir -p /opt/app
WORKDIR /opt/app

ARG JAR_PATH
COPY $JAR_PATH /opt/app
RUN wget -O apm-agent.jar https://search.maven.org/remotecontent?filepath=co/elastic/apm/elastic-apm-agent/1.45.0/elastic-apm-agent-1.45.0.jar

CMD java -javaagent:/opt/app/apm-agent.jar $JVM_OPTIONS -jar $JAR_NAME

apm-服务器 yml,dockerfile:

apm-server:
  host: "localhost:8200"
  concurrent_requests: 5
  rum:
    enabled: true

queue.mem.events: 4096
max_procs: 4

output.elasticsearch:
  hosts: ["https://elasticsearch:9200"]
  username: "elastic"
  password: "changeme"
  ssl.verification_mode: none


setup.kibana.host: "kibana:5601"
setup.dashboards.enabled: true

#logging.level: info
#logging.to_files: false

logging.level: debug
logging.to_files: false
FROM docker.elastic.co/apm/apm-server:8.11.3
COPY --chmod=0644 --chown=1000:1000 apm-server.yml /usr/share/apm-server/apm-server.yml

Elasticsearchconf,dockerfile:

cluster.name: ${cluster.name}
network.host: 0.0.0.0

# minimum_master_nodes need to be explicitly set when bound on a public IP
# set to 1 to allow single node clusters
# Details: https://github.com/elastic/elasticsearch/pull/17288
discovery.type: single-node
FROM docker.elastic.co/elasticsearch/elasticsearch:8.11.3
COPY --chown=elasticsearch:elasticsearch elasticsearch.yml /usr/share/elasticsearch/config/

CMD ["elasticsearch", "-Elogger.level=INFO"]

我可以使用

apm-server test output
命令从 apm-server 连接到 elasticsearch:

elasticsearch: https://elasticsearch:9200...
  parse url... OK
  connection...
    parse host... OK
    dns lookup... OK
    addresses: 172.23.0.3
    dial up... OK
  TLS...
    security... WARN server's certificate chain verification is disabled
    handshake... OK
    TLS version: TLSv1.3
    dial up... OK
  talk to server... OK
  version: 8.11.3

但是在java应用程序日志中出现以下错误:

2024-01-10 08:05:29,528 [main] INFO  co.elastic.apm.agent.configuration.StartupInfo - Starting Elastic APM 1.45.0 as order-service (0.0.1) on Java 21.0.1 Runtime version: 21.0.1+12-LTS VM version: 21.0.1+12-LTS (Eclipse Adoptium) Linux 6.5.11-linuxkit
2024-01-10T08:05:29.529074548Z 2024-01-10 08:05:29,528 [main] INFO  co.elastic.apm.agent.configuration.StartupInfo - server_urls: 'http://apm:8200' (source: Environment Variables)
2024-01-10T08:05:29.529416131Z 2024-01-10 08:05:29,529 [main] INFO  co.elastic.apm.agent.configuration.StartupInfo - application_packages: 'org.elk.orderservice' (source: Environment Variables)
2024-01-10T08:05:30.587938173Z 2024-01-10 08:05:30,587 [elastic-apm-server-healthcheck] WARN  co.elastic.apm.agent.report.ApmServerHealthChecker - Elastic APM server http://apm:8200/ is not available (Connection refused)
2024-01-10T08:05:30.589589381Z 2024-01-10 08:05:30,589 [main] INFO  co.elastic.apm.agent.impl.ElasticApmTracer - Tracer switched to RUNNING state
2024-01-10T08:05:31.430460173Z 2024-01-10 08:05:31,429 [elastic-apm-remote-config-poller] ERROR co.elastic.apm.agent.configuration.ApmServerConfigurationSource - Connection refused

所有服务都绑定到单个“应用程序网络”。 那么java-agent和apm-server之间的连接问题可能是什么原因造成的呢? 如何调试从 java 到 elk 的流程?

java docker elasticsearch docker-compose elastic-apm
1个回答
0
投票

APM Server 绑定到 localhost,暴露端口时阻止访问。要解决此问题,请将 apm.server.yml 更改为:

apm-server:
  host: "localhost:8200"

至:

apm-server:
  host: "0.0.0.0:8200"
© www.soinside.com 2019 - 2024. All rights reserved.