install_prometheus.yaml 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. ---
  2. # set facts
  3. - include_tasks: facts.yaml
  4. - name: Ensure that Prometheus has nodes to run on
  5. import_role:
  6. name: openshift_control_plane
  7. tasks_from: ensure_nodes_matching_selector.yml
  8. vars:
  9. openshift_master_ensure_nodes_selector: "{{ openshift_prometheus_node_selector | map_to_pairs }}"
  10. openshift_master_ensure_nodes_service: Prometheus
  11. # namespace
  12. - name: Add prometheus project
  13. oc_project:
  14. state: present
  15. name: "{{ openshift_prometheus_namespace }}"
  16. node_selector: ""
  17. description: Prometheus
  18. # secrets
  19. - name: Set alert, alertmanager and prometheus secrets
  20. oc_secret:
  21. state: present
  22. name: "{{ item }}-proxy"
  23. namespace: "{{ openshift_prometheus_namespace }}"
  24. contents:
  25. - path: session_secret
  26. data: "{{ 43 | lib_utils_oo_random_word }}="
  27. with_items:
  28. - prometheus
  29. - alerts
  30. - alertmanager
  31. # serviceaccount
  32. - name: create prometheus serviceaccount
  33. oc_serviceaccount:
  34. state: present
  35. name: "{{ openshift_prometheus_service_name }}"
  36. namespace: "{{ openshift_prometheus_namespace }}"
  37. changed_when: no
  38. # serviceaccount reader
  39. - name: create openshift_prometheus_reader_serviceaccount_name serviceaccount
  40. oc_serviceaccount:
  41. state: present
  42. name: "{{ openshift_prometheus_reader_serviceaccount_name }}"
  43. namespace: "{{ openshift_prometheus_namespace }}"
  44. changed_when: no
  45. # TODO remove this when annotations are supported by oc_serviceaccount
  46. - name: annotate serviceaccount
  47. command: >
  48. {{ openshift_client_binary }} --config={{ openshift.common.config_base }}/master/admin.kubeconfig annotate --overwrite -n {{ openshift_prometheus_namespace }}
  49. serviceaccount {{ openshift_prometheus_service_name }} {{ item }}
  50. with_items:
  51. "{{ openshift_prometheus_serviceaccount_annotations }}"
  52. # add required permissions to prometheus for scraping router metrics
  53. - name: Create router-metrics cluster role
  54. oc_clusterrole:
  55. state: present
  56. name: router-metrics
  57. rules:
  58. - apiGroups: ["route.openshift.io"]
  59. resources: ["routers/metrics"]
  60. verbs: ["get"]
  61. # create clusterrolebinding for prometheus serviceaccount
  62. - name: Set clusterrole permissions for prometheus
  63. oc_adm_policy_user:
  64. state: present
  65. namespace: "{{ openshift_prometheus_namespace }}"
  66. resource_kind: cluster-role
  67. resource_name: "{{ item }}"
  68. user: "system:serviceaccount:{{ openshift_prometheus_namespace }}:{{ openshift_prometheus_service_name }}"
  69. with_items:
  70. - cluster-reader
  71. - router-metrics
  72. # create view role for prometheus-reader serviceaccount
  73. - name: Set view permissions for prometheus reader
  74. oc_adm_policy_user:
  75. state: present
  76. namespace: "{{ openshift_prometheus_namespace }}"
  77. resource_kind: cluster-role
  78. resource_name: view
  79. user: "system:serviceaccount:{{ openshift_prometheus_namespace }}:{{ openshift_prometheus_reader_serviceaccount_name }}"
  80. - name: create services for prometheus
  81. oc_service:
  82. name: "{{ openshift_prometheus_service_name }}"
  83. namespace: "{{ openshift_prometheus_namespace }}"
  84. labels:
  85. name: prometheus
  86. annotations:
  87. oprometheus.io/scrape: 'true'
  88. oprometheus.io/scheme: https
  89. service.alpha.openshift.io/serving-cert-secret-name: prometheus-tls
  90. ports:
  91. - name: prometheus
  92. port: "{{ openshift_prometheus_service_port }}"
  93. targetPort: "{{ openshift_prometheus_service_targetport }}"
  94. protocol: TCP
  95. selector:
  96. app: prometheus
  97. - name: create services for alert buffer
  98. oc_service:
  99. name: "{{ openshift_prometheus_alerts_service_name }}"
  100. namespace: "{{ openshift_prometheus_namespace }}"
  101. labels:
  102. name: prometheus
  103. annotations:
  104. service.alpha.openshift.io/serving-cert-secret-name: alerts-tls
  105. ports:
  106. - name: prometheus
  107. port: "{{ openshift_prometheus_service_port }}"
  108. targetPort: "{{ openshift_prometheus_alerts_service_targetport }}"
  109. protocol: TCP
  110. selector:
  111. app: prometheus
  112. - name: create services for alertmanager
  113. oc_service:
  114. name: "{{ openshift_prometheus_alertmanager_service_name }}"
  115. namespace: "{{ openshift_prometheus_namespace }}"
  116. labels:
  117. name: prometheus
  118. annotations:
  119. service.alpha.openshift.io/serving-cert-secret-name: alertmanager-tls
  120. ports:
  121. - name: prometheus
  122. port: "{{ openshift_prometheus_service_port }}"
  123. targetPort: "{{ openshift_prometheus_alertmanager_service_targetport }}"
  124. protocol: TCP
  125. selector:
  126. app: prometheus
  127. # create prometheus and alerts routes
  128. # TODO: oc_route module should support insecureEdgeTerminationPolicy: Redirect
  129. - name: create prometheus and alerts routes
  130. oc_route:
  131. state: present
  132. name: "{{ item.name }}"
  133. host: "{{ item.host }}"
  134. namespace: "{{ openshift_prometheus_namespace }}"
  135. service_name: "{{ item.name }}"
  136. tls_termination: reencrypt
  137. with_items:
  138. - name: prometheus
  139. host: "{{ openshift_prometheus_hostname }}"
  140. - name: alerts
  141. host: "{{ openshift_prometheus_alerts_hostname }}"
  142. - name: alertmanager
  143. host: "{{ openshift_prometheus_alertmanager_hostname }}"
  144. # Storage
  145. - name: create prometheus pvc
  146. oc_pvc:
  147. namespace: "{{ openshift_prometheus_namespace }}"
  148. name: "{{ openshift_prometheus_pvc_name }}"
  149. access_modes: "{{ openshift_prometheus_pvc_access_modes }}"
  150. volume_capacity: "{{ openshift_prometheus_pvc_size }}"
  151. selector: "{{ openshift_prometheus_pvc_pv_selector }}"
  152. storage_class_name: "{{ openshift_prometheus_sc_name }}"
  153. when: openshift_prometheus_storage_type == 'pvc'
  154. - name: create alertmanager pvc
  155. oc_pvc:
  156. namespace: "{{ openshift_prometheus_namespace }}"
  157. name: "{{ openshift_prometheus_alertmanager_pvc_name }}"
  158. access_modes: "{{ openshift_prometheus_alertmanager_pvc_access_modes }}"
  159. volume_capacity: "{{ openshift_prometheus_alertmanager_pvc_size }}"
  160. selector: "{{ openshift_prometheus_alertmanager_pvc_pv_selector }}"
  161. storage_class_name: "{{ openshift_prometheus_alertmanager_sc_name }}"
  162. when: openshift_prometheus_alertmanager_storage_type == 'pvc'
  163. - name: create alertbuffer pvc
  164. oc_pvc:
  165. namespace: "{{ openshift_prometheus_namespace }}"
  166. name: "{{ openshift_prometheus_alertbuffer_pvc_name }}"
  167. access_modes: "{{ openshift_prometheus_alertbuffer_pvc_access_modes }}"
  168. volume_capacity: "{{ openshift_prometheus_alertbuffer_pvc_size }}"
  169. selector: "{{ openshift_prometheus_alertbuffer_pvc_pv_selector }}"
  170. storage_class_name: "{{ openshift_prometheus_alertbuffer_sc_name }}"
  171. when: openshift_prometheus_alertbuffer_storage_type == 'pvc'
  172. # prometheus configmap
  173. # Copy the additional rules file if it is defined
  174. - name: Copy additional rules file to host
  175. copy:
  176. src: "{{ openshift_prometheus_additional_rules_file }}"
  177. dest: "{{ tempdir }}/prometheus.additional.rules"
  178. when:
  179. - openshift_prometheus_additional_rules_file is defined
  180. - openshift_prometheus_additional_rules_file is not none
  181. - openshift_prometheus_additional_rules_file | trim | length > 0
  182. - stat:
  183. path: "{{ tempdir }}/prometheus.additional.rules"
  184. register: additional_rules_stat
  185. - template:
  186. src: prometheus.yml.j2
  187. dest: "{{ tempdir }}/prometheus.yml"
  188. changed_when: no
  189. - template:
  190. src: prometheus.rules.j2
  191. dest: "{{ tempdir }}/prometheus.rules"
  192. changed_when: no
  193. # In prometheus configmap create "additional.rules" section if file exists
  194. - name: Set prometheus configmap
  195. oc_configmap:
  196. state: present
  197. name: "prometheus"
  198. namespace: "{{ openshift_prometheus_namespace }}"
  199. from_file:
  200. prometheus.rules: "{{ tempdir }}/prometheus.rules"
  201. prometheus.additional.rules: "{{ tempdir }}/prometheus.additional.rules"
  202. prometheus.yml: "{{ tempdir }}/prometheus.yml"
  203. when: additional_rules_stat.stat.exists == True
  204. - name: Set prometheus configmap
  205. oc_configmap:
  206. state: present
  207. name: "prometheus"
  208. namespace: "{{ openshift_prometheus_namespace }}"
  209. from_file:
  210. prometheus.rules: "{{ tempdir }}/prometheus.rules"
  211. prometheus.yml: "{{ tempdir }}/prometheus.yml"
  212. when: additional_rules_stat.stat.exists == False
  213. # alertmanager configmap
  214. - template:
  215. src: alertmanager.yml.j2
  216. dest: "{{ tempdir }}/alertmanager.yml"
  217. changed_when: no
  218. - name: Set alertmanager configmap
  219. oc_configmap:
  220. state: present
  221. name: "alertmanager"
  222. namespace: "{{ openshift_prometheus_namespace }}"
  223. from_file:
  224. alertmanager.yml: "{{ tempdir }}/alertmanager.yml"
  225. # create prometheus stateful set
  226. - name: Set prometheus template
  227. template:
  228. src: prometheus.j2
  229. dest: "{{ tempdir }}/templates/prometheus.yaml"
  230. vars:
  231. namespace: "{{ openshift_prometheus_namespace }}"
  232. # prom_replicas: "{{ openshift_prometheus_replicas }}"
  233. - name: Set prometheus stateful set
  234. oc_obj:
  235. state: present
  236. name: "prometheus"
  237. namespace: "{{ openshift_prometheus_namespace }}"
  238. kind: statefulset
  239. files:
  240. - "{{ tempdir }}/templates/prometheus.yaml"
  241. delete_after: true