In today’s article, we’ll cover some of the ways to deploy MetricBeat using Ansible. Before doing this exercise, I hope you have completed the previous exercise:

  1. How to deploy Elastic Stack-Overview using Ansible
  2. Deploying Elastic Stack-ElasticSearch with Ansible
  3. Deploying Elastic Stack-Kibana with Ansible
  4. Deploying Elastic Stack-Security with Ansible

The Elastic Stack we deployed on the Ubunut machine had security Settings. We repeat the previous steps to create roles and Playbooks.

You can find the source at github.com/liu-xiao-gu…

 

Create the MetricBeat role

We used the following command under ElasticSearch/Roles:

$ pwd
/Users/liuxg/ansible/elasticsearch/roles
$ ansible-galaxy init metricbeat
- Role metricbeat was created successfully
Copy the code

Next, let’s configure the MetricBeat Role. Now let’s modify the main.yml file in the Defaults directory of MetricBeat:

metricbeat/defaults/main.yml

---
# defaults file for metricbeat

#------------ Live reload settings ---------
reload_enabled: true
reload_period: 10s

#------------ Template settings ------------
index_number_of_shards: 1
index_codec: best_compression



#-----------  dashboard----------------------
setup_dashboards_enabled: true

tag: elk

#----------- Lopgging -----------------------
logging_level: debug

logging_to_files: true
logging_path: /var/log/metricbeat
logging_file_name: metricbeat
Logging_keepfiles: 7
logging_file_permissions: 0644

#---------- Monitoring ---------------------
monitoring_enabled: true
Copy the code

In the above file, we define variables that will be referenced in other files. Next, we’ll create a file called metricbeat.yml under Templates:

metricbeat/templates/metricbeat.yml

###################### Metricbeat Configuration Example #######################

# This file is an example configuration file highlighting only the most common
# options. The metricbeat.reference.yml file from the same directory contains all the
# supported options with more comments. You can use it as a reference.
#
# You can find the full configuration reference here:
# https://www.elastic.co/guide/en/beats/metricbeat/index.html

# =========================== Modules configuration ============================

