docker-compose快速部署elasticsearch-8.x集群+kibana

x33g5p2x  于2022-06-13 转载在 Docker  
字(13.2k)|赞(0)|评价(0)|浏览(933)

欢迎访问我的GitHub

这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos

本篇概览

  • 前文《Docker下elasticsearch8部署、扩容、基本操作实战(含kibana)》介绍了用docker快速部署es和kibana的过程,然而整个过程人工操作步骤还是多了点,能不能更简单些呢?毕竟很多时候大家关注的是使用,不愿在部署上费太多时间
  • 借助docker-compose,可以将es集群+kibana的安装过程可以进一步简化,精简后的步骤如下图,已经省的不能再省了…

  • 本文会按照上述流程进行实战,一共实战两次:第一次部署带证书账号密码的安全版本,第二次部署没有任何安全检查的版本,装好直接访问使用
  • 请注意docker部署ElasticSearch的适用场景:我这边只在开发过程中使用,此种方式在生产环境是否适合是有待商榷的,用于生产环境时请您慎重考虑
  • 本篇由以下内容构成
  1. 介绍我这边实战的环境供您参考
  2. Linxu用户需要额外注意的地方
  3. 编写配置文件
  4. 启动
  5. 验证

环境信息

  • 以下是本次实战的环境信息,可以作为参考
  1. 操作系统:macOS Monterey(M1 Pro芯片的MacBook Pro,16G内存)
  2. Docker:Docker Desktop 4.7.1 (77678)
  3. ElasticSearch:8.2.2
  4. Kibana:8.2.2

Linux用户请注意

  • 如果您的环境是Linux,注意要做以下操作,否则es可能会启动失败
  1. 用编辑工具打开文件/etc/sysctl.conf
  2. 在尾部添加一行配置vm.max_map_count = 262144,如果已存在就修改,数值不能低于262144
  3. 修改保存,然后执行命令sudo sysctl -p使其立即生效

