Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for docker autodiscover to monitor containers on host network #6708

Merged
merged 2 commits into from
Jun 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ https://github.com/elastic/beats/compare/v6.2.3...master[Check the HEAD diff]
- Add experimental Jolokia Discovery autodiscover provider. {pull}7141[7141]
- Add owner object info to Kubernetes metadata. {pull}7231[7231]
- Add beat export dashboard command. {pull}7239[7239]
- Add support for docker autodiscover to monitor containers on host network {pull}6708[6708]

*Auditbeat*

Expand Down
1 change: 0 additions & 1 deletion libbeat/autodiscover/providers/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ func (d *Provider) emitContainer(event bus.Event, flag string) {
if len(container.IPAddresses) > 0 {
host = container.IPAddresses[0]
}

labelMap := common.MapStr{}
for k, v := range container.Labels {
safemapstr.Put(labelMap, k, v)
Expand Down
16 changes: 15 additions & 1 deletion libbeat/common/docker/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ type Container struct {
// Client for docker interface
type Client interface {
ContainerList(ctx context.Context, options types.ContainerListOptions) ([]types.Container, error)
ContainerInspect(ctx context.Context, container string) (types.ContainerJSON, error)
Events(ctx context.Context, options types.EventsOptions) (<-chan events.Message, <-chan error)
}

Expand Down Expand Up @@ -290,7 +291,20 @@ func (w *watcher) listContainers(options types.ContainerListOptions) ([]*Contain
for _, c := range containers {
var ipaddresses []string
for _, net := range c.NetworkSettings.Networks {
ipaddresses = append(ipaddresses, net.IPAddress)
if net.IPAddress != "" {
ipaddresses = append(ipaddresses, net.IPAddress)
}
}

// If there are no network interfaces, assume that the container is on host network
// Inspect the container directly and use the hostname as the IP address in order
if len(ipaddresses) == 0 {
info, err := w.client.ContainerInspect(w.ctx, c.ID)
if err == nil {
ipaddresses = append(ipaddresses, info.Config.Hostname)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

metricbeat running in docker will only be able to resolve the hostname of the host if it is in the DNS or if the host exists in /etc/hosts in the container, what is not always necessarily true.

I think we should check here if metricbeat is running in docker (or in a network namespace).
If it is, then we use its gateway, that should be an IP in the host, as you proposed in a previous version, but now we are sure of knowing the hostname.
If it isn't running as docker it is also running in host mode and localhost or a local IP can be used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i checked this behavior and it seems to work.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

neither approach work (using hostname and gateway ip) on mac however

} else {
logp.Warn("unable to inspect container %s due to error %v", c.ID, err)
}
}
result = append(result, &Container{
ID: c.ID,
Expand Down
5 changes: 5 additions & 0 deletions libbeat/common/docker/watcher_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package docker

import (
"errors"
"testing"
"time"

Expand Down Expand Up @@ -46,6 +47,10 @@ func (m *MockClient) Events(ctx context.Context, options types.EventsOptions) (<
return eventsC, errorsC
}

func (m *MockClient) ContainerInspect(ctx context.Context, container string) (types.ContainerJSON, error) {
return types.ContainerJSON{}, errors.New("unimplemented")
}

func TestWatcherInitialization(t *testing.T) {
watcher := runWatcher(t, true,
[][]types.Container{
Expand Down