github.com/muhammadn/cortex@v1.9.1-0.20220510110439-46bb7000d03d/docs/blocks-storage/migrate-storage-from-thanos-and-prometheus.md (about) 1 --- 2 title: "Migrate the storage from Thanos and Prometheus" 3 linkTitle: "Migrate the storage from Thanos and Prometheus" 4 weight: 7 5 slug: migrate-storage-from-thanos-and-prometheus 6 --- 7 8 The Cortex blocks storage engine stores series in TSDB blocks uploaded in the storage bucket. This makes very easy to migrate the storage from Thanos and/or Prometheus to Cortex, when running the blocks storage. 9 10 ## Cortex blocks storage requirements 11 12 The Cortex blocks storage has few requirements that should be considered when migrating TSDB blocks from Thanos / Prometheus to Cortex: 13 14 1. **The blocks in the bucket should be located at `bucket://<tenant-id>/`**<br /> 15 Cortex isolates blocks on a per-tenant basis in the bucket and, for this reason, each tenant blocks should be uploaded to a different location in the bucket. The bucket prefix, where a specific tenant blocks should be uploaded, is `/<tenant-id>/`; if Cortex is running with auth disabled (no multi-tenancy) then the `<tenant-id>` to use is `fake`. 16 2. **Remove Thanos external labels and inject `__org_id__` into each block's `meta.json`**<br /> 17 Every block has a little metadata file named `meta.json`. Thanos stores external labels at `thanos` > `labels`, which should be all removed when migrating to Cortex, while the `"__org_id__": "<tenant-id>"` added. 18 19 ## How to migrate the storage 20 21 ### Upload TSDB blocks to Cortex bucket 22 23 TSDB blocks stored in Prometheus local disk or Thanos bucket should be copied/uploaded to the Cortex bucket at the location `bucket://<tenant-id>/` (when Cortex is running with auth disabled then `<tenant-id>` must be `fake`). 24 25 ### Migrate block metadata (`meta.json`) to Cortex 26 27 For each block copied/uploaded to the Cortex bucket, there are a few changes required to the `meta.json`. 28 29 #### Automatically migrate metadata using the `thanosconvert` tool 30 31 `thanosconvert` can iterate over a Cortex bucket and make sure that each `meta.json` has the correct `thanos` > `labels` layout. 32 33 ⚠ Warning ⚠ `thanosconvert` will modify files in the bucket you specify. It's recommended that you have backups or enable object versioning before running this tool. 34 35 To run `thanosconvert`, you need to provide it with the bucket configuration in the same format as the [blocks storage bucket configuration](../configuration/config-file-reference.md#blocks_storage_config). 36 ```yaml 37 # bucket-config.yaml 38 backend: s3 39 s3: 40 endpoint: s3.us-east-1.amazonaws.com 41 bucket_name: my-cortex-bucket 42 ``` 43 44 You can run thanosconvert directly using Go: 45 ```bash 46 go install github.com/cortexproject/cortex/cmd/thanosconvert 47 thanosconvert 48 ``` 49 50 Or use the provided docker image: 51 ```bash 52 docker run quay.io/cortexproject/thanosconvert 53 ``` 54 55 You can run the tool in dry-run mode first to find out what which blocks it will migrate: 56 57 ```bash 58 thanosconvert -config ./bucket-config.yaml -dry-run 59 ``` 60 61 Once you're happy with the results, you can run without dry run to migrate blocks: 62 ```bash 63 thanosconvert -config ./bucket-config.yaml 64 ``` 65 66 You can cancel a conversion in progress (with Ctrl+C) and rerun `thanosconvert`. It won't change any blocks which have been written by Cortex or already converted from Thanos, so you can run `thanosconvert` multiple times. 67 68 69 #### Migrate metadata manually 70 71 If you need to migrate the block metadata manually, you need to: 72 73 1. Download the `meta.json` to the local filesystem 74 2. Decode the JSON 75 3. Manipulate the data structure (_see below_) 76 4. Re-encode the JSON 77 5. Re-upload it to the bucket (overwriting the previous version of the `meta.json` file) 78 79 The `meta.json` should be manipulated in order to ensure: 80 81 - It contains the `thanos` root-level entry 82 - The `thanos` > `labels` do not contain any Thanos-specific external label 83 - The `thanos` > `labels` contain the Cortex-specific external label `"__org_id__": "<tenant-id>"` 84 85 86 ##### When migrating from Thanos 87 88 When migrating from Thanos, the easiest approach would be keep the existing `thanos` root-level entry as is, except: 89 90 1. Completely remove the content of `thanos` > `labels` 91 2. Add `"__org_id__": "<tenant-id>"` to `thanos` > `labels` 92 93 For example, when migrating a block from Thanos for the tenant `user-1`, the `thanos` root-level property within the `meta.json` file will look like: 94 95 ```json 96 { 97 "thanos": { 98 "labels": { 99 "__org_id__": "user-1" 100 }, 101 "downsample": { 102 "resolution": 0 103 }, 104 "source": "compactor" 105 } 106 } 107 ``` 108 109 ##### When migrating from Prometheus 110 111 When migrating from Prometheus, the `meta.json` file will not contain any `thanos` root-level entry and, for this reason, it would need to be generated: 112 113 1. Create the `thanos` root-level entry (_see below_) 114 2. Add `"__org_id__": "<tenant-id>"` to `thanos` > `labels` 115 116 For example, when migrating a block from Prometheus for the tenant `user-1`, the `thanos` root-level property within the `meta.json` file should be as follow: 117 118 ```json 119 { 120 "thanos": { 121 "labels": { 122 "__org_id__": "user-1" 123 }, 124 "downsample": { 125 "resolution": 0 126 }, 127 "source": "compactor" 128 } 129 } 130 ```