自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

在 Kubernetes 中無侵入安裝 OpenTelemetry 探針,你學(xué)會了嗎?

云計(jì)算 云原生
OpenTelemetry Operator 通過 CRD(OpenTelemetryCollector[3]、Instrumentation[4]、OpAMPBridge[5]) 實(shí)現(xiàn)在 Kubernetes 集群中自動部署和管理 OpenTelemetry Collector;在工作負(fù)載中自動安裝 OpenTelemetry 探針。

背景

OpenTelemetry 探針

OpenTelemetry(簡稱 Otel,最新的版本是 1.27) 是一個(gè)用于觀察性的開源項(xiàng)目,提供了一套工具、APIs 和 SDKs,用于收集、處理和導(dǎo)出遙測數(shù)據(jù)(如指標(biāo)、日志和追蹤信息)。應(yīng)用程序遙測數(shù)據(jù)(如追蹤、指標(biāo)和日志)的收集是通過探針來完成的,探針通常以庫的形式集成到應(yīng)用程序中,自動捕獲重要信息協(xié)助監(jiān)控和調(diào)試。OpenTelemetry 探針支持市面上大多數(shù)的編程語言,探針的安裝(通常被稱為插樁,Instrumentation)分為手動和自動兩種方式。

  • 手動插樁:指開發(fā)者直接在其應(yīng)用程序代碼中顯式地添加遙測數(shù)據(jù)收集的代碼,需要手動完成 SDK 初始化、插入追蹤點(diǎn)、添加上下文信息等一系列操作。
  • 自動插樁:利用 OpenTelemetry 提供的庫自動捕獲應(yīng)用程序的遙測數(shù)據(jù),無需或只需很少的代碼更改。比如,Java 通過 `javaagent` 實(shí)現(xiàn)探針的自動安裝[1]。

二者各有優(yōu)劣:手動插樁適用于需要高度定制和精確控制遙測數(shù)據(jù)收集的場景;自動插樁適合快速啟動和簡化集成,特別是在使用標(biāo)準(zhǔn)框架和庫的應(yīng)用程序中。

OpenTelemetry Operator 介紹

OpenTelemetry Operator[2] 是一個(gè)為了簡化 OpenTelemetry 組件在 Kubernetes 環(huán)境中的部署和管理而設(shè)計(jì)的 Kubernetes Operator。

OpenTelemetry Operator 通過 CRD(OpenTelemetryCollector[3]、Instrumentation[4]、OpAMPBridge[5]) 實(shí)現(xiàn)在 Kubernetes 集群中自動部署和管理 OpenTelemetry Collector;在工作負(fù)載中自動安裝 OpenTelemetry 探針。

今天我們就將體驗(yàn)如何使用 OpenTelemetry Operator 自動安裝探針,實(shí)現(xiàn)鏈路跟蹤。

演示

架構(gòu)

這是演示的架構(gòu),Otel 提供了 多種語言的 instrumentation SDK[6],這篇文章中我們將使用 Java 和 Go 兩種語言的應(yīng)用。這兩種語言會使用全自動和半自動的注入安裝:

  • Java 全自動注入安裝,Otel Operator 通過使用 init container 引入 sdk ,并通過 JAVA_TOOL_OPTIONS 來指定 javaagent 來插樁。這里將使用 pinakispecial/spring-boot-rest 鏡像來運(yùn)行一個(gè)簡單的 Spring Boot REST 服務(wù)。
  • Go 半自動注入安裝,為什么是半自動?Go 的全自動是通過 eBPF 的方式實(shí)現(xiàn)的:在 Pod 注入獨(dú)立的容器,加載 BPF 程序。但是 eBPF 的實(shí)現(xiàn)對內(nèi)核要求十分苛刻 5.4 - 5.14。這里演示半自動的方式:手動引入 Go instrumentation SDK[7],自動注入配置[8]。

圖片圖片

Jaeger

為了便于演示這里使用 jaegertracing/all-in-one 鏡像來部署 Jaeger,這個(gè)鏡像包含了 Jaeger 收集器、內(nèi)存存儲、查詢服務(wù)和 UI 等組件,非常適合開發(fā)和測試使用。

通過環(huán)境變量 COLLECTOR_OTLP_ENABLED 啟動對 OTLP(OpenTelemetry Protocol)[9] 的支持,OTEL 在 8。

kubectl apply -f - <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
  name: jaeger
spec:
  replicas: 1
  selector:
    matchLabels:
      app: jaeger
  template:
    metadata:
      labels:
        app: jaeger
    spec:
      containers:
      - name: jaeger
        image: jaegertracing/all-in-one:latest
        env:
        - name: COLLECTOR_OTLP_ENABLED
          value: "true"
        ports:
        - containerPort: 16686
        - containerPort: 14268
