Kubernetes prometheus services is forbidden: User \“system:serviceaccount:monitoring:prometheus-k8s\

ts=2024-05-15T03:14:22.131Z caller=klog.go:108 level=warn component=k8s_client_runtime func=Warningf msg=“pkg/mod/k8s.io/client-go@v0.26.2/tools/cache/reflector.go:169: failed to list *v1.Pod: pods is forbidden: User “system:serviceaccount:monitoring:prometheus-k8s” cannot list resource “pods” in API group “” in the namespace “middle””
ts=2024-05-15T03:14:22.132Z caller=klog.go:116 level=error component=k8s_client_runtime func=ErrorDepth msg=“pkg/mod/k8s.io/client-go@v0.26.2/tools/cache/reflector.go:169: Failed to watch *v1.Pod: failed to list *v1.Pod: pods is forbidden: User “system:serviceaccount:monitoring:prometheus-k8s” cannot list resource “pods” in API group “” in the namespace “middle””
ts=2024-05-15T03:14:22.273Z caller=klog.go:108 level=warn component=k8s_client_runtime func=Warningf msg=“pkg/mod/k8s.io/client-go@v0.26.2/tools/cache/reflector.go:169: failed to list *v1.Service: services is forbidden: User “system:serviceaccount:monitoring:prometheus-k8s” cannot list resource “services” in API group “” in the namespace “middle””
ts=2024-05-15T03:14:22.274Z caller=klog.go:116 level=error component=k8s_client_runtime func=ErrorDepth msg=“pkg/mod/k8s.io/client-go@v0.26.2/tools/cache/reflector.go:169: Failed to watch *v1.Service: failed to list *v1.Service: services is forbidden: User “system:serviceaccount:monitoring:prometheus-k8s” cannot list resource “services” in API group “” in the namespace “middle””

如果k8s部署服务,开启了/metrics:
metrics:
enabled: true
image:
registry: harbor.zdwk8s.com
repository: middle/redis-exporter
tag: 1.50.0-debian-11-r13
digest: “”
pullPolicy: IfNotPresent
pullSecrets: []
startupProbe:
enabled: false
initialDelaySeconds: 10
periodSeconds: 10
timeoutSeconds: 5
successThreshold: 1
failureThreshold: 5
livenessProbe:
enabled: true
initialDelaySeconds: 10
periodSeconds: 10
timeoutSeconds: 5
successThreshold: 1
failureThreshold: 5
readinessProbe:
enabled: true
initialDelaySeconds: 5
periodSeconds: 10
timeoutSeconds: 1
successThreshold: 1
failureThreshold: 3
customStartupProbe: {}
customLivenessProbe: {}
customReadinessProbe: {}
command: []
redisTargetHost: “localhost”
extraArgs: {}
extraEnvVars: []
containerSecurityContext:
enabled: true
runAsUser: 1001
extraVolumes: []
extraVolumeMounts: []
resources:
limits: {}
requests: {}
podLabels: {}
podAnnotations:
prometheus.io/scrape: “true”
prometheus.io/port: “9121”
service:
type: ClusterIP
port: 9121
externalTrafficPolicy: Cluster
extraPorts: []
loadBalancerIP: “”
loadBalancerSourceRanges: []
annotations: {}
clusterIP: “”
serviceMonitor:
enabled: true
namespace: “monitoring”
interval: 30s
scrapeTimeout: “”
relabellings: []
metricRelabelings: []
honorLabels: false
additionalLabels: {}
podTargetLabels: []
prometheusRule:
enabled: true
namespace: “monitoring”
additionalLabels: {}
rules: []

先确认可以获取数据:
[root@china ~]# kubectl get svc -n middle
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
redis-headless ClusterIP None 6379/TCP 81m
redis-master NodePort 10.227.93.201 6379:32379/TCP 81m
redis-metrics ClusterIP 10.227.183.204 9121/TCP 81m

[root@china ~]# curl 10.227.183.204:9121/metrics

