1
0

configuring-tls-docker.asciidoc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. - cluster.initial_master_nodes=es01,es02
  92. - ELASTIC_PASSWORD=$ELASTIC_PASSWORD <1>
  93. - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
  94. - xpack.license.self_generated.type=trial <2>
  95. - xpack.security.enabled=true
  96. - xpack.security.http.ssl.enabled=true
  97. - xpack.security.transport.ssl.enabled=true
  98. - xpack.security.transport.ssl.verification_mode=certificate <3>
  99. - xpack.ssl.certificate_authorities=$CERTS_DIR/ca/ca.crt
  100. - xpack.ssl.certificate=$CERTS_DIR/es01/es01.crt
  101. - xpack.ssl.key=$CERTS_DIR/es01/es01.key
  102. volumes: ['esdata_01:/usr/share/elasticsearch/data', './certs:$CERTS_DIR']
  103. ports:
  104. - 9200:9200
  105. healthcheck:
  106. test: curl --cacert $CERTS_DIR/ca/ca.crt -s https://localhost:9200 >/dev/null; if [[ $$? == 52 ]]; then echo 0; else echo 1; fi
  107. interval: 30s
  108. timeout: 10s
  109. retries: 5
  110. es02:
  111. container_name: es02
  112. image: {docker-image}
  113. environment:
  114. - node.name=es02
  115. - discovery.zen.ping.unicast.hosts=es01
  116. - cluster.initial_master_nodes=es01,es02
  117. - ELASTIC_PASSWORD=$ELASTIC_PASSWORD
  118. - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
  119. - xpack.license.self_generated.type=trial
  120. - xpack.security.enabled=true
  121. - xpack.security.http.ssl.enabled=true
  122. - xpack.security.transport.ssl.enabled=true
  123. - xpack.security.transport.ssl.verification_mode=certificate
  124. - xpack.ssl.certificate_authorities=$CERTS_DIR/ca/ca.crt
  125. - xpack.ssl.certificate=$CERTS_DIR/es02/es02.crt
  126. - xpack.ssl.key=$CERTS_DIR/es02/es02.key
  127. volumes: ['esdata_02:/usr/share/elasticsearch/data', './certs:$CERTS_DIR']
  128. wait_until_ready:
  129. image: {docker-image}
  130. command: /usr/bin/true
  131. depends_on: {"es01": {"condition": "service_healthy"}}
  132. volumes: {"esdata_01": {"driver": "local"}, "esdata_02": {"driver": "local"}}
  133. ----
  134. <1> Bootstrap `elastic` with the password defined in `.env`. See
  135. {stack-ov}/built-in-users.html#bootstrap-elastic-passwords[the Elastic Bootstrap Password].
  136. <2> Automatically generate and apply a trial subscription, in order to enable
  137. {security-features}.
  138. <3> Disable verification of authenticity for inter-node communication. Allows
  139. creating self-signed certificates without having to pin specific internal IP addresses.
  140. endif::[]
  141. [float]
  142. ==== Run the example
  143. . Generate the certificates (only needed once):
  144. +
  145. --
  146. ["source","sh"]
  147. ----
  148. docker-compose -f create-certs.yml up
  149. ----
  150. --
  151. . Start two {es} nodes configured for SSL/TLS:
  152. +
  153. --
  154. ["source","sh"]
  155. ----
  156. docker-compose up -d
  157. ----
  158. --
  159. . Access the {es} API over SSL/TLS using the bootstrapped password:
  160. +
  161. --
  162. ["source","sh"]
  163. ----
  164. curl --cacert certs/ca/ca.crt -u elastic:PleaseChangeMe https://localhost:9200
  165. ----
  166. // NOTCONSOLE
  167. --
  168. . The `elasticsearch-setup-passwords` tool can also be used to generate random
  169. passwords for all users:
  170. +
  171. --
  172. WARNING: Windows users not running PowerShell will need to remove `\` and join lines in the snippet below.
  173. ["source","sh"]
  174. ----
  175. docker exec es01 /bin/bash -c "bin/elasticsearch-setup-passwords \
  176. auto --batch \
  177. -Expack.ssl.certificate=certificates/es01/es01.crt \
  178. -Expack.ssl.certificate_authorities=certificates/ca/ca.crt \
  179. -Expack.ssl.key=certificates/es01/es01.key \
  180. --url https://localhost:9200"
  181. ----
  182. --