SideCar模式接入SkyWalking服务

SideCar模式接入SkyWalking服务,第1张

SideCar模式接入SkyWalking服务

微服务Kubernetes发布文件集成SkyWalking Agent实现埋点,其主要原理:通过Kubernetes的初始化容器initContainers来实现的,initContainers是一种专用容器,可以在应用容器启动之前运行,可以用于完成应用启动前的必要初始化工作。

你并没有发现为了将Java服务接入SkyWalking,你需要在Java微服务本身做任何动作,而接下来在k8s部署文件中的将演示,为什么要将这种方式称之为SideCar。

具体的Kubernetes部署文件(deploy-skywalking.yml)内容如下:

Name:                   test-base-service-r9vnm9
Namespace:              sailfish-uat
CreationTimestamp:      Tue, 06 Jul 2021 16:42:08 +0800
Labels:                 sailfish-agent=v3.0.0
                        sailfish-release=test-base-service-r9vnm9
                        sailfish-version=2021.12.14-154033-featrue-skywalking-20211214
Annotations:            deployment.kubernetes.io/revision: 244
Selector:               app=test-base-service-r9vnm9
Replicas:               1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:       app=test-base-service-r9vnm9
                sailfish-agent=v3.0.0
                sailfish-release=test-base-service-r9vnm9
                sailfish-version=2021.12.14-154033-featrue-skywalking-20211214
  #构建初始化镜像(通过初始化镜像的方式集成SkyWalking Agent)
  Init Containers:
   tools:
    Image:      registry.dockhub.com/tools/sailfishdbtool:0.0.5
    Port:       
    Host Port:  
    Command:
      sh
      -c
      cp -rf /var/sailfish/* /tools
    Environment:  
    Mounts:
      /tools from tools-jar (rw)
   init-db:
    Image:      registry.dockhub.com/devcloud/test-base-service:2021.12.14-154033-featrue-skywalking-20211214
    Port:       
    Host Port:  
    Command:
      /bin/sh
      -c
      java --Dspring.cloud.nacos.config.server-addr='https://nacos.xxxxxx.com' --Dspring.cloud.nacos.config.username='devcloud' --Dspring.cloud.nacos.config.password='07823691654a43fba94382e800fd6a70' --Dspring.cloud.nacos.config.namespace='devcloud' --Dspring.cloud.nacos.config.group='test-base-service' --Dspring.cloud.nacos.config.file-extension='properties' --Dspring.cloud.nacos.config.shared-configs[0].dataid='sailfish-shared-uat.properties' --Dspring.cloud.nacos.config.shared-configs[0].group='sailfish-configure' --Dspring.application.name=test-base-service --Dspring.profiles.active=uat --Ddata.jar=/test-base-service.jar -jar /var/sailfish/sailfish-liquibase.jar
    Environment:  
    Mounts:
      /var/sailfish from tools-jar (rw)
   sw-agent-sidecar:
    Image:      registry.dockhub.com/tools/skywalking-java-agent:8.8.0-alpine
    Port:       
    Host Port:  
    Command:
      sh
      -c
      set -ex;mkdir -p /opt/skywalking/agent;cp -r /skywalking/agent/* /opt/skywalking/agent;
    Environment:  
    Mounts:
      /opt/skywalking/agent from sw-agent (rw)
  Containers:
   test-base-service-r9vnm9:
    Image:      registry.dockhub.com/devcloud/test-base-service:2021.12.14-154033-featrue-skywalking-20211214
    Port:       8093/TCP
    Host Port:  0/TCP
    Limits:
      memory:  2Gi
    Requests:
      memory:   1Gi
    Readiness:  exec [/bin/sh -c curl -s localhost:8093/actuator/health --fail] delay=60s timeout=60s period=10s #success=1 #failure=3
    Environment:
      SPRING_CLOUD_NACOS_CONFIG_DEFAULT_EXTENSION:  sailfish-extension-uat.properties
      SPRING_CLOUD_NACOS_CONFIG_DEFAULT_GROUP:      sailfish-configure
      SPRING_CLOUD_NACOS_CONFIG_DEFAULT_SHARED:     sailfish-shared-uat.properties
      SPRING_CLOUD_NACOS_CONFIG_FILE_EXTENSION:     properties
      SPRING_CLOUD_NACOS_CONFIG_GROUP:              test-base-service
      SPRING_CLOUD_NACOS_CONFIG_NAMESPACE:          devcloud
      SPRING_CLOUD_NACOS_CONFIG_PASSWORD:           07823691654a43fba94382e800fd6a70
      SPRING_CLOUD_NACOS_CONFIG_SERVER_ADDR:        https://nacos.xxxxxx.com
      SPRING_CLOUD_NACOS_CONFIG_USERNAME:           devcloud
      SPRING_PROFILES_ACTIVE:                       uat
      
      #这里通过JAVA_TOOL_OPTIONS,而不是JAVA_OPTS可以实现不通过将agent命令加入到java应用jvm参数而实现agent的集成
      JAVA_TOOL_OPTIONS:                            -javaagent:/skywalking/agent/skywalking-agent.jar
      SW_AGENT_COLLECTOR_BACKEND_SERVICES:          skywalking-oap.skywalking.svc:11800
      SW_AGENT_NAME:                                test-base-service

    Mounts:
      /Charts from data (rw)
      /skywalking/agent from sw-agent (rw)
  Volumes:
   tools-jar:
    Type:       EmptyDir (a temporary directory that shares a pod's lifetime)
    Medium:
    SizeLimit:  
   sw-agent:
    Type:       EmptyDir (a temporary directory that shares a pod's lifetime)
    Medium:
    SizeLimit:  
   data:
    Type:       EmptyDir (a temporary directory that shares a pod's lifetime)
    Medium:
    SizeLimit:  
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  
NewReplicaSet:   test-base-service-r9vnm9-6d8ccc6f85 (1/1 replicas created)

以上是挂载sidecar的k8s发布文件,主要是通过共享volume的方式挂载agent。其中initContainers通过 sw-agent 卷挂载了 sw-agent-sidecar 镜像中的 /opt/skywalking/agent,并将上面构建好的镜像中的agent目录cp到了 /skywalking/agent 目录,完成之后微服务容器启动时也挂载了 sw-agent 卷,并将其挂载到容器的 /skywalking/agent 目录,这样就完成了共享过程。

这里有一个有意思的点,Java服务通过Agent接入SkyWalking一般情况下还需要在启动命令中加入JVM参数,例如:“-javaagent:/usr/skywalking/agent/skywalking-agent.jar”。这就需要我们在定义Java程序镜像打包的Dockerfile文件中通过“ENTRYPOINT”加入相关参数,例如:

ENTRYPOINT [ "sh", "-c", "java ${JAVA_OPTS} -javaagent:/app/agent/skywalking-agent.jar -Dskywalking.collector.backend_service=${SW_AGENT_COLLECTOR_BACKEND_SERVICES} -Dskywalking.agent.service_name=${SW_AGENT_NAME} -Dskywalking.agent.instance_name=${HOSTNAME} -Djava.security.egd=file:/dev/./urandom -jar /app/app.jar $PROFILE"

但这种方式需要在Dockerfile文件中额外设置SkyWalking Agent相关的JVM参数,所以你可能没注意到,在上述k8s部署件中我所使用的是 “JAVA_TOOL_OPTIONS” 这个参数,而不是最常见的“JAVA_OPTS”。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5673184.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存