---
apiVersion: v1
kind: Service
metadata:
  name: jaeger
spec:
  selector:
    app: jaeger
  type: ClusterIP
  ports:
    - name: ui
      port: 16686
      targetPort: 16686
    - name: collector
      port: 14268
      targetPort: 14268
    - name: http
      protocol: TCP
      port: 4318
      targetPort: 4318
    - name: grpc
      protocol: TCP
      port: 4317
      targetPort: 4317      
EOF

安裝 cert-manager

Otel Operator 依賴 cert-manager 進(jìn)行證書的管理,安裝 operator 之前需要安裝 cert-manager。

kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.13.2/cert-manager.yaml

安裝 OpenTelemetry Operator

執(zhí)行下面命令安裝 Otel Operator

kubectl apply -f https://github.com/open-telemetry/opentelemetry-operator/releases/latest/download/opentelemetry-operator.yaml

配置 OpenTelemetry Collector

通過創(chuàng)建 CR OpenTelemetryCollector 來配置 Otel 的采集器,這里我們配置了:

  • otel 接收器:支持 grpc(端口 4317)和 http(端口 4318)
  • memory_limiter 和 batch 處理器,但是為了方便快速查看數(shù)據(jù),這兩個(gè)并沒有啟用,僅作展示用。
  • debug 和 otlp/jaeger 的輸出器,分別用于在標(biāo)準(zhǔn)輸出中打印信息和使用 otlp 協(xié)議輸出到 Jaeger。
  • pipeline 服務(wù),用于配置跟蹤數(shù)據(jù)的處理流程:接收、處理和輸出。
kubectl apply -f - <<EOF
apiVersion: opentelemetry.io/v1alpha1
kind: OpenTelemetryCollector
metadata:
  name: otel
spec:
  config: |
    receivers:
      otlp:
        protocols:
          grpc:
          http:
    processors:
      memory_limiter:
        check_interval: 1s
        limit_percentage: 75
        spike_limit_percentage: 15
      batch:
        send_batch_size: 10000
        timeout: 10s

    exporters:
      debug:
      otlp/jaeger:
        endpoint: "jaeger.default:4317"
        tls:
          insecure: true

    service:
      pipelines:
        traces:
          receivers: [otlp]
          processors: []
          exporters: [debug,otlp/jaeger]
EOF

創(chuàng)建 CR OpenTelemetryCollector 后,Otel Operator 會創(chuàng)建一個(gè) deployment 和 多個(gè) service。

kubectl get deployment,service -l app.kubernetes.io/compnotallow=opentelemetry-collector
NAME                             READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/otel-collector   1/1     1            1           12h

NAME                                TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                               AGE
service/otel-collector              ClusterIP   10.43.152.81    <none>        4317/TCP,4318/TCP,8889/TCP,9411/TCP   12h
service/otel-collector-headless     ClusterIP   None            <none>        4317/TCP,4318/TCP,8889/TCP,9411/TCP   12h
service/otel-collector-monitoring   ClusterIP   10.43.115.103   <none>        8888/TCP                              12h

Collector 部署的四種部署模型[10] Deployment、DaemonSet、StatefulSet、Sidecar,默認(rèn)為 Deployment。

配置 Instrumentation

Instrumentation 是 Otel Operator 的另一個(gè) CRD,用于自動安裝 Otel 探針和配置:

  • propagators 用于配置跟蹤信息在上下文的傳遞方式。
  • sampler 采樣器
  • env 和 [language].env 添加到容器的環(huán)境變量

更多配置說明,請參考 Instrumentation API 文檔[11]。

kubectl apply -f - <<EOF
apiVersion: opentelemetry.io/v1alpha1
kind: Instrumentation
metadata:
  name: instrumentation-sample
spec:
  propagators:
    - tracecontext
    - baggage
    - b3
  sampler:
    type: parentbased_traceidratio
    argument: "1"
  env:
    - name: OTEL_EXPORTER_OTLP_ENDPOINT
      value: otel-collector.default:4318
  java:    
    env:
      - name: OTEL_EXPORTER_OTLP_ENDPOINT
        value: http://otel-collector.default:4317   
EOF

Java 示例應(yīng)用

為 Pod 添加注解 instrumentation.opentelemetry.io/inject-java: "true" 通知 Otel Operator 該應(yīng)用的類型以便注入正確的探針。

kubectl apply -f - <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
  name: java-sample
