skywalking 实现收集kubernetes环境dubbo微服务链路跟踪案例

 

  将zookeeper和dubbo部署在k8s容器,并使用skywalking收集

 

  部署zookeeper

  创建自定义namespace

  并验证实现创建好的zookeeper

root@master1:~# kubectl create ns test

root@master1:~/dockerfile/skywalking/java_agent/dubbo-provider# kubectl get pods
NAME                          READY   STATUS    RESTARTS   AGE
zookeeper1-654c4f44c-rs5bc    1/1     Running   0          11m
zookeeper2-645fdbd686-q995t   1/1     Running   0          11m
zookeeper3-b879d8d8f-f2cgk    1/1     Running   0          11m

 

  构建dubbo容器

  1、构建dubbo-provider

  修改dubbo-provider的配置文件指定连接k8s的test ns下的zookeeper集群

root@master1:~/dockerfile/skywalking/java_agent/dubbo-provider# vim config.properties
#dubbo.registry=zookeeper://zookeeper1.myserver.svc.cluster.local:2181?backup=zookeeper2.myserver.svc.cluster.local:2181,zookeeper3.myserver.svc.cluster.loca
l:2181
dubbo.registry=zookeeper://zookeeper1.test.svc.cluster.local:2181?backup=zookeeper2.test.svc.cluster.local:2181,zookeeper3.test.svc.cluster.local:2181
dubbo.port=20880

 

  创建启动脚本

root@master1:~/dockerfile/skywalking/dubbo-provider# cat run.sh 
#!/bin/bash
cd /usr/local/dubbo/provider/
java -javaagent:/usr/local/skywalking-agent/skywalking-agent.jar -jar dubbo-server.jar

 

  配置skywalking java agent

  编辑skywalking java agent的配置文件

root@master1:~/dockerfile/skywalking/java_agent/dubbo-provider/skywalking-agent/config# vim agent.config

# The agent namespace
agent.namespace=${SW_AGENT_NAMESPACE:kubernetes}  #自定义namespace

# The service name in UI
agent.service_name=${SW_AGENT_NAME:dubbo-provider}  #指定agent名称

# Backend service addresses.
collector.backend_service=${SW_AGENT_COLLECTOR_BACKEND_SERVICES:192.168.100.90:11800}    #指定外部的skywalking server地址

 

  打包skywalking java agent

root@master1:~/dockerfile/skywalking/java_agent# tar zcf skywalking-agent.tar.gz skywalking-agent

 

  编辑Dockerfile

root@master1:~/dockerfile/skywalking/java_agent/dubbo-provider# cat Dockerfile

#Dubbo provider
FROM harbor.cncf.net/baseimages/jdk:1.8.191

RUN apt install file netcat-traditional -y
RUN mkdir -p /usr/local/dubbo/provider

ADD dubbo-server.jar  /usr/local/dubbo/provider
ADD skywalking-agent.tar.gz /usr/local/
ADD run.sh /usr/local/dubbo/provider/
RUN chmod a+x /usr/local/dubbo/provider/run.sh

CMD ["/usr/local/dubbo/provider/run.sh"]

 

  创建打包image命令

root@master1:~/dockerfile/skywalking/dubbo-client# cat build.sh 
#!/bin/bash
TAG=$1
nerdctl build -t harbor.cncf.net/project/dubbo-skywalking-provider:${TAG} . 
nerdctl push harbor.cncf.net/project/dubbo-skywalking-provider:${TAG}

 

  2、构建dubbo-consumer

  编辑配置dubbo-consumer的配置文件连接k8s test ns 下的zookeeper

root@master1:~/dockerfile/skywalking/dubbo-client# vim config.properties
#dubbo.registry=zookeeper://zookeeper1.myserver.svc.cluster.local:2181?backup=zookeeper2.myserver.svc.cluster.local:2181,zookeeper3.myserver.svc.cluster.loca
l:2181
dubbo.registry=zookeeper://zookeeper1.test.svc.cluster.local:2181?backup=zookeeper2.test.svc.cluster.local:2181,zookeeper3.test.svc.cluster.local:2181

 

  创建启动脚本

root@master1:~/dockerfile/skywalking/dubbo-client# cat run.sh 
#!/bin/bash
cd /usr/local/dubbo/client/
java -javaagent:/usr/local/skywalking-agent/skywalking-agent.jar -jar dubbo-client.jar

 

  配置skywalking java agent

  编辑skywalking java agent的配置文件

root@master1:~/dockerfile/skywalking/dubbo-client# vim skywalking-agent/config/agent.config

# The agent namespace
agent.namespace=${SW_AGENT_NAMESPACE:kubernetes}  #自定义namespace

# The service name in UI
agent.service_name=${SW_AGENT_NAME:dubbo-client}  #指定agent名称

# Backend service addresses.
collector.backend_service=${SW_AGENT_COLLECTOR_BACKEND_SERVICES:192.168.100.90:11800}    #指定外部的skywalking server地址

 

  打包skywalking java agent

root@master1:~/dockerfile/skywalking/dubbo-client# tar zcf skywalking-agent.tar.gz skywalking-agent/

 

  编辑Dockerfile

root@master1:~/dockerfile/skywalking/dubbo-client# cat Dockerfile 
#Dubbo provider
FROM harbor.cncf.net/baseimages/jdk:1.8.191

RUN apt install file netcat-traditional -y
RUN mkdir -p /usr/local/dubbo/client

ADD dubbo-client.jar  /usr/local/dubbo/client
ADD skywalking-agent.tar.gz /usr/local/
ADD run.sh /usr/local/dubbo/client/
RUN chmod a+x /usr/local/dubbo/client/run.sh

CMD ["/usr/local/dubbo/client/run.sh"]

 

  创建打包image命令

