当前位置:   article > 正文

ELK本地虚拟机部署(8.12版本)_elk8.12部署可观测

elk8.12部署可观测

 鉴于公司服务器上日志越来越多,需要搭建ELK对日志进行采集和分析,用于后续故障排查。

程序下载地址:

elasticsearch:Elasticsearch 8.12.0 | Elastic

logstash:Logstash 8.12.0 | Elastic

kibana:Kibana 8.12.0 | Elastic

filebeat:Filebeat 8.12.0 | Elastic


00)服务器信息

角色主机地址
es1192.168.133.200elasticsearch集群
es2192.168.133.201elasticsearch集群
es3192.168.133.202elasticsearch集群
kibana192.168.133.203kibana部署地址
logstash,filebeat192.168.133.204logstash以及filebeat

01)elasticsearch集群

1.1 系统设置(elasticsearch集群所有节点

  1. vim /etc/security/limits.conf
  2. ---------------------------
  3. * soft nofile 65536
  4. * hard nofile 65536
  5. * soft nproc 32000
  6. * hard nproc 32000
  7. * hard memlock unlimited
  8. * soft memlock unlimited
  9. ---------------------------
  10. vim /etc/systemd/system.conf
  11. ---------------------------
  12. DefaultLimitNOFILE=65536
  13. DefaultLimitNPROC=32000
  14. DefaultLimitMEMLOCK=infinity
  15. ---------------------------
  16. vim /etc/sysctl.conf
  17. ---------------------------
  18. vm.max_map_count=655360
  19. fs.file-max = 655360
  20. ---------------------------
  21. sysctl -p
  22. vim /etc/hosts
  23. ---------------------------
  24. 192.168.133.200
  25. 192.168.133.201
  26. 192.168.133.202
  27. ---------------------------

1.2 环境变量(elasticsearch集群所有节点

  1. vim /etc/profile
  2. --------末尾添加如下内容------------
  3. export HISTSIZE=3000
  4. export HISTFILESIZE=10000
  5. export PATH
  6. export JAVA_HOME=/app/elasticsearch/jdk
  7. export PATH=$JAVA_HOME/bin:$PATH
  8. export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
  9. --------配置完成------------------
  10. source /etc/profile

1.3 程序配置

1.3.1 节点1配置

elasticsearch程序部署的路径为/app/elasticsearch

  1. ## 搭建集群相关的配置,配置文件elasticsearch.yml
  2. vim /app/elasticsearch/config/elasticsearch.yml
  3. -----------------------------
  4. cluster.name: test
  5. node.name: node1
  6. network.host: 192.168.133.200
  7. bootstrap.memory_lock: true
  8. path.data: /data/elasticsearch/data
  9. path.logs: /data/elasticsearch/logs
  10. http.port: 9200
  11. transport.port: 9300
  12. discovery.seed_hosts: ["192.168.133.200:9300","192.168.133.201:9300","192.168.133.202:9300"]
  13. cluster.initial_master_nodes: ["node1", "node2","node3"]
  14. http.cors.enabled: true
  15. http.cors.allow-origin: '*'
  16. action.auto_create_index: true
  17. -----------------------------
  18. ## 创建用户es,并将家目录设置为程序目录/app/elasticsearch
  19. groupadd es
  20. useradd -g es -p es -d /app/elasticsearch es
  21. chmod 755 -R /app/elasticsearch
  22. ## 复制文件至家目录,避免出现(-bash-4.2$)
  23. cp /etc/skel/.bash_logout /app/elasticsearch/
  24. cp /etc/skel/.bash_profile /app/elasticsearch/
  25. cp /etc/skel/.bashrc /app/elasticsearch/
  26. ## 创建路径
  27. mkdir -p /data/elasticsearch/data
  28. mkdir -p /data/elasticsearch/logs
  29. mkdir -p /data/supervisord/
  30. chown -R es:es /data/elasticsearch/
  31. chown -R es:es /app/elasticsearch
  32. chmod 755 -R /app/elasticsearch
  33. ## systemd配置
  34. vim /etc/systemd/system/elasticsearch.service
  35. ---------------------
  36. [Unit]
  37. Description=elasticsearch
  38. After=network.target
  39. Wants=network.target
  40. [Service]
  41. Type=simple
  42. User=es
  43. Group=es
  44. LimitNOFILE=655350
  45. LimitNPROC=320000
  46. LimitMEMLOCK=infinity
  47. Restart=on-failure
  48. ExecStart=/app/elasticsearch/bin/elasticsearch
  49. ExecReload=/bin/kill -HUP $MAINPID
  50. PrivateTmp=true
  51. KillMode=mixed
  52. [Install]
  53. WantedBy=multi-user.target
  54. ---------------------
  55. ## 脚本配置
  56. mkdir /xyz
  57. cat >> /xyz/1-start.sh << EOF
  58. #!/bin/bash
  59. ## 启动脚本
  60. systemctl start elasticsearch.service
  61. EOF
  62. cat >> /xyz/2-ps.sh << EOF
  63. #!/bin/bash
  64. ## 检查状态
  65. systemctl --no-pager status elasticsearch.service
  66. EOF
  67. cat >> /xyz/3-stop.sh << EOF
  68. #!/bin/bash
  69. ## 停止脚本
  70. systemctl stop elasticsearch.service
  71. EOF
  72. cat >> /xyz/4-restart.sh << EOF
  73. #!/bin/bash
  74. ## 重启脚本
  75. systemctl restart elasticsearch.service
  76. EOF
  77. cat >> /xyz/5-update.sh << EOF
  78. #!/bin/bash
  79. ## 更新脚本
  80. systemctl daemon-reload
  81. EOF
  82. chmod -R 755 /xyz/

1.3.2 节点2配置

elasticsearch程序部署的路径为/app/elasticsearch

  1. ## 搭建集群相关的配置,配置文件elasticsearch.yml
  2. vim /app/elasticsearch/config/elasticsearch.yml
  3. -----------------------------
  4. cluster.name: test
  5. node.name: node2
  6. network.host: 192.168.133.201
  7. bootstrap.memory_lock: true
  8. path.data: /data/elasticsearch/data
  9. path.logs: /data/elasticsearch/logs
  10. http.port: 9200
  11. transport.port: 9300
  12. discovery.seed_hosts: ["192.168.133.200:9300","192.168.133.201:9300","192.168.133.202:9300"]
  13. cluster.initial_master_nodes: ["node1", "node2","node3"]
  14. http.cors.enabled: true
  15. http.cors.allow-origin: '*'
  16. action.auto_create_index: true
  17. -----------------------------
  18. ## 创建用户es,并将家目录设置为程序目录/app/elasticsearch
  19. groupadd es
  20. useradd -g es -p es -d /app/elasticsearch es
  21. chmod 755 -R /app/elasticsearch
  22. ## 复制文件至家目录,避免出现(-bash-4.2$)
  23. cp /etc/skel/.bash_logout /app/elasticsearch/
  24. cp /etc/skel/.bash_profile /app/elasticsearch/
  25. cp /etc/skel/.bashrc /app/elasticsearch/
  26. ## 创建路径
  27. mkdir -p /data/elasticsearch/data
  28. mkdir -p /data/elasticsearch/logs
  29. mkdir -p /data/supervisord/
  30. chown -R es:es /data/elasticsearch/
  31. chown -R es:es /app/elasticsearch
  32. chmod 755 -R /app/elasticsearch
  33. ## systemd配置
  34. vim /etc/systemd/system/elasticsearch.service
  35. ---------------------
  36. [Unit]
  37. Description=elasticsearch
  38. After=network.target
  39. Wants=network.target
  40. [Service]
  41. Type=simple
  42. User=es
  43. Group=es
  44. LimitNOFILE=655350
  45. LimitNPROC=320000
  46. LimitMEMLOCK=infinity
  47. Restart=on-failure
  48. ExecStart=/app/elasticsearch/bin/elasticsearch
  49. ExecReload=/bin/kill -HUP $MAINPID
  50. PrivateTmp=true
  51. KillMode=mixed
  52. [Install]
  53. WantedBy=multi-user.target
  54. ---------------------
  55. ## 脚本配置
  56. mkdir /xyz
  57. cat >> /xyz/1-start.sh << EOF
  58. #!/bin/bash
  59. ## 启动脚本
  60. systemctl start elasticsearch.service
  61. EOF
  62. cat >> /xyz/2-ps.sh << EOF
  63. #!/bin/bash
  64. ## 检查状态
  65. systemctl --no-pager status elasticsearch.service
  66. EOF
  67. cat >> /xyz/3-stop.sh << EOF
  68. #!/bin/bash
  69. ## 停止脚本
  70. systemctl stop elasticsearch.service
  71. EOF
  72. cat >> /xyz/4-restart.sh << EOF
  73. #!/bin/bash
  74. ## 重启脚本
  75. systemctl restart elasticsearch.service
  76. EOF
  77. cat >> /xyz/5-update.sh << EOF
  78. #!/bin/bash
  79. ## 更新脚本
  80. systemctl daemon-reload
  81. EOF
  82. chmod -R 755 /xyz/

1.3.3 节点3配置

elasticsearch程序部署的路径为/app/elasticsearch

  1. ## 搭建集群相关的配置,配置文件elasticsearch.yml
  2. vim /app/elasticsearch/config/elasticsearch.yml
  3. -----------------------------
  4. cluster.name: test
  5. node.name: node3
  6. network.host: 192.168.133.202
  7. bootstrap.memory_lock: true
  8. path.data: /data/elasticsearch/data
  9. path.logs: /data/elasticsearch/logs
  10. http.port: 9200
  11. transport.port: 9300
  12. discovery.seed_hosts: ["192.168.133.200:9300","192.168.133.201:9300","192.168.133.202:9300"]
  13. cluster.initial_master_nodes: ["node1", "node2","node3"]
  14. http.cors.enabled: true
  15. http.cors.allow-origin: '*'
  16. action.auto_create_index: true
  17. -----------------------------
  18. ## 创建用户es,并将家目录设置为程序目录/app/elasticsearch
  19. groupadd es
  20. useradd -g es -p es -d /app/elasticsearch es
  21. chmod 755 -R /app/elasticsearch
  22. ## 复制文件至家目录,避免出现(-bash-4.2$)
  23. cp /etc/skel/.bash_logout /app/elasticsearch/
  24. cp /etc/skel/.bash_profile /app/elasticsearch/
  25. cp /etc/skel/.bashrc /app/elasticsearch/
  26. ## 创建路径
  27. mkdir -p /data/elasticsearch/data
  28. mkdir -p /data/elasticsearch/logs
  29. mkdir -p /data/supervisord/
  30. chown -R es:es /data/elasticsearch/
  31. chown -R es:es /app/elasticsearch
  32. chmod 755 -R /app/elasticsearch
  33. ## systemd配置
  34. vim /etc/systemd/system/elasticsearch.service
  35. ---------------------
  36. [Unit]
  37. Description=elasticsearch
  38. After=network.target
  39. Wants=network.target
  40. [Service]
  41. Type=simple
  42. User=es
  43. Group=es
  44. LimitNOFILE=655350
  45. LimitNPROC=320000
  46. LimitMEMLOCK=infinity
  47. Restart=on-failure
  48. ExecStart=/app/elasticsearch/bin/elasticsearch
  49. ExecReload=/bin/kill -HUP $MAINPID
  50. PrivateTmp=true
  51. KillMode=mixed
  52. [Install]
  53. WantedBy=multi-user.target
  54. ---------------------
  55. ## systemd脚本配置
  56. mkdir /xyz
  57. cat >> /xyz/1-start.sh << EOF
  58. #!/bin/bash
  59. ## 启动脚本
  60. systemctl start elasticsearch.service
  61. EOF
  62. cat >> /xyz/2-ps.sh << EOF
  63. #!/bin/bash
  64. ## 检查状态
  65. systemctl --no-pager status elasticsearch.service
  66. EOF
  67. cat >> /xyz/3-stop.sh << EOF
  68. #!/bin/bash
  69. ## 停止脚本
  70. systemctl stop elasticsearch.service
  71. EOF
  72. cat >> /xyz/4-restart.sh << EOF
  73. #!/bin/bash
  74. ## 重启脚本
  75. systemctl restart elasticsearch.service
  76. EOF
  77. cat >> /xyz/5-update.sh << EOF
  78. #!/bin/bash
  79. ## 更新脚本
  80. systemctl daemon-reload
  81. EOF
  82. chmod -R 755 /xyz/

1.4 安全设置

当为使用生产许可证运行的集群启用Elasticsearch安全性时,必须使用 TLS/SSL 进行传输通信,并且必须正确设置。此外,一旦启用安全性,与 Elasticsearch 集群的所有通信都必须经过身份验证,包括来自 Kibana 和/或应用程序服务器的通信。
Kibana 和/或应用程序服务器向 Elasticsearch 集群进行身份验证的最简单方法是在其配置文件或源代码中嵌入用户名和密码。但是,在许多组织中,禁止在此类位置存储用户名和密码。在这种情况下,一种替代方法是使用公钥基础设施 (PKI - Public Key Infrastructure)(客户端证书)对 Elasticsearch 集群进行身份验证。

1.4.1 传输层 TLS/SSL 加密

Elasticsearch 附带一个名为 elasticsearch-certutil 的实用程序,可用于生成自签名证书,该证书可用于加密 Elasticsearch 集群内的内部通信。

建立加密时注意以下内容:

第一步中Enter password for CA (elastic-stack-ca.p12) :,设置CA文件的密码,可以直接回车不用输入密码,这里为了安全起见还是设置了密码,为了便于搭建集群中所有的和系统安全有关的密码均相同。

第二步Please enter the desired output file [elastic-certificates.p12]:,设置CA授权文件的文件名,使用默认文件名的话可以直接回车。

第三步Enter password for elastic-certificates.p12 :,设置CA授权文件的密码,建议和CA文件密码保持一致。

  1. cd /app/elasticsearch/
  2. ## 生成可用于传输通信的证书
  3. [es@es1 elasticsearch]$ bin/elasticsearch-certutil ca
  4. warning: ignoring JAVA_HOME=/app/elasticsearch/jdk; using bundled JDK
  5. This tool assists you in the generation of X.509 certificates and certificate
  6. signing requests for use with SSL/TLS in the Elastic stack.
  7. The 'ca' mode generates a new 'certificate authority'
  8. This will create a new X.509 certificate and private key that can be used
  9. to sign certificate when running in 'cert' mode.
  10. Use the 'ca-dn' option if you wish to configure the 'distinguished name'
  11. of the certificate authority
  12. By default the 'ca' mode produces a single PKCS#12 output file which holds:
  13. * The CA certificate
  14. * The CA's private key'
  15. If you elect to generate PEM format certificates (the -pem option), then the output will
  16. be a zip file containing individual files for the CA certificate and private key
  17. Please enter the desired output file [elastic-stack-ca.p12]:
  18. Enter password for elastic-stack-ca.p12 :
  19. [es@es1 elasticsearch]$ ls
  20. bin config elastic-stack-ca.p12 jdk lib LICENSE.txt logs modules NOTICE.txt plugins README.asciidoc
  21. ## 根据生成的ca证书,生成
  22. [es@es1 elasticsearch]$ bin/elasticsearch-certutil cert --ca elastic-stack-ca.p12
  23. warning: ignoring JAVA_HOME=/app/elasticsearch/jdk; using bundled JDK
  24. This tool assists you in the generation of X.509 certificates and certificate
  25. signing requests for use with SSL/TLS in the Elastic stack.
  26. The 'cert' mode generates X.509 certificate and private keys.
  27. * By default, this generates a single certificate and key for use
  28. on a single instance.
  29. * The '-multiple' option will prompt you to enter details for multiple
  30. instances and will generate a certificate and key for each one
  31. * The '-in' option allows for the certificate generation to be automated by describing
  32. the details of each instance in a YAML file
  33. * An instance is any piece of the Elastic Stack that requires an SSL certificate.
  34. Depending on your configuration, Elasticsearch, Logstash, Kibana, and Beats
  35. may all require a certificate and private key.
  36. * The minimum required value for each instance is a name. This can simply be the
  37. hostname, which will be used as the Common Name of the certificate. A full
  38. distinguished name may also be used.
  39. * A filename value may be required for each instance. This is necessary when the
  40. name would result in an invalid file or directory name. The name provided here
  41. is used as the directory name (within the zip) and the prefix for the key and
  42. certificate files. The filename is required if you are prompted and the name
  43. is not displayed in the prompt.
  44. * IP addresses and DNS names are optional. Multiple values can be specified as a
  45. comma separated string. If no IP addresses or DNS names are provided, you may
  46. disable hostname verification in your SSL configuration.
  47. * All certificates generated by this tool will be signed by a certificate authority (CA)
  48. unless the --self-signed command line option is specified.
  49. The tool can automatically generate a new CA for you, or you can provide your own with
  50. the --ca or --ca-cert command line options.
  51. By default the 'cert' mode produces a single PKCS#12 output file which holds:
  52. * The instance certificate
  53. * The private key for the instance certificate
  54. * The CA certificate
  55. If you specify any of the following options:
  56. * -pem (PEM formatted output)
  57. * -multiple (generate multiple certificates)
  58. * -in (generate certificates from an input file)
  59. then the output will be be a zip file containing individual certificate/key files
  60. Enter password for CA (elastic-stack-ca.p12) :
  61. Please enter the desired output file [elastic-certificates.p12]:
  62. Enter password for elastic-certificates.p12 :
  63. Certificates written to /app/elasticsearch/elastic-certificates.p12
  64. This file should be properly secured as it contains the private key for
  65. your instance.
  66. This file is a self contained file and can be copied and used 'as is'
  67. For each Elastic product that you wish to configure, you should copy
  68. this '.p12' file to the relevant configuration directory
  69. and then follow the SSL configuration instructions in the product guide.
  70. For client applications, you may only need to copy the CA certificate and
  71. configure the client to trust this certificate.
  72. ## 在当前的目录中生产一个叫做&nbsp;elastic-certificates.p12 的文件,这个文件用于加密通信的 TLS/SSL 证书。
  73. [es@es1 elasticsearch]$ ls -al
  74. total 2248
  75. drwxr-xr-x 9 es es 4096 Mar 28 16:21 .
  76. drwxr-xr-x 4 root root 4096 Mar 28 11:46 ..
  77. drwxr-xr-x 2 es es 4096 Jan 11 18:11 bin
  78. drwxr-xr-x 3 es es 4096 Mar 28 16:00 config
  79. -rw------- 1 es es 3596 Mar 28 16:21 elastic-certificates.p12
  80. -rw------- 1 es es 2672 Mar 28 16:20 elastic-stack-ca.p12
  81. drwxr-xr-x 8 es es 4096 Jan 11 18:11 jdk
  82. drwxr-xr-x 5 es es 4096 Jan 11 18:11 lib
  83. -rwxr-xr-x 1 es es 3860 Jan 11 18:04 LICENSE.txt
  84. drwxr-xr-x 2 es es 4096 Mar 28 15:59 logs
  85. drwxr-xr-x 81 es es 4096 Jan 11 18:11 modules
  86. -rwxr-xr-x 1 es es 2239562 Jan 11 18:07 NOTICE.txt
  87. drwxr-xr-x 2 es es 4096 Jan 11 18:06 plugins
  88. -rwxr-xr-x 1 es es 8426 Jan 11 18:04 README.asciidoc
  89. ## 建立证书存放目录
  90. [es@es1 elasticsearch]$ pwd
  91. /app/elasticsearch
  92. [es@es1 elasticsearch]$ mkdir config/certs
  93. ## 移动证书至存放目录
  94. [es@es1 elasticsearch]$ mv elastic-* config/certs/
  95. [es@es1 elasticsearch]$ ls -al /app/elasticsearch/config/certs/
  96. total 16
  97. drwxr-xr-x 2 es es 4096 Mar 28 16:32 .
  98. drwxr-xr-x 4 es es 4096 Mar 28 16:31 ..
  99. -rw------- 1 es es 3596 Mar 28 16:21 elastic-certificates.p12
  100. -rw------- 1 es es 2672 Mar 28 16:20 elastic-stack-ca.p12
  101. ## 修改elasticsearch配置文件(所有节点)
  102. vim /app/elasticsearch/config/elasticsearch.yml
  103. ---------------------
  104. xpack.security.enabled: true
  105. xpack.security.transport.ssl.enabled: true
  106. xpack.security.transport.ssl.verification_mode: certificate
  107. xpack.security.transport.ssl.keystore.path: /app/elasticsearch/config/certs/elastic-certificates.p12
  108. xpack.security.transport.ssl.truststore.path: /app/elasticsearch/config/certs/elastic-certificates.p12
  109. ---------------------
  110. ## 将/app/elasticsearch/config/certs/elastic-certificates.p12,以及文件/app/elasticsearch/config/certs/elastic-stack-ca.p12传输至其他节点,并且按照相同的配置修改elasticsearch配置文件

1.4.2 HTTP TLS/SSL 加密(所有节点)

对于 HTTP 通信,Elasticsearch 节点将仅充当服务器,因此可以使用服务器证书。HTTP TLS/SSL 证书不需要启用客户端身份验证。
用于加密 HTTP 通信的证书可以完全独立于用于传输通信(transport communication)的证书。
方便起见也可以使用与传输通信相同的证书进行 HTTP 通信。

  1. ## 使用与传输通信相同的证书进行 HTTP 通信,修改elasticsearch配置文件
  2. vim /app/elasticsearch/config/elasticsearch.yml
  3. ---------------------
  4. xpack.security.http.ssl.enabled: true
  5. xpack.security.http.ssl.keystore.path: /app/elasticsearch/config/certs/elastic-certificates.p12
  6. xpack.security.http.ssl.truststore.path: /app/elasticsearch/config/certs/elastic-certificates.p12
  7. xpack.security.http.ssl.verification_mode: certificate
  8. xpack.security.http.ssl.client_authentication: optional
  9. ---------------------

1.4.3 配置密钥存储库(所有节点)

本次搭建所有密码均一致,即加密CA文件时使用的密码。

  1. ## 需要在每个节点上执行,如果在创建证书的过程中加了密码,需要输入这个密码
  2. [es@es1 ~]$ ./bin/elasticsearch-keystore add xpack.security.transport.ssl.keystore.secure_password
  3. [es@es1 ~]$ ./bin/elasticsearch-keystore add xpack.security.transport.ssl.truststore.secure_password
  4. [es@es1 ~]$ ./bin/elasticsearch-keystore add xpack.security.http.ssl.keystore.secure_password
  5. [es@es1 ~]$ ./bin/elasticsearch-keystore add xpack.security.http.ssl.truststore.secure_password

配置完成所有节点的密钥存储库后,就可以开启elastic集群

1.4.4 创建用户密码

创建用户密码的前提是开启集群,使用配置好的脚本启动,sh /xyz/1-start.sh

(也可以使用命令systemctl start elasticsearch.service,效果相同)

  1. ## 自动创建密码(执行命令目录:/home/elastic)
  2. [root@es1 elasticsearch]# ./bin/elasticsearch-setup-passwords auto
  3. ## 手动创建密码(执行命令目录:/home/elastic)
  4. [root@es1 elasticsearch]# ./bin/elasticsearch-setup-passwords interactive
  5. ## 重置用户密码(随机密码)(执行命令目录:/home/elastic)
  6. [root@es1 elasticsearch]# ./bin/elasticsearch-reset-password -u elastic
  7. ## 重置用户密码(指定密码)(执行命令目录:/home/elastic)
  8. [root@es1 elasticsearch]# ./bin/elasticsearch-reset-password -u elastic -i <password>
  9. ## 本次使用手动创建,密码均为:CTZQxy601108
  10. #### 手动方式建账号
  11. [root@es1 elasticsearch]# ./bin/elasticsearch-setup-passwords interactive
  12. warning: ignoring JAVA_HOME=/app/elasticsearch/jdk; using bundled JDK
  13. ******************************************************************************
  14. Note: The 'elasticsearch-setup-passwords' tool has been deprecated. This command will be removed in a future release.
  15. ******************************************************************************
  16. Initiating the setup of passwords for reserved users elastic,apm_system,kibana,kibana_system,logstash_system,beats_system,remote_monitoring_user.
  17. You will be prompted to enter passwords as the process progresses.
  18. Please confirm that you would like to continue [y/N]y
  19. Enter password for [elastic]:
  20. Reenter password for [elastic]:
  21. Enter password for [apm_system]:
  22. Reenter password for [apm_system]:
  23. Enter password for [kibana_system]:
  24. Reenter password for [kibana_system]:
  25. Enter password for [logstash_system]:
  26. Reenter password for [logstash_system]:
  27. Enter password for [beats_system]:
  28. Reenter password for [beats_system]:
  29. Enter password for [remote_monitoring_user]:
  30. Reenter password for [remote_monitoring_user]:
  31. Changed password for user [apm_system]
  32. Changed password for user [kibana_system]
  33. Changed password for user [kibana]
  34. Changed password for user [logstash_system]
  35. Changed password for user [beats_system]
  36. Changed password for user [remote_monitoring_user]
  37. Changed password for user [elastic]
  38. [root@es1 elasticsearch]#

1.6 最终配置

node1(192.168.133.200)

设置开机自启动并启动服务systemctl enable elasticsearch --now

  1. cluster.name: cttest
  2. node.name: node1
  3. network.host: 192.168.133.200
  4. bootstrap.memory_lock: true
  5. path.data: /data/elasticsearch/data
  6. path.logs: /data/elasticsearch/logs
  7. http.port: 9200
  8. transport.port: 9300
  9. discovery.seed_hosts: ["192.168.133.200:9300","192.168.133.201:9300","192.168.133.202:9300"]
  10. cluster.initial_master_nodes: ["node1", "node2","node3"]
  11. #允许http跨域访问,使用kibana必须开启
  12. http.cors.enabled: true
  13. http.cors.allow-origin: '*'
  14. action.auto_create_index: true
  15. #添加这个配置以后在kibana中才会显示联机状态,否则会显示脱机状态
  16. xpack.monitoring.collection.enabled: true
  17. xpack.security.enabled: true
  18. xpack.security.transport.ssl.enabled: true
  19. xpack.security.transport.ssl.verification_mode: certificate
  20. xpack.security.transport.ssl.keystore.path: /app/elasticsearch/config/certs/elastic-certificates.p12
  21. xpack.security.transport.ssl.truststore.path: /app/elasticsearch/config/certs/elastic-certificates.p12
  22. xpack.security.http.ssl.enabled: true
  23. xpack.security.http.ssl.keystore.path: /app/elasticsearch/config/certs/elastic-certificates.p12
  24. xpack.security.http.ssl.truststore.path: /app/elasticsearch/config/certs/elastic-certificates.p12
  25. xpack.security.http.ssl.verification_mode: certificate
  26. xpack.security.http.ssl.client_authentication: optional

node2(192.168.133.201)

设置开机自启动并启动服务systemctl enable elasticsearch --now

  1. cluster.name: cttest
  2. node.name: node2
  3. network.host: 192.168.133.201
  4. bootstrap.memory_lock: true
  5. path.data: /data/elasticsearch/data
  6. path.logs: /data/elasticsearch/logs
  7. http.port: 9200
  8. transport.port: 9300
  9. discovery.seed_hosts: ["192.168.133.200:9300","192.168.133.201:9300","192.168.133.202:9300"]
  10. cluster.initial_master_nodes: ["node1", "node2","node3"]
  11. #允许http跨域访问,使用kibana必须开启
  12. http.cors.enabled: true
  13. http.cors.allow-origin: '*'
  14. action.auto_create_index: true
  15. #添加这个配置以后在kibana中才会显示联机状态,否则会显示脱机状态
  16. xpack.monitoring.collection.enabled: true
  17. xpack.security.enabled: true
  18. xpack.security.transport.ssl.enabled: true
  19. xpack.security.transport.ssl.verification_mode: certificate
  20. xpack.security.transport.ssl.keystore.path: /app/elasticsearch/config/certs/elastic-certificates.p12
  21. xpack.security.transport.ssl.truststore.path: /app/elasticsearch/config/certs/elastic-certificates.p12
  22. xpack.security.http.ssl.enabled: true
  23. xpack.security.http.ssl.keystore.path: /app/elasticsearch/config/certs/elastic-certificates.p12
  24. xpack.security.http.ssl.truststore.path: /app/elasticsearch/config/certs/elastic-certificates.p12
  25. xpack.security.http.ssl.verification_mode: certificate
  26. xpack.security.http.ssl.client_authentication: optional

node3(192.168.133.202)

设置开机自启动并启动服务systemctl enable elasticsearch --now

  1. cluster.name: cttest
  2. node.name: node3
  3. network.host: 192.168.133.202
  4. bootstrap.memory_lock: true
  5. path.data: /data/elasticsearch/data
  6. path.logs: /data/elasticsearch/logs
  7. http.port: 9200
  8. transport.port: 9300
  9. discovery.seed_hosts: ["192.168.133.200:9300","192.168.133.201:9300","192.168.133.202:9300"]
  10. cluster.initial_master_nodes: ["node1", "node2","node3"]
  11. #允许http跨域访问,使用kibana必须开启
  12. http.cors.enabled: true
  13. http.cors.allow-origin: '*'
  14. action.auto_create_index: true
  15. #添加这个配置以后在kibana中才会显示联机状态,否则会显示脱机状态
  16. xpack.monitoring.collection.enabled: true
  17. xpack.security.enabled: true
  18. xpack.security.transport.ssl.enabled: true
  19. xpack.security.transport.ssl.verification_mode: certificate
  20. xpack.security.transport.ssl.keystore.path: /app/elasticsearch/config/certs/elastic-certificates.p12
  21. xpack.security.transport.ssl.truststore.path: /app/elasticsearch/config/certs/elastic-certificates.p12
  22. xpack.security.http.ssl.enabled: true
  23. xpack.security.http.ssl.keystore.path: /app/elasticsearch/config/certs/elastic-certificates.p12
  24. xpack.security.http.ssl.truststore.path: /app/elasticsearch/config/certs/elastic-certificates.p12
  25. xpack.security.http.ssl.verification_mode: certificate
  26. xpack.security.http.ssl.client_authentication: optional

02)kibana

1.安装(192.168.133.203)

  1. ## 上传并解压至目录
  2. tar -zxvf kibana-8.12.0-linux-x86_64.tar.gz -C /app
  3. mv kibana-8.12.0 kibana
  4. ## 创建kibana用户
  5. groupadd kibana
  6. useradd -g kibana -p es kibana
  7. ## 复制文件至家目录,避免出现(-bash-4.2$)
  8. [root@kibana home]# cp /etc/skel/.bash_logout /app/kibana
  9. [root@kibana home]# cp /etc/skel/.bash_profile /app/kibana
  10. [root@kibana home]# cp /etc/skel/.bashrc /app/kibana
  11. ## 修改文件属性
  12. [root@kibana home]# chown -R kibana:kibana /app/kibana
  13. [root@kibana home]# chmod -R 755 /app/kibana
  14. ## 配置systemd服务
  15. vim /etc/systemd/system/kibana.service
  16. ------------------------------
  17. [Unit]
  18. Description=kibana service daemon
  19. After=network.target
  20. [Service]
  21. User=kibana
  22. Group=kibana
  23. LimitNOFILE=65536
  24. LimitNPROC=4096
  25. ExecStart=/app/kibana/bin/kibana
  26. ExecReload=/bin/kill -HUP \$MAINPID
  27. KillMode=process
  28. Restart=on-failure
  29. RestartSec=10s
  30. [Install]
  31. WantedBy=multi-user.target
  32. ------------------------------
  33. ## 配置启停脚本
  34. mkdir /xyz
  35. cat >> /xyz/1-start.sh << EOF
  36. #!/bin/bash
  37. ## 启动脚本
  38. systemctl start kibana.service
  39. EOF
  40. cat >> /xyz/2-ps.sh << EOF
  41. #!/bin/bash
  42. ## 检查状态
  43. systemctl --no-pager status kibana.service
  44. EOF
  45. cat >> /xyz/3-stop.sh << EOF
  46. #!/bin/bash
  47. ## 停止脚本
  48. systemctl stop kibana.service
  49. EOF
  50. cat >> /xyz/4-restart.sh << EOF
  51. #!/bin/bash
  52. ## 重启脚本
  53. systemctl restart kibana.service
  54. EOF
  55. cat >> /xyz/5-update.sh << EOF
  56. #!/bin/bash
  57. ## 更新脚本
  58. systemctl daemon-reload
  59. EOF
  60. chmod -R 755 /xyz/
  61. ## 重新加载
  62. systemctl daemon-reload

2.https访问elastic

现在我们已经在 Elasticsearch 集群上启用了安全性,必须对与集群的通信进行身份验证。 因此,如果我们计划使用 Kibana 与集群交互,那么我们必须启用安全性并配置 Kibana 以通过 HTTPS 以 kibana 用户身份向集群进行身份验证。 

认证方式

  1. ## elastic服务器上密钥所在目录(/app/elasticsearch/config/certs)执行命令
  2. openssl pkcs12 -in elastic-stack-ca.p12 -out newfile.crt.pem -clcerts -nokeys
  3. ## 生成的文件(newfile.crt.pem)上传到kibana服务器上,具体目录(/app/kibana/config/certs)
  4. ## 修改kibana.yml配置文件
  5. vim /app/kibana/config/kibana.yml
  6. -----------------------------
  7. elasticsearch.hosts: ["https://192.168.133.200:9300","https://192.168.133.201:9300","https://192.168.133.202:9300"]
  8. elasticsearch.ssl.certificateAuthorities: ["/app/kibana/config/certs/newfile.crt.pem"]
  9. elasticsearch.ssl.verificationMode: none
  10. -----------------------------

3.nginx代理访问kibana配置

  1. location /kibana {
  2. proxy_pass http://192.168.133.203:5601;
  3. proxy_redirect off;
  4. proxy_set_header Host $host;
  5. proxy_set_header X-Real-IP $remote_addr;
  6. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  7. }

kibana配置

  1. server.basePath: "/kibana"
  2. server.rewriteBasePath: true

4.kibana开启监控

Kibana 的 Alert 模块主要用于 Elastic Stack 的监控告警。以一种相对较低的使用成本,将复杂的查询条件,编辑完成后监控不同的 Elastic Stack 的技术产品中产生的数据,最终把符合条件的告警信息以需要的方式反馈给用户。

前提条件
1. Elasticsearch 集群启用了HTTPS的安全设置
2. 在 Kibana 的配置文件中,添加 xpack.encryptedSavedObjects.encryptionKey 设置,它的值为至少 32 位的字符。

参考:https://blog.csdn.net/qq_33816243/article/details/132352607

  1. ## 切换至kibana用户
  2. su - kibana
  3. ## Kibana 提供了一个命令行工具来生成加密字符串,该命令行工具在 bin 目录下,使用方式如下:
  4. ./kibana-encryption-keys generate
  5. -----------------------------
  6. Kibana is currently running with legacy OpenSSL providers enabled! For details and instructions on how to disable see https://www.elastic.co/guide/en/kibana/8.12/production.html#openssl-legacy-provider
  7. ## Kibana Encryption Key Generation Utility
  8. The 'generate' command guides you through the process of setting encryption keys for:
  9. xpack.encryptedSavedObjects.encryptionKey
  10. Used to encrypt stored objects such as dashboards and visualizations
  11. https://www.elastic.co/guide/en/kibana/current/xpack-security-secure-saved-objects.html#xpack-security-secure-saved-objects
  12. xpack.reporting.encryptionKey
  13. Used to encrypt saved reports
  14. https://www.elastic.co/guide/en/kibana/current/reporting-settings-kb.html#general-reporting-settings
  15. xpack.security.encryptionKey
  16. Used to encrypt session information
  17. https://www.elastic.co/guide/en/kibana/current/security-settings-kb.html#security-session-and-cookie-settings
  18. Already defined settings are ignored and can be regenerated using the --force flag. Check the documentation links for instructions on how to rotate encryption keys.
  19. Definitions should be set in the kibana.yml used configure Kibana.
  20. Settings:
  21. xpack.encryptedSavedObjects.encryptionKey: b87e67507f0282a3a0aae8d2cb569fb4
  22. xpack.reporting.encryptionKey: e66823775d4c0a099db7767870582717
  23. xpack.security.encryptionKey: b06948f600f733ec36cc61470cc4d451
  24. -----------------------------

5.kibana生产环境访问配置

生产环境下还需要配置server.publicBaseUrl,否则会造成某些功能不正常。而且每次登陆都会进行提醒。

  1. ## 生产环境访问的域名为http://192.168.133.204/kibana/
  2. NGINX上的location配置为:
  3. location /kibana ##后面不要带/
  4. ## kibana文件配置,地址后面不要带/:
  5. vim /app/kibana/config/kibana.yml
  6. -----------------------------
  7. server.publicBaseUrl: "http://192.168.133.204/kibana/"
  8. -----------------------------

6.kibana最终配置

  1. server.port: 5601
  2. server.host: "192.168.133.203"
  3. server.basePath: "/kibana"
  4. server.rewriteBasePath: true
  5. server.publicBaseUrl: "http://192.168.133.204/kibana"
  6. elasticsearch.hosts: ["https://192.168.133.200:9200","https://192.168.133.201:9200","https://192.168.133.202:9200"]
  7. i18n.locale: "zh-CN"
  8. elasticsearch.ssl.certificateAuthorities: ["/app/kibana/config/certs/newfile.crt.pem"]
  9. elasticsearch.ssl.verificationMode: none
  10. xpack.encryptedSavedObjects.encryptionKey: b87e67507f0282a3a0aae8d2cb569fb4
  11. xpack.reporting.encryptionKey: e66823775d4c0a099db7767870582717
  12. xpack.security.encryptionKey: b06948f600f733ec36cc61470cc4d451

 启动kibana:sh /xyz/1-start.sh

设置开机自启动并启动服务systemctl enable kibana --now

03)logstash