spec:
  replicas: 1
  selector:
    matchLabels:
      app: java-sample
  template:
    metadata:
      labels:
        app: java-sample
      annotations:
        instrumentation.opentelemetry.io/inject-java: "true"
    spec:
      containers:
      - name: java-sample
        image: pinakispecial/spring-boot-rest
        ports:
        - containerPort: 8080
EOF

可以看到 Otel Operator 向 Pod 中注入了一個(gè) otel 的初始化容器。

圖片圖片

以及在 java 容器中注入了一系列的環(huán)境變量進(jìn)行配置。

圖片圖片

Go 示例應(yīng)用

前面提到 Go 語言的自動注入演示使用半自動的方式,與本文的標(biāo)題不符,屬于嵌入式的。我寫了一個(gè) 簡單的 Go 應(yīng)用[12],使用手動的方式來安裝 Otel 探針,有興趣的可以查看源碼。

kubectl apply -f https://raw.githubusercontent.com/addozhang/http-sample/main/manifests/service-v1.yaml

查看 Pod 同樣可以看到通過環(huán)境變量的方式注入的 Otel 配置。

測試

pod_name="$(kubectl get pod -n default -l app=service-a -o jsnotallow='{.items[0].metadata.name}')"
kubectl port-forward $pod_name 8080:8080 &

curl localhost:8080
service-a(version: v1, ip: 10.42.0.68, hostname: service-a-5bf98748f5-l9pjw) -> service-b(version: v1, ip: 10.42.0.70, hostname: service-b-676c56fb98-rjbwv) -> service-c(version: v1, ip: 10.42.0.69, hostname: service-c-79985dc75d-bh68k)

打開 Jaeger UI。

jaeger_pod="$(kubectl get pod -l app=jaeger -o jsnotallow='{.items[0].metadata.name}')"
kubectl port-forward $jaeger_pod 16686:16686 &

Bingo!

訪問 Jaeger UI 就可以看到這個(gè)訪問的鏈路信息了。

圖片圖片

參考資料

[1] Java 通過 javaagent 實(shí)現(xiàn)探針的自動安裝: https://opentelemetry.io/docs/instrumentation/java/automatic/

[2] OpenTelemetry Operator: https://opentelemetry.io/docs/kubernetes/operator/

[3] OpenTelemetryCollector: https://github.com/open-telemetry/opentelemetry-operator/blob/main/docs/api.md#opentelemetrycollector

[4] Instrumentation: https://github.com/open-telemetry/opentelemetry-operator/blob/main/docs/api.md#instrumentation

[5] OpAMPBridge: https://github.com/open-telemetry/opentelemetry-operator/blob/main/docs/api.md#opampbridge

[6] 多種語言的 instrumentation SDK: https://opentelemetry.io/docs/instrumentation/

[7] 手動引入 Go instrumentation SDK: https://github.com/addozhang/http-sample/blob/main/otel.go

[8] 自動注入配置: https://github.com/open-telemetry/opentelemetry-operator/blob/main/README.md#opentelemetry-auto-instrumentation-injection

[9] OTLP(OpenTelemetry Protocol): https://opentelemetry.io/docs/specs/otlp/

[10] Collector 部署的四種部署模型: https://github.com/open-telemetry/opentelemetry-operator#deployment-modes

[11] Instrumentation API 文檔: https://github.com/open-telemetry/opentelemetry-operator/blob/main/docs/api.md#instrumentation

[12] 簡單的 Go 應(yīng)用: https://github.com/addozhang/http-sample

責(zé)任編輯:武曉燕 來源: 云原生指北
相關(guān)推薦

2024-07-29 10:35:44

KubernetesCSI存儲

2022-07-26 08:03:27

Kubernetes節(jié)點(diǎn)磁盤

2023-06-02 08:04:38

對象存儲數(shù)據(jù)

2024-01-30 18:29:29

微服務(wù)架構(gòu)Ingress

2024-10-16 11:28:42

2025-01-26 15:31:27

2023-01-10 08:43:15

定義DDD架構(gòu)

2024-02-04 00:00:00

Effect數(shù)據(jù)組件

2023-07-26 13:11:21

ChatGPT平臺工具

2024-01-19 08:25:38

死鎖Java通信

2024-01-02 12:05:26

Java并發(fā)編程

2023-08-01 12:51:18

WebGPT機(jī)器學(xué)習(xí)模型

2022-11-21 16:57:20

2024-01-26 06:05:16

KuberneteseBPF網(wǎng)絡(luò)

2021-06-05 06:52:16

Kubernetes

2024-01-05 07:46:15

JS克隆對象JSON

2023-12-26 10:12:19

虛擬DOM數(shù)據(jù)

2023-09-06 11:31:24

MERGE用法SQL

2023-10-13 09:04:09

2024-05-06 00:00:00

InnoDBView隔離
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號