NATS and Prometheus Operator

Installing the Operators

Install the NATS Operator:

  1. $ kubectl apply -f https://raw.githubusercontent.com/nats-io/nats-operator/master/deploy/00-prereqs.yaml
  2. $ kubectl apply -f https://raw.githubusercontent.com/nats-io/nats-operator/master/deploy/10-deployment.yaml

Install the Prometheus Operator along with its RBAC definition (prometheus-operator service account):

  1. apiVersion: rbac.authorization.k8s.io/v1
  2. kind: ClusterRoleBinding
  3. metadata:
  4. labels:
  5. app.kubernetes.io/component: controller
  6. app.kubernetes.io/name: prometheus-operator
  7. app.kubernetes.io/version: v0.30.0
  8. name: prometheus-operator
  9. roleRef:
  10. apiGroup: rbac.authorization.k8s.io
  11. kind: ClusterRole
  12. name: prometheus-operator
  13. subjects:
  14. - kind: ServiceAccount
  15. name: prometheus-operator
  16. namespace: default
  17. ---
  18. apiVersion: rbac.authorization.k8s.io/v1
  19. kind: ClusterRole
  20. metadata:
  21. labels:
  22. app.kubernetes.io/component: controller
  23. app.kubernetes.io/name: prometheus-operator
  24. app.kubernetes.io/version: v0.30.0
  25. name: prometheus-operator
  26. rules:
  27. - apiGroups:
  28. - apiextensions.k8s.io
  29. resources:
  30. - customresourcedefinitions
  31. verbs:
  32. - '*'
  33. - apiGroups:
  34. - monitoring.coreos.com
  35. resources:
  36. - alertmanagers
  37. - prometheuses
  38. - prometheuses/finalizers
  39. - alertmanagers/finalizers
  40. - servicemonitors
  41. - podmonitors
  42. - prometheusrules
  43. verbs:
  44. - '*'
  45. - apiGroups:
  46. - apps
  47. resources:
  48. - statefulsets
  49. verbs:
  50. - '*'
  51. - apiGroups:
  52. - ""
  53. resources:
  54. - configmaps
  55. - secrets
  56. verbs:
  57. - '*'
  58. - apiGroups:
  59. - ""
  60. resources:
  61. - pods
  62. verbs:
  63. - list
  64. - delete
  65. - apiGroups:
  66. - ""
  67. resources:
  68. - services
  69. - services/finalizers
  70. - endpoints
  71. verbs:
  72. - get
  73. - create
  74. - update
  75. - delete
  76. - apiGroups:
  77. - ""
  78. resources:
  79. - nodes
  80. verbs:
  81. - list
  82. - watch
  83. - apiGroups:
  84. - ""
  85. resources:
  86. - namespaces
  87. verbs:
  88. - get
  89. - list
  90. - watch
  91. ---
  92. apiVersion: apps/v1beta2
  93. kind: Deployment
  94. metadata:
  95. labels:
  96. app.kubernetes.io/component: controller
  97. app.kubernetes.io/name: prometheus-operator
  98. app.kubernetes.io/version: v0.30.0
  99. name: prometheus-operator
  100. namespace: default
  101. spec:
  102. replicas: 1
  103. selector:
  104. matchLabels:
  105. app.kubernetes.io/component: controller
  106. app.kubernetes.io/name: prometheus-operator
  107. template:
  108. metadata:
  109. labels:
  110. app.kubernetes.io/component: controller
  111. app.kubernetes.io/name: prometheus-operator
  112. app.kubernetes.io/version: v0.30.0
  113. spec:
  114. containers:
  115. - args:
  116. - --kubelet-service=kube-system/kubelet
  117. - --logtostderr=true
  118. - --config-reloader-image=quay.io/coreos/configmap-reload:v0.0.1
  119. - --prometheus-config-reloader=quay.io/coreos/prometheus-config-reloader:v0.30.0
  120. image: quay.io/coreos/prometheus-operator:v0.30.0
  121. name: prometheus-operator
  122. ports:
  123. - containerPort: 8080
  124. name: http
  125. resources:
  126. limits:
  127. cpu: 200m
  128. memory: 200Mi
  129. requests:
  130. cpu: 100m
  131. memory: 100Mi
  132. securityContext:
  133. allowPrivilegeEscalation: false
  134. readOnlyRootFilesystem: true
  135. nodeSelector:
  136. beta.kubernetes.io/os: linux
  137. securityContext:
  138. runAsNonRoot: true
  139. runAsUser: 65534
  140. serviceAccountName: prometheus-operator
  141. ---
  142. apiVersion: v1
  143. kind: ServiceAccount
  144. metadata:
  145. labels:
  146. app.kubernetes.io/component: controller
  147. app.kubernetes.io/name: prometheus-operator
  148. app.kubernetes.io/version: v0.30.0
  149. name: prometheus-operator
  150. namespace: default
  151. ---
  152. apiVersion: v1
  153. kind: Service
  154. metadata:
  155. labels:
  156. app.kubernetes.io/component: controller
  157. app.kubernetes.io/name: prometheus-operator
  158. app.kubernetes.io/version: v0.30.0
  159. name: prometheus-operator
  160. namespace: default
  161. spec:
  162. clusterIP: None
  163. ports:
  164. - name: http
  165. port: 8080
  166. targetPort: http
  167. selector:
  168. app.kubernetes.io/component: controller
  169. app.kubernetes.io/name: prometheus-operator

