configuring-tls-docker.asciidoc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. [role="xpack"]
  2. [[configuring-tls-docker]]
  3. === Encrypting communications in an {es} Docker Container
  4. Unless you are using a trial license, {stack} {security-features} require
  5. SSL/TLS 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. {stack-ov}/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. COMPOSE_PROJECT_NAME=es <1>
  38. CERTS_DIR=/usr/share/elasticsearch/config/certificates <2>
  39. ELASTIC_PASSWORD=PleaseChangeMe <3>
  40. ----
  41. <1> Use an `es_` prefix for all volumes and networks created by docker-compose.
  42. <2> The path, inside the Docker image, where certificates are expected to be found.
  43. <3> 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. yum install -y -q -e 0 unzip;
  61. if [[ ! -f /certs/bundle.zip ]]; then
  62. bin/elasticsearch-certutil cert --silent --pem --in config/certificates/instances.yml -out /certs/bundle.zip;
  63. unzip /certs/bundle.zip -d /certs; <1>
  64. fi;
  65. chown -R 1000:0 /certs
  66. '
  67. user: "0"
  68. working_dir: /usr/share/elasticsearch
  69. volumes: ['certs:/certs', '.:/usr/share/elasticsearch/config/certificates']
  70. volumes: {"certs"}
  71. ----
  72. <1> The new node certificates and CA certificate+key are placed in a docker volume `es_certs`.
  73. endif::[]
  74. [[getting-starter-tls-create-docker-compose]]
  75. `docker-compose.yml`:
  76. ifeval::["{release-state}"=="unreleased"]
  77. WARNING: Version {version} of {es} has not yet been released, so a
  78. `docker-compose.yml` is not available for this version.
  79. endif::[]
  80. ifeval::["{release-state}"!="unreleased"]
  81. ["source","yaml",subs="attributes"]
  82. ----
  83. version: '2.2'
  84. services:
  85. es01:
  86. container_name: es01
  87. image: {docker-image}
  88. environment:
  89. - node.name=es01
  90. - discovery.seed_hosts=es01,es02
  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.http.ssl.key=$CERTS_DIR/es01/es01.key
  98. - xpack.security.http.ssl.certificate_authorities=$CERTS_DIR/ca/ca.crt
  99. - xpack.security.http.ssl.certificate=$CERTS_DIR/es01/es01.crt
  100. - xpack.security.transport.ssl.enabled=true
  101. - xpack.security.transport.ssl.verification_mode=certificate <3>
  102. - xpack.security.transport.ssl.certificate_authorities=$CERTS_DIR/ca/ca.crt
  103. - xpack.security.transport.ssl.certificate=$CERTS_DIR/es01/es01.crt
  104. - xpack.security.transport.ssl.key=$CERTS_DIR/es01/es01.key
  105. volumes: ['data01:/usr/share/elasticsearch/data', 'certs:$CERTS_DIR']
  106. ports:
  107. - 9200:9200
  108. healthcheck:
  109. test: curl --cacert $CERTS_DIR/ca/ca.crt -s https://localhost:9200 >/dev/null; if [[ $$? == 52 ]]; then echo 0; else echo 1; fi
  110. interval: 30s
  111. timeout: 10s
  112. retries: 5
  113. es02:
  114. container_name: es02
  115. image: {docker-image}
  116. environment:
  117. - node.name=es02
  118. - discovery.seed_hosts=es01,es02
  119. - cluster.initial_master_nodes=es01,es02
  120. - ELASTIC_PASSWORD=$ELASTIC_PASSWORD
  121. - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
  122. - xpack.license.self_generated.type=trial
  123. - xpack.security.enabled=true
  124. - xpack.security.http.ssl.enabled=true
  125. - xpack.security.http.ssl.key=$CERTS_DIR/es02/es02.key
  126. - xpack.security.http.ssl.certificate_authorities=$CERTS_DIR/ca/ca.crt
  127. - xpack.security.http.ssl.certificate=$CERTS_DIR/es02/es02.crt
  128. - xpack.security.transport.ssl.enabled=true
  129. - xpack.security.transport.ssl.verification_mode=certificate <3>
  130. - xpack.security.transport.ssl.certificate_authorities=$CERTS_DIR/ca/ca.crt
  131. - xpack.security.transport.ssl.certificate=$CERTS_DIR/es02/es02.crt
  132. - xpack.security.transport.ssl.key=$CERTS_DIR/es02/es02.key
  133. volumes: ['data02:/usr/share/elasticsearch/data', 'certs:$CERTS_DIR']
  134. wait_until_ready:
  135. image: {docker-image}
  136. command: /usr/bin/true
  137. depends_on: {"es01": {"condition": "service_healthy"}}
  138. volumes: {"data01", "data02", "certs"}
  139. ----
  140. <1> Bootstrap `elastic` with the password defined in `.env`. See
  141. {stack-ov}/built-in-users.html#bootstrap-elastic-passwords[the Elastic Bootstrap Password].
  142. <2> Automatically generate and apply a trial subscription, in order to enable
  143. {security-features}.
  144. <3> Disable verification of authenticity for inter-node communication. Allows
  145. creating self-signed certificates without having to pin specific internal IP addresses.
  146. endif::[]
  147. [float]
  148. ==== Run the example
  149. . Generate the certificates (only needed once):
  150. +
  151. --
  152. ["source","sh"]
  153. ----
  154. docker-compose -f create-certs.yml run --rm create_certs
  155. ----
  156. --
  157. . Start two {es} nodes configured for SSL/TLS:
  158. +
  159. --
  160. ["source","sh"]
  161. ----
  162. docker-compose up -d
  163. ----
  164. --
  165. . Access the {es} API over SSL/TLS using the bootstrapped password:
  166. +
  167. --
  168. ["source","sh",subs="attributes"]
  169. ----
  170. docker run --rm -v es_certs:/certs --network=es_default {docker-image} curl --cacert /certs/ca/ca.crt -u elastic:PleaseChangeMe https://es01:9200
  171. ----
  172. // NOTCONSOLE
  173. --
  174. . The `elasticsearch-setup-passwords` tool can also be used to generate random
  175. passwords for all users:
  176. +
  177. --
  178. WARNING: Windows users not running PowerShell will need to remove `\` and join lines in the snippet below.
  179. ["source","sh"]
  180. ----
  181. docker exec es01 /bin/bash -c "bin/elasticsearch-setup-passwords \
  182. auto --batch \
  183. -Expack.security.http.ssl.certificate=certificates/es01/es01.crt \
  184. -Expack.security.http.ssl.certificate_authorities=certificates/ca/ca.crt \
  185. -Expack.security.http.ssl.key=certificates/es01/es01.key \
  186. --url https://localhost:9200"
  187. ----
  188. --
  189. [float]
  190. ==== Tear everything down
  191. To remove all the Docker resources created by the example, issue:
  192. --
  193. ["source","sh"]
  194. ----
  195. docker-compose down -v
  196. ----
  197. --