Skywalking由国内开源
大体架构是这样子
我用的是dockerhub的镜像
docker pull apache/skywalking-ui:8.5.0
docker pull apache/skywalking-oap-server:8.5.0-es7
docker pull elasticsearch:7.9.0
1.部署
搞了一个简单的es用
apiVersion: apps/v1
kind: Deployment
metadata:
name: es
namespace: default
spec:
progressDeadlineSeconds: 600
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
workload.user.cattle.io/workloadselector: deployment-default-es
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
type: RollingUpdate
template:
metadata:
annotations:
cattle.io/timestamp: "2022-11-10T03:19:31Z"
field.cattle.io/ports: '[[{"containerPort":9200,"dnsName":"es","hostPort":0,"kind":"ClusterIP","name":"db","protocol":"TCP"},{"containerPort":9300,"dnsName":"es","hostPort":0,"kind":"ClusterIP","name":"transport","protocol":"TCP"}]]'
creationTimestamp: null
labels:
workload.user.cattle.io/workloadselector: deployment-default-es
spec:
containers:
- env:
- name: discovery.type
value: single-node
image: elasticsearch:7.9.0
imagePullPolicy: IfNotPresent
name: es
ports:
- containerPort: 9200
name: db
protocol: TCP
- containerPort: 9300
name: transport
protocol: TCP
stdin: true
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
tty: true
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
部署skywalking-oap
apiVersion: apps/v1
kind: Deployment
metadata:
name: skywalking-oap
namespace: default
spec:
progressDeadlineSeconds: 600
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
workload.user.cattle.io/workloadselector: deployment-default-skywalking-oap
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
type: RollingUpdate
template:
metadata:
annotations:
cattle.io/timestamp: "2022-11-10T03:22:45Z"
field.cattle.io/ports: '[[{"containerPort":12800,"dnsName":"skywalking-oap","hostPort":0,"kind":"ClusterIP","name":"http","protocol":"TCP","sourcePort":0},{"containerPort":11800,"dnsName":"skywalking-oap","hostPort":0,"kind":"ClusterIP","name":"grpc","protocol":"TCP","sourcePort":0}]]'
creationTimestamp: null
labels:
workload.user.cattle.io/workloadselector: deployment-default-skywalking-oap
spec:
containers:
- env:
- name: SW_AGENT_COLLECTOR_BACKEND_SERVICES
value: skywalking-oap.skywalking:11800
- name: SW_AGENT_NAME
value: skywalking-oap
- name: SW_ES_PASSWORD
- name: SW_ES_USER
value: '"elastic"'
- name: SW_STORAGE
value: elasticsearch7
- name: SW_STORAGE_ES_CLUSTER_NODES
value: es:9200
image: apache/skywalking-oap-server:8.5.0-es7
imagePullPolicy: IfNotPresent
name: skywalking-oap
ports:
- containerPort: 12800
name: http
protocol: TCP
- containerPort: 11800
name: grpc
protocol: TCP
resources: {}
securityContext:
allowPrivilegeEscalation: false
capabilities: {}
privileged: false
readOnlyRootFilesystem: false
runAsNonRoot: false
stdin: true
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
tty: true
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
部署skywalking-ui
apiVersion: apps/v1
kind: Deployment
metadata:
name: skywalking-ui
namespace: default
spec:
progressDeadlineSeconds: 600
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
workload.user.cattle.io/workloadselector: deployment-default-skywalking-ui
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
type: RollingUpdate
template:
metadata:
annotations:
cattle.io/timestamp: "2022-11-10T03:27:36Z"
field.cattle.io/ports: '[[{"containerPort":8080,"dnsName":"skywalking-ui","hostPort":0,"kind":"ClusterIP","name":"http","protocol":"TCP","sourcePort":0}]]'
creationTimestamp: null
labels:
workload.user.cattle.io/workloadselector: deployment-default-skywalking-ui
spec:
containers:
- env:
- name: SW_OAP_ADDRESS
value: skywalking-oap:12800
image: apache/skywalking-ui:8.5.0
imagePullPolicy: IfNotPresent
name: skywalking-ui
ports:
- containerPort: 8080
name: http
protocol: TCP
resources: {}
securityContext:
allowPrivilegeEscalation: false
capabilities: {}
privileged: false
readOnlyRootFilesystem: false
runAsNonRoot: false
stdin: true
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
tty: true
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
2.java接入agent(用的Sidecar模式注入)
使用 sidecar 将 pod 接入链路追踪
下面介绍如何使用 sidecar 将 pod 接入链路追踪。Java微服务接入skywalking 可以使用 SkyWalking Java Agent 来上报监控数据,这就需要 java 微服务在启动参数中通过 -javaagent: 指定 skywalking agent 探针包,通常有以下三种方式集成:
1.使用官方提供的基础镜像 skywalking-base;
2.将 agent 包构建到已存在的镜像中;
3.通过 sidecar 模式挂载 agent;
前面两种方式在前面的文章中有简单介绍,这里主要介绍如何使用 sidecar 将 pod 接入链路追踪,这种方式不需要修改原来的基础镜像,也不需要重新构建新的服务镜像,而是会以sidecar模式,通过共享的 volume 将 agent 所需的相关文件直接挂载到已经存在的服务镜像中。sidecar模式原理很简单,就是在 pod 中再部署一个初始容器,这个初始容器的作用就是将 skywalking agent 和 pod 中的应用容器共享。
#这里通过JAVA_TOOL_OPTIONS,而不是JAVA_OPTS,JAVA_TOOL_OPTIONS可以实现不通过将agent命令加入到java应用jvm参数而实现agent的集成(具体的请自行查找)
什么是初始化容器 init container
Init Container 就是用来做初始化工作的容器,可以是一个或者多个,如果有多个的话,这些容器会按定义的顺序依次执行,只有所有的 Init Container 执行完后,主容器才会被启动。我们知道一个Pod里面的所有容器是共享数据卷和网络命名空间的,所以 Init Container 里面产生的数据可以被主容器使用到的。
官方下载地址:https://archive.apache.org/dist/skywalking/
首先做这个agent镜像
下载这个包 apache-skywalking-apm-es7-8.7.0.tar
解压这个包,里面有agent
# Dockerfile
FROM busybox:latest
ENV LANG=C.UTF-8
RUN set -eux && mkdir -p /usr/skywalking/
ADD agent/ /usr/skywalking/
WORKDIR /
docker build -t skywalking-agent:8.5.0 .
我用的应用是自己打成的spring
下面是yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: spring
namespace: default
spec:
progressDeadlineSeconds: 600
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
workload.user.cattle.io/workloadselector: deployment-default-spring
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
type: RollingUpdate
template:
metadata:
annotations:
cattle.io/timestamp: "2022-11-10T03:24:50Z"
field.cattle.io/ports: '[[{"containerPort":5000,"dnsName":"spring-nodeport","hostPort":0,"kind":"NodePort","name":"spring","protocol":"TCP","sourcePort":0}],[]]'
creationTimestamp: null
labels:
workload.user.cattle.io/workloadselector: deployment-default-spring
spec:
containers:
- env:
- name: JAVA_TOOL_OPTIONS
value: -javaagent:/usr/skywalking/agent/skywalking-agent.jar -XX:MaxRAMPercentage=75.0
- name: SW_AGENT_COLLECTOR_BACKEND_SERVICES
value: skywalking-oap:11800
- name: TZ
value: Asia/Shanghai
image: root/hello-spring:v0.1
imagePullPolicy: IfNotPresent
name: spring
ports:
- containerPort: 5000
name: spring
protocol: TCP
resources: {}
securityContext:
allowPrivilegeEscalation: false
capabilities: {}
privileged: false
readOnlyRootFilesystem: false
runAsNonRoot: false
stdin: true
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
tty: true
volumeMounts:
- mountPath: /usr/skywalking/agent
name: sw-agent
dnsConfig: {}
dnsPolicy: ClusterFirst
initContainers:
- command:
- /bin/sh
- -c
- mkdir -p /skywalking/agent && cp -r /usr/skywalking/agent/* /skywalking/agent
image: skywalking-agent:8.5.0
imagePullPolicy: IfNotPresent
name: agent
resources: {}
securityContext:
allowPrivilegeEscalation: false
privileged: false
readOnlyRootFilesystem: false
runAsNonRoot: false
stdin: true
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
tty: true
volumeMounts:
- mountPath: /skywalking/agent
name: sw-agent
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
volumes:
- hostPath:
path: /opt/test
type: ""
name: sw-agent
最后的结果