Create a NATS Cluster Instance

  1. apiVersion: "nats.io/v1alpha2"
  2. kind: "NatsCluster"
  3. metadata:
  4. name: "nats-cluster"
  5. spec:
  6. size: 3
  7. version: "1.4.1"
  8. pod:
  9. enableMetrics: true
  10. metricsImage: "synadia/prometheus-nats-exporter"
  11. metricsImageTag: "0.3.0"

Create a Prometheus instance

Create RBAC for the Prometheus instance

  1. ---
  2. apiVersion: v1
  3. kind: ServiceAccount
  4. metadata:
  5. name: prometheus
  6. ---
  7. apiVersion: rbac.authorization.k8s.io/v1beta1
  8. kind: ClusterRole
  9. metadata:
  10. name: prometheus
  11. rules:
  12. - apiGroups: [""]
  13. resources:
  14. - nodes
  15. - services
  16. - endpoints
  17. - pods
  18. verbs: ["get", "list", "watch"]
  19. - apiGroups: [""]
  20. resources:
  21. - configmaps
  22. verbs: ["get"]
  23. - nonResourceURLs: ["/metrics"]
  24. verbs: ["get"]
  25. ---
  26. apiVersion: rbac.authorization.k8s.io/v1beta1
  27. kind: ClusterRoleBinding
  28. metadata:
  29. name: prometheus
  30. roleRef:
  31. apiGroup: rbac.authorization.k8s.io
  32. kind: ClusterRole
  33. name: prometheus
  34. subjects:
  35. - kind: ServiceAccount
  36. name: prometheus
  37. namespace: default

Create the Prometheus instance

  1. ---
  2. apiVersion: monitoring.coreos.com/v1
  3. kind: Prometheus
  4. metadata:
  5. name: prometheus
  6. spec:
  7. serviceAccountName: prometheus
  8. serviceMonitorSelector:
  9. matchLabels:
  10. app: nats
  11. nats_cluster: nats-cluster
  12. resources:
  13. requests:
  14. memory: 400Mi
  15. enableAdminAPI: true

Create the ServiceMonitor for the NATS cluster

  1. apiVersion: monitoring.coreos.com/v1
  2. kind: ServiceMonitor
  3. metadata:
  4. name: nats-cluster
  5. labels:
  6. app: nats
  7. nats_cluster: nats-cluster
  8. spec:
  9. selector:
  10. matchLabels:
  11. app: nats
  12. nats_cluster: nats-cluster
  13. endpoints:
  14. - port: metrics

Confirm

  1. kubectl port-forward prometheus-prometheus-0 9090:9090

Results

NATS and Prometheus Operator - 图1