在SpringBoot中验证Skywalking(AWS EKS版)-步骤

时间:2024-03-25 17:31:40

从官网上面下载skywalking的java 代理包。具体如下:
skywalking代理包

选择可选插件

解压这个包,从可选插件里面,根据你自己的java项目的实际情况,选择可选包,复制到正式插件包里面。具体如下:
可选插件
将这些可选包从optional-plugins复制到plugins文件夹。

Dockerfile

FROM busybox:latest

ENV LANG=C.UTF-8

RUN set -eux && mkdir -p /usr/skywalking/agent/

ADD skywalking-agent/ /usr/skywalking/agent/

WORKDIR /

开始构建自己的skwalking agent镜像文件。

docker build -t sw-agent-sidecar .

推送到私有ECR库中

# 登录到ECR私有库
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin xxxxx.dkr.ecr.us-east-1.amazonaws.com
# 构建
docker build -t sw-agent-sidecar .
# 标记
docker tag sw-agent-sidecar:latest xxx.dkr.ecr.us-east-1.amazonaws.com/sw-agent-sidecar:latest
# 推送
docker push xxxx.dkr.ecr.us-east-1.amazonaws.com/sw-agent-sidecar:latest

Spring中k8s配置

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    name: demo-sw
  name: demo-sw
spec:
  replicas: 1
  selector:
    matchLabels:
      name: demo-sw
  template:
    metadata:
      labels:
        name: demo-sw
    spec:
      initContainers:
      - image: xxxx.dkr.ecr.us-east-1.amazonaws.com/sw-agent-sidecar:latest
        name: sw-agent-sidecar
        imagePullPolicy: IfNotPresent
        command: ['sh']
        args: ['-c','mkdir -p /skywalking/agent && cp -r /usr/skywalking/agent/* /skywalking/agent']
        volumeMounts:
        - mountPath: /skywalking/agent
          name: sw-agent
      containers:
      - image: nginx:1.7.9
        name: nginx
        volumeMounts:
        - mountPath: /usr/skywalking/agent
          name: sw-agent
        ports:
        - containerPort: 80
      volumes:
      - name: sw-agent
        emptyDir: {}

除了在Deployment设置sidecar之外,还需要在configmap里面给Spring设置3个环境变量:

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Values.services.demo2.name }}-configmap
  namespace: {{ .Release.Namespace }}
data:
  SPRING_PROFILES_ACTIVE: {{ .Values.spring.profiles.active }}
  MYSQL_HOST: {{ .Values.mysql.HOST }}
  JAVA_OPTS: -javaagent:/usr/skywalking/agent/skywalking-agent.jar
  SW_AGENT_NAME: demo2
  SW_AGENT_COLLECTOR_BACKEND_SERVICES: skywalking-skywalking-helm-oap.skywalking:11800

主要是下面3个变量:

  JAVA_OPTS: -javaagent:/usr/skywalking/agent/skywalking-agent.jar
  SW_AGENT_NAME: demo2
  SW_AGENT_COLLECTOR_BACKEND_SERVICES: skywalking-skywalking-helm-oap.skywalking:11800

这里涉及到k8s跨名字空间的服务调用——FQDN。