metricbeat.config.modules:
  # Glob pattern for configuration loading
  path: ${path.config}/modules.d/*.yml

  # Set to true to enable config reloading
  reload.enabled: true

  # Period on which files under path should be checked for changes
  reload.period: {{ reload_period }}

# ======================= Elasticsearch template setting =======================

setup.template.settings:
  index.number_of_shards: {{ index_number_of_shards }}
  index.codec: {{ index_codec }} 
  #_source.enabled: false


# ================================== General ===================================

# The name of the shipper that publishes the network data. It can be used to group
# all the transactions sent by a single shipper in the web interface.
#name:

# The tags of the shipper are included in their own field with each
# transaction published.
tags: ["{{ tag }}"]

# Optional fields that you can specify to add additional information to the
# output.
#fields:
#  env: staging

# ================================= Dashboards =================================
# These settings control loading the sample dashboards to the Kibana index. Loading
# the dashboards is disabled by default and can be enabled either by setting the
# options here or by using the `setup` command.
setup.dashboards.enabled: true

# The URL from where to download the dashboards archive. By default this URL
# has a value which is computed based on the Beat name and version. For released
# versions, this URL points to the dashboard archive on the artifacts.elastic.co
# website.
#setup.dashboards.url:

# =================================== Kibana ===================================

# Starting with Beats version 6.0.0, the dashboards are loaded via the Kibana API.
# This requires a Kibana endpoint configuration.
setup.kibana:

  # Kibana Host
  # Scheme and port can be left out and will be set to the default (http and 5601)
  # In case you specify and additional path, the scheme is required: http://localhost:5601/path
  # IPv6 addresses should always be defined as: https://[2001:db8::1]:5601
  host: "{{ kibana_host }}:{{ kibana_port }}"

  # Kibana Space ID
  # ID of the Kibana Space into which the dashboards should be loaded. By default,
  # the Default Space will be used.
  #space.id:

# =============================== Elastic Cloud ================================

# These settings simplify using Metricbeat with the Elastic Cloud (https://cloud.elastic.co/).

# The cloud.id setting overwrites the `output.elasticsearch.hosts` and
# `setup.kibana.host` options.
# You can find the `cloud.id` in the Elastic Cloud web UI.
#cloud.id:

# The cloud.auth setting overwrites the `output.elasticsearch.username` and
# `output.elasticsearch.password` settings. The format is `<user>:<pass>`.
#cloud.auth:

# ================================== Outputs ===================================

# Configure what output to use when sending the data collected by the beat.

# ---------------------------- Elasticsearch Output ----------------------------
output.elasticsearch:
  # Array of hosts to connect to.
  hosts: ["{{ elastic_host }}:{{ elastic_port }}"]

  # Protocol - either `http` (default) or `https`.
  protocol: "{{ elastic_protocol }}"

  # Authentication credentials - either API key or username/password.
  #api_key: "id:api_key"
  username: "{{ elastic_username }}"
  password: "{{ elastic_password }}"

# ------------------------------ Logstash Output -------------------------------
#output.logstash:
  # The Logstash hosts
  #hosts: ["localhost:5044"]

  # Optional SSL. By default is off.
  # List of root certificates for HTTPS server verifications
  #ssl.certificate_authorities: ["/etc/pki/root/ca.pem"]

  # Certificate for SSL client authentication
  #ssl.certificate: "/etc/pki/client/cert.pem"

  # Client Certificate Key
  #ssl.key: "/etc/pki/client/cert.key"

# ================================= Processors =================================

# Configure processors to enhance or manipulate events generated by the beat.

processors:
  - add_host_metadata: ~
  - add_cloud_metadata: ~
  - add_docker_metadata: ~
  - add_kubernetes_metadata: ~


# ================================== Logging ===================================

# Sets log level. The default log level is info.
# Available log levels are: error, warning, info, debug
logging.level: {{ logging_level }}

# At debug level, you can selectively enable logging only for some components.
# To enable all selectors use ["*"]. Examples of other selectors are "beat",
# "publish", "service".
logging.selectors: ["*"]

logging.to_files: {{ logging_to_files }}
logging.files:
path: {{ logging_path }}
name: {{ logging_file_name }}
keepfiles: {{ Logging_keepfiles }}
permissions: {{ logging_file_permissions }}

# ============================= X-Pack Monitoring ==============================
# Metricbeat can export internal metrics to a central Elasticsearch monitoring
# cluster.  This requires xpack monitoring to be enabled in Elasticsearch.  The
# reporting is disabled by default.

# Set to true to enable the monitoring reporter.
monitoring.enabled: true

# Sets the UUID of the Elasticsearch cluster under which monitoring data for this
# Metricbeat instance will appear in the Stack Monitoring UI. If output.elasticsearch
# is enabled, the UUID is derived from the Elasticsearch cluster referenced by output.elasticsearch.
#monitoring.cluster_uuid:

# Uncomment to send the metrics to Elasticsearch. Most settings from the
# Elasticsearch output are accepted here as well.
# Note that the settings should point to your Elasticsearch *monitoring* cluster.
# Any setting that is not set is automatically inherited from the Elasticsearch
# output configuration, so if you have the Elasticsearch output configured such
# that it is pointing to your Elasticsearch monitoring cluster, you can simply
# uncomment the following line.
monitoring.elasticsearch:

# ================================= Migration ==================================

# This allows to enable 6.7 migration aliases
#migration.6_to_7.enabled: true
Copy the code

The original file of this file can be from a previous manual installation of MetricBeat. In this file it uses the user account information we defined earlier in the credentials.yml file and the variables we just defined in the defaults/main.yml file.

Next, we create tasks. We’ll edit the Tasks /main.yml file:

metricbeat/tasks/main.yml

---
# tasks file for metricbeat

# ------Install Metricbeat--------


- name: Install Metricbeat
  apt:
   name: metricbeat
   update_cache: yes

# ----- Replacing the configuration file

- name: Replace default metricbeat configuration file
  template: 
   src: metricbeat.yml
   dest: /etc/metricbeat/metricbeat.yml

#--------- Starting metricbeat service

- name: Starting metricbeat
  service:
   name: metricbeat
   state: started
   enabled: yes
Copy the code

Above, we performed the following tasks:

  • Install metricbeat
  • Overrides the metricBeat configuration file metricBeat.yml in the MetricBeat installation
  • Start the MetricBeat service

 

Create the playbook

Next, we create a Playbook for MetricBeat, although we can add it to the previous deploy-demo.yml file. We create a file called deploy-metricbeat.yml in playboooks:

playbooks/deploy-metricbeat.yml

--- # This playbook will deploy elk stack - hosts: elk become: yes vars_files: - .. /vars/credentials.yml - .. /vars/main.yml roles: - .. /roles/metricbeatCopy the code

We use the following command to deploy:

$ pwd
/Users/liuxg/ansible/elasticsearch
$ ansible-playbook -K -i inventory/hosts.yml playbooks/deploy-metricbeat.yml
Copy the code
$ pwd /Users/liuxg/ansible/elasticsearch $ ansible-playbook -K -i inventory/hosts.yml playbooks/deploy-metricbeat.yml BECOME password: PLAY [elk] ********************************************************************* TASK [Gathering Facts] * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ok: [192.168.0.4] TASK [.. / roles/metricbeat: Install Metricbeat] * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ok: [192.168.0.4] TASK [.. / roles/Metricbeat: Replace Default metricBeat configuration file ***** changed: [192.168.0.4] TASK [../roles/metricbeat: Starting metricbeat] ******************************* changed: [192.168.0.4] PLAY RECAP * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 192.168.0.4: ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0Copy the code

From the output above, we can see that the deployment was successful.

We can go directly to Ubuntu OS to check:

service metricbeat status
Copy the code

 

It shows that the service has been started successfully. By default, the System module is started.

We logged in to Kibana and clicked on Dashboard, and we saw something like this:

 

 

We see the messages coming in.

If we go to Ubuntu and run the following command:

sudo metricbeat modules list
Copy the code
$ sudo metricbeat modules list [sudo] password for liuxg: Enabled: system Disabled: activemq aerospike apache appsearch aws azure beat beat-xpack ceph ceph-mgr cloudfoundry cockroachdb consul coredns couchbase couchdb docker dropwizard elasticsearch elasticsearch-xpack envoyproxy etcd golang googlecloud graphite haproxy http ibmmq iis istio jolokia kafka kibana kibana-xpack kubernetes kvm linux logstash logstash-xpack memcached mongodb mssql munin mysql nats nginx openmetrics oracle php_fpm postgresql prometheus rabbitmq redis redisenterprise sql  stan statsd tomcat traefik uwsgi vsphere windows zookeeperCopy the code

We will find that only the System module is started. If we want to start the Nginx module, we can do the following. Modify tasks/main.yml file:

metricbeat/tasks/main.yml

---
# tasks file for metricbeat

# ------Install Metricbeat--------


- name: Install Metricbeat
  apt:
   name: metricbeat
   update_cache: yes

# ----- Replacing the configuration file

- name: Replace default metricbeat configuration file
  template: 
   src: metricbeat.yml
   dest: /etc/metricbeat/modules.d/nginx

# ----- enable Nginx module

- name: Enable Nginx module
  command: mv /etc/metricbeat/modules.d/nginx.yml.disabled /etc/metricbeat/modules.d/nginx.yml

#--------- Starting metricbeat service

- name: Starting metricbeat
  service:
   name: metricbeat
   state: started
   enabled: yes
Copy the code

Above, we simply rename the nginx.yml. Disabled file. Redeploy:

ansible-playbook -K -i inventory/hosts.yml playbooks/deploy-metricbeat.yml
Copy the code

After executing the above commands, we re-run the command on Ubuntu OS:

sudo metricbeat modules list
Copy the code

We will find:

As you can see from the above, both nginx and system modules are started at the same time.