discovery-gce.asciidoc 16 KB

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