downsampling-manual.asciidoc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. [[downsampling-manual]]
  2. === Run downsampling manually
  3. ++++
  4. <titleabbrev>Run downsampling manually</titleabbrev>
  5. ++++
  6. This is a simplified example that allows you to see quickly how
  7. <<downsampling,downsampling>> works to reduce the storage size of a time series
  8. index. The example uses typical Kubernetes cluster monitoring data. To test out
  9. downsampling, follow these steps:
  10. . Check the <<downsampling-manual-prereqs,prerequisites>>.
  11. . <<downsampling-manual-create-index>>.
  12. . <<downsampling-manual-ingest-data>>.
  13. . <<downsampling-manual-run>>.
  14. . <<downsampling-manual-view-results>>.
  15. [discrete]
  16. [[downsampling-manual-prereqs]]
  17. ==== Prerequisites
  18. Refer to <<tsds-prereqs,time series data stream prerequisites>>.
  19. For the example you need a sample data file. Download the file from link:
  20. https://static-www.elastic.co/v3/assets/bltefdd0b53724fa2ce/bltf2fe7a300c3c59f7/631b4bc5cc56115de2f58e8c/sample-k8s-metrics.json[here]
  21. and save it in the local directory where you're running {es}.
  22. [discrete]
  23. [[downsampling-manual-create-index]]
  24. ==== Create a time series index
  25. This creates an index for a basic data stream. The available parameters for an
  26. index are described in detail in <<set-up-a-data-stream,Set up a time series
  27. data stream>>.
  28. The time series boundaries are set so that sampling data for the index begins at
  29. `2022-06-10T00:00:00Z` and ends at `2022-06-30T23:59:59Z`.
  30. For simplicity, in the time series mapping all `time_series_metric` parameters
  31. are set to type `gauge`, but <<time-series-metric,other values>> such as
  32. `counter` and `histogram` may also be used. The `time_series_metric` values
  33. determine the kind of statistical representations that are used during
  34. downsampling.
  35. The index template includes a set of static
  36. <<time-series-dimension,time series dimensions>>: `host`, `namespace`,
  37. `node`, and `pod`. The time series dimensions are not changed by the
  38. downsampling process.
  39. [source,console]
  40. ----
  41. PUT /sample-01
  42. {
  43. "settings": {
  44. "index": {
  45. "mode": "time_series",
  46. "time_series": {
  47. "start_time": "2022-06-10T00:00:00Z",
  48. "end_time": "2022-06-30T23:59:59Z"
  49. },
  50. "routing_path": [
  51. "kubernetes.namespace",
  52. "kubernetes.host",
  53. "kubernetes.node",
  54. "kubernetes.pod"
  55. ],
  56. "number_of_replicas": 0,
  57. "number_of_shards": 2
  58. }
  59. },
  60. "mappings": {
  61. "properties": {
  62. "@timestamp": {
  63. "type": "date"
  64. },
  65. "kubernetes": {
  66. "properties": {
  67. "container": {
  68. "properties": {
  69. "cpu": {
  70. "properties": {
  71. "usage": {
  72. "properties": {
  73. "core": {
  74. "properties": {
  75. "ns": {
  76. "type": "long"
  77. }
  78. }
  79. },
  80. "limit": {
  81. "properties": {
  82. "pct": {
  83. "type": "float"
  84. }
  85. }
  86. },
  87. "nanocores": {
  88. "type": "long",
  89. "time_series_metric": "gauge"
  90. },
  91. "node": {
  92. "properties": {
  93. "pct": {
  94. "type": "float"
  95. }
  96. }
  97. }
  98. }
  99. }
  100. }
  101. },
  102. "memory": {
  103. "properties": {
  104. "available": {
  105. "properties": {
  106. "bytes": {
  107. "type": "long",
  108. "time_series_metric": "gauge"
  109. }
  110. }
  111. },
  112. "majorpagefaults": {
  113. "type": "long"
  114. },
  115. "pagefaults": {
  116. "type": "long",
  117. "time_series_metric": "gauge"
  118. },
  119. "rss": {
  120. "properties": {
  121. "bytes": {
  122. "type": "long",
  123. "time_series_metric": "gauge"
  124. }
  125. }
  126. },
  127. "usage": {
  128. "properties": {
  129. "bytes": {
  130. "type": "long",
  131. "time_series_metric": "gauge"
  132. },
  133. "limit": {
  134. "properties": {
  135. "pct": {
  136. "type": "float"
  137. }
  138. }
  139. },
  140. "node": {
  141. "properties": {
  142. "pct": {
  143. "type": "float"
  144. }
  145. }
  146. }
  147. }
  148. },
  149. "workingset": {
  150. "properties": {
  151. "bytes": {
  152. "type": "long",
  153. "time_series_metric": "gauge"
  154. }
  155. }
  156. }
  157. }
  158. },
  159. "name": {
  160. "type": "keyword"
  161. },
  162. "start_time": {
  163. "type": "date"
  164. }
  165. }
  166. },
  167. "host": {
  168. "type": "keyword",
  169. "time_series_dimension": true
  170. },
  171. "namespace": {
  172. "type": "keyword",
  173. "time_series_dimension": true
  174. },
  175. "node": {
  176. "type": "keyword",
  177. "time_series_dimension": true
  178. },
  179. "pod": {
  180. "type": "keyword",
  181. "time_series_dimension": true
  182. }
  183. }
  184. }
  185. }
  186. }
  187. }
  188. ----
  189. [discrete]
  190. [[downsampling-manual-ingest-data]]
  191. ==== Ingest time series data
  192. In a terminal window with {es} running, run the following curl command to load
  193. the documents from the downloaded sample data file:
  194. [source,sh]
  195. ----
  196. curl -s -H "Content-Type: application/json" \
  197. -XPOST http://<elasticsearch-node>/sample-01/_bulk?pretty \
  198. --data-binary @sample-k8s-metrics.json
  199. ----
  200. // NOTCONSOLE
  201. Approximately 18,000 documents are added. Check the search results for the newly
  202. ingested data:
  203. [source,console]
  204. ----
  205. GET /sample-01*/_search
  206. ----
  207. // TEST[continued]
  208. The query has at least 10,000 hits and returns the first 10. In each document
  209. you can see the time series dimensions (`host`, `node`, `pod` and `container`)
  210. as well as the various CPU and memory time series metrics.
  211. [source,console-result]
  212. ----
  213. "hits": {
  214. "total": {
  215. "value": 10000,
  216. "relation": "gte"
  217. },
  218. "max_score": 1,
  219. "hits": [
  220. {
  221. "_index": "sample-01",
  222. "_id": "WyHN6N6AwdaJByQWAAABgYOOweA",
  223. "_score": 1,
  224. "_source": {
  225. "@timestamp": "2022-06-20T23:59:40Z",
  226. "kubernetes": {
  227. "host": "gke-apps-0",
  228. "node": "gke-apps-0-1",
  229. "pod": "gke-apps-0-1-0",
  230. "container": {
  231. "cpu": {
  232. "usage": {
  233. "nanocores": 80037,
  234. "core": {
  235. "ns": 12828317850
  236. },
  237. "node": {
  238. "pct": 0.0000277905
  239. },
  240. "limit": {
  241. "pct": 0.0000277905
  242. }
  243. }
  244. },
  245. "memory": {
  246. "available": {
  247. "bytes": 790830121
  248. },
  249. "usage": {
  250. "bytes": 139548672,
  251. "node": {
  252. "pct": 0.01770037710617187
  253. },
  254. "limit": {
  255. "pct": 0.00009923134671484496
  256. }
  257. },
  258. "workingset": {
  259. "bytes": 2248540
  260. },
  261. "rss": {
  262. "bytes": 289260
  263. },
  264. "pagefaults": 74843,
  265. "majorpagefaults": 0
  266. },
  267. "start_time": "2021-03-30T07:59:06Z",
  268. "name": "container-name-44"
  269. },
  270. "namespace": "namespace26"
  271. }
  272. }
  273. }
  274. ...
  275. ----
  276. // TEST[skip:todo]
  277. // TEST[continued]
  278. Next, you can run a terms aggregation on the set of time series dimensions (`_tsid`) to
  279. view a date histogram on a fixed interval of one day.
  280. [source,console]
  281. ----
  282. GET /sample-01*/_search
  283. {
  284. "size": 0,
  285. "aggs": {
  286. "tsid": {
  287. "terms": {
  288. "field": "_tsid"
  289. },
  290. "aggs": {
  291. "over_time": {
  292. "date_histogram": {
  293. "field": "@timestamp",
  294. "fixed_interval": "1d"
  295. },
  296. "aggs": {
  297. "min": {
  298. "min": {
  299. "field": "kubernetes.container.memory.usage.bytes"
  300. }
  301. },
  302. "max": {
  303. "max": {
  304. "field": "kubernetes.container.memory.usage.bytes"
  305. }
  306. },
  307. "avg": {
  308. "avg": {
  309. "field": "kubernetes.container.memory.usage.bytes"
  310. }
  311. }
  312. }
  313. }
  314. }
  315. }
  316. }
  317. }
  318. ----
  319. // TEST[continued]
  320. [discrete]
  321. [[downsampling-manual-run]]
  322. ==== Run downsampling for the index
  323. Before running downsampling, the index needs to be set to read only mode:
  324. [source,console]
  325. ----
  326. PUT /sample-01/_block/write
  327. ----
  328. // TEST[continued]
  329. And now, you can use the <<indices-downsample-data-stream,downsample API>> to
  330. downsample the index, setting the time series interval to one hour:
  331. [source,console]
  332. ----
  333. POST /sample-01/_downsample/sample-01-downsample
  334. {
  335. "fixed_interval": "1h"
  336. }
  337. ----
  338. // TEST[continued]
  339. Finally, delete the original index:
  340. [source,console]
  341. ----
  342. DELETE /sample-01
  343. ----
  344. // TEST[continued]
  345. [discrete]
  346. [[downsampling-manual-view-results]]
  347. ==== View the results
  348. Re-run your search query (note that when querying downsampled indices there are <<querying-downsampled-indices-notes,a few nuances to be aware of>>):
  349. [source,console]
  350. ----
  351. GET /sample-01*/_search
  352. ----
  353. // TEST[continued]
  354. In the query results, notice that the number of hits has been reduced to only 288
  355. documents. As well, for each time series metric statistical representations have
  356. been calculated: `min`, `max`, `sum`, and `value_count`.
  357. [source,console-result]
  358. ----
  359. "hits": {
  360. "total": {
  361. "value": 288,
  362. "relation": "eq"
  363. },
  364. "max_score": 1,
  365. "hits": [
  366. {
  367. "_index": "sample-01-downsample",
  368. "_id": "WyHN6N6AwdaJByQWAAABgYNYIYA",
  369. "_score": 1,
  370. "_source": {
  371. "@timestamp": "2022-06-20T23:00:00.000Z",
  372. "_doc_count": 81,
  373. "kubernetes.host": "gke-apps-0",
  374. "kubernetes.namespace": "namespace26",
  375. "kubernetes.node": "gke-apps-0-1",
  376. "kubernetes.pod": "gke-apps-0-1-0",
  377. "kubernetes.container.cpu.usage.nanocores": {
  378. "min": 23344,
  379. "max": 163408,
  380. "sum": 7488985,
  381. "value_count": 81
  382. },
  383. "kubernetes.container.memory.available.bytes": {
  384. "min": 167751844,
  385. "max": 1182251090,
  386. "sum": 58169948901,
  387. "value_count": 81
  388. },
  389. "kubernetes.container.memory.rss.bytes": {
  390. "min": 54067,
  391. "max": 391987,
  392. "sum": 17550215,
  393. "value_count": 81
  394. },
  395. "kubernetes.container.memory.pagefaults": {
  396. "min": 69086,
  397. "max": 428910,
  398. "sum": 20239365,
  399. "value_count": 81
  400. },
  401. "kubernetes.container.memory.workingset.bytes": {
  402. "min": 323420,
  403. "max": 2279342,
  404. "sum": 104233700,
  405. "value_count": 81
  406. },
  407. "kubernetes.container.memory.usage.bytes": {
  408. "min": 61401416,
  409. "max": 413064069,
  410. "sum": 18557182404,
  411. "value_count": 81
  412. }
  413. }
  414. },
  415. ...
  416. ----
  417. // TEST[skip:todo]
  418. You can now re-run the earlier aggregation. Even though the aggregation runs on
  419. the downsampled data stream that only contains 288 documents, it returns the
  420. same results as the earlier aggregation on the original data stream.
  421. [source,console]
  422. ----
  423. GET /sample-01*/_search
  424. {
  425. "size": 0,
  426. "aggs": {
  427. "tsid": {
  428. "terms": {
  429. "field": "_tsid"
  430. },
  431. "aggs": {
  432. "over_time": {
  433. "date_histogram": {
  434. "field": "@timestamp",
  435. "fixed_interval": "1d"
  436. },
  437. "aggs": {
  438. "min": {
  439. "min": {
  440. "field": "kubernetes.container.memory.usage.bytes"
  441. }
  442. },
  443. "max": {
  444. "max": {
  445. "field": "kubernetes.container.memory.usage.bytes"
  446. }
  447. },
  448. "avg": {
  449. "avg": {
  450. "field": "kubernetes.container.memory.usage.bytes"
  451. }
  452. }
  453. }
  454. }
  455. }
  456. }
  457. }
  458. }
  459. ----
  460. // TEST[continued]
  461. This example demonstrates how downsampling can dramatically reduce the number of
  462. records stored for time series data, within whatever time boundaries you choose.
  463. It's also possible to perform downsampling on already downsampled data, to
  464. further reduce storage and associated costs, as the time series data ages and
  465. the data resolution becomes less critical.
  466. Downsampling is very easily integrated within an ILM policy. To learn more, try
  467. the <<downsampling-ilm,Run downsampling with ILM>> example.