dotnet-monitor可以在Kubernetes中作为Sidecar运行,Sidecar是一个容器,它与应用程序在同一个Pod中运行,利用Sidecar模式使我们可以诊断及监控应用程序。

如下图所示,这是我们最终要实现的目标,通过可视化界面查看应用程序的指标信息。

应用服务

创建dotnetmonitor.yaml文件,如下所示。

apiVersion: apps/v1
kind: Deployment
metadata:
name: dotnet-monitor-example
spec:
replicas: 3
selector:
matchLabels:
app: dotnet-monitor-example
template:
metadata:
annotations:
prometheus.io/scrape: 'true'
prometheus.io/port: "52325"
labels:
app: dotnet-monitor-example
spec:
containers:
- name: server
image: mcr.microsoft.com/dotnet/core/samples:aspnetapp
ports:
- containerPort: 80
volumeMounts:
- mountPath: /tmp
name: tmp
- name: sidecar
image: mcr.microsoft.com/dotnet/monitor
ports:
- containerPort: 52323
resources:
requests:
cpu: 50m
memory: 32Mi
limits:
cpu: 250m
memory: 256Mi
args: ["--no-auth"]
env:
- name: DOTNETMONITOR_Urls
value: "http://+:52323"
volumeMounts:
- name: tmp
mountPath: /tmp
volumes:
- name: tmp
emptyDir: {}

Sidecar和应用程序共享tmp目录,同时将目录映射到emptyDir类型的 Volume中。接下来,创建dotnetmonitor-service.yaml,为应用程序和Sidecar开放端口。

apiVersion: v1
kind: Service
metadata:
name: dotnetmonitor
labels:
app: dotnetmonitor
spec:
type: NodePort
ports:
- name: sidecar
protocol: TCP
port: 52323
nodePort: 31623
- name: app
protocol: TCP
port: 80
nodePort: 31624
selector:
app: dotnet-monitor-example

Prometheus配置

创建prometheus-config.yaml文件,通过ConfigMaps管理Prometheus的配置文件,并写入如下内容。

apiVersion: v1
kind: ConfigMap
metadata:
name: prometheus-config
data:
prometheus.yaml: |
global:
scrape_interval: 2s
evaluation_interval: 2s
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: default/dotnet-monitor-example/0
honor_timestamps: true
scrape_interval: 10s
scrape_timeout: 10s
metrics_path: /metrics
scheme: http
follow_redirects: true
relabel_configs:
# 使用 Label "__meta_kubernetes_pod_container_name" 的值
- source_labels: [__meta_kubernetes_pod_container_name]
separator: ;
# 正则表达式,用于匹配源标签值使用的
regex: sidecar
# replacement指定的替换后的标签(target_label)对应的数值
replacement: $1
# keep就是保留符合正则表达式targets,并显示出来
action: keep
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
action: keep
regex: true
- source_labels: [__meta_kubernetes_pod_name]
action: replace
target_label: pod
kubernetes_sd_configs:
- role: endpoints
follow_redirects: true
namespaces:
names:
- default

在Prometheus中如果采用静态服务发现(static_configs)模式注册,那么HPA(HorizontalPodAutoscaler,Pod水平自动伸缩)的变动会导致服务很难快速的注册,如果频繁更改配置文件,那么也是得不偿失的,所以,在此处选择kubernetes服务发现(kubernetes_sd_configs)模式,除此之外Prometheus还支持其他方式的服务发现。

  • static_configs: 静态服务发现
  • dns_sd_configs: DNS 服务发现
  • file_sd_configs: 文件服务发现
  • kubernetes_sd_configs: Kubernetes 服务发现
  • gce_sd_configs: GCE 服务发现
  • ec2_sd_configs: EC2 服务发现
  • openstack_sd_configs: OpenStack 服务发现
  • azure_sd_configs: Azure 服务发现

现在,意味着我们会在Kubernetes中的会保留__meta_kubernetes_pod_container_name值为sidecar的,同时也需要满足__meta_kubernetes_pod_annotation_prometheus_io_scrape属性为true的Pod。