1.安装(192.168.133.204)

  1. ## 上传至数据库
  2. mkdir /app
  3. tar -zxvf logstash-8.12.0-linux-x86_64.tar.gz -C /app
  4. mv logstash-8.12.0 logstash
  5. ## 配置服务启动项
  6. vim /etc/systemd/system/logstash.service
  7. -----------------------------
  8. [Unit]
  9. Description=Logstash
  10. Requires=network.service
  11. After=network.service
  12. [Service]
  13. LimitNOFILE=65536
  14. LimitMEMLOCK=infinity
  15. ExecStart=/app/logstash/bin/logstash -f /app/logstash/config/logstash.conf
  16. ExecReload=/bin/kill -HUP $MAINPID
  17. KillMode=mixed
  18. SuccessExitStatus=143
  19. Restart=on-failure
  20. [Install]
  21. WantedBy=multi-user.target
  22. -----------------------------
  23. ## systemd脚本配置
  24. mkdir /xyz
  25. cat >> /xyz/1-start.sh << EOF
  26. #!/bin/bash
  27. ## 启动脚本
  28. systemctl start logstash.service
  29. EOF
  30. cat >> /xyz/2-ps.sh << EOF
  31. #!/bin/bash
  32. ## 检查状态
  33. systemctl --no-pager status logstash.service
  34. EOF
  35. cat >> /xyz/3-stop.sh << EOF
  36. #!/bin/bash
  37. ## 停止脚本
  38. systemctl stop logstash.service
  39. EOF
  40. cat >> /xyz/4-restart.sh << EOF
  41. #!/bin/bash
  42. ## 重启脚本
  43. systemctl restart logstash.service
  44. EOF
  45. cat >> /xyz/5-update.sh << EOF
  46. #!/bin/bash
  47. ## 更新脚本
  48. systemctl daemon-reload
  49. EOF
  50. chmod -R 755 /xyz/

