install_prometheus.yaml 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. ---
  2. # namespace
  3. - name: Add prometheus project
  4. oc_project:
  5. state: "{{ state }}"
  6. name: "{{ openshift_prometheus_namespace }}"
  7. node_selector: "{{ openshift_prometheus_node_selector | oo_selector_to_string_list() }}"
  8. description: Prometheus
  9. # secrets
  10. - name: Set alert and prometheus secrets
  11. oc_secret:
  12. state: "{{ state }}"
  13. name: "{{ item }}-proxy"
  14. namespace: "{{ openshift_prometheus_namespace }}"
  15. contents:
  16. - path: session_secret
  17. data: "{{ 43 | oo_random_word }}="
  18. with_items:
  19. - prometheus
  20. - alerts
  21. # serviceaccount
  22. - name: create prometheus serviceaccount
  23. oc_serviceaccount:
  24. state: "{{ state }}"
  25. name: prometheus
  26. namespace: "{{ openshift_prometheus_namespace }}"
  27. # TODO add annotations when supproted
  28. # annotations:
  29. # serviceaccounts.openshift.io/oauth-redirectreference.prom: '{"kind":"OAuthRedirectReference","apiVersion":"v1","reference":{"kind":"Route","name":"prometheus"}}'
  30. # serviceaccounts.openshift.io/oauth-redirectreference.alerts: '{"kind":"OAuthRedirectReference","apiVersion":"v1","reference":{"kind":"Route","name":"alerts"}}'
  31. secrets:
  32. - prometheus-secrets
  33. changed_when: no
  34. # TODO remove this when annotations are supported by oc_serviceaccount
  35. - name: annotate serviceaccount
  36. command: >
  37. {{ openshift.common.client_binary }} annotate --overwrite -n {{ openshift_prometheus_namespace }}
  38. serviceaccount prometheus
  39. serviceaccounts.openshift.io/oauth-redirectreference.prom='{"kind":"OAuthRedirectReference","apiVersion":"v1","reference":{"kind":"Route","name":"prometheus"}}'
  40. serviceaccounts.openshift.io/oauth-redirectreference.alerts='{"kind":"OAuthRedirectReference","apiVersion":"v1","reference":{"kind":"Route","name":"alerts"}}'
  41. # create clusterrolebinding for prometheus serviceaccount
  42. - name: Set cluster-reader permissions for prometheus
  43. oc_adm_policy_user:
  44. state: "{{ state }}"
  45. namespace: "{{ openshift_prometheus_namespace }}"
  46. resource_kind: cluster-role
  47. resource_name: cluster-reader
  48. user: "system:serviceaccount:{{ openshift_prometheus_namespace }}:prometheus"
  49. ######################################################################
  50. # NFS
  51. # In the case that we are not running on a cloud provider, volumes must be statically provisioned
  52. - include: nfs.yaml
  53. when: not (openshift_cloudprovider_kind is defined and (openshift_cloudprovider_kind == 'aws' or openshift_cloudprovider_kind == 'gce'))
  54. # create prometheus and alerts services
  55. # TODO join into 1 task with loop
  56. - name: Create prometheus service
  57. oc_service:
  58. state: "{{ state }}"
  59. name: "{{ item.name }}"
  60. namespace: "{{ openshift_prometheus_namespace }}"
  61. selector:
  62. app: prometheus
  63. labels:
  64. name: "{{ item.name }}"
  65. # TODO add annotations when supported
  66. # annotations:
  67. # service.alpha.openshift.io/serving-cert-secret-name: "{{item.name}}-tls"
  68. ports:
  69. - port: 443
  70. targetPort: 8443
  71. with_items:
  72. - name: prometheus
  73. - name: Create alerts service
  74. oc_service:
  75. state: "{{ state }}"
  76. name: "{{ item.name }}"
  77. namespace: "{{ openshift_prometheus_namespace }}"
  78. selector:
  79. app: prometheus
  80. labels:
  81. name: "{{ item.name }}"
  82. # TODO add annotations when supported
  83. # annotations:
  84. # service.alpha.openshift.io/serving-cert-secret-name: "{{item.name}}-tls"
  85. ports:
  86. - port: 443
  87. targetPort: 9443
  88. with_items:
  89. - name: alerts
  90. # Annotate services with secret name
  91. # TODO remove this when annotations are supported by oc_service
  92. - name: annotate prometheus service
  93. command: >
  94. {{ openshift.common.client_binary }} annotate --overwrite -n {{ openshift_prometheus_namespace }}
  95. service prometheus
  96. prometheus.io/scrape='true'
  97. prometheus.io/scheme=https
  98. service.alpha.openshift.io/serving-cert-secret-name=prometheus-tls
  99. - name: annotate alerts service
  100. command: >
  101. {{ openshift.common.client_binary }} annotate --overwrite -n {{ openshift_prometheus_namespace }}
  102. service alerts 'service.alpha.openshift.io/serving-cert-secret-name=prometheus-alerts-tls'
  103. # create prometheus and alerts routes
  104. - name: create prometheus and alerts routes
  105. oc_route:
  106. state: "{{ state }}"
  107. name: "{{ item.name }}"
  108. namespace: "{{ openshift_prometheus_namespace }}"
  109. service_name: "{{ item.name }}"
  110. tls_termination: reencrypt
  111. with_items:
  112. - name: prometheus
  113. - name: alerts
  114. # Storage
  115. - name: create prometheus pvc
  116. oc_pvc:
  117. namespace: "{{ openshift_prometheus_namespace }}"
  118. name: "{{ openshift_prometheus_pvc_name }}"
  119. access_modes: "{{ openshift_prometheus_pvc_access_modes }}"
  120. volume_capacity: "{{ openshift_prometheus_pvc_size }}"
  121. selector: "{{ openshift_prometheus_pvc_pv_selector }}"
  122. - name: create alertmanager pvc
  123. oc_pvc:
  124. namespace: "{{ openshift_prometheus_namespace }}"
  125. name: "{{ openshift_prometheus_alertmanager_pvc_name }}"
  126. access_modes: "{{ openshift_prometheus_alertmanager_pvc_access_modes }}"
  127. volume_capacity: "{{ openshift_prometheus_alertmanager_pvc_size }}"
  128. selector: "{{ openshift_prometheus_alertmanager_pvc_pv_selector }}"
  129. - name: create alertbuffer pvc
  130. oc_pvc:
  131. namespace: "{{ openshift_prometheus_namespace }}"
  132. name: "{{ openshift_prometheus_alertbuffer_pvc_name }}"
  133. access_modes: "{{ openshift_prometheus_alertbuffer_pvc_access_modes }}"
  134. volume_capacity: "{{ openshift_prometheus_alertbuffer_pvc_size }}"
  135. selector: "{{ openshift_prometheus_alertbuffer_pvc_pv_selector }}"
  136. # create prometheus deployment
  137. - name: Set prometheus deployment template
  138. template:
  139. src: prometheus_deployment.j2
  140. dest: "{{ tempdir }}/templates/prometheus.yaml"
  141. vars:
  142. namespace: "{{ openshift_prometheus_namespace }}"
  143. prom_replicas: "{{ openshift_prometheus_replicas }}"
  144. - name: Set prometheus deployment
  145. oc_obj:
  146. state: "{{ state }}"
  147. name: "prometheus"
  148. namespace: "{{ openshift_prometheus_namespace }}"
  149. kind: deployment
  150. files:
  151. - "{{ tempdir }}/templates/prometheus.yaml"
  152. delete_after: true
  153. # prometheus configmap
  154. # Copy the additional rules file if it is defined
  155. - name: Copy additional rules file to host
  156. copy:
  157. src: "{{ openshift_prometheus_additional_rules_file }}"
  158. dest: "{{ tempdir }}/prometheus.additional.rules"
  159. when:
  160. - openshift_prometheus_additional_rules_file is defined
  161. - openshift_prometheus_additional_rules_file is not none
  162. - openshift_prometheus_additional_rules_file | trim | length > 0
  163. - stat:
  164. path: "{{ tempdir }}/prometheus.additional.rules"
  165. register: additional_rules_stat
  166. # The kubernetes version impacts the prometheus scraping endpoint
  167. # so gathering it before constructing the configmap
  168. - name: get oc version
  169. oc_version:
  170. register: oc_version
  171. - set_fact:
  172. kubernetes_version: "{{ oc_version.results.kubernetes_short | float }}"
  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: "{{ state }}"
  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: "{{ state }}"
  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: "{{ state }}"
  209. name: "prometheus-alerts"
  210. namespace: "{{ openshift_prometheus_namespace }}"
  211. from_file:
  212. alertmanager.yml: "{{ tempdir }}/alertmanager.yml"