接下来,创建prometheus-rbac-setup.yaml文件,为了使Prometheus可以访问到Kubernetes API,我们需要对Prometheus进行访问授权,在Kubernetes中通过基于角色的访问控制模型(Role-Based Access Control),用于访问Kubernetes的资源。首先我们定义角色(ClusterRole)并设置相应的访问权限;为Prometheus创建账号(ServiceAccount);最后将账号与角色进行绑定(ClusterRoleBinding)。

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: prometheus
rules:
- apiGroups: [""]
resources:
- nodes
- nodes/proxy
- services
- endpoints
- pods
verbs: ["get", "list", "watch"]
- apiGroups:
- extensions
resources:
- ingresses
verbs: ["get", "list", "watch"]
- nonResourceURLs: ["/metrics"]
verbs: ["get"]
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: prometheus
namespace: default
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: prometheus
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: prometheus
subjects:
- kind: ServiceAccount
name: prometheus
namespace: default

创建prometheus-deployment.yaml文件。

apiVersion: apps/v1
kind: Deployment
metadata:
labels:
name: prometheus
name: prometheus
spec:
replicas: 1
selector:
matchLabels:
app: prometheus
template:
metadata:
labels:
app: prometheus
spec:
serviceAccountName: prometheus
containers:
- name: prometheus
image: prom/prometheus:latest
command:
- "/bin/prometheus"
args:
- "--config.file=/etc/prometheus/prometheus.yml"
ports:
- containerPort: 9090
protocol: TCP
volumeMounts:
- mountPath: "/etc/prometheus"
name: prometheus-config
volumes:
- name: prometheus-config
configMap:
name: prometheus-config

创建prometheus-service.yaml文件。

apiVersion: v1
kind: Service
metadata:
name: prometheus
labels:
name: prometheus
spec:
type: NodePort
ports:
- name: prometheus
protocol: TCP
port: 9090
targetPort: 9090
nodePort: 32732
selector:
app: prometheus

如下所示,展示了Prometheus仪表盘

Grafana

Grafana的内容不做展开了,当然你可以直接查看或使用我的dashboard文件。

https://github.com/hueifeng/dotnet-monitor-on-k8s

参考

部署Prometheus

https://dotnetos.org/blog/2021-11-22-dotnet-monitor-grafana/