编写配置文件

  • 再次确认接下来工作的目标:用docker-compose快速部署es集群+kibana,这个集群是带安全检查的(自签证书+账号密码)
  • 找个干净目录,新建名为.env的文件,内容如下,这是给docker-compose用到的配置文件每个配置项都有详细注释说明
  1. # elastic账号的密码 (至少六个字符)
  2. ELASTIC_PASSWORD=123456
  3. # kibana_system账号的密码 (至少六个字符),该账号仅用于一些kibana的内部设置,不能用来查询es
  4. KIBANA_PASSWORD=abcdef
  5. # es和kibana的版本
  6. STACK_VERSION=8.2.2
  7. # 集群名字
  8. CLUSTER_NAME=docker-cluster
  9. # x-pack安全设置,这里选择basic,基础设置,如果选择了trail,则会在30天后到期
  10. LICENSE=basic
  11. #LICENSE=trial
  12. # es映射到宿主机的的端口
  13. ES_PORT=9200
  14. # kibana映射到宿主机的的端口
  15. KIBANA_PORT=5601
  16. # es容器的内存大小,请根据自己硬件情况调整
  17. MEM_LIMIT=1073741824
  18. # 命名空间,会体现在容器名的前缀上
  19. COMPOSE_PROJECT_NAME=demo
  • 然后是docker-compose.yaml文件,这里面会用到刚才创建的.env文件,一共创建了五个容器:启动操作、三个es组成集群,一个kibana(多说一句:官方脚本,放心用)
  1. version: "2.2"
  2. services:
  3. setup:
  4. image: elasticsearch:${STACK_VERSION}
  5. volumes:
  6. - certs:/usr/share/elasticsearch/config/certs
  7. user: "0"
  8. command: >
  9. bash -c '
  10. if [ x${ELASTIC_PASSWORD} == x ]; then
  11. echo "Set the ELASTIC_PASSWORD environment variable in the .env file";
  12. exit 1;
  13. elif [ x${KIBANA_PASSWORD} == x ]; then
  14. echo "Set the KIBANA_PASSWORD environment variable in the .env file";
  15. exit 1;
  16. fi;
  17. if [ ! -f config/certs/ca.zip ]; then
  18. echo "Creating CA";
  19. bin/elasticsearch-certutil ca --silent --pem -out config/certs/ca.zip;
  20. unzip config/certs/ca.zip -d config/certs;
  21. fi;
  22. if [ ! -f config/certs/certs.zip ]; then
  23. echo "Creating certs";
  24. echo -ne \
  25. "instances:\n"\
  26. " - name: es01\n"\
  27. " dns:\n"\
  28. " - es01\n"\
  29. " - localhost\n"\
  30. " ip:\n"\
  31. " - 127.0.0.1\n"\
  32. " - name: es02\n"\
  33. " dns:\n"\
  34. " - es02\n"\
  35. " - localhost\n"\
  36. " ip:\n"\
  37. " - 127.0.0.1\n"\
  38. " - name: es03\n"\
  39. " dns:\n"\
  40. " - es03\n"\
  41. " - localhost\n"\
  42. " ip:\n"\
  43. " - 127.0.0.1\n"\
  44. > config/certs/instances.yml;
  45. bin/elasticsearch-certutil cert --silent --pem -out config/certs/certs.zip --in config/certs/instances.yml --ca-cert config/certs/ca/ca.crt --ca-key config/certs/ca/ca.key;
  46. unzip config/certs/certs.zip -d config/certs;
  47. fi;
  48. echo "Setting file permissions"
  49. chown -R root:root config/certs;
  50. find . -type d -exec chmod 750 \{\} \;;
  51. find . -type f -exec chmod 640 \{\} \;;
  52. echo "Waiting for Elasticsearch availability";
  53. until curl -s --cacert config/certs/ca/ca.crt https://es01:9200 | grep -q "missing authentication credentials"; do sleep 30; done;
  54. echo "Setting kibana_system password";
  55. until curl -s -X POST --cacert config/certs/ca/ca.crt -u elastic:${ELASTIC_PASSWORD} -H "Content-Type: application/json" https://es01:9200/_security/user/kibana_system/_password -d "{\"password\":\"${KIBANA_PASSWORD}\"}" | grep -q "^{}"; do sleep 10; done;
  56. echo "All done!";
  57. '
  58. healthcheck:
  59. test: ["CMD-SHELL", "[ -f config/certs/es01/es01.crt ]"]
  60. interval: 1s
  61. timeout: 5s
  62. retries: 120
  63. es01:
  64. depends_on:
  65. setup:
  66. condition: service_healthy
  67. image: elasticsearch:${STACK_VERSION}
  68. volumes:
  69. - certs:/usr/share/elasticsearch/config/certs
  70. - esdata01:/usr/share/elasticsearch/data
  71. ports:
  72. - ${ES_PORT}:9200
  73. environment:
  74. - node.name=es01
  75. - cluster.name=${CLUSTER_NAME}
  76. - cluster.initial_master_nodes=es01,es02,es03
  77. - discovery.seed_hosts=es02,es03
  78. - ELASTIC_PASSWORD=${ELASTIC_PASSWORD}
  79. - bootstrap.memory_lock=true
  80. - xpack.security.enabled=true
  81. - xpack.security.http.ssl.enabled=true
  82. - xpack.security.http.ssl.key=certs/es01/es01.key
  83. - xpack.security.http.ssl.certificate=certs/es01/es01.crt
  84. - xpack.security.http.ssl.certificate_authorities=certs/ca/ca.crt
  85. - xpack.security.http.ssl.verification_mode=certificate
  86. - xpack.security.transport.ssl.enabled=true
  87. - xpack.security.transport.ssl.key=certs/es01/es01.key
  88. - xpack.security.transport.ssl.certificate=certs/es01/es01.crt
  89. - xpack.security.transport.ssl.certificate_authorities=certs/ca/ca.crt
  90. - xpack.security.transport.ssl.verification_mode=certificate
  91. - xpack.license.self_generated.type=${LICENSE}
  92. mem_limit: ${MEM_LIMIT}
  93. ulimits:
  94. memlock:
  95. soft: -1
  96. hard: -1
  97. healthcheck:
  98. test:
  99. [
  100. "CMD-SHELL",
  101. "curl -s --cacert config/certs/ca/ca.crt https://localhost:9200 | grep -q 'missing authentication credentials'",
  102. ]
  103. interval: 10s
  104. timeout: 10s
  105. retries: 120
  106. es02:
  107. depends_on:
  108. - es01
  109. image: elasticsearch:${STACK_VERSION}
  110. volumes:
  111. - certs:/usr/share/elasticsearch/config/certs
  112. - esdata02:/usr/share/elasticsearch/data
  113. environment:
  114. - node.name=es02
  115. - cluster.name=${CLUSTER_NAME}
  116. - cluster.initial_master_nodes=es01,es02,es03
  117. - discovery.seed_hosts=es01,es03
  118. - bootstrap.memory_lock=true
  119. - xpack.security.enabled=true
  120. - xpack.security.http.ssl.enabled=true
  121. - xpack.security.http.ssl.key=certs/es02/es02.key
  122. - xpack.security.http.ssl.certificate=certs/es02/es02.crt
  123. - xpack.security.http.ssl.certificate_authorities=certs/ca/ca.crt
  124. - xpack.security.http.ssl.verification_mode=certificate
  125. - xpack.security.transport.ssl.enabled=true
  126. - xpack.security.transport.ssl.key=certs/es02/es02.key
  127. - xpack.security.transport.ssl.certificate=certs/es02/es02.crt
  128. - xpack.security.transport.ssl.certificate_authorities=certs/ca/ca.crt
  129. - xpack.security.transport.ssl.verification_mode=certificate
  130. - xpack.license.self_generated.type=${LICENSE}
  131. mem_limit: ${MEM_LIMIT}
  132. ulimits:
  133. memlock:
  134. soft: -1
  135. hard: -1
  136. healthcheck:
  137. test:
  138. [
  139. "CMD-SHELL",
  140. "curl -s --cacert config/certs/ca/ca.crt https://localhost:9200 | grep -q 'missing authentication credentials'",
  141. ]
  142. interval: 10s
  143. timeout: 10s
  144. retries: 120
  145. es03:
  146. depends_on:
  147. - es02
  148. image: elasticsearch:${STACK_VERSION}
  149. volumes:
  150. - certs:/usr/share/elasticsearch/config/certs
  151. - esdata03:/usr/share/elasticsearch/data
  152. environment:
  153. - node.name=es03
  154. - cluster.name=${CLUSTER_NAME}
  155. - cluster.initial_master_nodes=es01,es02,es03
  156. - discovery.seed_hosts=es01,es02
  157. - bootstrap.memory_lock=true
  158. - xpack.security.enabled=true
  159. - xpack.security.http.ssl.enabled=true
  160. - xpack.security.http.ssl.key=certs/es03/es03.key
  161. - xpack.security.http.ssl.certificate=certs/es03/es03.crt
  162. - xpack.security.http.ssl.certificate_authorities=certs/ca/ca.crt
  163. - xpack.security.http.ssl.verification_mode=certificate
  164. - xpack.security.transport.ssl.enabled=true
  165. - xpack.security.transport.ssl.key=certs/es03/es03.key
  166. - xpack.security.transport.ssl.certificate=certs/es03/es03.crt
  167. - xpack.security.transport.ssl.certificate_authorities=certs/ca/ca.crt
  168. - xpack.security.transport.ssl.verification_mode=certificate
  169. - xpack.license.self_generated.type=${LICENSE}
  170. mem_limit: ${MEM_LIMIT}
  171. ulimits:
  172. memlock:
  173. soft: -1
  174. hard: -1
  175. healthcheck:
  176. test:
  177. [
  178. "CMD-SHELL",
  179. "curl -s --cacert config/certs/ca/ca.crt https://localhost:9200 | grep -q 'missing authentication credentials'",
  180. ]
  181. interval: 10s
  182. timeout: 10s
  183. retries: 120
  184. kibana:
  185. depends_on:
  186. es01:
  187. condition: service_healthy
  188. es02:
  189. condition: service_healthy
  190. es03:
  191. condition: service_healthy
  192. image: kibana:${STACK_VERSION}
  193. volumes:
  194. - certs:/usr/share/kibana/config/certs
  195. - kibanadata:/usr/share/kibana/data
  196. ports:
  197. - ${KIBANA_PORT}:5601
  198. environment:
  199. - SERVERNAME=kibana
  200. - ELASTICSEARCH_HOSTS=https://es01:9200
  201. - ELASTICSEARCH_USERNAME=kibana_system
  202. - ELASTICSEARCH_PASSWORD=${KIBANA_PASSWORD}
  203. - ELASTICSEARCH_SSL_CERTIFICATEAUTHORITIES=config/certs/ca/ca.crt
  204. mem_limit: ${MEM_LIMIT}
  205. healthcheck:
  206. test:
  207. [
  208. "CMD-SHELL",
  209. "curl -s -I http://localhost:5601 | grep -q 'HTTP/1.1 302 Found'",
  210. ]
  211. interval: 10s
  212. timeout: 10s
  213. retries: 120
  214. volumes:
  215. certs:
  216. driver: local
  217. esdata01:
  218. driver: local
  219. esdata02:
  220. driver: local
  221. esdata03:
  222. driver: local
  223. kibanadata:
  224. driver: local
  • 注意:.env和docker-compose.yaml两个文件在同一目录下

