|  | 
|  | 1 | +# Consuming Metrics | 
|  | 2 | + | 
|  | 3 | +!!! warning | 
|  | 4 | +Metrics endpoints and ports are available as an alpha release and are subject to change in future versions. | 
|  | 5 | +The following procedure is provided as an example for testing purposes. Do not depend on alpha features in production clusters. | 
|  | 6 | + | 
|  | 7 | +Operator-Controller and CatalogD are configured to export metrics by default. The metrics are exposed on the `/metrics` endpoint of the respective services. | 
|  | 8 | + | 
|  | 9 | +The metrics are secured by [RBAC policies][rbac-k8s-docs], requiring appropriate permissions for access. | 
|  | 10 | +By default, they are exposed over HTTPS, necessitating valid certificates for integration with services like Prometheus. | 
|  | 11 | +The following sections cover enabling metrics, validating access, and integrating with the [Prometheus Operator][prometheus-operator]. | 
|  | 12 | + | 
|  | 13 | +Below, you will learn how to enable the metrics, validate access, and integrate with [Prometheus Operator][prometheus-operator]. | 
|  | 14 | + | 
|  | 15 | +--- | 
|  | 16 | + | 
|  | 17 | +## Operator-Controller Metrics | 
|  | 18 | + | 
|  | 19 | +### Step 1: Enable Access | 
|  | 20 | + | 
|  | 21 | +To enable access to the Operator-Controller metrics, create a `ClusterRoleBinding` to | 
|  | 22 | +allow the Operator-Controller service account to access the metrics. | 
|  | 23 | + | 
|  | 24 | +```shell | 
|  | 25 | +kubectl create clusterrolebinding operator-controller-metrics-binding \ | 
|  | 26 | +   --clusterrole=operator-controller-metrics-reader \ | 
|  | 27 | +   --serviceaccount=olmv1-system:operator-controller-controller-manager | 
|  | 28 | +``` | 
|  | 29 | + | 
|  | 30 | +### Step 2: Validate Access Manually | 
|  | 31 | + | 
|  | 32 | +#### Create a Token and Extract Certificates | 
|  | 33 | + | 
|  | 34 | +Generate a token for the service account and extract the required certificates: | 
|  | 35 | + | 
|  | 36 | +```shell | 
|  | 37 | +TOKEN=$(kubectl create token operator-controller-controller-manager -n olmv1-system) | 
|  | 38 | +echo $TOKEN | 
|  | 39 | +``` | 
|  | 40 | + | 
|  | 41 | +#### Deploy a Pod to Consume Metrics | 
|  | 42 | + | 
|  | 43 | +Ensure that the Pod is deployed in a namespace labeled to enforce restricted permissions. Apply the following: | 
|  | 44 | + | 
|  | 45 | +```shell | 
|  | 46 | +kubectl apply -f - <<EOF | 
|  | 47 | +apiVersion: v1 | 
|  | 48 | +kind: Pod | 
|  | 49 | +metadata: | 
|  | 50 | +  name: curl-metrics | 
|  | 51 | +  namespace: olmv1-system | 
|  | 52 | +spec: | 
|  | 53 | +  serviceAccountName: operator-controller-controller-manager | 
|  | 54 | +  containers: | 
|  | 55 | +  - name: curl | 
|  | 56 | +    image: curlimages/curl:latest | 
|  | 57 | +    command: | 
|  | 58 | +    - sh | 
|  | 59 | +    - -c | 
|  | 60 | +    - sleep 3600 | 
|  | 61 | +    securityContext: | 
|  | 62 | +      runAsNonRoot: true | 
|  | 63 | +      readOnlyRootFilesystem: true | 
|  | 64 | +      runAsUser: 1000 | 
|  | 65 | +      runAsGroup: 1000 | 
|  | 66 | +      allowPrivilegeEscalation: false | 
|  | 67 | +      capabilities: | 
|  | 68 | +        drop: | 
|  | 69 | +        - ALL | 
|  | 70 | +    volumeMounts: | 
|  | 71 | +    - mountPath: /tmp/cert | 
|  | 72 | +      name: olm-cert | 
|  | 73 | +      readOnly: true | 
|  | 74 | +  volumes: | 
|  | 75 | +  - name: olm-cert | 
|  | 76 | +    secret: | 
|  | 77 | +      secretName: olmv1-cert | 
|  | 78 | +  securityContext: | 
|  | 79 | +    runAsNonRoot: true | 
|  | 80 | +  restartPolicy: Never | 
|  | 81 | +EOF | 
|  | 82 | +``` | 
|  | 83 | + | 
|  | 84 | +#### Access the Pod and Test Metrics | 
|  | 85 | + | 
|  | 86 | +Access the pod: | 
|  | 87 | + | 
|  | 88 | +```shell | 
|  | 89 | +kubectl exec -it curl-metrics -n olmv1-system -- sh | 
|  | 90 | +``` | 
|  | 91 | + | 
|  | 92 | +From the shell use the `TOKEN` value obtained above to check the metrics: | 
|  | 93 | + | 
|  | 94 | +```shell | 
|  | 95 | +curl -v -k -H "Authorization: Bearer <TOKEN>" \ | 
|  | 96 | +https://operator-controller-service.olmv1-system.svc.cluster.local:8443/metrics | 
|  | 97 | +``` | 
|  | 98 | + | 
|  | 99 | +Validate using certificates and token: | 
|  | 100 | + | 
|  | 101 | +```shell | 
|  | 102 | +curl -v --cacert /tmp/cert/ca.crt --cert /tmp/cert/tls.crt --key /tmp/cert/tls.key \ | 
|  | 103 | +-H "Authorization: Bearer <TOKEN>" \ | 
|  | 104 | +https://operator-controller-service.olmv1-system.svc.cluster.local:8443/metrics | 
|  | 105 | +``` | 
|  | 106 | + | 
|  | 107 | +--- | 
|  | 108 | + | 
|  | 109 | +## CatalogD Metrics | 
|  | 110 | + | 
|  | 111 | +### Step 1: Enable Access | 
|  | 112 | + | 
|  | 113 | +To enable access to the CatalogD metrics, create a `ClusterRoleBinding` for the CatalogD service account: | 
|  | 114 | + | 
|  | 115 | +```shell | 
|  | 116 | +kubectl create clusterrolebinding catalogd-metrics-binding \ | 
|  | 117 | +   --clusterrole=catalogd-metrics-reader \ | 
|  | 118 | +   --serviceaccount=olmv1-system:catalogd-controller-manager | 
|  | 119 | +``` | 
|  | 120 | + | 
|  | 121 | +### Step 2: Validate Access Manually | 
|  | 122 | + | 
|  | 123 | +#### Create a Token and Extract Certificates | 
|  | 124 | + | 
|  | 125 | +Generate a token and get the required certificates: | 
|  | 126 | + | 
|  | 127 | +```shell | 
|  | 128 | +TOKEN=$(kubectl create token catalogd-controller-manager -n olmv1-system) | 
|  | 129 | +echo $TOKEN | 
|  | 130 | +``` | 
|  | 131 | + | 
|  | 132 | +#### Deploy a Pod to Consume Metrics | 
|  | 133 | + | 
|  | 134 | +From the shell use the `TOKEN` value obtained above to check the metrics: | 
|  | 135 | + | 
|  | 136 | +```shell | 
|  | 137 | +OLM_SECRET=$(kubectl get secret -n olmv1-system -o jsonpath="{.items[?(@.metadata.name | startswith('catalogd-service-cert'))].metadata.name}") | 
|  | 138 | +``` | 
|  | 139 | + | 
|  | 140 | +```shell | 
|  | 141 | +kubectl apply -f - <<EOF | 
|  | 142 | +apiVersion: v1 | 
|  | 143 | +kind: Pod | 
|  | 144 | +metadata: | 
|  | 145 | +  name: curl-metrics-catalogd | 
|  | 146 | +  namespace: olmv1-system | 
|  | 147 | +spec: | 
|  | 148 | +  serviceAccountName: catalogd-controller-manager | 
|  | 149 | +  containers: | 
|  | 150 | +  - name: curl | 
|  | 151 | +    image: curlimages/curl:latest | 
|  | 152 | +    command: | 
|  | 153 | +    - sh | 
|  | 154 | +    - -c | 
|  | 155 | +    - sleep 3600 | 
|  | 156 | +    securityContext: | 
|  | 157 | +      runAsNonRoot: true | 
|  | 158 | +      readOnlyRootFilesystem: true | 
|  | 159 | +      runAsUser: 1000 | 
|  | 160 | +      runAsGroup: 1000 | 
|  | 161 | +      allowPrivilegeEscalation: false | 
|  | 162 | +      capabilities: | 
|  | 163 | +        drop: | 
|  | 164 | +        - ALL | 
|  | 165 | +    volumeMounts: | 
|  | 166 | +    - mountPath: /tmp/cert | 
|  | 167 | +      name: catalogd-cert | 
|  | 168 | +      readOnly: true | 
|  | 169 | +  volumes: | 
|  | 170 | +  - name: catalogd-cert | 
|  | 171 | +    secret: | 
|  | 172 | +      secretName: $OLM_SECRET | 
|  | 173 | +  securityContext: | 
|  | 174 | +    runAsNonRoot: true | 
|  | 175 | +  restartPolicy: Never | 
|  | 176 | +EOF | 
|  | 177 | +``` | 
|  | 178 | + | 
|  | 179 | +#### Access the Pod and Test Metrics | 
|  | 180 | + | 
|  | 181 | +Access the pod: | 
|  | 182 | + | 
|  | 183 | +```shell | 
|  | 184 | +kubectl exec -it curl-metrics-catalogd -n olmv1-system -- sh | 
|  | 185 | +``` | 
|  | 186 | + | 
|  | 187 | +From the shell use the `TOKEN` value obtained above to check the metrics: | 
|  | 188 | + | 
|  | 189 | +```shell | 
|  | 190 | +curl -v -k -H "Authorization: Bearer <TOKEN>" \ | 
|  | 191 | +https://catalogd-service.olmv1-system.svc.cluster.local:7443/metrics | 
|  | 192 | +``` | 
|  | 193 | + | 
|  | 194 | +Validate using certificates and token: | 
|  | 195 | + | 
|  | 196 | +```shell | 
|  | 197 | +curl -v --cacert /tmp/cert/ca.crt --cert /tmp/cert/tls.crt --key /tmp/cert/tls.key \ | 
|  | 198 | +-H "Authorization: Bearer <TOKEN>" \ | 
|  | 199 | +https://catalogd-service.olmv1-system.svc.cluster.local:7443/metrics | 
|  | 200 | +``` | 
|  | 201 | + | 
|  | 202 | +--- | 
|  | 203 | + | 
|  | 204 | +## Enabling Integration with Prometheus | 
|  | 205 | + | 
|  | 206 | +If using [Prometheus Operator][prometheus-operator], create a `ServiceMonitor` to scrape metrics: | 
|  | 207 | + | 
|  | 208 | +!!! note | 
|  | 209 | +The following manifests are provided as examples. You may need to configure certain settings, such as `serviceMonitorSelector`,   | 
|  | 210 | +to ensure that metrics are properly scraped. This will depend on how Prometheus is configured and, for example, the namespace   | 
|  | 211 | +where the `ServiceMonitor` is applied. | 
|  | 212 | + | 
|  | 213 | +### For Operator-Controller | 
|  | 214 | + | 
|  | 215 | +```shell | 
|  | 216 | +kubectl apply -f - <<EOF | 
|  | 217 | +apiVersion: monitoring.coreos.com/v1 | 
|  | 218 | +kind: ServiceMonitor | 
|  | 219 | +metadata: | 
|  | 220 | +  labels: | 
|  | 221 | +    control-plane: operator-controller-controller-manager | 
|  | 222 | +  name: controller-manager-metrics-monitor | 
|  | 223 | +  namespace: system | 
|  | 224 | +spec: | 
|  | 225 | +  endpoints: | 
|  | 226 | +    - path: /metrics | 
|  | 227 | +      port: https | 
|  | 228 | +      scheme: https | 
|  | 229 | +      bearerTokenFile: /var/run/secrets/kubernetes.io/serviceaccount/token | 
|  | 230 | +      tlsConfig: | 
|  | 231 | +        insecureSkipVerify: false  | 
|  | 232 | +        serverName: operator-controller-controller-manager-metrics-service.olmv1-system.svc | 
|  | 233 | +        ca: | 
|  | 234 | +          secret: | 
|  | 235 | +            name: olmv1-cert | 
|  | 236 | +            key: ca.crt | 
|  | 237 | +        cert: | 
|  | 238 | +          secret: | 
|  | 239 | +            name: olmv1-cert | 
|  | 240 | +            key: tls.crt | 
|  | 241 | +        keySecret: | 
|  | 242 | +          name: olmv1-cert | 
|  | 243 | +          key: tls.key | 
|  | 244 | +  selector: | 
|  | 245 | +    matchLabels: | 
|  | 246 | +      control-plane: operator-controller-controller-manager | 
|  | 247 | +EOF | 
|  | 248 | +``` | 
|  | 249 | + | 
|  | 250 | +### For CatalogD | 
|  | 251 | + | 
|  | 252 | + | 
|  | 253 | +```shell | 
|  | 254 | +OLM_SECRET=$(kubectl get secret -n olmv1-system -o jsonpath="{.items[?(@.metadata.name | startswith('catalogd-service-cert'))].metadata.name}") | 
|  | 255 | +``` | 
|  | 256 | + | 
|  | 257 | +```shell | 
|  | 258 | +kubectl apply -f - <<EOF | 
|  | 259 | +apiVersion: monitoring.coreos.com/v1 | 
|  | 260 | +kind: ServiceMonitor | 
|  | 261 | +metadata: | 
|  | 262 | +  labels: | 
|  | 263 | +    control-plane: catalogd-controller-manager | 
|  | 264 | +  name: catalogd-metrics-monitor | 
|  | 265 | +  namespace: system | 
|  | 266 | +spec: | 
|  | 267 | +  endpoints: | 
|  | 268 | +    - path: /metrics | 
|  | 269 | +      port: https | 
|  | 270 | +      scheme: https | 
|  | 271 | +      bearerTokenFile: /var/run/secrets/kubernetes.io/serviceaccount/token | 
|  | 272 | +      tlsConfig: | 
|  | 273 | +        serverName: catalogd-service.olmv1-system.svc | 
|  | 274 | +        insecureSkipVerify: false | 
|  | 275 | +        ca: | 
|  | 276 | +          secret: | 
|  | 277 | +            name: $OLM_SECRET | 
|  | 278 | +            key: ca.crt | 
|  | 279 | +        cert: | 
|  | 280 | +          secret: | 
|  | 281 | +            name: $OLM_SECRET | 
|  | 282 | +            key: tls.crt | 
|  | 283 | +        keySecret: | 
|  | 284 | +          name: $OLM_SECRET | 
|  | 285 | +          key: tls.key | 
|  | 286 | +  selector: | 
|  | 287 | +    matchLabels: | 
|  | 288 | +      control-plane: catalogd-controller-manager | 
|  | 289 | +EOF | 
|  | 290 | +``` | 
|  | 291 | + | 
|  | 292 | +[prometheus-operator]: https://github.com/prometheus-operator/kube-prometheus | 
|  | 293 | +[rbac-k8s-docs]: https://kubernetes.io/docs/reference/access-authn-authz/rbac/ | 
0 commit comments