Kubernetes搭建Prometheus

序言

Prometheus是tsdb进行存储,部署在Kubernetes要注意数据持久化,有rule,config文件常规化变更,需要热加载,需要注意ConfigMap资源。

配置文件ConfigMap

保证配置常规化更新,支持热加载,将Prometheus yaml配置文件打包成ConfigMap进行管理。
master操作

#kubectl create ns prometheus

master操作

#cat > prometheus_cm.yaml << EOF
apiVersion: v1
kind: ConfigMap
metadata:
  name: prometheus-config
  namespace: prometheus
data:
  prometheus.yml: |
    global:
      scrape_interval: 15s
      scrape_timeout: 15s
    scrape_configs:
    - job_name: 'prometheus'
      static_configs:
      - targets: ['localhost:9090']
EOF
#kubectl apply -f prometheus_cm.yaml

数据持久化pv/pvc

本地利用StorageClass,生成pv资源,设定标签为Prometheus,local确定挂载path,kubernetes.io/hostname values设定亲和性,values的值为node的标签,master操作如下可以获取到标签值。不设定执行下面的yaml会报错。
在这里插入图片描述

#kubectl get nodes --show-labels

在这里插入图片描述

亲和性节点操作

#mkdir /data/k8s/prometheus -p

master操作

#cat > prometheus_pvc.yaml << EOF
apiVersion: v1
kind: PersistentVolume
metadata:
  name: prometheus-pv
  labels:
    app: prometheus
spec:
  accessModes:
  - ReadWriteOnce
  capacity:
    storage: 10Gi
  storageClassName: my-storage
  local:
    path: /data/k8s/prometheus
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - node1
  persistentVolumeReclaimPolicy: Retain
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: prometheus-pvc
  namespace: prometheus
spec:
  selector:
    matchLabels:
      app: prometheus
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
  storageClassName: my-storage
EOF
#kubectl apply -f prometheus_pvc.yaml

RBAC权限

为Prometheus生成一个集群内部权限,可能会出现apiserver版本错误
在这里插入图片描述

#kubectl explain ClusterRoleBinding.apiVersion
#cat > prometheus_rbac.yaml << EOF
apiVersion: v1
kind: ServiceAccount
metadata:
  name: prometheus
  namespace: prometheus
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: prometheus
rules:
- apiGroups:
  - ""
  resources:
  - nodes
  - services
  - endpoints
  - pods
  - nodes/proxy
  verbs:
  - get
  - list
  - watch
- apiGroups:
  - "extensions"
  resources:
    - ingresses
  verbs:
  - get
  - list
  - watch
- apiGroups:
  - ""
  resources:
  - configmaps
  - nodes/metrics
  verbs:
  - get
- nonResourceURLs:
  - /metrics
  verbs:
  - get
---
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: prometheus
EOF
#kubectl apply -f prometheus_rbac.yaml

Prometheus服务部署

将ConfigMap资源进行挂载,加上前面生成的ServiceAccount资源,以Deployment类型进行pod管理。

#cat > prometheus_deploy.yaml << EOF
apiVersion: apps/v1
kind: Deployment
metadata:
  name: prometheus
  namespace: prometheus
  labels:
    app: prometheus
spec:
  selector:
    matchLabels:
      app: prometheus
  template:
    metadata:
      labels:
        app: prometheus
    spec:
      securityContext:
        runAsUser: 0
      serviceAccountName: prometheus
      containers:
      - image: prom/prometheus:v2.30.2
        name: prometheus
        args:
        - "--config.file=/etc/prometheus/prometheus.yml"
        - "--storage.tsdb.path=/prometheus"
        - "--storage.tsdb.retention.time=24h"
        - "--web.enable-admin-api"
        - "--web.enable-lifecycle"
        ports:
        - containerPort: 9090
          name: http
        volumeMounts:
        - mountPath: "/etc/prometheus"
          name: config-volume
        - mountPath: "/prometheus"
          name: data
        resources:
          requests:
            cpu: 100m
            memory: 512Mi
          limits:
            cpu: 100m
            memory: 512Mi
      volumes:
      - name: data
        persistentVolumeClaim:
          claimName: prometheus-svc
      - name: config-volume
        configMap:
          name: prometheus-config
EOF          
#kubectl apply -f prometheus_deploy.yaml

服务暴露访问

以NodePort的service类型暴露服务在宿主机上,可以直接进行外部访问。

#cat > prometheus_svc.yaml << EOF
apiVersion: v1
kind: Service
metadata:
  name: prometheus
  namespace: prometheus
  labels:
    app: prometheus
