Architecture.pngserver_list.png

前置知识

  1. 后台启动容器:docker-compose up -d
  2. 查看容器运行情况:docker-compose ps
  3. 停止容器:docker-compose stop
  4. 启动容器:docker-compose start
  5. 停止并删除容器:docker-compose down
  6. 停止并删除容器并删除volumedocker-compose down --volumes
  7. rm -rf /data/es/{es01,es02,es03}/data && rm -rf /data/es/{es01,es02,es03}/log

1、防止JVM报错

  1. 修改宿主机配置:
  2. vi /etc/sysctl.conf
  3. 末尾追加:
  4. vm.max_map_count=262145
  5. 保存后执行(最好重启服务器):
  6. sysctl -p

2、搭建es集群

  1. 1、创建映射目录
  2. mkdir -p /data/es/{es01,es02,es03}/data && \
  3. mkdir -p /data/es/{es01,es02,es03}/log && \
  4. mkdir -p /data/es/plugins/elasticsearch-analysis-ik-7.2.0
  5. 下载分词器:
  6. https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.2.0/elasticsearch-analysis-ik-7.2.0.zip
  7. 解压到:
  8. /data/es/plugins/elasticsearch-analysis-ik-7.2.0
  9. 授权(因为es集群是使用非root用户启动的):
  10. chmod -R 777 /data/es
  11. 2、创建docker-compose.yml
  12. version: "3.4"
  13. services:
  14. es01:
  15. image: docker.elastic.co/elasticsearch/elasticsearch:7.2.0
  16. container_name: es01
  17. restart: always
  18. environment:
  19. - node.name=es01
  20. - cluster.name=spartacus
  21. - node.master=true
  22. - node.data=true
  23. - discovery.seed_hosts=es01,es02,es03
  24. - cluster.initial_master_nodes=es01,es02,es03
  25. - http.cors.enabled=true
  26. - http.cors.allow-origin="*"
  27. - xpack.security.enabled=true
  28. - xpack.security.transport.ssl.enabled=true
  29. - xpack.security.transport.ssl.keystore.type=PKCS12
  30. - xpack.security.transport.ssl.verification_mode=certificate
  31. - xpack.security.transport.ssl.keystore.path=/usr/share/elasticsearch/config/elastic-certificates.p12
  32. - xpack.security.transport.ssl.truststore.path=/usr/share/elasticsearch/config/elastic-certificates.p12
  33. - xpack.security.transport.ssl.truststore.type=PKCS12
  34. - bootstrap.memory_lock=true
  35. - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
  36. - "TZ=Asia/Shanghai"
  37. ulimits:
  38. memlock:
  39. soft: -1
  40. hard: -1
  41. nofile:
  42. soft: 65536
  43. hard: 65536
  44. ports:
  45. - "9200:9200"
  46. - "9300:9300"
  47. volumes:
  48. - ./elastic-certificates.p12:/usr/share/elasticsearch/config/elastic-certificates.p12
  49. - /data/es/es01/data:/usr/share/elasticsearch/data
  50. - /data/es/es01/log:/usr/share/elasticsearch/log
  51. - /data/es/plugins:/usr/share/elasticsearch/plugins
  52. networks:
  53. - net-es
  54. es02:
  55. image: docker.elastic.co/elasticsearch/elasticsearch:7.2.0
  56. container_name: es02
  57. restart: always
  58. environment:
  59. - node.name=es02
  60. - cluster.name=spartacus
  61. - node.master=true
  62. - node.data=true
  63. - discovery.seed_hosts=es01,es02,es03
  64. - cluster.initial_master_nodes=es01,es02,es03
  65. - http.cors.enabled=true
  66. - http.cors.allow-origin="*"
  67. - xpack.security.enabled=true
  68. - xpack.security.transport.ssl.enabled=true
  69. - xpack.security.transport.ssl.keystore.type=PKCS12
  70. - xpack.security.transport.ssl.verification_mode=certificate
  71. - xpack.security.transport.ssl.keystore.path=/usr/share/elasticsearch/config/elastic-certificates.p12
  72. - xpack.security.transport.ssl.truststore.path=/usr/share/elasticsearch/config/elastic-certificates.p12
  73. - xpack.security.transport.ssl.truststore.type=PKCS12
  74. - bootstrap.memory_lock=true
  75. - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
  76. - "TZ=Asia/Shanghai"
  77. ulimits:
  78. memlock:
  79. soft: -1
  80. hard: -1
  81. nofile:
  82. soft: 65536
  83. hard: 65536
  84. ports:
  85. - "9201:9200"
  86. - "9301:9300"
  87. volumes:
  88. - ./elastic-certificates.p12:/usr/share/elasticsearch/config/elastic-certificates.p12
  89. - /data/es/es02/data:/usr/share/elasticsearch/data
  90. - /data/es/es02/log:/usr/share/elasticsearch/log
  91. - /data/es/plugins:/usr/share/elasticsearch/plugins
  92. networks:
  93. - net-es
  94. es03:
  95. image: docker.elastic.co/elasticsearch/elasticsearch:7.2.0
  96. container_name: es03
  97. restart: always
  98. environment:
  99. - node.name=es03
  100. - cluster.name=spartacus
  101. - node.master=true
  102. - node.data=true
  103. - discovery.seed_hosts=es01,es02,es03
  104. - cluster.initial_master_nodes=es01,es02,es03
  105. - http.cors.enabled=true
  106. - http.cors.allow-origin="*"
  107. - xpack.security.enabled=true
  108. - xpack.security.transport.ssl.enabled=true
  109. - xpack.security.transport.ssl.keystore.type=PKCS12
  110. - xpack.security.transport.ssl.verification_mode=certificate
  111. - xpack.security.transport.ssl.keystore.path=/usr/share/elasticsearch/config/elastic-certificates.p12
  112. - xpack.security.transport.ssl.truststore.path=/usr/share/elasticsearch/config/elastic-certificates.p12
  113. - xpack.security.transport.ssl.truststore.type=PKCS12
  114. - bootstrap.memory_lock=true
  115. - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
  116. - "TZ=Asia/Shanghai"
  117. ulimits:
  118. memlock:
  119. soft: -1
  120. hard: -1
  121. nofile:
  122. soft: 65536
  123. hard: 65536
  124. ports:
  125. - "9202:9200"
  126. - "9302:9300"
  127. volumes:
  128. - ./elastic-certificates.p12:/usr/share/elasticsearch/config/elastic-certificates.p12
  129. - /data/es/es03/data:/usr/share/elasticsearch/data
  130. - /data/es/es03/log:/usr/share/elasticsearch/log
  131. - /data/es/plugins:/usr/share/elasticsearch/plugins
  132. networks:
  133. - net-es
  134. networks:
  135. net-es:
  136. driver: bridge
  137. 3、生成证书文件,用于es各节点之间进行ssl通信
  138. #启一个临时容器,用于创建证书用
  139. docker run -d --name es -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.2.0
  140. #进入容器
  141. docker exec -it es /bin/bash
  142. #生成ca: elastic-stack-ca.p12(enter键跳过设置证书名称、密码)
  143. ./bin/elasticsearch-certutil ca
  144. #再生成cert: elastic-certificates.p12(enter键跳过设置证书名称、密码)
  145. #退出容器,拷贝证书至宿主机(与docker-compose.yml同目录)
  146. docker cp es:/usr/share/elasticsearch/elastic-certificates.p12 .
  147. #销毁临时es容器
  148. docker stop es
  149. docker rm es
  150. 4、授权
  151. chmod 777 elastic-certificates.p12
  152. chmod 777 docker-compose.yml
  153. 5、后台启动
  154. docker-compose up -d
  155. 6、设置密码(多个节点只需要设置其中一个即可)
  156. 进入任意一个容器:
  157. docker exec -it --user root es01 /bin/bash
  158. 设置密码:
  159. ./bin/elasticsearch-setup-passwords interactive
  160. #控制台交互....
  161. 温馨提示:建议所有密码都设置一样,方便记忆,比如Pwd@123

