8.3-1-SpringCloud应用改造实践上
一、使用SkyWalking Agent
Java中使用agent,提供以下三种方式实现
- 使用官方提供的基础镜像skywalking-base
- 将agent 包构建到已经存在的基础镜像中
- sidecar模式挂载agent (推荐)
1.1 使用官方提供的基础镜像
https://skywalking.apache.org/downloads/
1.2 将agent 包构建到已经存在的基础镜像中
提供这种方式的原因是:官方的镜像属于精简镜像,并且是openjdk,可能需要安装很多常用命令。
1.3 sidecar 模式挂载agent
- 不需要修改原来的基础镜像,也无需重新构建服务镜像。以sidecar模式,通过共享volume的方式将agent所需的相关文件挂载到已经存在的服务镜像中。
- 在sidecar模式下,SkyWalking代理程序与应用程序是分离的,不会影响应用程序的执行,也不会干扰到应用的性能问题。
1.4 构建skywalking agent image
通过以下dockerfile 进行构建
FROM alpine:3.8
LABEL maintainer="ZHDYA"
ENV SKYWALKING_VERSION=8.5.0
ADD https://archive.apache.org/dist/skywalking/${SKYWALKING_VERSION}/apache-skywalking-apm-${SKYWALKING_VERSION}.tar.gz /
RUN tar -zxvf /apache-skywalking-apm-${SKYWALKING_VERSION}.tar.gz && \
mv apache-skywalking-apm-bin skywalking && \
mv /skywalking/agent/optional-plugins/apm-trace-ignore-plugin* /skywalking/agent/plugins/ && \
sed -i 's@# logging.max_history_files=${SW_LOGGING_MAX_HISTORY_FILES:-1}@logging.max_history_files=${SW_LOGGING_MAX_HISTORY_FILES:2}@' /skywalking/agent/config/agent.config && \
echo -e "\n# Ignore Path" >> /skywalking/agent/config/agent.config && \
echo "# see https://github.com/apache/skywalking/blob/v8.5.0/docs/en/setup/service-agent/java-agent/agent-optional-plugins/trace-ignore-plugin.md" >> /skywalking/agent/config/agent.config && \
echo 'trace.ignore_path=${SW_IGNORE_PATH:/health}' >> /skywalking/agent/config/agent.config
待docker build 完毕后,push到仓库即可
docker build -t harbor-local.kubernets.cn/library/skywalking-agent:8.5.0 .
docker push harbor-local.kubernets.cn/library/skywalking-agent:8.5.0
1.5 使用sidecar挂载
测试demo应用
apiVersion: apps/v1
kind: Deployment
metadata:
name: demo-skywalking
spec:
replicas: 1
selector:
matchLabels:
app: demo-skywalking
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
type: RollingUpdate
template:
metadata:
labels:
app: demo-skywalking
spec:
initContainers:
- name: init-skywalking-agent
image: harbor-local.kubernets.cn/library/skywalking-agent:8.5.0
command:
- 'sh'
- '-c'
- 'set -ex;mkdir -p /vmskywalking/agent;cp -r /skywalking/agent/* /vmskywalking/agent;'
volumeMounts:
- mountPath: /vmskywalking/agent
name: skywalking-agent
containers:
- image: nginx:1.7.9
imagePullPolicy: Always
name: nginx
ports:
- containerPort: 80
protocol: TCP
volumeMounts:
- mountPath: /opt/skywalking/agent
name: skywalking-agent
volumes:
- name: skywalking-agent
emptyDir: {}
以上是挂载sidecar的deployment.yaml文件,以nginx作为服务为例,主要是通过共享volume的方式挂载agent
首先initContainers通过skywalking-agent 卷挂载了sw-agent-sidecar中的/vmskywalking/agent ,并且将上面构建好的镜像中的agent目录cp到/vmskywalking/agent 目录。完成之后nginx启动时也挂载了skywalking-agent卷,并将其挂载到容器的/opt/skywalking/agent目录
二、改造SpringCloud应用
2.1 docker 打包并推送到仓库
修改dockerfile 配置,集成skywalking agent
# FROM openjdk:8-jdk
FROM harbor-local.kubernets.cn/library/openjdk:8-jdk
COPY ./springdemo2023.jar /opt/
RUN mkdir -p data/logs/ \
&& mkdir -p data/skywalking/agent
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo 'Asia/Shanghai' > /etc/timezone
ENTRYPOINT [ "java","-Dapp.id=springdemo2023","-javaagent:/data/skywalking/agent/skywalking-agent.jar","-Dskywalking.agent.service_name=springdemo2023","-Dskywalking.collector.backend_service=skywalking-oap.devops.svc.cluster.local:11800", "-jar", "/opt/springdemo2023.jar" ,"-Dfile.encoding=UTF-8"]
注意:k8s创建Service时,它会创建相应的DNS条目。此条例的格式为<service-name>.<namespace-name>.svc-cluster.local,如果容器只是有<Service-name>它将解析为本地服务到命名空间。如果要跨命名空间访问,泽需要使用完全限定的域名。
编译推送到私有仓库测试:
docker build -t harbor-local.kubernets.cn/demo/springdemo-skywalking:v1 .
2.2 编写k8s的yaml部署
apiVersion: v1
kind: Service
metadata:
annotations:
prometheus.io/http-probe: "true"
prometheus.io/http-probe-port: "8080"
prometheus.io/http-probe-path: "/apptwo"
name: springdemo2023
namespace: default
spec:
ports:
- name: web
port: 8080
protocol: TCP
targetPort: 8080
selector:
app: springdemo2023
sessionAffinity: None
type: ClusterIP
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: springdemo2023
namespace: default
spec:
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
app: springdemo2023
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
labels:
app: springdemo2023
spec:
imagePullSecrets:
- name: harborsecret
initContainers:
- name: init-skywalking-agent
image: harbor-local.kubernets.cn/library/skywalking-agent:8.5.0
command:
- 'sh'
- '-c'
- 'set -ex;mkdir -p /vmskywalking/agent;cp -r /skywalking/agent/* /vmskywalking/agent;'
volumeMounts:
- mountPath: /vmskywalking/agent
name: skywalking-agent
containers:
- image: harbor-local.kubernets.cn/demo/springdemo-skywalking:v1
imagePullPolicy: IfNotPresent
livenessProbe:
failureThreshold: 5
httpGet:
path: /apptwo
port: 8080
scheme: HTTP
initialDelaySeconds: 30
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 2
name: springdemo2023
ports:
- containerPort: 8080
name: web
protocol: TCP
readinessProbe:
failureThreshold: 5
httpGet:
path: /apptwo
port: 8080
scheme: HTTP
initialDelaySeconds: 20
periodSeconds: 5
successThreshold: 1
timeoutSeconds: 2
volumeMounts:
- mountPath: /data/skywalking/agent
name: skywalking-agent
dnsPolicy: ClusterFirst
restartPolicy: Always
terminationGracePeriodSeconds: 60
volumes:
- name: skywalking-agent
emptyDir: {}
8.3-2-SpringCloud应用改造实践下
三、线上真实场景模拟(spingboot微服务)
3.1 springboot微服务-ui服务
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: acme-financial-ui
name: acme-financial-ui
spec:
replicas: 1
selector:
matchLabels:
app: acme-financial-ui
template:
metadata:
labels:
app: acme-financial-ui
spec:
initContainers:
- name: init-skywalking-agent
image: harbor-local.kubernets.cn/library/skywalking-agent:8.5.0
command:
- 'sh'
- '-c'
- 'set -ex;mkdir -p /vmskywalking/agent;cp -r /skywalking/agent/* /vmskywalking/agent;'
volumeMounts:
- mountPath: /vmskywalking/agent
name: skywalking-agent
containers:
- env:
- name: JAVA_OPTS
value: "-javaagent:/opt/sw/agent/skywalking-agent.jar"
- name: SW_AGENT_NAME
value: "acme-financial-ui"
- name: SW_AGENT_COLLECTOR_BACKEND_SERVICES
value: "skywalking-oap.devops.svc.cluster.local:11800"
image: registry.cn-shanghai.aliyuncs.com/zhdya/acme-financial-ui:v0.1
imagePullPolicy: Always
name: ui
ports:
- containerPort: 8081
protocol: TCP
volumeMounts:
- mountPath: /opt/sw/agent
name: skywalking-agent
volumes:
- name: skywalking-agent
emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
labels:
app: acme-financial-ui
name: acme-financial-ui
spec:
ports:
- name: http
port: 8081
protocol: TCP
targetPort: 8081
selector:
app: acme-financial-ui
sessionAffinity: None
type: ClusterIP
3.2 Spingboot微服务-office服务
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: acme-financial-office
name: acme-financial-office
spec:
replicas: 1
selector:
matchLabels:
app: acme-financial-office
template:
metadata:
labels:
app: acme-financial-office
spec:
initContainers:
- name: init-skywalking-agent
image: harbor-local.kubernets.cn/library/skywalking-agent:8.5.0
command:
- 'sh'
- '-c'
- 'set -ex;mkdir -p /vmskywalking/agent;cp -r /skywalking/agent/* /vmskywalking/agent;'
volumeMounts:
- mountPath: /vmskywalking/agent
name: skywalking-agent
containers:
- env:
- name: JAVA_OPTS
value: "-javaagent:/opt/sw/agent/skywalking-agent.jar"
- name: SW_AGENT_NAME
value: "acme-financial-office"
- name: SW_AGENT_COLLECTOR_BACKEND_SERVICES
value: "skywalking-oap.devops.svc.cluster.local:11800"
image: registry.cn-shanghai.aliyuncs.com/zhdya/acme-financial-office:v0.1
imagePullPolicy: Always
name: office
ports:
- containerPort: 8082
protocol: TCP
volumeMounts:
- mountPath: /opt/sw/agent
name: skywalking-agent
volumes:
- name: skywalking-agent
emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
labels:
app: acme-financial-office
name: acme-financial-back-office
spec:
ports:
- name: http
port: 8082
protocol: TCP
targetPort: 8082
selector:
app: acme-financial-office
sessionAffinity: None
type: ClusterIP
3.3 Spingboot微服务 -account服务
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: acme-financial-account
name: acme-financial-account
spec:
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
app: acme-financial-account
template:
metadata:
labels:
app: acme-financial-account
spec:
initContainers:
- name: init-skywalking-agent
image: harbor-local.kubernets.cn/library/skywalking-agent:8.5.0
command:
- 'sh'
- '-c'
- 'set -ex;mkdir -p /vmskywalking/agent;cp -r /skywalking/agent/* /vmskywalking/agent;'
volumeMounts:
- mountPath: /vmskywalking/agent
name: skywalking-agent
containers:
- env:
- name: JAVA_OPTS
value: "-javaagent:/opt/sw/agent/skywalking-agent.jar"
- name: SW_AGENT_NAME
value: "acme-financial-account"
- name: SW_AGENT_COLLECTOR_BACKEND_SERVICES
value: "skywalking-oap.devops.svc.cluster.local:11800"
image: registry.cn-shanghai.aliyuncs.com/zhdya/acme-financial-account:v0.1
imagePullPolicy: Always
name: account
ports:
- containerPort: 8083
protocol: TCP
volumeMounts:
- mountPath: /opt/sw/agent
name: skywalking-agent
volumes:
- name: skywalking-agent
emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
labels:
app: acme-financial-account
name: acme-financial-account
spec:
ports:
- name: http
port: 8083
protocol: TCP
targetPort: 8083
selector:
app: acme-financial-account
sessionAffinity: None
type: ClusterIP
3.4 Spingboot微服务-customer服务
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: acme-financial-customer
name: acme-financial-customer
spec:
replicas: 1
selector:
matchLabels:
app: acme-financial-customer
template:
metadata:
labels:
app: acme-financial-customer
spec:
initContainers:
- name: init-skywalking-agent
image: harbor-local.kubernets.cn/library/skywalking-agent:8.5.0
command:
- 'sh'
- '-c'
- 'set -ex;mkdir -p /vmskywalking/agent;cp -r /skywalking/agent/* /vmskywalking/agent;'
volumeMounts:
- mountPath: /vmskywalking/agent
name: skywalking-agent
containers:
- env:
- name: JAVA_OPTS
value: "-javaagent:/opt/sw/agent/skywalking-agent.jar"
- name: SW_AGENT_NAME
value: "acme-financial-customer"
- name: SW_AGENT_COLLECTOR_BACKEND_SERVICES
value: "skywalking-oap.devops.svc.cluster.local:11800"
image: registry.cn-shanghai.aliyuncs.com/zhdya/acme-financial-customer:v0.1
imagePullPolicy: Always
name: customer
ports:
- containerPort: 8084
protocol: TCP
volumeMounts:
- mountPath: /opt/sw/agent
name: skywalking-agent
volumes:
- name: skywalking-agent
emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
labels:
app: acme-financial-customer
name: acme-financial-customer
namespace: default
spec:
ports:
- name: http
port: 8084
protocol: TCP
targetPort: 8084
selector:
app: acme-financial-customer
sessionAffinity: None
type: ClusterIP
3.5 创建UI-ingress
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
namespace: default
name: acme-financial-ing
spec:
ingressClassName: nginx
rules:
- host: acme.kubernets.cn
http:
paths:
- pathType: Prefix
backend:
service:
name: acme-financial-ui
port:
number: 8081
path: /
3.6 测试Spring 微服务API
通过Ingress访问UI服务的三个接口:/hello、/start、/readtimeout。
## 查看服务svc
kubectl get svc
## 模拟访问
for i in {1..5000}; do curl http://acme.kubernets.cn/hello && sleep 1; done
for i in {1..5000}; do curl http://acme.kubernets.cn/start && sleep 1; done
for i in {1..5000}; do curl http://acme.kubernets.cn/readtimeout && sleep 1; done
3.7 查看SkyWalking UI界面
- 吞吐量CPM,表示每分钟的调用
- Apdex分数:衡量服务器性能的标准
- 响应时间百分比,包括p99,p95,p75,p50
- SLA表示成功率。对应HTTP,表示响应为200的请求
拓扑图:用指标显示服务和实例之间的关系,点击服务显示监控数据。
跟踪查询:skywalk提供的分布式代理一样的典型特性
具体接口服务报错/响应慢的主要原因
四、Go应用如歌接入SkyWalking?
参考:https://skywalking.apache.org/zh/2023-06-01-quick-start-with-skywalking-go-agent/
五、总结
SkyWalking的主要功能和特点
- 分布式追踪:一个分布式追踪功能,可以帮助研发识别和调式分布式微服务应用中的性能问题。
- 应用性能监测:监测应用程序的性能情况,包括吞吐量、响应时间、错误率等指标
- 自定义告警:自定义告警条件和操作,以便及时发现和解决性能问题
- 多语言支持:多种变成语音。包括:Java、.net 、NodeJS、GO等
- 可扩展性:支持插件式的价格,可以方便地添加新的监测模块和数据存储引擎
标签:8.3,financial,name,acme,SpringCloud,app,agent,skywalking,8.4 From: https://www.cnblogs.com/pythonlx/p/17924600.html