install_prometheus.yaml 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. # create clusterrolebinding for prometheus serviceaccount
  53. - name: Set cluster-reader permissions for prometheus
  54. oc_adm_policy_user:
  55. state: present
  56. namespace: "{{ openshift_prometheus_namespace }}"
  57. resource_kind: cluster-role
  58. resource_name: cluster-reader
  59. user: "system:serviceaccount:{{ openshift_prometheus_namespace }}:{{ openshift_prometheus_service_name }}"
  60. # create view role for prometheus-reader serviceaccount
  61. - name: Set view permissions for prometheus reader
  62. oc_adm_policy_user:
  63. state: present
  64. namespace: "{{ openshift_prometheus_namespace }}"
  65. resource_kind: cluster-role
  66. resource_name: view
  67. user: "system:serviceaccount:{{ openshift_prometheus_namespace }}:{{ openshift_prometheus_reader_serviceaccount_name }}"
  68. - name: create services for prometheus
  69. oc_service:
  70. name: "{{ openshift_prometheus_service_name }}"
  71. namespace: "{{ openshift_prometheus_namespace }}"
  72. labels:
  73. name: prometheus
  74. annotations:
  75. oprometheus.io/scrape: 'true'
  76. oprometheus.io/scheme: https
  77. service.alpha.openshift.io/serving-cert-secret-name: prometheus-tls
  78. ports:
  79. - name: prometheus
  80. port: "{{ openshift_prometheus_service_port }}"
  81. targetPort: "{{ openshift_prometheus_service_targetport }}"
  82. protocol: TCP
  83. selector:
  84. app: prometheus
  85. - name: create services for alert buffer
  86. oc_service:
  87. name: "{{ openshift_prometheus_alerts_service_name }}"
  88. namespace: "{{ openshift_prometheus_namespace }}"
  89. labels:
  90. name: prometheus
  91. annotations:
  92. service.alpha.openshift.io/serving-cert-secret-name: alerts-tls
  93. ports:
  94. - name: prometheus
  95. port: "{{ openshift_prometheus_service_port }}"
  96. targetPort: "{{ openshift_prometheus_alerts_service_targetport }}"
  97. protocol: TCP
  98. selector:
  99. app: prometheus
  100. - name: create services for alertmanager
  101. oc_service:
  102. name: "{{ openshift_prometheus_alertmanager_service_name }}"
  103. namespace: "{{ openshift_prometheus_namespace }}"
  104. labels:
  105. name: prometheus
  106. annotations:
  107. service.alpha.openshift.io/serving-cert-secret-name: alertmanager-tls
  108. ports:
  109. - name: prometheus
  110. port: "{{ openshift_prometheus_service_port }}"
  111. targetPort: "{{ openshift_prometheus_alertmanager_service_targetport }}"
  112. protocol: TCP
  113. selector:
  114. app: prometheus
  115. # create prometheus and alerts routes
  116. # TODO: oc_route module should support insecureEdgeTerminationPolicy: Redirect
  117. - name: create prometheus and alerts routes
  118. oc_route:
  119. state: present
  120. name: "{{ item.name }}"
  121. host: "{{ item.host }}"
  122. namespace: "{{ openshift_prometheus_namespace }}"
  123. service_name: "{{ item.name }}"
  124. tls_termination: reencrypt
  125. with_items:
  126. - name: prometheus
  127. host: "{{ openshift_prometheus_hostname }}"
  128. - name: alerts
  129. host: "{{ openshift_prometheus_alerts_hostname }}"
  130. - name: alertmanager
  131. host: "{{ openshift_prometheus_alertmanager_hostname }}"
  132. # Storage
  133. - name: create prometheus pvc
  134. oc_pvc:
  135. namespace: "{{ openshift_prometheus_namespace }}"
  136. name: "{{ openshift_prometheus_pvc_name }}"
  137. access_modes: "{{ openshift_prometheus_pvc_access_modes }}"
  138. volume_capacity: "{{ openshift_prometheus_pvc_size }}"
  139. selector: "{{ openshift_prometheus_pvc_pv_selector }}"
  140. storage_class_name: "{{ openshift_prometheus_sc_name }}"
  141. when: openshift_prometheus_storage_type == 'pvc'
  142. - name: create alertmanager pvc
  143. oc_pvc:
  144. namespace: "{{ openshift_prometheus_namespace }}"
  145. name: "{{ openshift_prometheus_alertmanager_pvc_name }}"
  146. access_modes: "{{ openshift_prometheus_alertmanager_pvc_access_modes }}"
  147. volume_capacity: "{{ openshift_prometheus_alertmanager_pvc_size }}"
  148. selector: "{{ openshift_prometheus_alertmanager_pvc_pv_selector }}"
  149. storage_class_name: "{{ openshift_prometheus_alertmanager_sc_name }}"
  150. when: openshift_prometheus_alertmanager_storage_type == 'pvc'
  151. - name: create alertbuffer pvc
  152. oc_pvc:
  153. namespace: "{{ openshift_prometheus_namespace }}"
  154. name: "{{ openshift_prometheus_alertbuffer_pvc_name }}"
  155. access_modes: "{{ openshift_prometheus_alertbuffer_pvc_access_modes }}"
  156. volume_capacity: "{{ openshift_prometheus_alertbuffer_pvc_size }}"
  157. selector: "{{ openshift_prometheus_alertbuffer_pvc_pv_selector }}"
  158. storage_class_name: "{{ openshift_prometheus_alertbuffer_sc_name }}"
  159. when: openshift_prometheus_alertbuffer_storage_type == 'pvc'
  160. # prometheus configmap
  161. # Copy the additional rules file if it is defined
  162. - name: Copy additional rules file to host
  163. copy:
  164. src: "{{ openshift_prometheus_additional_rules_file }}"
  165. dest: "{{ tempdir }}/prometheus.additional.rules"
  166. when:
  167. - openshift_prometheus_additional_rules_file is defined
  168. - openshift_prometheus_additional_rules_file is not none
  169. - openshift_prometheus_additional_rules_file | trim | length > 0
  170. - stat:
  171. path: "{{ tempdir }}/prometheus.additional.rules"
  172. register: additional_rules_stat
  173. - template:
  174. src: prometheus.yml.j2
  175. dest: "{{ tempdir }}/prometheus.yml"
  176. changed_when: no
  177. - template:
  178. src: prometheus.rules.j2
  179. dest: "{{ tempdir }}/prometheus.rules"
  180. changed_when: no
  181. # In prometheus configmap create "additional.rules" section if file exists
  182. - name: Set prometheus configmap
  183. oc_configmap:
  184. state: present
  185. name: "prometheus"
  186. namespace: "{{ openshift_prometheus_namespace }}"
  187. from_file:
  188. prometheus.rules: "{{ tempdir }}/prometheus.rules"
  189. prometheus.additional.rules: "{{ tempdir }}/prometheus.additional.rules"
  190. prometheus.yml: "{{ tempdir }}/prometheus.yml"
  191. when: additional_rules_stat.stat.exists == True
  192. - name: Set prometheus configmap
  193. oc_configmap:
  194. state: present
  195. name: "prometheus"
  196. namespace: "{{ openshift_prometheus_namespace }}"
  197. from_file:
  198. prometheus.rules: "{{ tempdir }}/prometheus.rules"
  199. prometheus.yml: "{{ tempdir }}/prometheus.yml"
  200. when: additional_rules_stat.stat.exists == False
  201. # alertmanager configmap
  202. - template:
  203. src: alertmanager.yml.j2
  204. dest: "{{ tempdir }}/alertmanager.yml"
  205. changed_when: no
  206. - name: Set alertmanager configmap
  207. oc_configmap:
  208. state: present
  209. name: "alertmanager"
  210. namespace: "{{ openshift_prometheus_namespace }}"
  211. from_file:
  212. alertmanager.yml: "{{ tempdir }}/alertmanager.yml"
  213. # create prometheus stateful set
  214. - name: Set prometheus template
  215. template:
  216. src: prometheus.j2
  217. dest: "{{ tempdir }}/templates/prometheus.yaml"
  218. vars:
  219. namespace: "{{ openshift_prometheus_namespace }}"
  220. # prom_replicas: "{{ openshift_prometheus_replicas }}"
  221. - name: Set prometheus stateful set
  222. oc_obj:
  223. state: present
  224. name: "prometheus"
  225. namespace: "{{ openshift_prometheus_namespace }}"
  226. kind: statefulset
  227. files:
  228. - "{{ tempdir }}/templates/prometheus.yaml"
  229. delete_after: true