小言_互联网的博客

Elastic:如何使用 Ansible自动化部署 Elastic Stack - Elasticsearch (二)

330人阅读  评论(0)

这篇文章是 “Elastic:如何使用 Ansible自动化部署 Elastic Stack (一)” 的续篇。在上一篇文章中,我们对 Ansible 部署工具有所了解,并部署了一个简单的 Webserver。在今天的这篇文章中,我们来介绍如何部署 Elastic Stack。

你可以在地址找到源码:https://github.com/liu-xiao-guo/elk-ansible

 

部署 Elasticsearch

如果你之前还从来没有在 Linux 机器上手动部署过 Elasticsearch,那么我建议你阅读我之前的文章 “如何在 AWS 上一步一步地安装 Elastic Stack”。在那篇文章中,有详细地描述如何一步一步地在 Ubuntu 机器上安装 Elasticsearch。在今天的练习中,我们将一步一步地按照那里面的步骤来进行安装。

我们仿照在第一篇文章中部署 Webserver 的例子来进行部署。

 

添加 elastic repo


  
  1. $ pwd
  2. /Users/liuxg/ansible/elasticsearch
  3. $ cd roles
  4. $ ansible-galaxy init add-elastic-repo

通过上面的命令,我们就创建了一个叫做 add-elastic-repo 的角色:

我们在 tasks 子目录修改已经被创建的 main.yml 文件,并把如下的内容输入进去:

tasks/main.yml


  
  1. ---
  2. # tasks file for add-elastic-repo
  3. # Add elasticsearch PSK
  4. - name: add elasticsearch public signing key
  5. apt_key:
  6. url: "{{ elastic_psk_url }}"
  7. state: present
  8. # install Transport module
  9. - name: Install apt-transport-https
  10. apt:
  11. name: apt-transport-https
  12. update_cache: yes
  13. # Add elasticsearch repo definitions
  14. - name: Add elasticsearch repo definitions
  15. apt_repository:
  16. repo: deb {{ elastic_repo }} stable main
  17. # System update
  18. - name: system update
  19. apt:
  20. update_cache: yes

在上面,我们引入了两个变量 elastic_psk_url 及 elastic_repo。这两个变量可能依赖于部署的版本不同而不同以及 signing key 的位置改变而改变。这些变量可以被定义在 defaults 目录下的 main.yml 文件中:

在上面,我们把如下的内容写入到 main.yml 中去:

defaults/main.yml


  
  1. ---
  2. # defaults file for add-elastic-repo
  3. elastic_psk_url: https://artifacts.elastic.co/GPG-KEY-elasticsearch
  4. elastic_repo: https://artifacts.elastic.co/packages/7.x/apt

为了能够完成部署,我们在 playbooks 下创建一个叫做 deploy-demo.yml 的文件:

我们把如下的内容输入到 deploy-demo.yml 的文件中:

palybooks/deploy-demo.yml


  
  1. ---
  2. # This playbook will deploy webserver
  3. - hosts: all
  4. become: yes
  5. roles:
  6. - ../roles/add-elastic-repo

这样我们的配置就已经完成了。从上面我们可以看出来这个部署将安装到所有的机器上 all。all 是在我们的 hosts.yml 文件中定义的。我们可以使用如下的命令来进行部署:


  
  1. $ pwd
  2. /Users/liuxg /ansible/elasticsearch
  3. $ ansible-playbook -K -i inventory /hosts.yml playbooks/deploy-demo.yml

上面的命令执行的结果是:


  
  1. $ pwd
  2. /Users/liuxg/ansible/elasticsearch
  3. $ ansible-playbook -K -i inventory/hosts.yml playbooks/deploy-demo.yml
  4. BECOME password:
  5. PLAY [all] **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** *
  6. TASK [Gathering Facts] *********************************************************
  7. ok: [192.168.0.4]
  8. TASK [../roles/add-elastic-repo : add elasticsearch public signing key] **** ****
  9. ok: [192.168.0.4]
  10. TASK [../roles/add-elastic-repo : Install apt-transport-https] **** **** **** **** *
  11. ok: [192.168.0.4]
  12. TASK [../roles/add-elastic-repo : Add elasticsearch repo definitions] **********
  13. ok: [192.168.0.4]
  14. TASK [../roles/add-elastic-repo : system update] ** **** **** **** **** **** **** *****
  15. changed: [192.168.0.4]
  16. PLAY RECAP **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** *
  17. 192.168.0.4 : ok=5 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