spec:
  selector:
    app: prometheus
  type: NodePort
  ports:
    - name: web
      port: 9090
      targetPort: http
EOF
#kubectl apply -f prometheus_svc.yaml

查看集群状态、服务状态和service

#kubectl get pods -A

在这里插入图片描述

#kubectl get svc-A

在这里插入图片描述
查询到暴露在外部的端口是30622,可以页面直接masterip:30622访问

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
A:在 Kubernetes 中部署 Prometheus,可以按照以下步骤进行操作: 1. 创建一个 Kubernetes Namespace,可以使用以下命令: ``` kubectl create ns monitoring ``` 2. 创建一个 Prometheus 配置文件 prometheus.yml,可以使用以下命令: ``` vi prometheus.yml ``` 将以下内容复制到文件中: ``` global: scrape_interval: 15s scrape_configs: - job_name: 'prometheus' static_configs: - targets: ['localhost:9090'] - job_name: 'kubernetes-apiservers' kubernetes_sd_configs: - api_server: null role: endpoints namespaces: names: ['default'] relabel_configs: - source_labels: [__meta_kubernetes_namespace, __meta_kubernetes_service_name, __meta_kubernetes_endpoint_port_name] action: keep regex: default;kubernetes;https - job_name: 'kubernetes-nodes' kubernetes_sd_configs: - api_server: null role: node relabel_configs: - action: labelmap regex: __meta_kubernetes_node_label_(.+) - target_label: __address__ replacement: kubernetes.default.svc:443 - source_labels: [__meta_kubernetes_node_name] regex: (.+) target_label: __metrics_path__ replacement: /api/v1/nodes/${1}/proxy/metrics/cadvisor ``` 这个配置文件将监控使用 `https` 端口的 Kubernetes API Server、Kubernetes Node 的 CPU、内存和磁盘利用率等信息,以及已经部署的 Prometheus 本身。 3. 创建一个 ConfigMap,将以上配置文件导入 Kubernetes 集群,可以使用以下命令: ``` kubectl create configmap prometheus-cm --from-file=prometheus.yml -n monitoring ``` 4. 创建一个 Prometheus Deployment 并暴露一个 Service,可以使用以下 YAML 文件: ``` apiVersion: apps/v1 kind: Deployment metadata: name: prometheus namespace: monitoring spec: replicas: 1 selector: matchLabels: app: prometheus template: metadata: labels: app: prometheus spec: containers: - name: prometheus image: prom/prometheus:v2.22.2 args: - --config.file=/etc/prometheus/prometheus.yml - --storage.tsdb.path=/prometheus/data - --storage.tsdb.retention.time=7d volumeMounts: - name: prometheus-data mountPath: /prometheus - name: prometheus-config mountPath: /etc/prometheus volumes: - name: prometheus-data emptyDir: {} - name: prometheus-config configMap: name: prometheus-cm items: - key: prometheus.yml path: prometheus.yml --- apiVersion: v1 kind: Service metadata: name: prometheus namespace: monitoring spec: selector: app: prometheus ports: - name: prometheus-web port: 9090 protocol: TCP targetPort: 9090 ``` 以上 YAML 文件中,我们创建了一个名称为 prometheus 的 Deployment,副本数为 1,使用了 prom/prometheus:v2.22.2 镜像。然后创建了一个名称为 prometheus 的 Service,并将 targetPort 设置为 9090。 5. 使用以下命令,将以上 YAML 文件部署到 K8s 集群中: ``` kubectl apply -f prometheus.yaml ``` 等待几分钟后,可以执行以下命令验证 Prometheus 是否正常运行: ``` kubectl get pods -n monitoring ``` 以上命令输出的结果应该类似于以下内容: ``` NAME READY STATUS RESTARTS AGE prometheus-7b447fcfbc-wq2xn 1/1 Running 0 8h ``` 此外,还可以使用以下命令获取 Prometheus 的 Service 的 ClusterIP: ``` kubectl get svc prometheus -n monitoring ``` 以上命令输出的结果应该类似于以下内容: ``` NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE prometheus ClusterIP 10.100.82.214 <none> 9090/TCP 8h ``` 通过以上输出的 ClusterIP 和 9090/TCP,就可以在 K8s 集群内部访问 Prometheus 了,比如可以使用以下命令: ``` curl http://10.100.82.214:9090/ ``` 以上命令输出的内容应该是 HTML 格式的 Prometheus 首页。 至此,在 Kubernets 中部署 Prometheus 完成。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值