在容器外搭建

Prometheus在容器外搭建非常简单,只需要下载对应的release,启动二进制文件即可。
下载地址:https://prometheus.io/download/
然后可以直接用下面命令启动:

  1. ./prometheus --config.file=prometheus.yml

其中prometheus.yaml是主要的配置文件,主要配置信息如下:

  1. global:
  2. scrape_interval: 15s
  3. evaluation_interval: 15s
  4. rule_files:
  5. # - "first.rules"
  6. # - "second.rules"
  7. scrape_configs:
  8. - job_name: prometheus
  9. static_configs:
  10. - targets: ['localhost:9090']

上面配置信息主要包括三个模块:global,rule_files,scrape_configs。
(1)、global定义Prometheus server全局配置。

  • scrape_interval,定义采集频率
  • evaluation_interval,定义评估规则的频率,Prometheus使用规则产生的时间序列数据或者产生的警报

(2)、rule_file,用于指定规则,Prometheus使用规则产生的时间序列数据或者产生的警报
(3)、scrape_configs,用于控制监控的资源

Prometheus默认会通过/metrics路径采集metrics,比如:curl http://localhost:9090/metrics 就可以看到相应的资源对象了。

在容器内搭建

1、创建namespace:

  1. # kubectl create ns kube-ops

2、创建configmap,保存我们的主配置文件prometheus.yaml,这样我们要更新配置文件的话就只需要更新这个configmap即可。
prom-configmap.yaml

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: prometheus-config
  5. namespace: kube-ops
  6. data:
  7. prometheus.yaml: |
  8. global:
  9. scrape_interval: 15s
  10. scrape_timeout: 15s
  11. scrape_configs:
  12. - job_name: 'prometheus'
  13. static_configs:
  14. - targets: ['localhost:9090']

创建资源:

  1. # kubectl apply -f prom-configmap.yaml
  2. configmap/prometheus-config created
  3. # kubectl get configmap -n kube-ops
  4. NAME DATA AGE
  5. prometheus-config 1 16s

(3)、创建prometheus的Pod
prom-deploy.yaml

  1. apiVersion: extensions/v1beta1
  2. kind: Deployment
  3. metadata:
  4. name: prometheus-deploy
  5. namespace: kube-ops
  6. labels:
  7. app: prometheus
  8. spec:
  9. selector:
  10. matchLabels:
  11. app: prometheus
  12. replicas: 1
  13. template:
  14. metadata:
  15. labels:
  16. app: prometheus
  17. spec:
  18. serviceAccountName: prometheus-sa
  19. containers:
  20. - name: prometheus
  21. image: prom/prometheus:v2.14.0
  22. imagePullPolicy: IfNotPresent
  23. command:
  24. - "/bin/prometheus"
  25. args:
  26. - "--config.file=/etc/prometheus/prometheus.yaml"
  27. - "--storage.tsdb.path=/data/prometheus"
  28. - "--storage.tsdb.retention=24h"
  29. - "--web.enable-admin-api"
  30. - "--web.enable-lifecycle"
  31. ports:
  32. - name: http
  33. protocol: TCP
  34. containerPort: 9090
  35. volumeMounts:
  36. - name: data
  37. mountPath: "/data/prometheus"
  38. subPath: prometheus
  39. - name: prometheus-config
  40. mountPath: "/etc/prometheus"
  41. resources:
  42. requests:
  43. cpu: 100m
  44. memory: 500Mi
  45. limits:
  46. cpu: 100m
  47. memory: 500Mi
  48. securityContext:
  49. runAsUser: 0
  50. volumes:
  51. - name: data
  52. persistentVolumeClaim:
  53. claimName: prometheus
  54. - name: prometheus-config
  55. configMap:
  56. name: prometheus-config

我们把上面定义的configMap通过挂载的形式挂载到容器中,然后我们还要定义一个持久化PVC。

(4)、创建PV,PVC
prom-pvc.yaml

  1. ---
  2. apiVersion: v1
  3. kind: PersistentVolume
  4. metadata:
  5. name: prometheus-pv
  6. spec:
  7. capacity:
  8. storage: 10Gi
  9. accessModes:
  10. - ReadWriteOnce
  11. persistentVolumeReclaimPolicy: Recycle
  12. nfs:
  13. server: xx.xx.xx.xx
  14. path: /data/k8s/prometheus
  15. ---
  16. apiVersion: v1
  17. kind: PersistentVolumeClaim
  18. metadata:
  19. name: prometheus
  20. namespace: kube-ops
  21. spec:
  22. accessModes:
  23. - ReadWriteOnce
  24. resources:
  25. requests:
  26. storage: 10Gi

(5)、配置RBAC认证
我们在deploy的模板中定义了serviceAccount,我们就需要定义一个serviceAccount的RBAC。
prom-rbac.yaml

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

(6)、创建Service,用来暴露promethes服务
prom-service.yaml

  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. name: prometheus-svc
  5. namespace: kube-ops
  6. spec:
  7. type: NodePort
  8. selector:
  9. app: prometheus
  10. ports:
  11. - name: prometheus-web
  12. port: 9090
  13. targetPort: http

(7)、创建配置清单
创建PVC

  1. # kubectl apply -f prom-pvc.yaml
  2. persistentvolume/prometheus-pv created
  3. persistentvolumeclaim/prometheus created
  4. # kubectl get pv -n kube-ops
  5. NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
  6. prometheus-pv 10Gi RWO Recycle Bound kube-ops/prometheus 7s
  7. # kubectl get pvc -n kube-ops
  8. NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
  9. prometheus Bound prometheus-pv 10Gi RWO 13s

创建RBAC

  1. # kubectl apply -f prom-rbac.yaml
  2. serviceaccount/prometheus-sa created
  3. clusterrole.rbac.authorization.k8s.io/prometheus created
  4. clusterrolebinding.rbac.authorization.k8s.io/prometheus created
  5. # kubectl get clusterrole -n kube-ops | grep prometheus
  6. prometheus 35s
  7. # kubectl get clusterrolebinding -n kube-ops | grep prometheus
  8. prometheus 46s

创建Pod

  1. # kubectl apply -f prom-deploy.yaml
  2. deployment.extensions/prometheus-deploy created
  3. # kubectl get deploy -n kube-ops
  4. NAME READY UP-TO-DATE AVAILABLE AGE
  5. prometheus-deploy 1/1 1 0 10s
  6. # kubectl get pod -n kube-ops
  7. NAME READY STATUS RESTARTS AGE
  8. prometheus-deploy-694446b7cb-ssdqm 1/1 Running 0 18s

创建Service

  1. # kubectl apply -f prom-service.yaml
  2. service/prometheus-svc created
  3. # kubectl get svc -n kube-ops
  4. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  5. prometheus-svc NodePort 10.68.254.74 <none> 9090:23050/TCP 6

然后就可以通过浏览器访问WEB界面了
image.png