discovery-gce.asciidoc 16 KB

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