启动应用

  • 在docker-compose.yaml文件所在目录,执行命令docker-compose up -d启动所有容器
  1. docker-compose up -d
  2. Creating network "demo_default" with the default driver
  3. Pulling setup (elasticsearch:8.2.2)...
  4. 8.2.2: Pulling from library/elasticsearch
  5. Digest: sha256:8c666cb1e76650306655b67644a01663f9c7a5422b2c51dd570524267f11ce3d
  6. Status: Downloaded newer image for elasticsearch:8.2.2
  7. Pulling kibana (kibana:8.2.2)...
  8. 8.2.2: Pulling from library/kibana
  9. Digest: sha256:cf34801f36a2e79c834b3cdeb0a3463ff34b8d8588c3ccdd47212c4e0753f8a5
  10. Status: Downloaded newer image for kibana:8.2.2
  11. Creating demo_setup_1 ... done
  12. Creating demo_es01_1 ... done
  13. Creating demo_es02_1 ... done
  14. Creating demo_es03_1 ... done
  15. Creating demo_kibana_1 ... done
  • 查看容器状态,负责启动的demo_setup_1已退出,其他的正常运行
  1. docker ps -a
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. c8ce010cddfc kibana:8.2.2 "/bin/tini -- /usr/l…" 20 minutes ago Up 20 minutes (healthy) 0.0.0.0:5601->5601/tcp demo_kibana_1
  4. 78662d44ae31 elasticsearch:8.2.2 "/bin/tini -- /usr/l…" 21 minutes ago Up 21 minutes (healthy) 9200/tcp, 9300/tcp demo_es03_1
  5. 7e96273872cb elasticsearch:8.2.2 "/bin/tini -- /usr/l…" 21 minutes ago Up 21 minutes (healthy) 9200/tcp, 9300/tcp demo_es02_1
  6. 8b8be1d645ba elasticsearch:8.2.2 "/bin/tini -- /usr/l…" 21 minutes ago Up 21 minutes (healthy) 0.0.0.0:9200->9200/tcp, 9300/tcp demo_es01_1
  7. c48ffb724ca2 elasticsearch:8.2.2 "/bin/tini -- /usr/l…" 21 minutes ago Exited (0) 20 minutes ago demo_setup_1
  • 看看demo_setup_1的日志,提示启动顺利
  1. docker logs demo_setup_1
  2. Setting file permissions
  3. Waiting for Elasticsearch availability
  4. Setting kibana_system password
  5. All done!
  • 如果要使用curl命令向ES发请求,需要提前将crt文件从容器中复制出来
  1. docker cp demo_es01_1:/usr/share/elasticsearch/config/certs/es01/es01.crt .

