|
| 1 | +# Running Cortex in Production |
| 2 | + |
| 3 | +This document assumes you have read the |
| 4 | +[architecture](architecture.md) document. |
| 5 | + |
| 6 | +## Planning |
| 7 | + |
| 8 | +### Tenants |
| 9 | + |
| 10 | +If you will run Cortex as a multi-tenant system, you need to give each |
| 11 | +tenant a unique ID - this can be any string. Managing tenants and |
| 12 | +allocating IDs must be done outside of Cortex. You must also configure |
| 13 | +[Authentication and Authorisation](auth.md). |
| 14 | + |
| 15 | +### Storage |
| 16 | + |
| 17 | +Cortex requires a scalable storage back-end. Commercial cloud options |
| 18 | +are DynamoDB and Bigtable: the advantage is you don't have to know how |
| 19 | +to manage them, but the downside is they have specific costs. |
| 20 | +Alternatively you can choose Cassandra, which you will have to install |
| 21 | +and manage. |
| 22 | + |
| 23 | +### Components |
| 24 | + |
| 25 | +Every Cortex installation will need Distributor, Ingester and Querier. |
| 26 | +Alert-manager, Ruler and Query-frontend are optional. |
| 27 | + |
| 28 | +### Other dependencies |
| 29 | + |
| 30 | +Cortex needs a KV store to track sharding of data between |
| 31 | +processes. This can be either Etcd or Consul. |
| 32 | + |
| 33 | +If you want to configure recording and alerting rules (i.e. if you |
| 34 | +will run the Ruler and Alert-manager components) then a Postgres |
| 35 | +database is required to store configs. |
| 36 | + |
| 37 | +Memcached is not essential but highly recommended. |
| 38 | + |
| 39 | +### Ingester replication factor |
| 40 | + |
| 41 | +The standard replication factor is three, so that we can drop one |
| 42 | +sample and be unconcerned, as we still have two copies of the data |
| 43 | +left for redundancy. This is configurable: you can run with more |
| 44 | +redundancy or less, depending on your risk appetite. |
| 45 | + |
| 46 | +### Index schema |
| 47 | + |
| 48 | +Choose schema version 9 in most cases; version 10 if you expect |
| 49 | +hundreds of thousands of timeseries under a single name. Anything |
| 50 | +older than v9 is much less efficient. |
| 51 | + |
| 52 | +### Chunk encoding |
| 53 | + |
| 54 | +Standard choice would be Bigchunk, which is the most flexible chunk |
| 55 | +encoding. You may get better compression from Varbit, if many of your |
| 56 | +timeseries do not change value from one day to the next. |
| 57 | + |
| 58 | +### Sizing |
| 59 | + |
| 60 | +You will want to estimate how many nodes are required, how many of |
| 61 | +each component to run, and how much storage space will be required. |
| 62 | +In practice, these will vary greatly depending on the metrics being |
| 63 | +sent to Cortex. |
| 64 | + |
| 65 | +Some key parameters are: |
| 66 | + |
| 67 | + 1. The number of active series. If you have Prometheus already you |
| 68 | + can query `prometheus_tsdb_head_series` to see this number. |
| 69 | + 2. Sampling rate, e.g. a new sample for each series every 15 |
| 70 | + seconds. Multiply this by the number of active series to get the |
| 71 | + total rate at which samples will arrive at Cortex. |
| 72 | + 3. The rate at which series are added and removed. This can be very |
| 73 | + high if you monitor objects that come and go - for example if you run |
| 74 | + thousands of batch jobs lasting a minute or so and capture metrics |
| 75 | + with a unique ID for each one. [Read how to analyse this on |
| 76 | + Prometheus](https://www.robustperception.io/using-tsdb-analyze-to-investigate-churn-and-cardinality) |
| 77 | + 4. How compressible the time-series data are. If a metric stays at |
| 78 | + the same value constantly, then Cortex can compress it very well, so |
| 79 | + 12 hours of data sampled every 15 seconds would be around 2KB. On |
| 80 | + the other hand if the value jumps around a lot it might take 10KB. |
| 81 | + There are not currently any tools available to analyse this. |
| 82 | + 5. How long you want to retain data for, e.g. 1 month or 2 years. |
| 83 | + |
| 84 | +Other parameters which can become important if you have particularly |
| 85 | +high values: |
| 86 | + |
| 87 | + 6. Number of different series under one metric name. |
| 88 | + 7. Number of labels per series. |
| 89 | + 8. Rate and complexity of queries. |
| 90 | + |
| 91 | +Now, some rules of thumb: |
| 92 | + |
| 93 | + 1. Each million series in an ingester takes 15GB of RAM. Total number |
| 94 | + of series in ingesters is number of active series times the |
| 95 | + replication factor. This is with the default of 12-hour chunks - RAM |
| 96 | + required will reduce if you set `-ingester.max-chunk-age` lower |
| 97 | + (trading off more back-end database IO) |
| 98 | + 2. Each million series (including churn) consumes 15GB of chunk |
| 99 | + storage and 4GB of index, per day (so multiply by the retention |
| 100 | + period). |
| 101 | + 3. Each 100,000 samples/sec arriving takes 1 CPU in distributors. |
| 102 | + Distributors don't need much RAM. |
| 103 | + |
| 104 | +If you turn on compression between distributors and ingesters (for |
| 105 | +example to save on inter-zone bandwidth charges at AWS) they will use |
| 106 | +significantly more CPU. |
| 107 | + |
| 108 | +### Caching |
| 109 | + |
| 110 | +Cortex can retain data in-process or in Memcached to speed up various |
| 111 | +queries by caching: |
| 112 | + |
| 113 | + * individual chunks |
| 114 | + * index lookups for one label on one day |
| 115 | + * the results of a whole query |
| 116 | + |
| 117 | +You should always include Memcached in your Cortex install so results |
| 118 | +from one process can be re-used by another. In-process caching can cut |
| 119 | +fetch times slightly and reduce the load on Memcached. |
| 120 | + |
| 121 | +Ingesters can also be configured to use Memcached to avoid re-writing |
| 122 | +index and chunk data which has already been stored in the back-end |
| 123 | +database. Again, highly recommended. |
| 124 | + |
| 125 | +### Orchestration |
| 126 | + |
| 127 | +Because Cortex is designed to run multiple instances of each component |
| 128 | +(ingester, querier, etc.), you probably want to automate the placement |
| 129 | +and shepherding of these instances. Most users choose Kubernetes to do |
| 130 | +this, but this is not mandatory. |
| 131 | + |
| 132 | +## Configuration |
| 133 | + |
| 134 | +### Resource requests |
| 135 | + |
| 136 | +If using Kubernetes, each container should specify resource requests |
| 137 | +so that the scheduler can place them on a node with sufficient capacity. |
| 138 | + |
| 139 | +For example an ingester might request: |
| 140 | + |
| 141 | +``` |
| 142 | + resources: |
| 143 | + requests: |
| 144 | + cpu: 4 |
| 145 | + memory: 10Gi |
| 146 | +``` |
| 147 | + |
| 148 | +The specific values here should be adjusted based on your own |
| 149 | +experiences running Cortex - they are very dependent on rate of data |
| 150 | +arriving and other factors such as series churn. |
| 151 | + |
| 152 | +### Spread out ingesters |
| 153 | + |
| 154 | +Don't run multiple ingesters on the same node, as that raises the risk |
| 155 | +that you will lost multiple replicas of data at the same time. |
| 156 | + |
| 157 | +In Kubernetes this can be expressed as: |
| 158 | + |
| 159 | +``` |
| 160 | + affinity: |
| 161 | + podAntiAffinity: |
| 162 | + preferredDuringSchedulingIgnoredDuringExecution: |
| 163 | + - weight: 100 |
| 164 | + podAffinityTerm: |
| 165 | + labelSelector: |
| 166 | + matchExpressions: |
| 167 | + - key: name |
| 168 | + operator: In |
| 169 | + values: |
| 170 | + - ingester |
| 171 | + topologyKey: "kubernetes.io/hostname" |
| 172 | +``` |
0 commit comments