使用istio结合skywalking进行链路追踪

2021-06-25 21:40:23 浏览数 (1)

在最新的istio代码里实现了Telemetry API,引入了Telemetry CRD,从而可以更友好的配置istio Telemetry

构建istio

在目前代码里根据我的测试有些问题,skywalking进行trace需要设置StartChildSpan: true,

修改pilot/pkg/xds/filters/filters.go代码

代码语言:javascript复制
    Router = &hcm.HttpFilter{        Name: wellknown.Router,        ConfigType: &hcm.HttpFilter_TypedConfig{            TypedConfig: util.MessageToAny(&router.Router{                StartChildSpan: true,            }),        },    }

拉取istio代码,构建最新版本istiod 镜像

代码语言:javascript复制
git clone https://github.com/istio/istiocd istiomake docker

配置安装istio

获取default profile的配置

代码语言:javascript复制
istioctl profile dump default > config.yaml

在meshconfig中配置

代码语言:javascript复制
    extensionProviders:    - skywalking:        service: skywalking-oap.skywalking.svc.cluster.local        port: 11800      name: envoy.tracers.skywalking

Telemetry

创建Telemetry配置 引用extensionProviders配置

代码语言:javascript复制
apiVersion: telemetry.istio.io/v1alpha1kind: Telemetrymetadata:  name: mesh-default  namespace: istio-systemspec:  tracing:  - customTags:      my_new_foo_tag:        literal:          value: foo    providers:    - name: envoy.tracers.skywalking    randomSamplingPercentage: 100

编写测试程序

代码语言:javascript复制
package mainimport (    "io/ioutil"    "log"    "net/http"    "os")var target string = ""func main() {    log.SetFlags(log.Lshortfile)    if len(os.Args) > 1 {        target = os.Args[1]    }    http.HandleFunc("/", gethandler)    err := http.ListenAndServe(":9002", nil)    if err != nil {        log.Println(err)    }}var headersToCopy = []string{    "sw8",}func gethandler(w http.ResponseWriter, r *http.Request) {    client := &http.Client{}    if target != "" {        req, err := http.NewRequest("GET", target, nil)        if err!=nil {            return        }        for _, h := range headersToCopy {            val := r.Header.Get(h)            if val != "" {                req.Header.Set(h, val)            }        }        resp, err := client.Do(req)        if err != nil {            w.Write(nil)            return        }        result, err := ioutil.ReadAll(resp.Body)        if err != nil {            w.Write(nil)            return        }        w.Write([]byte(result))    }else {        w.Write([]byte("hello world"))    }}

创建三个deployment,串行调用

代码语言:javascript复制
apiVersion: apps/v1kind: Deploymentmetadata:  name: demo  namespace: defaultspec:  selector:    matchLabels:      app: demo  template:    metadata:      labels:        app: demo    spec:      containers:        - args:            - http://demo1:9002          image: xxx/library/swdemo:v1          imagePullPolicy: Always          name: demo---apiVersion: apps/v1kind: Deploymentmetadata:  name: demo1  namespace: defaultspec:  selector:    matchLabels:      app: demo1  template:    metadata:      labels:        app: demo1    spec:      containers:        - args:            - http://demo2:9002          image: xxx/library/swdemo:v1          imagePullPolicy: Always          name: demo1---apiVersion: apps/v1kind: Deploymentmetadata:  name: demo2  namespace: defaultspec:  selector:    matchLabels:      app: demo2  template:    metadata:      labels:        app: demo2    spec:      containers:        - image: xxx/library/swdemo:v1          imagePullPolicy: Always          name: demo2

创建svc

代码语言:javascript复制
kubectl expose deploy demo --port=9002 --target-port=9002kubectl expose deploy demo1 --port=9002 --target-port=9002kubectl expose deploy demo2 --port=9002 --target-port=9002

效果

通过gateway调用demo,查看skywalking ui

调用链路

0 人点赞