discovery-gce.asciidoc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. [[discovery-gce]]
  2. === GCE Discovery Plugin
  3. The Google Compute Engine Discovery plugin uses the GCE API for unicast discovery.
  4. [[discovery-gce-install]]
  5. [float]
  6. ==== Installation
  7. This plugin can be installed using the plugin manager:
  8. [source,sh]
  9. ----------------------------------------------------------------
  10. sudo bin/elasticsearch-plugin install discovery-gce
  11. ----------------------------------------------------------------
  12. The plugin must be installed on every node in the cluster, and each node must
  13. be restarted after installation.
  14. [[discovery-gce-remove]]
  15. [float]
  16. ==== Removal
  17. The plugin can be removed with the following command:
  18. [source,sh]
  19. ----------------------------------------------------------------
  20. sudo bin/elasticsearch-plugin remove discovery-gce
  21. ----------------------------------------------------------------
  22. The node must be stopped before removing the plugin.
  23. [[discovery-gce-usage]]
  24. ==== GCE Virtual Machine Discovery
  25. Google Compute Engine VM discovery allows to use the google APIs to perform automatic discovery (similar to multicast
  26. in non hostile multicast environments). Here is a simple sample configuration:
  27. [source,yaml]
  28. --------------------------------------------------
  29. cloud:
  30. gce:
  31. project_id: <your-google-project-id>
  32. zone: <your-zone>
  33. discovery:
  34. type: gce
  35. --------------------------------------------------
  36. The following gce settings (prefixed with `cloud.gce`) are supported:
  37. `project_id`::
  38. Your Google project id (mandatory).
  39. `zone`::
  40. helps to retrieve instances running in a given zone (mandatory). It should be one of the
  41. https://developers.google.com/compute/docs/zones#available[GCE supported zones].
  42. See also <<discovery-gce-usage-zones>>.
  43. `retry`::
  44. If set to `true`, client will use
  45. https://developers.google.com/api-client-library/java/google-http-java-client/backoff[ExponentialBackOff]
  46. policy to retry the failed http request. Defaults to `true`.
  47. `max_wait`::
  48. The maximum elapsed time after the client instantiating retry. If the time elapsed goes past the
  49. `max_wait`, client stops to retry. A negative value means that it will wait indefinitely. Defaults to `0s` (retry
  50. indefinitely).
  51. `refresh_interval`::
  52. How long the list of hosts is cached to prevent further requests to the GCE API. `0s` disables caching.
  53. A negative value will cause infinite caching. Defaults to `0s`.
  54. [IMPORTANT]
  55. .Binding the network host
  56. ==============================================
  57. It's important to define `network.host` as by default it's bound to `localhost`.
  58. You can use {ref}/modules-network.html[core network host settings] or
  59. <<discovery-gce-network-host,gce specific host settings>>:
  60. ==============================================
  61. [[discovery-gce-network-host]]
  62. ==== GCE Network Host
  63. When the `discovery-gce` plugin is installed, the following are also allowed
  64. as valid network host settings:
  65. [cols="<,<",options="header",]
  66. |==================================================================
  67. |GCE Host Value |Description
  68. |`_gce:privateIp:X_` |The private IP address of the machine for a given network interface.
  69. |`_gce:hostname_` |The hostname of the machine.
  70. |`_gce_` |Same as `_gce:privateIp:0_` (recommended).
  71. |==================================================================
  72. Examples:
  73. [source,yaml]
  74. --------------------------------------------------
  75. # get the IP address from network interface 1
  76. network.host: _gce:privateIp:1_
  77. # shortcut for _gce:privateIp:0_
  78. network.host: _gce_
  79. # Using GCE internal hostname (recommended)
  80. network.host: _gce:hostname_
  81. --------------------------------------------------
  82. [[discovery-gce-usage-short]]
  83. ===== How to start (short story)
  84. * Create Google Compute Engine instance (with compute rw permissions)
  85. * Install Elasticsearch
  86. * Install Google Compute Engine Cloud plugin
  87. * Modify `elasticsearch.yml` file
  88. * Start Elasticsearch
  89. [[discovery-gce-usage-long]]
  90. ==== Setting up GCE Discovery
  91. [[discovery-gce-usage-long-prerequisites]]
  92. ===== Prerequisites
  93. Before starting, you need:
  94. * Your project ID, e.g. `es-cloud`. Get it from https://code.google.com/apis/console/[Google API Console].
  95. * To install https://developers.google.com/cloud/sdk/[Google Cloud SDK]
  96. If you did not set it yet, you can define your default project you will work on:
  97. [source,sh]
  98. --------------------------------------------------
  99. gcloud config set project es-cloud
  100. --------------------------------------------------
  101. [[discovery-gce-usage-long-login]]
  102. ===== Login to Google Cloud
  103. If you haven't already, login to Google Cloud
  104. [source,sh]
  105. --------------------------------------------------
  106. gcloud auth login
  107. --------------------------------------------------
  108. This will open your browser. You will be asked to sign-in to a Google account and
  109. authorize access to the Google Cloud SDK.
  110. [[discovery-gce-usage-long-first-instance]]
  111. ===== Creating your first instance
  112. [source,sh]
  113. --------------------------------------------------
  114. gcloud compute instances create myesnode1 \
  115. --zone <your-zone> \
  116. --scopes compute-rw
  117. --------------------------------------------------
  118. When done, a report like this one should appears:
  119. [source,text]
  120. --------------------------------------------------
  121. Created [https://www.googleapis.com/compute/v1/projects/es-cloud-1070/zones/us-central1-f/instances/myesnode1].
  122. NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS
  123. myesnode1 us-central1-f n1-standard-1 10.240.133.54 104.197.94.25 RUNNING
  124. --------------------------------------------------
  125. You can now connect to your instance:
  126. [source,sh]
  127. --------------------------------------------------
  128. # Connect using google cloud SDK
  129. gcloud compute ssh myesnode1 --zone europe-west1-a
  130. # Or using SSH with external IP address
  131. ssh -i ~/.ssh/google_compute_engine 192.158.29.199
  132. --------------------------------------------------
  133. [IMPORTANT]
  134. .Service Account Permissions
  135. ==============================================
  136. It's important when creating an instance that the correct permissions are set. At a minimum, you must ensure you have:
  137. [source,text]
  138. --------------------------------------------------
  139. scopes=compute-rw
  140. --------------------------------------------------
  141. Failing to set this will result in unauthorized messages when starting Elasticsearch.
  142. See <<discovery-gce-usage-tips-permissions>>.
  143. ==============================================
  144. Once connected, install Elasticsearch:
  145. [source,sh]
  146. --------------------------------------------------
  147. sudo apt-get update
  148. # Download Elasticsearch
  149. wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-2.0.0.deb
  150. # Prepare Java installation (Oracle)
  151. sudo echo "deb http://ppa.launchpad.net/webupd8team/java/ubuntu trusty main" | sudo tee /etc/apt/sources.list.d/webupd8team-java.list
  152. sudo echo "deb-src http://ppa.launchpad.net/webupd8team/java/ubuntu trusty main" | sudo tee -a /etc/apt/sources.list.d/webupd8team-java.list
  153. sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys EEA14886
  154. sudo apt-get update
  155. sudo apt-get install oracle-java8-installer
  156. # Prepare Java installation (or OpenJDK)
  157. # sudo apt-get install java8-runtime-headless
  158. # Prepare Elasticsearch installation
  159. sudo dpkg -i elasticsearch-2.0.0.deb
  160. --------------------------------------------------
  161. [[discovery-gce-usage-long-install-plugin]]
  162. ===== Install elasticsearch discovery gce plugin
  163. Install the plugin:
  164. [source,sh]
  165. --------------------------------------------------
  166. # Use Plugin Manager to install it
  167. sudo bin/elasticsearch-plugin install discovery-gce
  168. --------------------------------------------------
  169. Open the `elasticsearch.yml` file:
  170. [source,sh]
  171. --------------------------------------------------
  172. sudo vi /etc/elasticsearch/elasticsearch.yml
  173. --------------------------------------------------
  174. And add the following lines:
  175. [source,yaml]
  176. --------------------------------------------------
  177. cloud:
  178. gce:
  179. project_id: es-cloud
  180. zone: europe-west1-a
  181. discovery:
  182. type: gce
  183. --------------------------------------------------
  184. Start elasticsearch:
  185. [source,sh]
  186. --------------------------------------------------
  187. sudo /etc/init.d/elasticsearch start
  188. --------------------------------------------------
  189. If anything goes wrong, you should check logs:
  190. [source,sh]
  191. --------------------------------------------------
  192. tail -f /var/log/elasticsearch/elasticsearch.log
  193. --------------------------------------------------
  194. If needed, you can change log level to `trace` by opening `log4j2.properties`:
  195. [source,sh]
  196. --------------------------------------------------
  197. sudo vi /etc/elasticsearch/log4j2.properties
  198. --------------------------------------------------
  199. and adding the following line:
  200. [source,yaml]
  201. --------------------------------------------------
  202. # discovery
  203. logger.discovery_gce.name = discovery.gce
  204. logger.discovery_gce.level = trace
  205. --------------------------------------------------
  206. [[discovery-gce-usage-cloning]]
  207. ==== Cloning your existing machine
  208. In order to build a cluster on many nodes, you can clone your configured instance to new nodes.
  209. You won't have to reinstall everything!
  210. First create an image of your running instance and upload it to Google Cloud Storage:
  211. [source,sh]
  212. --------------------------------------------------
  213. # Create an image of your current instance
  214. sudo /usr/bin/gcimagebundle -d /dev/sda -o /tmp/
  215. # An image has been created in `/tmp` directory:
  216. ls /tmp
  217. e4686d7f5bf904a924ae0cfeb58d0827c6d5b966.image.tar.gz
  218. # Upload your image to Google Cloud Storage:
  219. # Create a bucket to hold your image, let's say `esimage`:
  220. gsutil mb gs://esimage
  221. # Copy your image to this bucket:
  222. gsutil cp /tmp/e4686d7f5bf904a924ae0cfeb58d0827c6d5b966.image.tar.gz gs://esimage
  223. # Then add your image to images collection:
  224. gcloud compute images create elasticsearch-2-0-0 --source-uri gs://esimage/e4686d7f5bf904a924ae0cfeb58d0827c6d5b966.image.tar.gz
  225. # If the previous command did not work for you, logout from your instance
  226. # and launch the same command from your local machine.
  227. --------------------------------------------------
  228. [[discovery-gce-usage-start-new-instances]]
  229. ===== Start new instances
  230. As you have now an image, you can create as many instances as you need:
  231. [source,sh]
  232. --------------------------------------------------
  233. # Just change node name (here myesnode2)
  234. gcloud compute instances create myesnode2 --image elasticsearch-2-0-0 --zone europe-west1-a
  235. # If you want to provide all details directly, you can use:
  236. gcloud compute instances create myesnode2 --image=elasticsearch-2-0-0 \
  237. --zone europe-west1-a --machine-type f1-micro --scopes=compute-rw
  238. --------------------------------------------------
  239. [[discovery-gce-usage-remove-instance]]
  240. ===== Remove an instance (aka shut it down)
  241. You can use https://cloud.google.com/console[Google Cloud Console] or CLI to manage your instances:
  242. [source,sh]
  243. --------------------------------------------------
  244. # Stopping and removing instances
  245. gcloud compute instances delete myesnode1 myesnode2 \
  246. --zone=europe-west1-a
  247. # Consider removing disk as well if you don't need them anymore
  248. gcloud compute disks deleted boot-myesnode1 boot-myesnode2 \
  249. --zone=europe-west1-a
  250. --------------------------------------------------
  251. [[discovery-gce-usage-zones]]
  252. ==== Using GCE zones
  253. `cloud.gce.zone` helps to retrieve instances running in a given zone. It should be one of the
  254. https://developers.google.com/compute/docs/zones#available[GCE supported zones].
  255. The GCE discovery can support multi zones although you need to be aware of network latency between zones.
  256. To enable discovery across more than one zone, just enter add your zone list to `cloud.gce.zone` setting:
  257. [source,yaml]
  258. --------------------------------------------------
  259. cloud:
  260. gce:
  261. project_id: <your-google-project-id>
  262. zone: ["<your-zone1>", "<your-zone2>"]
  263. discovery:
  264. type: gce
  265. --------------------------------------------------
  266. [[discovery-gce-usage-tags]]
  267. ==== Filtering by tags
  268. The GCE discovery can also filter machines to include in the cluster based on tags using `discovery.gce.tags` settings.
  269. For example, setting `discovery.gce.tags` to `dev` will only filter instances having a tag set to `dev`. Several tags
  270. set will require all of those tags to be set for the instance to be included.
  271. One practical use for tag filtering is when an GCE cluster contains many nodes that are not running
  272. elasticsearch. In this case (particularly with high `discovery.zen.ping_timeout` values) there is a risk that a new
  273. node's discovery phase will end before it has found the cluster (which will result in it declaring itself master of a
  274. new cluster with the same name - highly undesirable). Adding tag on elasticsearch GCE nodes and then filtering by that
  275. tag will resolve this issue.
  276. Add your tag when building the new instance:
  277. [source,sh]
  278. --------------------------------------------------
  279. gcloud compute instances create myesnode1 --project=es-cloud \
  280. --scopes=compute-rw \
  281. --tags=elasticsearch,dev
  282. --------------------------------------------------
  283. Then, define it in `elasticsearch.yml`:
  284. [source,yaml]
  285. --------------------------------------------------
  286. cloud:
  287. gce:
  288. project_id: es-cloud
  289. zone: europe-west1-a
  290. discovery:
  291. type: gce
  292. gce:
  293. tags: elasticsearch, dev
  294. --------------------------------------------------
  295. [[discovery-gce-usage-port]]
  296. ==== Changing default transport port
  297. By default, elasticsearch GCE plugin assumes that you run elasticsearch on 9300 default port.
  298. But you can specify the port value elasticsearch is meant to use using google compute engine metadata `es_port`:
  299. [[discovery-gce-usage-port-create]]
  300. ===== When creating instance
  301. Add `--metadata es_port=9301` option:
  302. [source,sh]
  303. --------------------------------------------------
  304. # when creating first instance
  305. gcloud compute instances create myesnode1 \
  306. --scopes=compute-rw,storage-full \
  307. --metadata es_port=9301
  308. # when creating an instance from an image
  309. gcloud compute instances create myesnode2 --image=elasticsearch-1-0-0-RC1 \
  310. --zone europe-west1-a --machine-type f1-micro --scopes=compute-rw \
  311. --metadata es_port=9301
  312. --------------------------------------------------
  313. [[discovery-gce-usage-port-run]]
  314. ===== On a running instance
  315. [source,sh]
  316. --------------------------------------------------
  317. gcloud compute instances add-metadata myesnode1 \
  318. --zone europe-west1-a \
  319. --metadata es_port=9301
  320. --------------------------------------------------
  321. [[discovery-gce-usage-tips]]
  322. ==== GCE Tips
  323. [[discovery-gce-usage-tips-projectid]]
  324. ===== Store project id locally
  325. If you don't want to repeat the project id each time, you can save it in the local gcloud config
  326. [source,sh]
  327. --------------------------------------------------
  328. gcloud config set project es-cloud
  329. --------------------------------------------------
  330. [[discovery-gce-usage-tips-permissions]]
  331. ===== Machine Permissions
  332. If you have created a machine without the correct permissions, you will see `403 unauthorized` error messages. The only
  333. way to alter these permissions is to delete the instance (NOT THE DISK). Then create another with the correct permissions.
  334. Creating machines with gcloud::
  335. +
  336. --
  337. Ensure the following flags are set:
  338. [source,text]
  339. --------------------------------------------------
  340. --scopes=compute-rw
  341. --------------------------------------------------
  342. --
  343. Creating with console (web)::
  344. +
  345. --
  346. When creating an instance using the web portal, click _Show advanced options_.
  347. At the bottom of the page, under `PROJECT ACCESS`, choose `>> Compute >> Read Write`.
  348. --
  349. Creating with knife google::
  350. +
  351. --
  352. Set the service account scopes when creating the machine:
  353. [source,sh]
  354. --------------------------------------------------
  355. knife google server create www1 \
  356. -m n1-standard-1 \
  357. -I debian-8 \
  358. -Z us-central1-a \
  359. -i ~/.ssh/id_rsa \
  360. -x jdoe \
  361. --gce-service-account-scopes https://www.googleapis.com/auth/compute.full_control
  362. --------------------------------------------------
  363. Or, you may use the alias:
  364. [source,sh]
  365. --------------------------------------------------
  366. --gce-service-account-scopes compute-rw
  367. --------------------------------------------------
  368. --
  369. [[discovery-gce-usage-testing]]
  370. ==== Testing GCE
  371. Integrations tests in this plugin require working GCE configuration and
  372. therefore disabled by default. To enable tests prepare a config file
  373. elasticsearch.yml with the following content:
  374. [source,yaml]
  375. --------------------------------------------------
  376. cloud:
  377. gce:
  378. project_id: es-cloud
  379. zone: europe-west1-a
  380. discovery:
  381. type: gce
  382. --------------------------------------------------
  383. Replaces `project_id` and `zone` with your settings.
  384. To run test:
  385. [source,sh]
  386. --------------------------------------------------
  387. mvn -Dtests.gce=true -Dtests.config=/path/to/config/file/elasticsearch.yml clean test
  388. --------------------------------------------------