k8s 使用skywalking-agent 部署java应用

目录

一、前言

二、安装docker

1、卸载旧的docker

2、安装docker

三、安装docker-compose

四、使用docker部署SkyWalking

五、制作javaagent镜像

六、应用里配置skywalking-agent

七、验证


一、前言

        使用skywalking对k8s集群的java应用监控,通过docker-compose方式部署skywalking集群,创建skywalking-agent镜像,以sidecar的方式部署到k8s集群的java应用中。 

 注意:     需要注意的是skywalking和skywalking-agent的版本兼容性,若是skywalking和agent的版本不兼容,会无法通过agent成功获取java指标,不能成功上报给skywalking后端

二、安装docker

        centos 系统安装docker,参考官网:https://docs.docker.com/engine/install/centos/

1、卸载旧的docker

        根据实际情况来决定是要卸载当前已运行的docker,卸载docker的方法为:

## 卸载docker命令
sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

2、安装docker

## 安装 docker
sudo yum install -y yum-utils
sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo

sudo yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin

## docker 开机自启动

sudo systemctl start docker && sudo systemctl enable docker

三、安装docker-compose

curl -SL https://github.com/docker/compose/releases/download/v2.12.2/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose 

chmod a+x /usr/local/bin/docker-compose

四、使用docker部署SkyWalking

       1、 SkyWalking访问地址: http://ip:8080

       2、 使用docker-compose安装SkyWalking的编排内容见docker-compose.yaml, 尤其要注意oap服务的环境变量,指定使用的elasticsearch版本是SW_STORAGE: elasticsearch7,若SW_STORAGE: elasticsearch则表示使用的是 elasticsearch 6版本。

## cat docker-compose.yaml
## docker-compose up -d
version: '3.3'
services:
  elasticsearch:
    image: elasticsearch:7.4.2
    container_name: elasticsearch
    restart: always
    ports:
      - 9200:9200
    environment:
      discovery.type: single-node
      TZ: Asia/Shanghai
   # volumes:
   #    - ./elasticsearch/logs:/usr/share/elasticsearch/logs
   #    - ./elasticsearch/data:/usr/share/elasticsearch/data
  oap:
    image: apache/skywalking-oap-server:8.7.0-es7
    container_name: skywalking-oap
    depends_on:
      - elasticsearch
    links:
      - elasticsearch
    restart: always
    ports:
      - 11800:11800
      - 12800:12800
    environment:
      SW_STORAGE: elasticsearch7  # 指定ES
      SW_STORAGE_ES_CLUSTER_NODES: elasticsearch:9200
      TZ: Asia/Shanghai
  ui:
    image: apache/skywalking-ui:8.7.0
    container_name: sykwolking-ui
    depends_on:
      - sykwolking-oap
    links:
      - sykwolking-oap
    restart: always
    ports:
      - 8080:8080
    environment:
      SW_OAP_ADDRESS: http://oap:12800
      TZ: Asia/Shanghai

        3、启动SkyWalking

# 进入到docker-compose.yaml 当前目录,执行以下命令来启动

docker-compose up -d

五、制作javaagent镜像

## 参考博客:https://blog.csdn.net/dyangel2013/article/details/119183022

软件下载地址:https://archive.apache.org/dist/skywalking/

## cat Dcokerfile
## docker build -t 172.18.6.97/test/skywalking-agent:v1  . --no-cache


FROM   docker.io/library/busybox:latest AS base
LABEL  creator="fanqietudou",date="2022-11-02"


# 使用 ADD 下载 java-agent,你也可以使用其他下载命令,或者下载到本地,再 使用 COPY或者ADD 指令添加到 镜像里
ADD    https://archive.apache.org/dist/skywalking/java-agent/8.8.0/apache-skywalking-java-agent-8.8.0.tgz /tmp/
RUN    mkdir -p /opt \
    && tar -xzf /tmp/apache-skywalking-java-agent-8.8.0.tgz -C /opt/ \
    && rm -rf /tmp/*
#ADD skywalking-agent.tar.gz /opt/
RUN    mkdir -p /javaagent


## java-agent 使用 方法
##  java -javaagent:/opt/skywalking-agent/skywalking-agent.jar -jar yourApp.jar
##  -javaagent:/opt/skywalking-agent/skywalking-agent.jar=agent.service_name=yourAppName,collector.backend_service=127.0.0.1:11800
##  -javaagent:/opt/skywalking-agent/skywalking-agent.jar -Dskywalking.agent.service_name=yourAppName -Dskywalking.collector.backend_service=127.0.0.1:11800
#
# docker push 172.18.6.97/test/skywalking-agent:v1

六、应用配置skywalking-agent

        我这里以k8s部署的应用配置skywalking-agent为例,且以rocketmq-dashboard应用作为jar应用例子,可以按照实际情况将rocketmq-dashboard 替换成您的java应用来部署,rocketmq-dashboard部署可以参考博客https://blog.csdn.net/weixin_44770684/article/details/127520224?spm=1001.2014.3001.5501

## cat javaagent.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
  labels:
    app: agent
  name: agent
spec:
  replicas: 1
  selector:
    matchLabels:
      app: agent
  template:
    metadata:
      labels:
        app: agent
    spec:
      affinity: {}
      volumes:
      - hostPath:
          path: /etc/localtime
        name: localtime
      - emptyDir: {}
        name: jar
        
      containers:
      - env:
        - name: appname
          valueFrom:
            fieldRef:
              fieldPath: metadata.labels['app']
        - name: JAVA_OPT
          value: -Drocketmq.namesrv.addr=rocketmq-namesrv:9876 -Dcom.rocketmq.sendMessageWithVIPChannel=false
        - name: SKY
          value: -Xms512m -Xmx512m   -javaagent:/javaagent/skywalking-agent/skywalking-agent.jar -Dskywalking.agent.service_name=k8s::$(appname) -Dskywalking.collector.backend_service=172.18.6.97:11800
        - name: JAVA_OPTS
          value: $(JAVA_OPT) $(SKY)
        image: 172.18.6.97/test/rocketmq-dashboard:v1
        imagePullPolicy: IfNotPresent
        name: ui
        ports:
        - containerPort: 9876
          name: ui
          protocol: TCP
        volumeMounts:
        - mountPath: /etc/localtime
          name: localtime
          readOnly: true
        - mountPath: /javaagent
          name: jar
      initContainers:
      - command:
        - cp
        - -r
        - /opt/skywalking-agent
        - /javaagent
        image: 172.18.6.97/test/skywalking-agent:v1
        imagePullPolicy: IfNotPresent
        name: java
        volumeMounts:
        - mountPath: /etc/localtime
          name: localtime
          readOnly: true
        - mountPath: /javaagent
          name: jar
---

说明:

        要着重注意,jar应用和agent的共享存储部分,jar应用和agent的共享存储是为实现将skywalking-agent应用共享给jar应用,以便jar应用可以在启动时候加载 skywalking-agent.jar

七、验证

        jar应用成功运行后,可以登录skywalking 浏览页面查看上报的数据。

skywalking的访问地址:http://ip:8080

k8s 使用skywalking-agent 部署java应用_第1张图片

你可能感兴趣的:(java,kubernetes,skywalking)