欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

k8s环境下搭建prometheus

程序员文章站 2022-06-16 08:31:27
前言啥都不说,直接开造!开造首先在k8s集群创建命名空间monitoringkubectl create namespace monitoring服务账户(prometheus-sa.yaml)apiVersion: v1kind: ServiceAccountmetadata: name: prometheus namespace: monitoring集群角色(prometheus-clusterRole.yaml)apiVersion: rbac.authorizati...

前言

啥都不说,直接开造!

开造

首先在k8s集群创建命名空间monitoring

kubectl create namespace monitoring

服务账户(prometheus-sa.yaml)

apiVersion: v1
kind: ServiceAccount
metadata:
  name: prometheus
  namespace: monitoring

集群角色(prometheus-clusterRole.yaml)

apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
metadata:
  name: prometheus
rules:
- apiGroups: [""]
  resources:
  - nodes
  - nodes/proxy
  - services
  - endpoints
  - pods
  verbs: ["get", "list", "watch"]
- apiGroups: [""]
  resources:
  - configmaps
  verbs: ["get"]
- nonResourceURLs: ["/metrics"]
  verbs: ["get"]

服务账户与集群角色绑定,完成授权(prometheus-clusterRoleBinding.yaml)

apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
  name: prometheus
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: prometheus
subjects:
- kind: ServiceAccount
  name: prometheus
  namespace: monitoring

创建相关内容

kubectl apply -f prometheus-sa.yaml
kubectl apply -f prometheus-clusterRole.yaml
kubectl apply -f prometheus-clusterRoleBinding.yaml

创建prometheus需要用到的配置文件,以configmap形式挂载到pod内
prometheus.yaml

global:
  scrape_interval:     15s 
  evaluation_interval: 15s 
  external_labels:
    cluster: k8s-cluster # 添加公共标签,在被其他prometheus采集时标识数据来源
alerting:
  alertmanagers:
  - static_configs:
    - targets:
       - alertmanager.monitoring.svc:9093 # 在monitoring命名空间下创建alertmanager后访问地址
 
rule_files:
  - /etc/prometheus-rules/*_rules.yaml  # rules配置文件挂载位置
  - /etc/prometheus-rules/*_alerts.yaml
 
scrape_configs:
   config.
  - job_name: 'prometheus'
    scrape_interval: 5s
    static_configs:
    - targets: ['localhost:9090']

#remote_write:
  #- url: "http://localhost:8086/api/v1/prom/write?db=prometheus&u=username&p=password"

/rules/example_rules.yaml

groups:
  - name: example
    rules:
    - record: up:count
      expr: sum(up) by (job)

/rules/example_alerts.yaml

groups:
- name: example
  rules:
  - alert: InstanceDown
    expr: up == 0.5
    for: 10m
    labels:
      severity: page
    annotations:
      summary: Instance {{ $labels.instance }} down

分别创建configmap

kubectl create configmap prometheus-rules --from-file=/rules -n monitoring
kubectl create configmap prometheus-config --from-file=prometheus.yaml -n monitoring

创建pod,如果需要将监控数据做持久化存储,需要挂载pv,同时最好使用statefuset类型创建pod,我这边使用deployment创建,将数据远程写入influxdb中,statefuset类型pod创建可以参考canal-server搭建那篇文档,文末我会将地址列出
prometheus-deploy.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: prometheus
  namespace: monitoring
  labels:
    app: prometheus
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: prometheus
    spec:
      serviceAccountName: prometheus
      containers:
      - image: prom/prometheus
        name: prometheus
        command:
        - "/bin/prometheus"
        args:
        - "--config.file=/etc/prometheus/prometheus.yml"
        - "--storage.tsdb.path=/prometheus" # 使用自身Pod进行存储
        - "--storage.tsdb.retention=30d" # 数据保留30天
        - "--web.enable-admin-api"  # 控制对admin HTTP API的访问,其中包括删除时间序列等功能
        - "--web.enable-lifecycle"  # 支持热更新,直接执行localhost:9090/-/reload立即生效
        ports:
        - containerPort: 9090
          protocol: TCP
          name: http
        volumeMounts:
        - mountPath: "/etc/prometheus"
          name: config-volume
        - mountPath: "/etc/prometheus-rules"
          name: rule-volume
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
          limits:
            cpu: 4000m
            memory: 10Gi
      securityContext:
        runAsUser: 0
      volumes:
      - configMap:
          name: prometheus-config
        name: config-volume
      - name: rule-volume
        configMap:
          name: prometheus-rules

创建pod

kubectl apply -f prometheus-deploy.yaml

创建node-port类型svc,指定30090端口供外部访问
prometheus-svc.yaml

apiVersion: v1
kind: Service
metadata:
  name: prometheus
  namespace: monitoring
  labels:
    app: prometheus
spec:
  selector:
    app: prometheus
  type: NodePort
  ports:
    - name: web
      nodePort: 30090
      port: 9090
      targetPort: http

创建svc

kubectl apply -f prometheus-svc.yaml

访问master-ip:30090查看是否部署成功

总结

到这里,prometheus在k8s环境下的搭建就完成了,后面我会介绍如何通过prometheus监控整个k8s集群,点关注,不迷路哦!

欢迎关注我的个人微信公众号,一个菜鸟程序猿的技术分享和奔溃日常

k8s环境下搭建prometheus

本文地址:https://blog.csdn.net/XUEJIA2S/article/details/107392719