|
| 1 | +// Module included in the following assemblies: |
| 2 | +// |
| 3 | +// * observability/monitoring/configuring-the-monitoring-stack.adoc |
| 4 | + |
| 5 | +:_mod-docs-content-type: REFERENCE |
| 6 | +[id="example-service-endpoint-authentication-settings_{context}"] |
| 7 | += Example service endpoint authentication settings |
| 8 | + |
| 9 | +You can configure authentication for service endpoints for user-defined project monitoring by using `ServiceMonitor` and `PodMonitor` custom resource definitions (CRDs). |
| 10 | + |
| 11 | +The following samples show different authentication settings for a `ServiceMonitor` resource. |
| 12 | +Each sample shows how to configure a corresponding `Secret` object that contains authentication credentials and other relevant settings. |
| 13 | + |
| 14 | +== Sample YAML authentication with a bearer token |
| 15 | + |
| 16 | +The following sample shows bearer token settings for a `Secret` object named `example-bearer-auth` in the `ns1` namespace: |
| 17 | + |
| 18 | +.Example bearer token secret |
| 19 | +[source,yaml] |
| 20 | +---- |
| 21 | +apiVersion: v1 |
| 22 | +kind: Secret |
| 23 | +metadata: |
| 24 | + name: example-bearer-auth |
| 25 | + namespace: ns1 |
| 26 | +stringData: |
| 27 | + token: <authentication_token> #<1> |
| 28 | +---- |
| 29 | +<1> Specify an authentication token. |
| 30 | + |
| 31 | +The following sample shows bearer token authentication settings for a `ServiceMonitor` CRD. The example uses a `Secret` object named `example-bearer-auth`: |
| 32 | + |
| 33 | +[id="sample-yaml-bearer-token_{context}"] |
| 34 | +.Example bearer token authentication settings |
| 35 | +[source,yaml] |
| 36 | +---- |
| 37 | +apiVersion: monitoring.coreos.com/v1 |
| 38 | +kind: ServiceMonitor |
| 39 | +metadata: |
| 40 | + name: prometheus-example-monitor |
| 41 | + namespace: ns1 |
| 42 | +spec: |
| 43 | + endpoints: |
| 44 | + - authorization: |
| 45 | + credentials: |
| 46 | + key: token #<1> |
| 47 | + name: example-bearer-auth #<2> |
| 48 | + port: web |
| 49 | + selector: |
| 50 | + matchLabels: |
| 51 | + app: prometheus-example-app |
| 52 | +---- |
| 53 | +<1> The key that contains the authentication token in the specified `Secret` object. |
| 54 | +<2> The name of the `Secret` object that contains the authentication credentials. |
| 55 | + |
| 56 | +[IMPORTANT] |
| 57 | +===== |
| 58 | +Do not use `bearerTokenFile` to configure bearer token. If you use the `bearerTokenFile` configuration, the `ServiceMonitor` resource is rejected. |
| 59 | +===== |
| 60 | + |
| 61 | +[id="sample-yaml-basic-auth_{context}"] |
| 62 | +== Sample YAML for Basic authentication |
| 63 | + |
| 64 | +The following sample shows Basic authentication settings for a `Secret` object named `example-basic-auth` in the `ns1` namespace: |
| 65 | + |
| 66 | +.Example Basic authentication secret |
| 67 | +[source,yaml] |
| 68 | +---- |
| 69 | +apiVersion: v1 |
| 70 | +kind: Secret |
| 71 | +metadata: |
| 72 | + name: example-basic-auth |
| 73 | + namespace: ns1 |
| 74 | +stringData: |
| 75 | + user: <basic_username> #<1> |
| 76 | + password: <basic_password> #<2> |
| 77 | +---- |
| 78 | +<1> Specify a username for authentication. |
| 79 | +<2> Specify a password for authentication. |
| 80 | + |
| 81 | +The following sample shows Basic authentication settings for a `ServiceMonitor` CRD. The example uses a `Secret` object named `example-basic-auth`: |
| 82 | + |
| 83 | +.Example Basic authentication settings |
| 84 | +[source,yaml] |
| 85 | +---- |
| 86 | +apiVersion: monitoring.coreos.com/v1 |
| 87 | +kind: ServiceMonitor |
| 88 | +metadata: |
| 89 | + name: prometheus-example-monitor |
| 90 | + namespace: ns1 |
| 91 | +spec: |
| 92 | + endpoints: |
| 93 | + - basicAuth: |
| 94 | + username: |
| 95 | + key: user #<1> |
| 96 | + name: example-basic-auth #<2> |
| 97 | + password: |
| 98 | + key: password #<3> |
| 99 | + name: example-basic-auth #<2> |
| 100 | + port: web |
| 101 | + selector: |
| 102 | + matchLabels: |
| 103 | + app: prometheus-example-app |
| 104 | +---- |
| 105 | +<1> The key that contains the username in the specified `Secret` object. |
| 106 | +<2> The name of the `Secret` object that contains the Basic authentication. |
| 107 | +<3> The key that contains the password in the specified `Secret` object. |
| 108 | + |
| 109 | +[id="sample-yaml-oauth-20_{context}"] |
| 110 | +== Sample YAML authentication with OAuth 2.0 |
| 111 | + |
| 112 | +The following sample shows OAuth 2.0 settings for a `Secret` object named `example-oauth2` in the `ns1` namespace: |
| 113 | + |
| 114 | +.Example OAuth 2.0 secret |
| 115 | +[source,yaml] |
| 116 | +---- |
| 117 | +apiVersion: v1 |
| 118 | +kind: Secret |
| 119 | +metadata: |
| 120 | + name: example-oauth2 |
| 121 | + namespace: ns1 |
| 122 | +stringData: |
| 123 | + id: <oauth2_id> #<1> |
| 124 | + secret: <oauth2_secret> #<2> |
| 125 | +---- |
| 126 | +<1> Specify an Oauth 2.0 ID. |
| 127 | +<2> Specify an Oauth 2.0 secret. |
| 128 | + |
| 129 | +The following sample shows OAuth 2.0 authentication settings for a `ServiceMonitor` CRD. The example uses a `Secret` object named `example-oauth2`: |
| 130 | + |
| 131 | +.Example OAuth 2.0 authentication settings |
| 132 | +[source,yaml] |
| 133 | +---- |
| 134 | +apiVersion: monitoring.coreos.com/v1 |
| 135 | +kind: ServiceMonitor |
| 136 | +metadata: |
| 137 | + name: prometheus-example-monitor |
| 138 | + namespace: ns1 |
| 139 | +spec: |
| 140 | + endpoints: |
| 141 | + - oauth2: |
| 142 | + clientId: |
| 143 | + secret: |
| 144 | + key: id #<1> |
| 145 | + name: example-oauth2 #<2> |
| 146 | + clientSecret: |
| 147 | + key: secret #<3> |
| 148 | + name: example-oauth2 #<2> |
| 149 | + tokenUrl: https://example.com/oauth2/token #<4> |
| 150 | + port: web |
| 151 | + selector: |
| 152 | + matchLabels: |
| 153 | + app: prometheus-example-app |
| 154 | +---- |
| 155 | +<1> The key that contains the OAuth 2.0 ID in the specified `Secret` object. |
| 156 | +<2> The name of the `Secret` object that contains the OAuth 2.0 credentials. |
| 157 | +<3> The key that contains the OAuth 2.0 secret in the specified `Secret` object. |
| 158 | +<4> The URL used to fetch a token with the specified `clientId` and `clientSecret`. |
0 commit comments