configuring-tls-docker.asciidoc 6.7 KB

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