在k8s中运行的springboot项目通过skywalking来作为链路追踪中间件。springboot 项目启动时结合skywalking提供的agent来实现链路追踪,没有任何代码入侵
skywalking和elasticsearch直接安装在物理机环境没有部署到k8s环境
springboot是打包成docker镜像后才通过yaml在k8s运行的,使用skywalking agent是其实就是将agent包拷贝到每一个springboot镜像,通过java -jar 命令启动springboot就完成链路追踪查看
我们直接把skywalking的agent打包成镜像,通过yaml deployment发布springboot项目时通过initContainers等完成agent拷贝
略
$tar -zxvf apache-skywalking-apm-es7-8.1.0.tar.gz
web访问端口 8080->38080 (本机8080已被其它服务占用)
$vi /home/spark/skywalking/apache-skywalking-apm-bin-es7/webapp/webapp.yml
server:
port: 38080
$/home/spark/skywalking/apache-skywalking-apm-bin-es7/config/application.yml
storage:
selector: ${SW_STORAGE:elasticsearch7}
elasticsearch7:
nameSpace: ${SW_NAMESPACE:"my-application"} #通过访问192.168.10.58:9200 即可获得就是cluster_name的值
clusterNodes: ${SW_STORAGE_ES_CLUSTER_NODES:192.168.10.58:9200}
$/usr/local/src/apache-skywalking-apm-bin-es7/bin/startup.sh
#cat “/usr/local/src/apache-skywalking-apm-bin-es7/bin/startup.sh” >> /etc/rc.d/rc.local
#chmod +x /etc/rc.d/rc.local
192.168.10.59:38080
#cd /home/spark/skywalking/apache-skywalking-apm-bin-es7
#vi Dockerfile
FROM busybox:latest
ENV LANG=C.UTF-8
RUN set -eux && mkdir -p /opt/skywalking/agent/
ADD agent/ /opt/skywalking/agent/
WORKDIR /
#docker build -t=“frame/skywalking-agent:v1” .
若报路径找不到,可新建路径复制对应文件到报错路径即可
登录harbor
#docker login 192.168.10.59:8080
打包tag
#docker tag frame/skywalking-agent:v1 192.168.10.59:8080/frame/skywalking-agent:v1
推送到私服
#docker push 192.168.10.59:8080/frame/skywalking-agent:v1
apiVersion: v1
kind: Service
metadata:
labels:
app: demo-istio
service: demo-istio
name: demo-istio
namespace: default
spec:
ports:
- name: demo-istio
port: 8070
selector:
app: demo-istio
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: demo-istio
version: v1
name: demo-istio-v1
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: demo-istio
version: v1
template:
metadata:
annotations:
prometheus.io/scrape: false
prometheus.io/path: /actuator/prometheus
prometheus.io/port: 8070
labels:
app: demo-istio
version: v1
spec:
containers:
- env:
- name: LIMITS_MEMORY
valueFrom:
resourceFieldRef:
divisor: 1Mi
resource: limits.memory
- name: JAVA_OPTS
value: -Xmx$(LIMITS_MEMORY)m -XshowSettings:vm -Duser.timezone=Asia/Shanghai
image: 192.168.10.59:8080/frame/demo-frame-istio:1.0.RELEASE
imagePullPolicy: Always
name: demo-istio
ports:
- containerPort: 8070
resources:
limits:
cpu: 2048m
memory: 2048Mi
requests:
cpu: 1024m
memory: 1024Mi
hostAliases:
- hostnames:
- www.zipkin.com
ip: 192.168.10.80
imagePullSecrets:
- name: regsecret
使用skywalking需对deployment做如下修改
和普通yaml主要区别在于deployment中:
新增了initContainers
在containers新增了volumeMounts
spec中新增了volumes:
apiVersion: v1
kind: Service
metadata:
labels:
app: demo-istio
service: demo-istio
name: demo-istio
namespace: default
spec:
ports:
- name: demo-istio
port: 8070
selector:
app: demo-istio
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: demo-istio
version: v1
name: demo-istio-v1
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: demo-istio
version: v1
template:
metadata:
annotations:
prometheus.io/scrape: false
prometheus.io/path: /actuator/prometheus
prometheus.io/port: 8070
labels:
app: demo-istio
version: v1
spec:
containers:
- env:
- name: LIMITS_MEMORY
valueFrom:
resourceFieldRef:
divisor: 1Mi
resource: limits.memory
- name: JAVA_OPTS
value: -Xmx$(LIMITS_MEMORY)m -XshowSettings:vm -Duser.timezone=Asia/Shanghai
image: 192.168.10.59:8080/frame/demo-frame-istio:1.0.RELEASE
imagePullPolicy: Always
name: demo-istio
ports:
- containerPort: 8070
resources:
limits:
cpu: 2048m
memory: 2048Mi
requests:
cpu: 1024m
memory: 1024Mi
volumeMounts:
- mountPath: /sidecar
name: sidecar
hostAliases:
- hostnames:
- www.zipkin.com
ip: 192.168.10.80
imagePullSecrets:
- name: regsecret
initContainers:
- command:
- cp
- -r
- /opt/skywalking/agent
- /sidecar
image: 192.168.10.59:8080/frame/skywalking-agent:v1
imagePullPolicy: Always
name: sidecar
volumeMounts:
- mountPath: /sidecar
name: sidecar
volumes:
- emptyDir: {}
name: sidecar
#istioctl kube-inject -f v1-IDeploy-demo-istio.yaml >> v1-IDeploy-demo-istio-istio.yaml
#kubectl apply -f v1-IDeploy-demo-istio-istio.yaml
#vi all-IGateway-demo-istio.yaml
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: demo-istio
namespace: default
spec:
selector:
istio: ingressgateway
servers:
- hosts:
- '*.demo-istio.com'
port:
name: http
number: 80
protocol: HTTP
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: demo-istio
namespace: default
spec:
gateways:
- demo-istio
hosts:
- www.demo-istio.com
http:
- retries:
attempts: 3
perTryTimeout: 3s
route:
- destination:
host: demo-istio
port:
number: 8070
subset: v1
timeout: 9s
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: demo-istio
namespace: default
spec:
host: demo-istio
subsets:
- labels:
version: v1
name: v1
trafficPolicy:
connectionPool:
http:
http1MaxPendingRequests: 1024
http2MaxRequests: 2048
maxRequestsPerConnection: 200
maxRetries: 3
tcp:
connectTimeout: 3s
maxConnections: 2048
outlierDetection:
baseEjectionTime: 3m
consecutiveErrors: 3
interval: 3s
maxEjectionPercent: 100
#kubectl apply -f all-IGateway-demo-istio.yaml
修改本机hosts加上域名映射.k8s master ip:虚拟域名
192.168.10.80 www.demo-istio.com
通过浏览器访问www.demo-istio.com即可
properties
<properties>
<!--私服ip-->
<dockerRegistryIp>192.168.10.59</dockerRegistryIp>
<!--tag 端口-->
<dockerTagPort>8080</dockerTagPort>
<!--push 端口-->
<dockerPushPort>2375</dockerPushPort>
<!--项目属于一个大的分类-->
<namespace>frame</namespace>
<!--docker images-->
<dockerImageName>${project.artifactId}:${project.version}</dockerImageName>
<!--dockerFile 中 jar名称-->
<dockerFileName>${project.artifactId}-${project.version}</dockerFileName>
<!--docker tag-->
<dockerImageTag>
${dockerRegistryIp}:${dockerTagPort}/${namespace}/${project.artifactId}:${project.version}
</dockerImageTag>
<!--docker host-->
<dockerHost>http://${dockerRegistryIp}:${dockerPushPort}</dockerHost>
<rootPath>/dockerTemp</rootPath>
<skywalkingService>192.168.10.59:11800</skywalkingService>
</properties>
build
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.1.0</version>
<executions>
<execution>
<id>build-image</id> <!–定义一个执行命令的id–>
<phase>package</phase> <!–绑定mvn的哪个命令–>
<goals>
<goal>build</goal> <!–要执行的命令 –>
</goals>
</execution>
<execution>
<id>image-tag</id>
<phase>package</phase>
<goals>
<goal>tag</goal> <!–tag命令,相当于docker的tag命令–>
</goals>
<configuration>
<image>${dockerImageName}</image> <!–镜像名–>
<newName>${dockerImageTag}</newName> <!–打的标签名–>
</configuration>
</execution>
<execution>
<id>package-push</id>
<phase>package</phase>
<goals>
<goal>push</goal> <!–相当于docker的push命令–>
</goals>
<configuration>
<imageName>${dockerImageTag}</imageName> <!–要push的镜像名–>
</configuration>
</execution>
</executions>
<configuration>
<serverId>docker-harbor</serverId>
<registryUrl>${dockerRegistryIp}:${dockerTagPort}</registryUrl>
<imageName>${dockerImageName}</imageName>
<baseImage>java</baseImage>
<maintainer>docker_maven lvmoney build</maintainer>
<workdir>${rootPath}</workdir>
<entryPoint>java -javaagent:/sidecar/agent/skywalking-agent.jar -Dskywalking.agent.service_name=${dockerFileName} -Dskywalking.collector.backend_service=${skywalkingService} -jar ${project.build.finalName}.jar</entryPoint>
<resources>
<resource>
<targetPath>${rootPath}</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
链接: link.
``