go_gc_duration_seconds{quantile=“0”} 3.9832e-05
go_gc_duration_seconds{quantile=“0.25”} 0.000139377
go_gc_duration_seconds{quantile=“0.5”} 0.000236065
go_gc_duration_seconds{quantile=“0.75”} 0.000308893
go_gc_duration_seconds{quantile=“1”} 0.001813312
go_gc_duration_seconds_sum 0.01587861
go_gc_duration_seconds_count 61

但是prometheus无法获取数据,看日志:
kubectl logs prometheus-k8s-0 -n monitoring --tail 200

报错误:forbidden: User "system:serviceaccount:monitoring

在部署prometheus的文件中添加权限,在/kube-prometheus/manifests/prometheus-clusterRole.yaml

添加:
[root@china manifests]# cat prometheus-clusterRole.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
labels:
app.kubernetes.io/component: prometheus
app.kubernetes.io/instance: k8s
app.kubernetes.io/name: prometheus
app.kubernetes.io/part-of: kube-prometheus
app.kubernetes.io/version: 2.44.0
name: prometheus-k8s
rules:

  • apiGroups:
    • “”
      resources:
    • nodes
    • services
    • endpoints
    • pods
    • nodes/proxy
    • nodes/metrics
      verbs:
    • get
    • list
    • watch
  • nonResourceURLs:
    • /metrics
      verbs:
    • get
      [root@china manifests]#

主要添加:
在这里插入图片描述

重新生效:
[root@china manifests]# kubectl apply -f prometheus-clusterRole.yaml

在这里插入图片描述

这是一个用于部署Prometheus监控系统的YAML文件,下面对其各部分进行解释: ``` apiVersion: monitoring.coreos.com/v1 kind: Prometheus metadata: labels: prometheus: k8s name: k8s namespace: monitoring spec: alerting: alertmanagers: - name: alertmanager-main namespace: monitoring port: web image: quay.io/prometheus/prometheus:v2.20.0 nodeSelector: kubernetes.io/os: linux podMonitorNamespaceSelector: {} podMonitorSelector: {} replicas: 2 resources: requests: memory: 400Mi ruleSelector: matchLabels: prometheus: k8s role: alert-rules securityContext: fsGroup: 2000 runAsNonRoot: true runAsUser: 1000 serviceAccountName: prometheus-k8s serviceMonitorNamespaceSelector: {} serviceMonitorSelector: {} version: v2.20.0 ``` - `apiVersion`: 定义该对象所使用的Kubernetes API版本,这里使用的是monitoring.coreos.com/v1。 - `kind`: 定义该对象的类型,这里是Prometheus类型。 - `metadata`: 定义该对象的元数据,包括名称、标签和命名空间等信息。 - `spec`: 定义该对象的具体配置信息,包括如何部署和配置Prometheus监控系统。 - `alerting`: 定义如何设置警报。这里设置了警报管理器,使用名称为`alertmanager-main`的警报管理器,并指定其所在的命名空间和Web端口。 - `image`: 定义Prometheus监控系统的镜像,这里使用了`quay.io/prometheus/prometheus:v2.20.0`。 - `nodeSelector`: 定义用于选择部署Prometheus监控系统的节点的标签。这里选择了标签为`kubernetes.io/os: linux`的节点。 - `podMonitorNamespaceSelector` 和 `podMonitorSelector`: 定义选择哪些Pod进行监控。这里未指定任何选择条件,表示将监控所有Pod。 - `replicas`: 定义Prometheus实例的副本数量,这里设置为2。 - `resources`: 定义Prometheus实例使用的资源请求量,这里设置了内存请求为400Mi。 - `ruleSelector`: 定义如何选择要应用的告警规则。这里选择了标签为`prometheus: k8s`和`role: alert-rules`的规则。 - `securityContext`: 定义Prometheus容器的安全上下文,包括运行容器的用户和组等。 - `serviceAccountName`: 指定Prometheus容器所使用的服务账户。 - `serviceMonitorNamespaceSelector` 和 `serviceMonitorSelector`: 定义选择哪些Service进行监控。这里未指定任何选择条件,表示将监控所有Service。 - `version`: 指定Prometheus的版本,这
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值