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/

最新文章

  1. Reverse Core 第二部分 - 14&15章 - 运行时压缩&调试UPX压缩的notepad
  2. [Bootstrap]7天深入Bootstrap(3)CSS布局
  3. webservice原理
  4. java的nio之:浅析I/O模型
  5. Android手机一键Root原理分析
  6. Java 高效检查一个数组中是否包含某个值
  7. 带着项目学PHP第九讲 - 如何给ecshop的wap版本首页和商品页添加商品图片
  8. 【NOI模拟】谈笑风生(主席树)
  9. 浅谈Java语言中ArrayList和HashSet的区别
  10. 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第6章编程练习7
  11. 存在一个足够大的二维数组,每个数组中的值都是整数,使用javascript如何实现按每个数组中的平均值,从大到小排序这个二维数组?
  12. Luogu P3165 [CQOI2014]排序机械臂
  13. Pandas简易入门(一)
  14. Docker+STF在ubuntu下测试环境搭建(详细搭建步骤及踩坑记录)
  15. uiautomatorviewer 双击闪退问题解决
  16. qt study2
  17. Speculative Execution in Hadoop
  18. 在命令行上 Ubuntu 下使用 mutt 和 msmtp 发送 Gmail 邮件
  19. VS2015解决方案资源管理器空白,不显示内容
  20. linux 中安装mysql8.0

热门文章

  1. .NET C#基础(5):结构体 - 高性能代码的基石
  2. 渗透测试之sql注入点查询
  3. 使用VMware安装Ubuntu虚拟机
  4. Xilinx DMA的几种方式与架构
  5. Java 泛型中的通配符
  6. react native 0.6x 在创建项目时,CocoaPods 的依赖安装步骤卡解决方案
  7. Linux文件的特殊属性
  8. Sentiment analysis in nlp
  9. 毕设之Python爬取天气数据及可视化分析
  10. 查询效率提升10倍!3种优化方案,帮你解决MySQL深分页问题