验证

  • 现在来验证es集群和kibana能不能正常工作
  • 浏览器访问https://localhost:9200/,注意是https,会看到以下警告页面

  • 此时直接键入thisisunsafe再回车,会提示输入账号密码,根据之前的配置账号elastic,密码123456

  • 浏览器显示如下,证明es成功响应了

  • 如果chrome上安装了eshead插件,此时就能查看es集群情况了(注意内部的地址栏中,要用https,而非http),如下图,一共三个节点,es02前面有五角星标志,表示其主节点的身份

  • 目前看来es集群部署和运行都已经正常,再看kibana是否可用
  • 访问http://localhost:5601/,账号elastic,密码123456

  • 点击下图红框位置,进入输入命令的页面

  • 如下图,左侧输入创建索引的命令,再点击红框中的按钮,右侧会显示执行结果

  • 批量写入两条记录

  • 最后是查询操作

清理

  • 如果要删除es,执行docker-compose down就会删除容器,但是,此命令不会删除数据,下次执行docker-compose up -d后,新的es集群中会出现刚才创建的test001索引,并且数据也在
  • 这是因为docker-compose.yaml中使用了数据卷volume存储es集群的关键数据,这些输入被保存在宿主机的磁盘上
  1. docker volume ls
  2. DRIVER VOLUME NAME
  3. local demo_certs
  4. local demo_esdata01
  5. local demo_esdata02
  6. local demo_esdata03
  7. local demo_kibanadata
  • 执行docker volume rm demo_certs demo_esdata01 demo_esdata02 demo_esdata03即可将它们彻底清除
  • 以上就是快速部署es集群+kibana的整个过程了,是不是很简单呢?