root@master1:~/dockerfile/skywalking/dubbo-client# cat build.sh 
#!/bin/bash
TAG=$1
nerdctl build -t harbor.cncf.net/project/dubbo-skywalking-client:${TAG} . 
nerdctl push harbor.cncf.net/project/dubbo-skywalking-client:${TAG}

 

  3、构建dubbo-admin管理界面容器

  创建tomcat容器启动脚本

root@deploy:/dockerfile/project/dubbo/dubbo-admin# vim run_tomcat.sh
#!/bin/bash

/usr/local/tomcat/bin/catalina.sh start
tail -f /usr/local/tomcat/logs/catalina.out

 

  修改dubbo-admin配置文件

  修改zookeeper pod服务地址,root和guest用户密码

root@deploy:/dockerfile/project/dubbo/dubbo-admin# unzip dubboadmin.war
root@deploy:/dockerfile/project/dubbo/dubbo-admin# vim dubboadmin/WEB-INF/dubbo.properties
dubbo.registry.address=zookeeper://zookeeper1.test.svc.cluster.local:2181
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest

 

  创建dubbo-admin Dockerfile

root@deploy:/dockerfile/project/dubbo/dubbo-admin# cat Dockerfile 
FROM harbor.cncf.net/web/tomcat:8.5.79

RUN apt install unzip -y  
ADD run_tomcat.sh /usr/local/tomcat/bin/run_tomcat.sh
ADD dubboadmin  /usr/local/tomcat/webapps/dubboadmin 

EXPOSE 8080 8443

CMD ["/usr/local/tomcat/bin/run_tomcat.sh"]

 

  创建镜像构建脚本

root@master1:~/dockerfile/skywalking/dubbo-admin# cat build.sh 
#!/bin/bash
TAG=$1
nerdctl build -t harbor.cncf.net/project/dubboadmin:${TAG}  .
nerdctl push  harbor.cncf.net/project/dubboadmin:${TAG}

 

  k8s创建资源

  1、创建dubbo-provider

  创建deployment和service

root@master1:~/yaml/skywalking-agent/dubbo-provider# cat provider.yaml 
kind: Deployment
apiVersion: apps/v1
metadata:
  labels:
    app: dubbo-provider
  name: dubbo-provider-deployment
  namespace: test
spec:
  replicas: 3
  selector:
    matchLabels:
      app: dubbo-provider
  template:
    metadata:
      labels:
        app: dubbo-provider
    spec:
      containers:
      - name: dubbo-provider-container
        image: harbor.cncf.net/project/dubbo-skywalking-provider:1.3 
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 8080
          protocol: TCP
          name: http

---
kind: Service
apiVersion: v1
metadata:
  labels:
    app: dubbo-provider
  name: dubbo-provider-spec
  namespace: test
spec:
  type: NodePort
  ports:
  - name: http
    port: 8080
    protocol: TCP
    targetPort: 8080
    nodePort: 38800
  selector:
    app: dubbo-provider

 

  创建验证

 

  查看provider容器日志

root@master1:~/yaml/skywalking-agent/dubbo-client# kubectl logs -f dubbo-provider-deployment-7496c6fb7b-9z27p

 

  zookeeper客户端验证

 

  登录skywalking界面验证

 

  2、创建dubbo-client

  创建deployment和service

root@master1:~/yaml/skywalking-agent/dubbo-client# cat consumer.yaml 
kind: Deployment
apiVersion: apps/v1
metadata:
  labels:
    app: dubbo-consumer
  name: dubbo-consumer-deployment
  namespace: test
spec:
  replicas: 1
  selector:
    matchLabels:
      app: dubbo-consumer
  template:
    metadata:
      labels:
        app: dubbo-consumer
    spec:
      imagePullSecrets:
      containers:
      - name: dubbo-consumer-container
        image: harbor.cncf.net/project/dubbo-skywalking-client:1.0 
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 8080
          protocol: TCP
          name: http
---
kind: Service
apiVersion: v1
metadata:
  labels:
    app: dubbo-consumer
  name: dubbo-consumer-server
  namespace: test
spec:
  type: NodePort
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: 8080
    nodePort: 30001
  selector:
    app: dubbo-consumer

 

   验证创建

 

   查看client容器运行日志

root@master1:~/yaml/skywalking-agent/dubbo-client# kubectl logs -f dubbo-consumer-deployment-766748d47-2sgln

 

  登录zookeeper客户端验证

  登录consumer测试页面,访问k8s nodeport请求测试——http://k8s_node_IP:30001/hello?name=k8s

 

  登录skywalking界面验证

 

 

  3、创建dubbo-admin资源

  创建dubbo-admin k8s yaml资源文件

kind: Deployment
apiVersion: apps/v1
metadata:
  labels:
    app: dubboadmin
  name: dubboadmin-deployment
  namespace: test
spec:
  replicas: 1
  selector:
    matchLabels:
      app: dubboadmin
  template:
    metadata:
      labels:
        app: dubboadmin
    spec:
      containers:
      - name: dubboadmin-container
        image: harbor.cncf.net/project/dubboadmin:1.0.5
        imagePullPolicy: Always
        ports:
        - containerPort: 8080
          protocol: TCP
          name: http

---
kind: Service
apiVersion: v1
metadata:
  labels:
    app: dubboadmin
  name: dubboadmin-service
  namespace: test
spec:
  type: NodePort
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: 8080
    nodePort: 30080
  selector:
    app: dubboadmin

 

  验证创建dubboadmin svc和pods:

 

  浏览器访问:k8s任意节点http://ip:30080/dubboadmin

  用户名密码输入:root/root

 

  进入首页

 

  查看provider

 

posted @ 2023-03-09 16:06  PunchLinux  阅读(190)  评论(0编辑  收藏  举报