创建命名空间

  1. apiVersion: v1
  2. kind: Namespace
  3. metadata:
  4. name: monitor

准备prometheus配置文件,因此使用configmap的形式保存

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

prometheus的资源文件(deployment)

出现Prometheus数据存储权限问题,因为Prometheus内部使用nobody启动进程,挂载数据目录后权限为root,因此使用initContainer进行目录权限修复:

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: prometheus
  5. namespace: monitor
  6. labels:
  7. app: prometheus
  8. spec:
  9. selector:
  10. matchLabels:
  11. app: prometheus
  12. template:
  13. metadata:
  14. labels:
  15. app: prometheus
  16. spec:
  17. serviceAccountName: prometheus
  18. nodeSelector:
  19. app: prometheus
  20. initContainers:
  21. - name: "change-permission-of-directory"
  22. image: busybox
  23. command: ["/bin/sh"]
  24. args: ["-c", "chown -R 65534:65534 /prometheus"]
  25. securityContext:
  26. privileged: true
  27. volumeMounts:
  28. - mountPath: "/etc/prometheus"
  29. name: config-volume
  30. - mountPath: "/prometheus"
  31. name: data
  32. containers:
  33. - image: prom/prometheus:v2.19.2
  34. name: prometheus
  35. args:
  36. - "--config.file=/etc/prometheus/prometheus.yml"
  37. - "--storage.tsdb.path=/prometheus" # 指定tsdb数据路径
  38. - "--web.enable-lifecycle" # 支持热更新,直接执行localhost:9090/-/reload立即生效
  39. - "--web.console.libraries=/usr/share/prometheus/console_libraries"
  40. - "--web.console.templates=/usr/share/prometheus/consoles"
  41. ports:
  42. - containerPort: 9090
  43. name: http
  44. volumeMounts:
  45. - mountPath: "/etc/prometheus"
  46. name: config-volume
  47. - mountPath: "/prometheus"
  48. name: data
  49. resources:
  50. requests:
  51. cpu: 100m
  52. memory: 512Mi
  53. limits:
  54. cpu: 100m
  55. memory: 512Mi
  56. volumes:
  57. - name: data
  58. hostPath:
  59. path: /data/prometheus/
  60. - configMap:
  61. name: prometheus-config
  62. name: config-volume

创建rbac,prometheus会调用k8s api做服务发现进行抓取指标

  1. apiVersion: v1
  2. kind: ServiceAccount
  3. metadata:
  4. name: prometheus
  5. namespace: monitor
  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/v1beta1
  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: monitor

提供Service,为Ingress使用

  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. name: prometheus
  5. namespace: monitor
  6. labels:
  7. app: prometheus
  8. spec:
  9. selector:
  10. app: prometheus
  11. type: ClusterIP
  12. ports:
  13. - name: web
  14. port: 9090
  15. targetPort: http
  16. $ cat prometheus-ingress.yaml
  17. apiVersion: extensions/v1beta1
  18. kind: Ingress
  19. metadata:
  20. name: prometheus
  21. namespace: monitor
  22. spec:
  23. rules:
  24. - host: prometheus.luffy.com
  25. http:
  26. paths:
  27. - path: /
  28. backend:
  29. serviceName: prometheus
  30. servicePort: 9090

部署上述资源

  1. # 命名空间
  2. $ kubectl create prometheus-namespace.yaml
  3. # 给node打上label
  4. $ kubectl label node k8s-slave1 app=prometheus
  5. #部署configmap
  6. $ kubectl create -f prometheus-configmap.yaml
  7. # rbac
  8. $ kubectl create -f prometheus-rbac.yaml
  9. # deployment
  10. $ kubectl create -f prometheus-deployment.yaml
  11. # service
  12. $ kubectl create -f prometheus-svc.yaml
  13. # ingress
  14. $ kubectl create -f prometheus-ingress.yaml
  15. # 访问测试
  16. $ kubectl -n monitor get ingress