configuring-tls-docker.asciidoc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. [role="xpack"]
  2. [[configuring-tls-docker]]
  3. === Encrypting Communications in an {es} Docker Container
  4. Starting with version 6.0.0, {security} (Gold, Platinum or Enterprise subscriptions) https://www.elastic.co/guide/en/elasticsearch/reference/6.0/breaking-6.0.0-xes.html[requires SSL/TLS]
  5. encryption for the transport networking layer.
  6. This section demonstrates an easy path to get started with SSL/TLS for both
  7. HTTPS and transport using the {es} Docker image. The example uses
  8. Docker Compose to manage the containers.
  9. For further details, please refer to
  10. {xpack-ref}/encrypting-communications.html[Encrypting Communications] and
  11. https://www.elastic.co/subscriptions[available subscriptions].
  12. [float]
  13. ==== Prepare the environment
  14. <<docker,Install {es} with Docker>>.
  15. Inside a new, empty directory, create the following four files:
  16. `instances.yml`:
  17. ["source","yaml"]
  18. ----
  19. instances:
  20. - name: es01
  21. dns:
  22. - es01 <1>
  23. - localhost
  24. ip:
  25. - 127.0.0.1
  26. - name: es02
  27. dns:
  28. - es02
  29. - localhost
  30. ip:
  31. - 127.0.0.1
  32. ----
  33. <1> Allow use of embedded Docker DNS server names.
  34. `.env`:
  35. [source,yaml]
  36. ----
  37. CERTS_DIR=/usr/share/elasticsearch/config/certificates <1>
  38. ELASTIC_PASSWORD=PleaseChangeMe <2>
  39. ----
  40. <1> The path, inside the Docker image, where certificates are expected to be found.
  41. <2> Initial password for the `elastic` user.
  42. [[getting-starter-tls-create-certs-composefile]]
  43. `create-certs.yml`:
  44. ifeval::["{release-state}"=="unreleased"]
  45. WARNING: Version {version} of {es} has not yet been released, so a
  46. `create-certs.yml` is not available for this version.
  47. endif::[]
  48. ifeval::["{release-state}"!="unreleased"]
  49. ["source","yaml",subs="attributes"]
  50. ----
  51. version: '2.2'
  52. services:
  53. create_certs:
  54. container_name: create_certs
  55. image: docker.elastic.co/elasticsearch/elasticsearch-platinum:{version}
  56. command: >
  57. bash -c '
  58. if [[ ! -d config/certificates/certs ]]; then
  59. mkdir config/certificates/certs;
  60. fi;
  61. if [[ ! -f /local/certs/bundle.zip ]]; then
  62. bin/elasticsearch-certgen --silent --in config/certificates/instances.yml --out config/certificates/certs/bundle.zip;
  63. unzip config/certificates/certs/bundle.zip -d config/certificates/certs; <1>
  64. fi;
  65. chgrp -R 0 config/certificates/certs
  66. '
  67. user: $\{UID:-1000\}
  68. working_dir: /usr/share/elasticsearch
  69. volumes: ['.:/usr/share/elasticsearch/config/certificates']
  70. ----
  71. <1> The new node certificates and CA certificate+key are placed under the local directory `certs`.
  72. endif::[]
  73. [[getting-starter-tls-create-docker-compose]]
  74. `docker-compose.yml`:
  75. ifeval::["{release-state}"=="unreleased"]
  76. WARNING: Version {version} of {es} has not yet been released, so a
  77. `docker-compose.yml` is not available for this version.
  78. endif::[]
  79. ifeval::["{release-state}"!="unreleased"]
  80. ["source","yaml",subs="attributes"]
  81. ----
  82. version: '2.2'
  83. services:
  84. es01:
  85. container_name: es01
  86. image: docker.elastic.co/elasticsearch/elasticsearch-platinum:{version}
  87. environment:
  88. - node.name=es01
  89. - discovery.zen.minimum_master_nodes=2
  90. - ELASTIC_PASSWORD=$ELASTIC_PASSWORD <1>
  91. - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
  92. - xpack.license.self_generated.type=trial <2>
  93. - xpack.security.enabled=true
  94. - xpack.security.http.ssl.enabled=true
  95. - xpack.security.transport.ssl.enabled=true
  96. - xpack.security.transport.ssl.verification_mode=certificate <3>
  97. - xpack.ssl.certificate_authorities=$CERTS_DIR/ca/ca.crt
  98. - xpack.ssl.certificate=$CERTS_DIR/es01/es01.crt
  99. - xpack.ssl.key=$CERTS_DIR/es01/es01.key
  100. volumes: ['esdata_01:/usr/share/elasticsearch/data', './certs:$CERTS_DIR']
  101. ports:
  102. - 9200:9200
  103. healthcheck:
  104. test: curl --cacert $CERTS_DIR/ca/ca.crt -s https://localhost:9200 >/dev/null; if [[ $$? == 52 ]]; then echo 0; else echo 1; fi
  105. interval: 30s
  106. timeout: 10s
  107. retries: 5
  108. es02:
  109. container_name: es02
  110. image: docker.elastic.co/elasticsearch/elasticsearch:{version}
  111. environment:
  112. - node.name=es02
  113. - discovery.zen.minimum_master_nodes=2
  114. - ELASTIC_PASSWORD=$ELASTIC_PASSWORD
  115. - discovery.zen.ping.unicast.hosts=es01
  116. - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
  117. - xpack.license.self_generated.type=trial
  118. - xpack.security.enabled=true
  119. - xpack.security.http.ssl.enabled=true
  120. - xpack.security.transport.ssl.enabled=true
  121. - xpack.security.transport.ssl.verification_mode=certificate
  122. - xpack.ssl.certificate_authorities=$CERTS_DIR/ca/ca.crt
  123. - xpack.ssl.certificate=$CERTS_DIR/es02/es02.crt
  124. - xpack.ssl.key=$CERTS_DIR/es02/es02.key
  125. volumes: ['esdata_02:/usr/share/elasticsearch/data', './certs:$CERTS_DIR']
  126. wait_until_ready:
  127. image: docker.elastic.co/elasticsearch/elasticsearch:{version}
  128. command: /usr/bin/true
  129. depends_on: {"es01": {"condition": "service_healthy"}}
  130. volumes: {"esdata_01": {"driver": "local"}, "esdata_02": {"driver": "local"}}
  131. ----
  132. <1> Bootstrap `elastic` with the password defined in `.env`. See
  133. {stack-ov}/built-in-users.html#bootstrap-elastic-passwords[the Elastic Bootstrap Password].
  134. <2> Automatically generate and apply a trial subscription, in order to enable
  135. {security}.
  136. <3> Disable verification of authenticity for inter-node communication. Allows
  137. creating self-signed certificates without having to pin specific internal IP addresses.
  138. endif::[]
  139. [float]
  140. ==== Run the example
  141. . Generate the certificates (only needed once):
  142. +
  143. --
  144. ["source","sh"]
  145. ----
  146. docker-compose -f create-certs.yml up
  147. ----
  148. --
  149. . Start two {es} nodes configured for SSL/TLS:
  150. +
  151. --
  152. ["source","sh"]
  153. ----
  154. docker-compose up -d
  155. ----
  156. --
  157. . Access the {es} API over SSL/TLS using the bootstrapped password:
  158. +
  159. --
  160. ["source","sh"]
  161. ----
  162. curl --cacert certs/ca/ca.crt -u elastic:PleaseChangeMe https://localhost:9200
  163. ----
  164. // NOTCONSOLE
  165. --
  166. . The `elasticsearch-setup-passwords` tool can also be used to generate random
  167. passwords for all users:
  168. +
  169. --
  170. WARNING: Windows users not running PowerShell will need to remove `\` and join lines in the snippet below.
  171. ["source","sh"]
  172. ----
  173. docker exec es01 /bin/bash -c "bin/elasticsearch-setup-passwords \
  174. auto --batch \
  175. -Expack.ssl.certificate=certificates/es01/es01.crt \
  176. -Expack.ssl.certificate_authorities=certificates/ca/ca.crt \
  177. -Expack.ssl.key=certificates/es01/es01.key \
  178. --url https://localhost:9200"
  179. ----
  180. --