• k8s集群授权prometheus(集群外部署)


    一、前言

    在集群外部prometheus想要调用k8s集群的apiserver获取监控数据需要通过token和ca验证,在集群内部部署的prometheus就不会有这个情况,因为集群内部部署prometheus pod的时候就已经注入了访问集群的token和ca文件,所以以下就针对k8s集群外部署的prometheus创建token和ca文件使得prometheus能够访问k8s集群的apiserver获取监控数据


    二、k8s集群创建RBAC对象

    vi prometheus-rabc.yaml

    1. apiVersion: v1
    2. kind: ServiceAccount
    3. metadata:
    4. name: prometheus
    5. namespace: prometheus
    6. ---
    7. apiVersion: rbac.authorization.k8s.io/v1
    8. kind: ClusterRole
    9. metadata:
    10. name: prometheus
    11. rules:
    12. - apiGroups:
    13. - ""
    14. resources:
    15. - nodes
    16. - services
    17. - endpoints
    18. - pods
    19. - nodes/proxy
    20. verbs:
    21. - get
    22. - list
    23. - watch
    24. - apiGroups:
    25. - "extensions"
    26. resources:
    27. - ingresses
    28. verbs:
    29. - get
    30. - list
    31. - watch
    32. - apiGroups:
    33. - ""
    34. resources:
    35. - configmaps
    36. - nodes/metrics
    37. verbs:
    38. - get
    39. - nonResourceURLs:
    40. - /metrics
    41. verbs:
    42. - get
    43. ---
    44. apiVersion: rbac.authorization.k8s.io/v1
    45. kind: ClusterRoleBinding
    46. metadata:
    47. name: prometheus
    48. roleRef:
    49. apiGroup: rbac.authorization.k8s.io
    50. kind: ClusterRole
    51. name: prometheus
    52. subjects:
    53. - kind: ServiceAccount
    54. name: prometheus
    55. namespace: prometheus

    创建命名空间

    kubectl create namespace prometheus

    创建rbac

    kubectl create -f prometheus-rabc.yaml

    在新版本的k8s中创建ServiceAccount不会默认创建secret,需要自己手动创建

    vi prometheus-secret.yaml 

    1. apiVersion: v1
    2. kind: Secret
    3. metadata:
    4. name: prometheus
    5. namespace: prometheus
    6. annotations:
    7. kubernetes.io/service-account.name: prometheus #这里需要填写创建的ServiceAccount的名称
    8. type: kubernetes.io/service-account-token

    查看token

    kubectl describe secret prometheus -n prometheus

     查看一下ServiceAccount

    kubectl describe sa prometheus -n prometheus

     prometheus调用k8s集群接口获取监控数据,使用以上的token和集群ca证书即可

    k8s集群的ca证书在以下目录中

    ls /etc/kubenetes/pki

  • 相关阅读:
    MySQL备份与恢复
    Spring小记
    微信小程序中使用wx.showToast()进行界面交互
    Excel学习 WPS版
    【Python进阶】近200页md文档14大体系第4篇:Python进程使用详解(图文演示)
    经典卷积神经网络 - ResNet
    Arthas实操-Web Console
    面向对象编程之类方法@classmethod
    Syncfusion Essential Studio 2022 Vol 3 注册版
    STM32项目 -- 选题分享(部分)
  • 原文地址:https://blog.csdn.net/ApexPredator/article/details/133879977