discovery-gce.asciidoc 15 KB

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