使用dotnet-monitor分析在Kubernetes的应用程序:Sidecar模式的更多相关文章

  1. 【翻译】.NET 6 中的 dotnet monitor

    原文:Announcing dotnet monitor in .NET 6 我们在 2020 年 6 月首次推出了dotnet monitor 作为实验工具,并在去年(2020年)努力将其转变为生产 ...

  2. 部署Dotnet Core应用到Kubernetes(一)

    最近闲了点,写个大活:部署Dotnet应用到K8s.   写在前边的话 一直想完成这个主题.但这个主题实在太大了,各种拖延症的小宇宙不时爆发一下,结果就拖到了现在.   这个主题,会是一个系列.在这个 ...

  3. 使用dotnet-monitor sidecar模式 dump docker运行的dotnet程序.

    前情概要 随着容器和云技术的发展, 大量的应用运行在云上的容器中, 它们的好处是毋庸置疑的, 例如极大的提高了我们的研发部署速度, 快速的扩缩容等等, 但是也存在一些小小的问题, 例如难以调试. 基于 ...

  4. Kubernetes K8S在IPVS代理模式下Service服务的ClusterIP类型访问失败处理

    Kubernetes K8S使用IPVS代理模式,当Service的类型为ClusterIP时,如何处理访问service却不能访问后端pod的情况. 背景现象 Kubernetes K8S使用IPV ...

  5. ASP.NET Core 中文文档 第二章 指南(8) 使用 dotnet watch 开发 ASP.NET Core 应用程序

    原文:Developing ASP.NET Core applications using dotnet watch 作者:Victor Hurdugaci 翻译:谢炀(Kiler) 校对:刘怡(Al ...

  6. 使用 dotnet watch 开发 ASP.NET Core 应用程序

    使用 dotnet watch 开发 ASP.NET Core 应用程序 原文:Developing ASP.NET Core applications using dotnet watch作者:Vi ...

  7. Docker Kubernetes Service 网络服务代理模式详解

    Docker Kubernetes  Service 网络服务代理模式详解 Service service是实现kubernetes网络通信的一个服务 主要功能:负载均衡.网络规则分布到具体pod 注 ...

  8. Android:日常学习笔记(2)——分析第一个Android应用程序

    Android:日常学习笔记(2)——分析第一个Android应用程序 Android项目结构 整体目录结构分析 说明: 除了APP目录外,其他目录都是自动生成的.APP目录的下的内容才是我们的工作重 ...

  9. 分析现有 WPF / Windows Forms 程序能否顺利迁移到 .NET Core 3.0

    本文转自 https://blog.csdn.net/WPwalter/article/details/82859449 使用 .NET Core 3.0 Desktop API Analyzer 分 ...

随机推荐

  1. CentOS 7.0 使用 yum 安装 MariaDB

    CentOS 7.0 使用 yum 安装 MariaDB 与 MariaDB 的简单配置   1.安装MariaDB 安装命令 yum -y install mariadb mariadb-serve ...

  2. Linux文件拷贝脚本

    在工作中,我们经常遇到要从Linux服务器拷贝日志至本地或者定期清理日志的需求,在服务器上,大型系统的日志是按模块存储的,这就导致日志的文件目录较多且层级不统一.我们从众多的目录手工筛选要下载或者删除 ...

  3. 开源流程引擎该如何选择flowable还是camunda

    市场上比较有名的开源流程引擎有osworkflow.jbpm.activiti.flowable.camunda.现在国内用的最多的是activiti.flowable.camunda,下面主要从功能 ...

  4. ExtJS 布局-VBox布局(VBox layout)

    更新记录: 2022年6月11日 优化文章结构. 2022年6月9日 发布. 2022年6月1日 开始. 1.说明 vbox布局类似auto布局,将子组件一个接一个垂直向下放置,既可以在水平方向也可以 ...

  5. rpm 系 linux 系统中 repo 文件中的 $release 到底等于多少?

    rpm 系 linux 系统中 repo 文件中的 $release 到底等于多少? 结论 对于 8 来说,通过以下命令 #/usr/libexec/platform-python -c 'impor ...

  6. 1.2 操作系统的第二个功能——并发功能 -《zobolの操作系统学习札记》

    1.2 操作系统的第二个功能--并发功能 目录 1.2 操作系统的第二个功能--并发功能 问1:什么是并发功能?并发功能是必要的吗? 问2:并发功能必须要求拥有多核CPU吗? 问3:多核CPU和单核C ...

  7. Tensor的向量化

    向量化操作是指可以在同一时间进行批量地并行计算,例如矩阵运算,以达到更好效率的一种方式. 尽量使用向量化直接对Tensor操作,避免低效率的for循环对元素逐个操作.

  8. Vue最新防抖方案

    函数防抖(debounce):当持续触发事件时,一定时间段内没有再触发事件,事件处理函数才会执行一次,如果设定的时间到来之前,又一次触发了事件,就重新开始延时.举个栗子,持续触发scroll事件时,并 ...

  9. WPF开发随笔收录-报警闪烁效果实现

    一.前言 工作中目前经手的项目是医疗相关的监护软件,所以会涉及到一些报警效果的实现,今天在这里就简单分享一下实现方式 二.正文 1.实现的方式比较的简单,就是通过一个Border控件,然后搭配Data ...

  10. 【RocketMQ】消息的刷盘机制

    刷盘策略 CommitLog的asyncPutMessage方法中可以看到在写入消息之后,调用了submitFlushRequest方法执行刷盘策略: public class CommitLog { ...