不带密码的集群

  • 有时候咱们部署es不需要安全认证,例如开发环境,或者有防火墙禁止外部访问的环境,那么刚才的部署就不够用了,咱们需要一个更简单的、部署完了立刻能用的集群,接下来动手试试吧
  • 找个干净目录,新建名为.env的文件,内容如下,和安全版相比去掉了一些不需要的内容
  1. # kibana_system账号的密码 (至少六个字符),该账号仅用于一些kibana的内部设置,不能用来查询es
  2. KIBANA_PASSWORD=abcdef
  3. # es和kibana的版本
  4. STACK_VERSION=8.2.2
  5. # 集群名字
  6. CLUSTER_NAME=docker-cluster
  7. # es映射到宿主机的的端口
  8. ES_PORT=9200
  9. # kibana映射到宿主机的的端口
  10. KIBANA_PORT=5601
  11. # es容器的内存大小,请根据自己硬件情况调整
  12. MEM_LIMIT=1073741824
  13. # 命名空间,会体现在容器名的前缀上
  14. COMPOSE_PROJECT_NAME=demo
  • 然后是docker-compose.yaml文件,这里面会用到刚才创建的.env文件,和安全版相比去掉了启动容器,和安全相关的配置和脚本也删除了
  1. version: "2.2"
  2. services:
  3. es01:
  4. image: elasticsearch:${STACK_VERSION}
  5. volumes:
  6. - esdata01:/usr/share/elasticsearch/data
  7. ports:
  8. - ${ES_PORT}:9200
  9. environment:
  10. - node.name=es01
  11. - cluster.name=${CLUSTER_NAME}
  12. - cluster.initial_master_nodes=es01,es02,es03
  13. - discovery.seed_hosts=es02,es03
  14. - bootstrap.memory_lock=true
  15. - xpack.security.enabled=false
  16. - xpack.security.http.ssl.enabled=false
  17. - xpack.security.transport.ssl.enabled=false
  18. mem_limit: ${MEM_LIMIT}
  19. ulimits:
  20. memlock:
  21. soft: -1
  22. hard: -1
  23. es02:
  24. depends_on:
  25. - es01
  26. image: elasticsearch:${STACK_VERSION}
  27. volumes:
  28. - esdata02:/usr/share/elasticsearch/data
  29. environment:
  30. - node.name=es02
  31. - cluster.name=${CLUSTER_NAME}
  32. - cluster.initial_master_nodes=es01,es02,es03
  33. - discovery.seed_hosts=es01,es03
  34. - bootstrap.memory_lock=true
  35. - xpack.security.enabled=false
  36. - xpack.security.http.ssl.enabled=false
  37. - xpack.security.transport.ssl.enabled=false
  38. mem_limit: ${MEM_LIMIT}
  39. ulimits:
  40. memlock:
  41. soft: -1
  42. hard: -1
  43. es03:
  44. depends_on:
  45. - es02
  46. image: elasticsearch:${STACK_VERSION}
  47. volumes:
  48. - esdata03:/usr/share/elasticsearch/data
  49. environment:
  50. - node.name=es03
  51. - cluster.name=${CLUSTER_NAME}
  52. - cluster.initial_master_nodes=es01,es02,es03
  53. - discovery.seed_hosts=es01,es02
  54. - bootstrap.memory_lock=true
  55. - xpack.security.enabled=false
  56. - xpack.security.http.ssl.enabled=false
  57. - xpack.security.transport.ssl.enabled=false
  58. mem_limit: ${MEM_LIMIT}
  59. ulimits:
  60. memlock:
  61. soft: -1
  62. hard: -1
  63. kibana:
  64. image: kibana:${STACK_VERSION}
  65. volumes:
  66. - kibanadata:/usr/share/kibana/data
  67. ports:
  68. - ${KIBANA_PORT}:5601
  69. environment:
  70. - SERVERNAME=kibana
  71. - ELASTICSEARCH_HOSTS=http://es01:9200
  72. - ELASTICSEARCH_USERNAME=kibana_system
  73. - ELASTICSEARCH_PASSWORD=${KIBANA_PASSWORD}
  74. mem_limit: ${MEM_LIMIT}
  75. volumes:
  76. esdata01:
  77. driver: local
  78. esdata02:
  79. driver: local
  80. esdata03:
  81. driver: local
  82. kibanadata:
  83. driver: local
  • 注意:.env和docker-compose.yaml两个文件在同一目录下

