allocation_awareness.asciidoc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. [[shard-allocation-awareness]]
  2. ==== Shard allocation awareness
  3. You can use custom node attributes as _awareness attributes_ to enable {es}
  4. to take your physical hardware configuration into account when allocating shards.
  5. If {es} knows which nodes are on the same physical server, in the same rack, or
  6. in the same zone, it can distribute the primary shard and its replica shards to
  7. minimize the risk of losing all shard copies in the event of a failure.
  8. When shard allocation awareness is enabled with the
  9. <<dynamic-cluster-setting,dynamic>>
  10. `cluster.routing.allocation.awareness.attributes` setting, shards are only
  11. allocated to nodes that have values set for the specified awareness attributes.
  12. If you use multiple awareness attributes, {es} considers each attribute
  13. separately when allocating shards.
  14. NOTE: The number of attribute values determines how many shard copies are
  15. allocated in each location. If the number of nodes in each location is
  16. unbalanced and there are a lot of replicas, replica shards might be left
  17. unassigned.
  18. TIP: Learn more about <<high-availability-cluster-design-large-clusters,designing resilient clusters>>.
  19. [[enabling-awareness]]
  20. ===== Enabling shard allocation awareness
  21. To enable shard allocation awareness:
  22. . Specify the location of each node with a custom node attribute. For example,
  23. if you want Elasticsearch to distribute shards across different racks, you might
  24. use an awareness attribute called `rack_id`.
  25. +
  26. You can set custom attributes in two ways:
  27. - By editing the `elasticsearch.yml` config file:
  28. +
  29. [source,yaml]
  30. --------------------------------------------------------
  31. node.attr.rack_id: rack_one
  32. --------------------------------------------------------
  33. +
  34. - Using the `-E` command line argument when you start a node:
  35. +
  36. [source,sh]
  37. --------------------------------------------------------
  38. ./bin/elasticsearch -Enode.attr.rack_id=rack_one
  39. --------------------------------------------------------
  40. . Tell {es} to take one or more awareness attributes into account when
  41. allocating shards by setting
  42. `cluster.routing.allocation.awareness.attributes` in *every* master-eligible
  43. node's `elasticsearch.yml` config file.
  44. +
  45. --
  46. [source,yaml]
  47. --------------------------------------------------------
  48. cluster.routing.allocation.awareness.attributes: rack_id <1>
  49. --------------------------------------------------------
  50. <1> Specify multiple attributes as a comma-separated list.
  51. --
  52. +
  53. You can also use the
  54. <<cluster-update-settings,cluster-update-settings>> API to set or update
  55. a cluster's awareness attributes:
  56. +
  57. [source,console]
  58. --------------------------------------------------
  59. PUT /_cluster/settings
  60. {
  61. "persistent" : {
  62. "cluster.routing.allocation.awareness.attributes" : "rack_id"
  63. }
  64. }
  65. --------------------------------------------------
  66. With this example configuration, if you start two nodes with
  67. `node.attr.rack_id` set to `rack_one` and create an index with 5 primary
  68. shards and 1 replica of each primary, all primaries and replicas are
  69. allocated across the two node.
  70. .All primaries and replicas allocated across two nodes in the same rack
  71. image::images/shard-allocation/shard-allocation-awareness-one-rack.png[All primaries and replicas are allocated across two nodes in the same rack]
  72. If you add two nodes with `node.attr.rack_id` set to `rack_two`,
  73. {es} moves shards to the new nodes, ensuring (if possible)
  74. that no two copies of the same shard are in the same rack.
  75. .Primaries and replicas allocated across four nodes in two racks, with no two copies of the same shard in the same rack
  76. image::images/shard-allocation/shard-allocation-awareness-two-racks.png[Primaries and replicas are allocated across four nodes in two racks with no two copies of the same shard in the same rack]
  77. If `rack_two` fails and takes down both its nodes, by default {es}
  78. allocates the lost shard copies to nodes in `rack_one`. To prevent multiple
  79. copies of a particular shard from being allocated in the same location, you can
  80. enable forced awareness.
  81. [[forced-awareness]]
  82. ===== Forced awareness
  83. By default, if one location fails, {es} spreads its shards across the remaining
  84. locations. This might be undesirable if the cluster does not have sufficient
  85. resources to host all its shards when one location is missing.
  86. To prevent the remaining locations from being overloaded in the event of a
  87. whole-location failure, specify the attribute values that should exist with the
  88. `cluster.routing.allocation.awareness.force.*` settings. This will mean that
  89. {es} will prefer to leave some replicas unassigned in the event of a
  90. whole-location failure instead of overloading the nodes in the remaining
  91. locations.
  92. For example, if you have an awareness attribute called `zone` and configure
  93. nodes in `zone1` and `zone2`, you can use forced awareness to make {es} leave
  94. half of your shard copies unassigned if only one zone is available:
  95. [source,yaml]
  96. -------------------------------------------------------------------
  97. cluster.routing.allocation.awareness.attributes: zone
  98. cluster.routing.allocation.awareness.force.zone.values: zone1,zone2 <1>
  99. -------------------------------------------------------------------
  100. <1> Specify all possible `zone` attribute values.
  101. With this example configuration, if you have two nodes with `node.attr.zone`
  102. set to `zone1` and an index with `number_of_replicas` set to `1`, {es}
  103. allocates all the primary shards but none of the replicas. It will assign the
  104. replica shards once nodes with a different value for `node.attr.zone` join the
  105. cluster. In contrast, if you do not configure forced awareness, {es} will
  106. allocate all primaries and replicas to the two nodes even though they are in
  107. the same zone.