downsampling-manual.asciidoc 15 KB

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