skywalking 链路追踪监控并接入golang,php环境

  1. k8s安装skywalking
[root@master skping]# cat apm-oap-server.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: skywalking-oap-server
  name: skywalking-oap-server
  namespace: ops-share
spec:
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: skywalking-oap-server
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: skywalking-oap-server
    spec:
      containers:
        - env:
            - name: SW_STORAGE
              value: elasticsearch7
            - name: SW_STORAGE_ES_CLUSTER_NODES
              value: '192.168.14.27:9200'  #es地址
          image: 'apache/skywalking-oap-server:8.5.0-es7'
          imagePullPolicy: IfNotPresent
          securityContext:
            privileged: true
          name: skywalking-oap-server
          ports:
            - containerPort: 1234
              protocol: TCP
            - containerPort: 11800
              protocol: TCP
            - containerPort: 12800
              protocol: TCP
          resources: {}
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
---
apiVersion: v1
kind: Service
metadata:
  name: skywalking-oap-server
  namespace: ops-share
  labels:
    app: skywalking-oap-server
spec:
  type: ClusterIP
  ports:
    - name: http1
      protocol: TCP
      port: 1234
      targetPort: 1234
    - name: http2
      protocol: TCP
      port: 11800
      targetPort: 11800
    - name: http3
      protocol: TCP
      port: 12800
      targetPort: 12800
  selector:
    app: skywalking-oap-server
[root@master skping]# cat skywalking-ui.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: skywalking-ui
  name: skywalking-ui
  namespace: ops-share
spec:
  replicas: 1
  selector:
    matchLabels:
      app: skywalking-ui
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: skywalking-ui
    spec:
      containers:
        - env:
            - name: SW_OAP_ADDRESS
              value: 'skywalking-oap-server.ops-share:12800'
          image: 'apache/skywalking-ui:8.5.0'
          imagePullPolicy: IfNotPresent
          name: skywalking-ui
          ports:
            - containerPort: 8080
              name: http
              protocol: TCP
          resources: {}
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
---
apiVersion: v1
kind: Service
metadata:
  name: skywalking-ui
  namespace: ops-share
  labels:
    app: skywalking-ui
spec:
  #type: ClusterIP
  type: NodePort
  ports:
    - name: http
      protocol: TCP
      port: 8080
      targetPort: http
  selector:
    app: skywalking-ui
  1. 启动
kubectl ns create ops-share
kubectl apply -f ./
kubectl get pod,svc -n ops-share
  1. 访问ip+端口
    skywalking 链路追踪监控并接入golang,php环境_第1张图片
    skywalking 链路追踪监控并接入golang,php环境_第2张图片

  2. golang代码接入skywalking

#cat test.go
package main
import (
    "fmt"
    "github.com/SkyAPM/go2sky"
    v3 "github.com/SkyAPM/go2sky-plugins/gin/v3"
    "github.com/SkyAPM/go2sky/reporter"
    "github.com/gin-gonic/gin"

    "net/http"
    "time"
)


func main() {
    r := gin.Default()
    //skywalking 的端口需要nodeport映射出来,如果你使用的go容器,那么直接使用svc的地址也行。
    rp, err := reporter.NewGRPCReporter("192.168.14.27:11800", reporter.WithCheckInterval(time.Second))
    if err != nil{
        fmt.Println("NewGRPCReporter failed!")
        return
    }
    tracer, err := go2sky.NewTracer("demo2", go2sky.WithReporter(rp))
    if err != nil{
        fmt.Println("NewTracer failed!")
        return
    }
    //使用go2sky-plugins的middleware,就不用我们自己写span了,插件帮我们完成
    go2sky.SetGlobalTracer(tracer)
    r.Use(v3.Middleware(r, tracer))
    r.GET("/",test)

    r.Run(":7070")

}

func test(c *gin.Context)  {
    span,ctx, _ :=go2sky.GetGlobalTracer().CreateLocalSpan(c.Request.Context(),go2sky.WithOperationName("demo2-test"))
    if span!=nil{
        span.Log(time.Now(),"start demo2-test")
        defer span.End()
    }
    fmt.Println(fmt.Sprintf("traceid:%s  spaneId:%d, test 7070",go2sky.TraceID(ctx),go2sky.SpanID(ctx)))
    c.String(http.StatusOK, "test 7070")
}
#cat test2.go
package main