2.服务配置

首先在kibana界面配置API

 然后将API的密钥复制到kibana.yml配置文件内

  1. ## 配置logstash.conf
  2. vim /app/logstash/config/logstash.conf
  3. -----------------------------
  4. input {
  5. beats {
  6. port => 5044
  7. }
  8. }
  9. filter {
  10. if [message] =~ /200/ {
  11. drop {}
  12. }
  13. json {
  14. source => "message"
  15. remove_field => ["message","beat"]
  16. }
  17. date {
  18. match => ["timestamp","dd/MMM/yyyy:HH:mm:ss Z"]
  19. }
  20. }
  21. output {
  22. elasticsearch {
  23. hosts => ["https://192.168.133.200:9200","https://192.168.133.201:9200","https://192.168.133.202:9200"]
  24. index => "test-nginx-%{+YYYY.MM.dd}"
  25. ssl => true
  26. api_key => "BPmgoo4B7uBAvai7BEUG:gbQN4kI2TXaaH_IpSVFkIg"
  27. ilm_enabled => true
  28. ssl_certificate_verification => false
  29. }
  30. }
  31. -----------------------------

04)filebeat

1.安装(192.168.133.204)

安装程序目录为:/usr/local/filebeat

  1. ## 安装
  2. tar -zxvf filebeat-8.12.0-linux-x86_64.tar.gz -C /usr/local
  3. cd /usr/local
  4. mv filebeat-8.12.0 filebeat
  5. ## 配置服务启动项
  6. vim /etc/systemd/system/filebeat.service
  7. -----------------------------
  8. [Unit]
  9. Description=Filebeat
  10. Wants=network.service
  11. After=network.service
  12. [Service]
  13. ExecStart=/usr/local/filebeat/filebeat -e -c /usr/local/filebeat/filebeat.yml
  14. Restart=always
  15. ExecReload=/bin/kill -HUP $MAINPID
  16. KillMode=mixed
  17. Restart=on-failure
  18. [Install]
  19. WantedBy=multi-user.target
  20. -----------------------------

2.配置

本次采集的日志文件为/data/logs,采集目录下所有的日志的话,设置为:/data/logs/*.log

采集的数据输出至logstash:192.168.133.204:5044

  1. vim /usr/local/filebeat/filebeat.yml
  2. -----------------------------
  3. filebeat.inputs:
  4. - type: log
  5. enabled: true
  6. paths:
  7. - /data/logs/*.log
  8. tags: ["nginx"]
  9. json.keys_under_root: true
  10. json.add_error_key: true
  11. fields:
  12. log_topic: "nginx_log"
  13. output.logstash:
  14. hosts: ["192.168.133.204:5044"]
  15. -----------------------------

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/524438
推荐阅读
相关标签
  

闽ICP备14008679号