启动和验证

  • 启动前,请先停止和清理掉刚才部署的安全版
  • 在docker-compose.yaml文件所在目录,执行命令docker-compose up -d启动所有容器,稍等片刻,可见所有容器已经就绪
  1. docker ps -a
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. 11663375288d elasticsearch:8.2.2 "/bin/tini -- /usr/l…" 4 minutes ago Up 4 minutes 9200/tcp, 9300/tcp demo_es03_1
  4. ad6f0390b9cf elasticsearch:8.2.2 "/bin/tini -- /usr/l…" 4 minutes ago Up 4 minutes 9200/tcp, 9300/tcp demo_es02_1
  5. 5080709e5358 kibana:8.2.2 "/bin/tini -- /usr/l…" 4 minutes ago Up 4 minutes 0.0.0.0:5601->5601/tcp demo_kibana_1
  6. 4b1e576fbfd3 elasticsearch:8.2.2 "/bin/tini -- /usr/l…" 4 minutes ago Up 4 minutes 0.0.0.0:9200->9200/tcp, 9300/tcp demo_es01_1
  • 浏览器访问http://localhost:9200/ ,注意是http,收到es响应

  • chrome的eshead插件也能正常获取es集群信息

  • 访问kibana,地址是http://localhost:5601/ ,注意是http,能够正常使用,下图是成功创建索引的操作

  • 至此,基于docker-compose部署es集群+kibana的部署已经完成,借助娴熟的复制粘贴操作,快速部署一个es集群简直易如反掌,如果您正要快速部署一套es集群,希望本文能给您一些参考

你不孤单,欣宸原创一路相伴

  1. Java系列
  2. Spring系列
  3. Docker系列
  4. kubernetes系列
  5. 数据库+中间件系列
  6. DevOps系列

相关文章

最新文章

更多