3、搭建Logstash

  1. 1、创建logstash配置文件(/apps/logstash/logstash.conf
  2. input {
  3. tcp {
  4. mode => "server"
  5. host => "0.0.0.0" #监听指定的IP,此处为不限制来源IP
  6. port => 4560
  7. codec => json_lines
  8. }
  9. }
  10. filter {
  11. #增加一个字段,计算 timestamp 8小时
  12. ruby {
  13. code => "event.set('timestamp', event.get('@timestamp').time.utc+8*60*60)"
  14. }
  15. #用 mutate 插件先转换为 string 类型,gsub只处理string类型的数据
  16. #再用正则匹配,最终得到想要的日期
  17. mutate {
  18. convert => ["timestamp", "string"]
  19. gsub => ["timestamp", "T([\S\s]*?)Z", ""]
  20. gsub => ["timestamp", "-", "."]
  21. }
  22. }
  23. output {
  24. if "spartacus-discovery" == [applicationName] {
  25. elasticsearch {
  26. hosts => ["10.0.0.5:9200","10.0.0.5:9201","10.0.0.5:9202"]
  27. action => "index"
  28. codec => json
  29. index => "spartacus-discovery-logs-%{timestamp}"
  30. user => elastic
  31. password => "Pwd@123"
  32. }
  33. }
  34. if "spartacus-article" == [applicationName] {
  35. elasticsearch {
  36. hosts => ["10.0.0.5:9200","10.0.0.5:9201","10.0.0.5:9202"]
  37. action => "index"
  38. codec => json
  39. index => "spartacus-article-logs-%{timestamp}"
  40. user => elastic
  41. password => "Pwd@123"
  42. }
  43. }
  44. if "spartacus-auth" == [applicationName] {
  45. elasticsearch {
  46. hosts => ["10.0.0.5:9200","10.0.0.5:9201","10.0.0.5:9202"]
  47. action => "index"
  48. codec => json
  49. index => "spartacus-auth-logs-%{timestamp}"
  50. user => elastic
  51. password => "Pwd@123"
  52. }
  53. }
  54. if "spartacus-chat" == [applicationName] {
  55. elasticsearch {
  56. hosts => ["10.0.0.5:9200","10.0.0.5:9201","10.0.0.5:9202"]
  57. action => "index"
  58. codec => json
  59. index => "spartacus-chat-logs-%{timestamp}"
  60. user => elastic
  61. password => "Pwd@123"
  62. }
  63. }
  64. if "spartacus-comment" == [applicationName] {
  65. elasticsearch {
  66. hosts => ["10.0.0.5:9200","10.0.0.5:9201","10.0.0.5:9202"]
  67. action => "index"
  68. codec => json
  69. index => "spartacus-comment-logs-%{timestamp}"
  70. user => elastic
  71. password => "Pwd@123"
  72. }
  73. }
  74. if "spartacus-datasyner" == [applicationName] {
  75. elasticsearch {
  76. hosts => ["10.0.0.5:9200","10.0.0.5:9201","10.0.0.5:9202"]
  77. action => "index"
  78. codec => json
  79. index => "spartacus-datasyner-logs-%{timestamp}"
  80. user => elastic
  81. password => "Pwd@123"
  82. }
  83. }
  84. if "spartacus-gateway" == [applicationName] {
  85. elasticsearch {
  86. hosts => ["10.0.0.5:9200","10.0.0.5:9201","10.0.0.5:9202"]
  87. action => "index"
  88. codec => json
  89. index => "spartacus-gateway-logs-%{timestamp}"
  90. user => elastic
  91. password => "Pwd@123"
  92. }
  93. }
  94. if "spartacus-monitor" == [applicationName] {
  95. elasticsearch {
  96. hosts => ["10.0.0.5:9200","10.0.0.5:9201","10.0.0.5:9202"]
  97. action => "index"
  98. codec => json
  99. index => "spartacus-monitor-logs-%{timestamp}"
  100. user => elastic
  101. password => "Pwd@123"
  102. }
  103. }
  104. if "spartacus-friday" == [applicationName] {
  105. elasticsearch {
  106. hosts => ["10.0.0.5:9200","10.0.0.5:9201","10.0.0.5:9202"]
  107. action => "index"
  108. codec => json
  109. index => "spartacus-friday-logs-%{timestamp}"
  110. user => elastic
  111. password => "Pwd@123"
  112. }
  113. }
  114. if "spartacus-sunday" == [applicationName] {
  115. elasticsearch {
  116. hosts => ["10.0.0.5:9200","10.0.0.5:9201","10.0.0.5:9202"]
  117. action => "index"
  118. codec => json
  119. index => "spartacus-sunday-logs-%{timestamp}"
  120. user => elastic
  121. password => "Pwd@123"
  122. }
  123. }
  124. if "spartacus-resource" == [applicationName] {
  125. elasticsearch {
  126. hosts => ["10.0.0.5:9200","10.0.0.5:9201","10.0.0.5:9202"]
  127. action => "index"
  128. codec => json
  129. index => "spartacus-resource-logs-%{timestamp}"
  130. user => elastic
  131. password => "Pwd@123"
  132. }
  133. }
  134. if "spartacus-system" == [applicationName] {
  135. elasticsearch {
  136. hosts => ["10.0.0.5:9200","10.0.0.5:9201","10.0.0.5:9202"]
  137. action => "index"
  138. codec => json
  139. index => "spartacus-system-logs-%{timestamp}"
  140. user => elastic
  141. password => "Pwd@123"
  142. }
  143. }
  144. }
  145. 注:
  146. input中的tcp输入块可配置多个,表示logstash后端会起多个线程监听多个输入源(可利用这个特点变相实现单节点logstash的高可用)
  147. inputoutput配置项可参考:
  148. https://www.elastic.co/guide/en/logstash/current/plugins-inputs-tcp.html#plugins-inputs-tcp-host
  149. 2、创建docker-compose.yml
  150. version: "3.4"
  151. services:
  152. logstash:
  153. image: logstash:7.2.0
  154. container_name: logstash
  155. restart: always
  156. volumes:
  157. - /apps/logstash/logstash.conf:/usr/share/logstash/pipeline/logstash.conf
  158. ports:
  159. - 4560:4560
  160. networks:
  161. - net-lt
  162. networks:
  163. net-lt:
  164. driver: bridge
  165. 3、授权
  166. chmod 777 docker-compose.yml
  167. 4、后台启动logstash
  168. docker-compose up -d
  169. 5、安装json_lines插件
  170. # 进入logstash容器
  171. docker exec -it logstash /bin/bash
  172. # 进入bin目录
  173. cd /bin/
  174. # 安装插件
  175. logstash-plugin install logstash-codec-json_lines
  176. # 退出容器
  177. exit
  178. # 重启logstash服务
  179. docker restart logstash

4、搭建kibana

  1. 1、创建docker-compose.yml
  2. version: "3.4"
  3. services:
  4. kibana:
  5. image: kibana:7.2.0
  6. container_name: kibana
  7. restart: always
  8. environment:
  9. - SERVER_HOST="0.0.0.0"
  10. - ELASTICSEARCH_HOSTS=["http://10.0.0.5:9200","http://10.0.0.5:9201","http://10.0.0.5:9202"]
  11. - ELASTICSEARCH_USERNAME="elastic"
  12. - ELASTICSEARCH_PASSWORD=Pwd@123"
  13. ports:
  14. - "5601:5601"
  15. networks:
  16. - net-kb
  17. networks:
  18. net-kb:
  19. driver: bridge
  20. 2、授权
  21. chmod 777 docker-compose.yml
  22. 3、后台启动kibana
  23. docker-compose up -d
  24. 注意:如果创建索引模式报错: POST 403 (forbidden)
  25. 查看索引状态:
  26. GET _cat/indices
  27. 查看全局配置
  28. GET .kibana/_settings
  29. 重点关注一个指标:read_only_allow_delete是否为true,如果是则置为false:
  30. PUT _settings
  31. {
  32. "index": {
  33. "blocks": {
  34. "read_only_allow_delete": "false"
  35. }
  36. }
  37. }

5、云服务器安全组

开放云服务器5601端口(kibana控制台访问端口):
image.png

然后访问kibana控制台:
http://云主机公网IP:5601

账号密码即是 步骤2 中为各个账号设置的密码(如果认真操作了肯定知道我在说什么)!

如果搞不定,请加群讨论,扫码关注,发送“加群”
mp_qrcode.jpg