从上面的结果我们可以看出来安装是成功的。

 

安装 Elasticsearch

如法炮制,我们先在 roles 下创建一个叫做 elasticsearch 的 role:


  
  1. $ pwd
  2. /Users/liuxg/ansible/elasticsearch/roles
  3. $ ansible-galaxy init elasticsearch
  4. - Role elasticsearch was created successfully

如果你从来还没有安装过 Elasticsearch,那么我建议你先去阅读我之前的文章 “Elastic:菜鸟上手指南” 去阅读文章 “如何在 Linux,MacOS 及 Windows 上进行安装 Elasticsearch”。我们可以把 Elasticsearch 安装于一个本地目录中,并在它的安装目录 config 下找到 elasticsearch.yml 文件。我们把这个 elasticsearch.yml 文件拷贝到如下的 templates 目录中:

上面的 elastcsearch.yml 的内容如下:

templates/elasticsearch.yml


  
  1. # ======================== Elasticsearch Configuration =========================
  2. #
  3. # NOTE: Elasticsearch comes with reasonable defaults for most settings.
  4. # Before you set out to tweak and tune the configuration, make sure you
  5. # understand what are you trying to accomplish and the consequences.
  6. #
  7. # The primary way of configuring a node is via this file. This template lists
  8. # the most important settings you may want to configure for a production cluster.
  9. #
  10. # Please consult the documentation for further information on configuration options:
  11. # https://www.elastic.co/guide/en/elasticsearch/reference/index.html
  12. #
  13. # ---------------------------------- Cluster -----------------------------------
  14. #
  15. # Use a descriptive name for your cluster:
  16. #
  17. cluster.name: demo-elk
  18. #
  19. # ------------------------------------ Node ------------------------------------
  20. #
  21. # Use a descriptive name for the node:
  22. #
  23. node.name: elk-1
  24. #
  25. # Add custom attributes to the node:
  26. #
  27. #node.attr.rack: r1
  28. #
  29. # ----------------------------------- Paths ------------------------------------
  30. #
  31. # Path to directory where to store the data (separate multiple locations by comma):
  32. #
  33. path.data: /var/lib/elasticsearch
  34. #
  35. # Path to log files:
  36. #
  37. path.logs: /var/log/elasticsearch
  38. #
  39. # ----------------------------------- Memory -----------------------------------
  40. #
  41. # Lock the memory on startup:
  42. #
  43. #bootstrap.memory_lock: true
  44. #
  45. # Make sure that the heap size is set to about half the memory available
  46. # on the system and that the owner of the process is allowed to use this
  47. # limit.
  48. #
  49. # Elasticsearch performs poorly when the system is swapping the memory.
  50. #
  51. # ---------------------------------- Network -----------------------------------
  52. #
  53. # Set the bind address to a specific IP (IPv4 or IPv6):
  54. #
  55. network.host: 0.0 .0 .0
  56. #
  57. # Set a custom port for HTTP:
  58. #
  59. http.port: 9200
  60. #
  61. # For more information, consult the network module documentation.
  62. #
  63. # --------------------------------- Discovery ----------------------------------
  64. #
  65. # Pass an initial list of hosts to perform discovery when this node is started:
  66. # The default list of hosts is ["127.0.0.1", "[::1]"]
  67. #
  68. #discovery.seed_hosts: ["host1", "host2"]
  69. #
  70. # Bootstrap the cluster using an initial set of master-eligible nodes:
  71. #
  72. #cluster.initial_master_nodes: ["node-1", "node-2"]
  73. #
  74. # For more information, consult the discovery and cluster formation module documentation.
  75. #
  76. # ---------------------------------- Gateway -----------------------------------
  77. #
  78. # Block initial recovery after a full cluster restart until N nodes are started:
  79. #
  80. #gateway.recover_after_nodes: 3
  81. #
  82. # For more information, consult the gateway module documentation.
  83. #
  84. # ---------------------------------- Various -----------------------------------
  85. #
  86. # Require explicit names when deleting indices:
  87. #
  88. #action.destructive_requires_name: true
  89. discovery.type: single-node