import (
    "context"
    "crypto/tls"
    "fmt"
    "github.com/SkyAPM/go2sky"
    v3 "github.com/SkyAPM/go2sky-plugins/gin/v3"
    "github.com/SkyAPM/go2sky/reporter"
    "github.com/gin-gonic/gin"
    "io/ioutil"
    "net/http"
    "time"
)

func main() {
    r := gin.Default()
    //skywalking 的端口需要nodeport映射出来,如果你使用的go容器,那么直接使用svc的地址也行。
    rp, err := reporter.NewGRPCReporter("192.168.14.27:11800", reporter.WithCheckInterval(time.Second))
    if err != nil {
        fmt.Println("NewGRPCReporter failed!")
        return
    }
    tracer, err := go2sky.NewTracer("demo1", go2sky.WithReporter(rp))
    if err != nil {
        fmt.Println("NewTracer failed!")
        return
    }
    //使用go2sky-plugins的middleware,就不用我们自己写span了,插件帮我们完成
    go2sky.SetGlobalTracer(tracer)
    r.Use(v3.Middleware(r, tracer))
    r.GET("/", test)

    r.Run(":8085")

}

func test(c *gin.Context) {
    span, ctx, _ := go2sky.GetGlobalTracer().CreateLocalSpan(c.Request.Context(), go2sky.WithOperationName("demo1-test"))
    if span != nil {
        defer span.End()
    }
    fmt.Println(fmt.Sprintf("traceid:%s  spaneId:%d, test 8085",go2sky.TraceID(ctx),go2sky.SpanID(ctx)))
    HttpRequest(ctx)
    c.String(http.StatusOK, "test 8085")
}

func HttpRequest(ctx context.Context) {
    url := "http://localhost:7070/"
    request, _ := http.NewRequest(http.MethodGet, url, nil)
    span, _ := go2sky.GetGlobalTracer().CreateExitSpan(ctx, "httpRequest", url, func(headerKey, headerValue string) error {
        request.Header.Set(headerKey, headerValue)
        return nil
    })
    defer span.End()
    span.Log(time.Now(), "start "+url)

    request = request.WithContext(ctx)
    client := http.Client{Transport: &http.Transport{DisableKeepAlives: true, TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}}

    response, err := client.Do(request)
    if err != nil {
        span.Error(time.Now(), fmt.Sprintf("%v", err))
    } else {
        defer response.Body.Close()
        responseByte, _ := ioutil.ReadAll(response.Body)
        span.Log(time.Now(), "response:"+string(responseByte))
        fmt.Println(fmt.Sprintf("traceid:%s  spaneId:%d, response%s", go2sky.TraceID(ctx), go2sky.SpanID(ctx), string(responseByte)))
    }
}

  1. 启动go服务
go run test.go 
go run test2.go 
  1. 访问ip+8085地址
    skywalking 链路追踪监控并接入golang,php环境_第3张图片

  2. 多请求一点数据让skywalking数据展示出来

for i in {1..100};do sleep 2; hey -n 100 -m get  http://192.168.10.100:8085 ;done

skywalking 链路追踪监控并接入golang,php环境_第4张图片
skywalking 链路追踪监控并接入golang,php环境_第5张图片
skywalking 链路追踪监控并接入golang,php环境_第6张图片

  1. php-fpm容器接入skywalking
docker pull  skyapm/skywalking-php:v4.2.0-7.2-fpm-alpine
docker run -itd  -p 8080:8080 skyapm/skywalking-php:v4.2.0-7.2-fpm-alpine bash 
docker exec -it id bash
cd /usr/local/etc/php/conf.d/
cat ext-skywalking.ini
[skywalking]
extension=skywalking.so
skywalking.app_code = hello_skywalking
skywalking.enable = 1
skywalking.version = 8
skywalking.grpc = 192.168.14.27:11800
#skywalking.error_handler_enable = 0
#skywalking.sample_n_per_3_secs = -1
#skywalking.instance_name = ""
cd /var/www/html
cat 2.php
<?php
/*
 * Copyright 2021 SkyAPM
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
$url = 'http://192.168.10.100:8085/';
echo file_get_contents($url);
?>
  1. 重启docker php-fpm容器并访问8080/2.php
    页面显示test 8085 因为访问的8085的接口

  2. 查看skywalking是否监控到了php
    skywalking 链路追踪监控并接入golang,php环境_第7张图片

skywalking 链路追踪监控并接入golang,php环境_第8张图片

你可能感兴趣的:(k8s,监控,skywalking,golang,php)