Add Docker container support

This commit is contained in:
Jeroen Geusebroek
2019-01-22 16:24:21 +01:00
parent 8cdce07205
commit 9f98ae6a76
9 changed files with 168 additions and 20 deletions

View File

@ -14,22 +14,30 @@ Design goals are to have a minimal memory footprint with a plugin system so that
## Requirements ## Requirements
### Operating systems ### Supported systems
This role will work on the following operating systems: This role supports the following systems:
* Red Hat * Red Hat
* Debian * Debian
* Ubuntu * Ubuntu
* Docker container
* Windows (Best effort) * Windows (Best effort)
* (Open)Suse * (Open)Suse
So, you'll need one of those operating systems.. :-) So, you'll need one of those systems.. :-)
Please sent Pull Requests or suggestions when you want to use this role for other Operating systems. Please sent Pull Requests or suggestions when you want to use this role for other systems.
### InfluxDB ### InfluxDB
You'll need an InfluxDB instance running somewhere on your network. You'll need an InfluxDB instance running somewhere on your network.
### Docker
Docker needs to be installed on the target host. I can recommend these roles to install Docker:
* [jgeusebroek.docker](https://galaxy.ansible.com/jgeusebroek/docker)
* [geerlingguy.docker](https://galaxy.ansible.com/geerlingguy/docker)
## Upgrade ## Upgrade
### 0.7.0 ### 0.7.0
@ -60,7 +68,14 @@ The following parameters can be set for the Telegraf agent:
* `telegraf_agent_logfile`: The agent logfile name. Default: '' (means to log to stdout) (since v1.1) * `telegraf_agent_logfile`: The agent logfile name. Default: '' (means to log to stdout) (since v1.1)
* `telegraf_agent_omit_hostname`: Do no set the "host" tag in the agent. Default: `False` (since v1.1) * `telegraf_agent_omit_hostname`: Do no set the "host" tag in the agent. Default: `False` (since v1.1)
Full agent settings reference: https://github.com/influxdata/telegraf/blob/master/docs/CONFIGURATION.md#agent-configuration Docker specific role variables:
* `telegraf_agent_docker`: Install Telegraf as a docker container. Default: `False`
* `telegraf_agent_docker_name`: Name of the docker container. Default: `telegraf`
* `telegraf_agent_docker_network_mode`: Networking mode of the docker container. Default: `bridge`
* `telegraf_agent_docker_restart_policy`: Docker container restart policy. Default: `unless-stopped`
Full agent settings reference: [https://github.com/influxdata/telegraf/blob/master/docs/CONFIGURATION.md#agent-configuration](https://github.com/influxdata/telegraf/blob/master/docs/CONFIGURATION.md#agent-configuration).
You can set tags for the host running telegraf: You can set tags for the host running telegraf:
@ -84,6 +99,55 @@ The config will be printed line by line into the configuration, so you could als
- # Print an documentation line - # Print an documentation line
and it will be printed in the configuration file. and it will be printed in the configuration file.
### Docker specifics
#### Docker image
The official [Influxdata Telegraf image](https://hub.docker.com/_/telegraf) is used. `telegraf_agent_version` will translate to the image tag.
#### Docker mounts
Please note that the Docker container bind mounts basicly your whole system (read-only) to monitor the Docker Engine Host from within the container. To be precise:
- /etc/telegraf:/etc/telegraf:ro
- /:/hostfs:ro
- /etc:/hostfs/etc:ro
- /proc:/hostfs/proc:ro
- /sys:/hostfs/sys:ro
- /var/run:/var/run:ro
More information: [https://github.com/influxdata/telegraf/blob/master/docs/FAQ.md](https://github.com/influxdata/telegraf/blob/master/docs/FAQ.md).
#### Example Docker configuration
telegraf_agent_docker: True
# Force host networking mode, so Docker Engine Host traffic metrics can be gathered.
telegraf_agent_docker_network_mode: host
# Force a specific image tag.
telegraf_agent_version: 1.9.2-alpine
telegraf_plugins_default:
- plugin: cpu
config:
- percpu = "true"
- plugin: disk
tags:
- diskmetrics = "true"
tagpass:
- fstype = [ "ext4", "xfs" ]
tagdrop:
- path = [ "/etc", "/etc/telegraf", "/etc/hostname", "/etc/hosts", "/etc/resolv.conf" ]
- plugin: io
- plugin: mem
- plugin: system
- plugin: swap
- plugin: netstat
- plugin: processes
- plugin: docker
config:
- endpoint = "unix:///var/run/docker.sock"
- timeout = "5s"
## Windows specific Variables ## Windows specific Variables
@ -177,6 +241,7 @@ The following have contributed to this Ansible role:
* Romain BUREAU * Romain BUREAU
* TheCodeAssassin * TheCodeAssassin
* tjend * tjend
* Jeroen Geusebroek
Thank you all! Thank you all!

View File

@ -14,6 +14,12 @@ telegraf_agent_flush_jitter: 0
telegraf_agent_aws_tags: False telegraf_agent_aws_tags: False
telegraf_agent_aws_tags_prefix: "" telegraf_agent_aws_tags_prefix: ""
# Docker specific settings
telegraf_agent_docker: False
telegraf_agent_docker_name: telegraf
telegraf_agent_docker_network_mode: bridge
telegraf_agent_docker_restart_policy: unless-stopped
# v0.13 settings (not sure if supported in older version): # v0.13 settings (not sure if supported in older version):
telegraf_agent_collection_jitter: 0 telegraf_agent_collection_jitter: 0
telegraf_agent_metric_batch_size: 1000 telegraf_agent_metric_batch_size: 1000

View File

@ -6,8 +6,14 @@
name: telegraf name: telegraf
state: restarted state: restarted
enabled: yes enabled: yes
when: not telegraf_agent_docker
become: yes become: yes
- name: "Restart Telegraf container"
docker_container:
name: "{{ telegraf_agent_docker_name }}"
restart: True
- name: "Restart Windows Telegraf" - name: "Restart Windows Telegraf"
win_service: win_service:
name: Telegraf name: Telegraf

View File

@ -45,7 +45,7 @@
when: when:
- ansible_lsb is not defined or ansible_lsb.codename is not defined - ansible_lsb is not defined or ansible_lsb.codename is not defined
- name: "Debian | Install telegraf package" - name: "Debian | Install Telegraf package"
apt: apt:
name: "{{ telegraf_agent_package }}" name: "{{ telegraf_agent_package }}"
state: "{{ telegraf_agent_package_state }}" state: "{{ telegraf_agent_package_state }}"

View File

@ -20,7 +20,7 @@
gpgcheck: yes gpgcheck: yes
gpgkey: https://repos.influxdata.com/influxdb.key gpgkey: https://repos.influxdata.com/influxdb.key
- name: "RedHat | Install telegraf package" - name: "RedHat | Install Telegraf package"
package: package:
name: "{{ telegraf_agent_package }}" name: "{{ telegraf_agent_package }}"
state: "{{ telegraf_agent_package_state }}" state: "{{ telegraf_agent_package_state }}"

View File

@ -1,11 +1,11 @@
--- ---
- name: "Suse | Adding telegraf group" - name: "Suse | Adding Telegraf group"
group: group:
name: telegraf name: telegraf
state: present state: present
- name: "Suse | Adding telegraf user" - name: "Suse | Adding Telegraf user"
user: user:
name: telegraf name: telegraf
group: telegraf group: telegraf

View File

@ -25,7 +25,9 @@
become: yes become: yes
when: when:
- telegraf_agent_version is version_compare('0.10.0', '<') - telegraf_agent_version is version_compare('0.10.0', '<')
notify: "Restart Telegraf" notify:
- Restart Telegraf
- Restart Telegraf container
- name: "Copy the template for versions >= 0.10.0" - name: "Copy the template for versions >= 0.10.0"
template: template:
@ -37,7 +39,9 @@
become: yes become: yes
when: when:
- telegraf_agent_version is version_compare('0.10.0', '>=') - telegraf_agent_version is version_compare('0.10.0', '>=')
notify: "Restart Telegraf" notify:
- Restart Telegraf
- Restart Telegraf container
- name: "Check if extra plugins directory exists in case of exclusive" - name: "Check if extra plugins directory exists in case of exclusive"
stat: stat:
@ -54,7 +58,9 @@
- telegraf_plugins_extra_exclusive - telegraf_plugins_extra_exclusive
- telegraf_directory.stat.exists - telegraf_directory.stat.exists
become: yes become: yes
notify: "Restart Telegraf" notify:
- Restart Telegraf
- Restart Telegraf container
- name: "Create telegraf extra plugin path" - name: "Create telegraf extra plugin path"
file: file:
@ -67,7 +73,9 @@
- telegraf_plugins_extra_exclusive - telegraf_plugins_extra_exclusive
- telegraf_directory.stat.exists - telegraf_directory.stat.exists
become: yes become: yes
notify: "Restart Telegraf" notify:
- Restart Telegraf
- Restart Telegraf container
- name: "Copy telegraf extra plugins" - name: "Copy telegraf extra plugins"
template: template:
@ -84,7 +92,9 @@
- telegraf_plugins_extra is iterable - telegraf_plugins_extra is iterable
- item.value.state|default('present') != 'absent' - item.value.state|default('present') != 'absent'
become: yes become: yes
notify: "Restart Telegraf" notify:
- Restart Telegraf
- Restart Telegraf container
- name: "Remove telegraf extra plugins" - name: "Remove telegraf extra plugins"
file: file:
@ -98,7 +108,9 @@
- telegraf_plugins_extra is iterable - telegraf_plugins_extra is iterable
- item.value.state|default('present') == 'absent' - item.value.state|default('present') == 'absent'
become: yes become: yes
notify: "Restart Telegraf" notify:
- Restart Telegraf
- Restart Telegraf container
- name: "Force restart service after reread config" - name: "Force restart service after reread config"
meta: flush_handlers meta: flush_handlers
@ -109,3 +121,4 @@
state: started state: started
enabled: yes enabled: yes
become: yes become: yes
when: not telegraf_agent_docker

55
tasks/docker.yml Normal file
View File

@ -0,0 +1,55 @@
- name: Adding Telegraf group
group:
name: telegraf
state: present
gid: 995
- name: Adding Telegraf user
user:
name: telegraf
group: telegraf
state: present
create_home: False
home: /etc/telegraf
uid: 998
system: True
become: yes
- name: Create /etc/telegraf (home) directory
file:
path: /etc/telegraf
owner: telegraf
group: telegraf
mode: 0750
state: directory
become: yes
- name: Create /etc/telegraf.d directory
file:
path: /etc/telegraf/telegraf.d
owner: telegraf
group: telegraf
mode: 0750
state: directory
become: yes
- name: Ensure Telegraf Docker container is running
docker_container:
name: "{{ telegraf_agent_docker_name }}"
image: "telegraf:{{ telegraf_agent_version }}"
state: started
restart_policy: "{{ telegraf_agent_docker_restart_policy }}"
command: -config /etc/telegraf/telegraf.conf -config-directory /etc/telegraf/telegraf.d
network_mode: "{{ telegraf_agent_docker_network_mode }}"
volumes:
- /etc/telegraf:/etc/telegraf:ro
- /:/hostfs:ro
- /etc:/hostfs/etc:ro
- /proc:/hostfs/proc:ro
- /sys:/hostfs/sys:ro
- /var/run:/var/run:ro
env:
HOST_MOUNT_PREFIX: /hostfs
HOST_ETC: /hostfs/etc
HOST_PROC: /hostfs/proc
HOST_SYS: /hostfs/sys

View File

@ -4,21 +4,24 @@
- name: "Install on RedHat" - name: "Install on RedHat"
include_tasks: "RedHat.yml" include_tasks: "RedHat.yml"
when: when:
- ansible_os_family == "RedHat" - ansible_os_family == "RedHat" and not telegraf_agent_docker
- name: "Install on Debian" - name: "Install on Debian"
include_tasks: "Debian.yml" include_tasks: "Debian.yml"
when: when:
- ansible_os_family == "Debian" - ansible_os_family == "Debian" and not telegraf_agent_docker
tags: tags:
- telegraf - telegraf
- packages - packages
- name: "Install on Suse" - name: "Install on Suse"
include_tasks: "Suse.yml" include_tasks: "Suse.yml"
when: ansible_os_family == "Suse" when: ansible_os_family == "Suse" and not telegraf_agent_docker
- name: "Configure telegraf" - include_tasks: "docker.yml"
when: telegraf_agent_docker
- name: "Configure Telegraf"
include_tasks: "configure_linux.yml" include_tasks: "configure_linux.yml"
when: when:
- ansible_os_family != "Windows" - ansible_os_family != "Windows"
@ -26,4 +29,4 @@
- name: "Install / Configure telegraf on Windows" - name: "Install / Configure telegraf on Windows"
include_tasks: "configure_windows.yml" include_tasks: "configure_windows.yml"
when: when:
- ansible_os_family == "Windows" - ansible_os_family == "Windows" and not telegraf_agent_docker