如上所示,所有在 elasticsearch.yml 文件中的定义都是固定的。在实际的部署中,我们希望这些是可以变化。依据不同的部署分别进行配置。为此,我们在 defaults/mail.yml 中分别为它们定义一个变量:

defaults/maim.yml


  
  1. ---
  2. # defaults file for elasticsearch
  3. cluster_name: demo-elk
  4. node_name: elk-1
  5. path_data: /var/lib/elasticsearch
  6. path_logs: /var/log/elasticsearch
  7. network_host: 0.0 .0 .0
  8. http_port: 9200
  9. discovery_type: single-node

由于这些变量的引入,我们修改我们的 elasticsearch.yml 文件如下:

templates/elasticsearch.yml


  
  1. # ======================== Elasticsearch Configuration =========================
  2. #
  3. # NOTE: Elasticsearch comes with reasonable defaults for most settings.
  4. # Before you set out to tweak and tune the configuration, make sure you
  5. # understand what are you trying to accomplish and the consequences.
  6. #
  7. # The primary way of configuring a node is via this file. This template lists
  8. # the most important settings you may want to configure for a production cluster.
  9. #
  10. # Please consult the documentation for further information on configuration options:
  11. # https://www.elastic.co/guide/en/elasticsearch/reference/index.html
  12. #
  13. # ---------------------------------- Cluster -----------------------------------
  14. #
  15. # Use a descriptive name for your cluster:
  16. #
  17. cluster.name: {{ cluster_name }}
  18. #
  19. # ------------------------------------ Node ------------------------------------
  20. #
  21. # Use a descriptive name for the node:
  22. #
  23. node.name: {{ node_name }}
  24. #
  25. # Add custom attributes to the node:
  26. #
  27. #node.attr.rack: r1
  28. #
  29. # ----------------------------------- Paths ------------------------------------
  30. #
  31. # Path to directory where to store the data (separate multiple locations by comma):
  32. #
  33. path.data: {{ path_data }}
  34. #
  35. # Path to log files:
  36. #
  37. path.logs: {{ path_logs }}
  38. #
  39. # ----------------------------------- Memory -----------------------------------
  40. #
  41. # Lock the memory on startup:
  42. #
  43. #bootstrap.memory_lock: true
  44. #
  45. # Make sure that the heap size is set to about half the memory available
  46. # on the system and that the owner of the process is allowed to use this
  47. # limit.
  48. #
  49. # Elasticsearch performs poorly when the system is swapping the memory.
  50. #
  51. # ---------------------------------- Network -----------------------------------
  52. #
  53. # Set the bind address to a specific IP (IPv4 or IPv6):
  54. #
  55. network.host: {{ network_host }}
  56. #
  57. # Set a custom port for HTTP:
  58. #
  59. http.port: {{ http_port }}
  60. #
  61. # For more information, consult the network module documentation.
  62. #
  63. # --------------------------------- Discovery ----------------------------------
  64. #
  65. # Pass an initial list of hosts to perform discovery when this node is started:
  66. # The default list of hosts is ["127.0.0.1", "[::1]"]
  67. #
  68. #discovery.seed_hosts: ["host1", "host2"]
  69. #
  70. # Bootstrap the cluster using an initial set of master-eligible nodes:
  71. #
  72. #cluster.initial_master_nodes: ["node-1", "node-2"]
  73. #
  74. # For more information, consult the discovery and cluster formation module documentation.
  75. #
  76. # ---------------------------------- Gateway -----------------------------------
  77. #
  78. # Block initial recovery after a full cluster restart until N nodes are started:
  79. #
  80. #gateway.recover_after_nodes: 3
  81. #
  82. # For more information, consult the gateway module documentation.
  83. #
  84. # ---------------------------------- Various -----------------------------------
  85. #
  86. # Require explicit names when deleting indices:
  87. #
  88. #action.destructive_requires_name: true
  89. discovery.type: {{ discovery_type }}

我们接下来修改 tasks/main.yml 文件:

tasks/main.yml


  
  1. ---
  2. # tasks file for elasticsearch
  3. # Installing Elasticsearch
  4. - name: Installing Elasticsearch
  5. apt:
  6. name: elasticsearch
  7. # Replce default elasticsearch.yml
  8. - name: Replace default elasticsearch.yml
  9. template:
  10. src: elasticsearch.yml
  11. dest: /etc/elasticsearch/elasticsearch.yml
  12. # Start Elasticsearch service
  13. - name:
  14. service:
  15. name: elasticsearch
  16. state: started
  17. enabled: yes

对于中国区域的很多开发者来说,安装一个 elasticsearch 可能会需要很长的时间来进行下载。你需要耐心!一种方法是你可以预先下载好的 deb 文件并放入到指定的目录来进行安装。这个依赖于你自己的网路情况。下面的例子展示如何下载一个 deb 安装包并安装:


  
  1. - name: Download Elastic Deb Package
  2. get_url:
  3. url: https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.10.1-amd64.deb
  4. dest: /tmp
  5. - name: Install Elastic Deb Package
  6. become: yes
  7. apt:
  8. deb: /tmp/elasticsearch-7.8.1-amd64.deb

我们接下来修改 deploy-demo.yml 文件:

playbooks/deploy-demo.yml


  
  1. ---
  2. # This playbook will deploy webserver
  3. - hosts: all
  4. become: yes
  5. roles:
  6. - ../roles/add-elastic-repo
  7. # This playbook will deploy ELK stack
  8. - hosts: elk
  9. become: yes
  10. roles:
  11. - ../roles/elasticsearch

我们把 Elasticsearch 部署到 elk 所定义的服务器上。好了,到目前为止,基本上我们的配置已经完成。我们接下来使用如下的命令来进行部署:

ansible-playbook -K -i inventory/hosts.yml playbooks/deploy-demo.yml

上面的命令所生成的结果为:


  
  1. $ pwd
  2. /Users/liuxg/ansible/elasticsearch
  3. $ ansible-playbook -K -i inventory/hosts.yml playbooks/deploy-demo.yml
  4. BECOME password:
  5. PLAY [all] **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** *
  6. TASK [Gathering Facts] *********************************************************
  7. ok: [192.168.0.4]
  8. TASK [../roles/add-elastic-repo : add elasticsearch public signing key] **** ****
  9. ok: [192.168.0.4]
  10. TASK [../roles/add-elastic-repo : Install apt-transport-https] **** **** **** **** *
  11. ok: [192.168.0.4]
  12. TASK [../roles/add-elastic-repo : Add elasticsearch repo definitions] **********
  13. ok: [192.168.0.4]
  14. TASK [../roles/add-elastic-repo : system update] ** **** **** **** **** **** **** *****
  15. changed: [192.168.0.4]
  16. PLAY [elk] **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** *
  17. TASK [Gathering Facts] *********************************************************
  18. ok: [192.168.0.4]
  19. TASK [../roles/elasticsearch : Installing Elasticsearch] **** **** **** **** **** ***
  20. ok: [192.168.0.4]
  21. TASK [../roles/elasticsearch : Replace default elasticsearch.yml] **************
  22. changed: [192.168.0.4]
  23. TASK [../roles/elasticsearch : service] ** **** **** **** **** **** **** **** **** **** **
  24. changed: [192.168.0.4]
  25. PLAY RECAP ** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** ***
  26. 192.168.0.4 : ok=9 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

从上面的输出中,我们可以看出来我们的部署是成功的。我们可以在 Ubuntu OS 的机器上运行如下的命令来查看 Elasticsearch 服务是否已经启动:

service elasticsearch status

我们现在通过如下的命令来检查部署是否成功:

curl -XGET "http://ubuntu:9200"

上面的命令的输出为:

从上面,我们可以看出来我们的 Elasticsearch 的部署是成功的。

参考:

【1】https://linuxize.com/post/how-to-install-elasticsearch-on-ubuntu-20-04/

【2】 https://github.com/lmakonem/ELK-SIEM-Ansible-Playbook


转载:https://blog.csdn.net/UbuntuTouch/article/details/113113445
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场