-javaagent:/usr/local/skywalking/apache-skywalking-apm-bin/agent/skywalking-agent/skywalking-agent.jar
-javaagent
指向挂载目录的agent包即可hostPath
的方式hostPath的方式挂载的目录生命周期和pod相同。如果容器挂了,目录依然存在
daemonset
的方式部署agent使用daemonset会保证每个node节点都会将agent文件挂载到对应的主机目录上
准备好java agent压缩包:https://dlcdn.apache.org/skywalking/java-agent/8.14.0/apache-skywalking-java-agent-8.14.0.tgz
新建一个Dockerfile文件,内容如下:
# 虽然第一句话看上去没用,但是必须要继承一个基础镜像
# 否则执行docker build 会报错
FROM livingobjects/jre8
RUN mkdir -p /data/paasops/sk/agent
# 复制压缩包并解压
ADD apache-skywalking-java-agent-8.14.0.tgz /data/paasops/sk/agent
# 准备当作挂载目录
RUN mkdir -p /data/paasops/sk/mount/agent
如果没有继承基础镜像,报错参考:
https://blog.csdn.net/lyc0424/article/details/103096955
docker build -t sk-agent .
-> 制作一个名为sk-agent的镜像
按照自己需求将镜像推送到对应仓库
新建一个daemonset.yml文件,内容如下:
apiVersion: apps/v1
kind: DaemonSet # 类型
metadata:
name: agent-daemonset #daemonset名字
namespace: kube-tracing # 指定命名空间
spec:
selector:
matchLabels:
app: agent-daemon-tag # 标签
template:
metadata:
labels:
app: agent-daemon-tag
spec:
containers:
- name: agent-c
image: 192.168.116.xx:18080/monitor-test/sk-agent:8.14 # agent镜像位置
imagePullPolicy: Always
# 执行多条命令
#1.将agent包拷贝到挂载目录
# 2. 执行一个死循环命令,让容器内部有常驻进程,防止容器退出
command: [ "/bin/bash", "-c", "--" ]
args: [ "cp -r /data/paasops/sk/agent/* /data/paasops/sk/mount/agent;while true; do sleep 30; done;" ]
volumeMounts:
- name: hostpath-agent-volume # 对应下边的挂载定义 name字段内容
mountPath: /data/paasops/sk/mount/agent # 容器的挂载目录
volumes: # 挂载方式定义
- name: hostpath-agent-volume
hostPath: # 挂载类型
path: /data/skywalking/agent # 主机的挂载目录
type: DirectoryOrCreate
注意:
/data/paasops/sk/mount/agent
目录挂载到主机的/data/skywalking/agent
; 如果挂载成功,主机目录
的内容会覆盖
容器目录内容。也就是说如果进入容器的/data/paasops/sk/mount/agent
目录,看到的内容是和主机/data/skywalking/agent
目录的内容相同
,并非
容器原本
目录的内容kubectl apply -f daemontset.yml
--> 执行daemontset配置文件
kubectl get daemonset -n kube-tracing
--> 查看kube-tacing命名空间的daemonset
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
agent-daemonset 5 5 5 5 5 73m
kubectl get pod -n kube-tracing
–> 查看pod的运行情况,看daemonset是否创建pod成功
NAME READY STATUS RESTARTS AGE
agent-daemonset-6hftx 1/1 Running 0 73m
agent-daemonset-8bh6f 1/1 Running 0 73m
agent-daemonset-gft2d 1/1 Running 0 73m
agent-daemonset-x4hhm 1/1 Running 0 73m
agent-daemonset-x5gvk 1/1 Running 0 73m
因为我有5个nodes,每个node对应创建了一个pod,所以共有5个pod,且状态都为RUNNING。表示daemonset成功
连接到node节点服务器上,进入/data/skywalking/agent
目录,查看是否有agent包相关内容,如果有,表示挂载成功.
FROM livingobjects/jre8
COPY lab-39-demo-2.2.2.RELEASE.jar lab-39-demo-2.2.2.RELEASE.jar
RUN mkdir -p /data/paasops/sk/agent
ENV AGENTPATH=/data/skywalking/agent/skywalking-agent/skywalking-agent.jar
EXPOSE 8079
ENTRYPOINT ["java","-javaagent:/data/paasops/sk/agent/skywalking-agent/skywalking-agent.jar","-jar","lab-39-demo-2.2.2.RELEASE.jar"]
docker build
命令然后再将镜像推送到仓库apiVersion: apps/v1
kind: Deployment
metadata:
name: demo-deployment
namespace: kube-tracing
labels:
app: demo-tag
spec:
selector:
matchLabels:
app: demo-tag
template:
metadata:
labels:
app: demo-tag
spec:
containers:
- name: demotest
image: 192.168.116.126:18080/skywalking/demotest:latest
imagePullPolicy: Always
ports:
- containerPort: 8079
env:
- name: SW_AGENT_COLLECTOR_BACKEND_SERVICES
value: 192.168.116.130:30078
- name: SW_AGENT_NAME
value: test-demo
volumeMounts:
- name: hostpath-agent-volume
mountPath: /data/paasops/sk/agent # 容器挂载路径
volumes: # 挂载方式
- name: hostpath-agent-volume
hostPath:
path: /data/skywalking/agent # 主机目录和agent容器配置的主机挂载目录相同
type: DirectoryOrCreate
---
apiVersion: v1
kind: Service
metadata:
name: demo-service
namespace: kube-tracing
labels:
service: demo
spec:
type: NodePort
selector:
app: demo-tag
ports:
- port: 8079
name: rest
nodePort: 30084
kubectl apply -f deployment.yml
-> 创建deployment
kubectl get pod -n kube-tracing
-> 查看pod, 状态Running,正常
5. kubectl exec -it demo-deployment-75c896b5c6-cdn4d -n kube-tracing /bin/bash
-> 进入容器内部
6. cd /data/paasops/sk/agent/
-> 进入容器的挂载目录,查看和主机挂载目录内容是否相同,相同则表示使用agent容器挂载出来的目录内容成功