Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

DNS: auto-add custom container names/aliases #3084

Closed
berlic opened this issue Aug 9, 2017 · 3 comments
Closed

DNS: auto-add custom container names/aliases #3084

berlic opened this issue Aug 9, 2017 · 3 comments

Comments

@berlic
Copy link
Contributor

berlic commented Aug 9, 2017

In our weave network we launch several distributed apps in containers on different hosts.
We name containers for myapp as myapp-1, myapp-2, myapp-N to be able to address them separately (for example from load balancers).
Also for some application we give them custom host names as myapp-master (for myapp-1) and myapp-slave (for myapp-2 .. myapp-N) to make use of DNS balancing.

Current version of weave automatically manages only {{.Config.Hostname}}.{{.Config.Domainname}} value. See watcher.go code.

We've made a quick poc to automatically add not only hostname, but also container name if it differs:

diff --git a/plugin/net/watcher.go b/plugin/net/watcher.go
index 9a79561..c17f48b 100644
--- a/plugin/net/watcher.go
+++ b/plugin/net/watcher.go
@@ -46,6 +46,13 @@ func (w *watcher) ContainerStarted(id string) {
                                if err := w.weave.RegisterWithDNS(id, fqdn, net.IPAddress); err != nil {
                                        w.driver.warn("ContainerStarted", "unable to register %s with weaveDNS: %s", id, err)
                                }
+                                alias := fmt.Sprintf("%s.weave.local.", info.Name[1:]) // Remove prefix slash from name
+                                // w.driver.warn("ContainerStarted", "Container alias: %s", alias)
+                                if ( alias != fqdn ) {
+                                        if err := w.weave.RegisterWithDNS(id, alias, net.IPAddress); err != nil {
+                                               w.driver.warn("ContainerStarted", "unable to register %s with weaveDNS: %s", id, err)
+                                        }
+                                }
                        }
                        rootDir := "/"
                        if w.driver.isPluginV2 {

This works fine and do the job nicely.

Is it possible to add such a functionality into main branch? I can submit PR if there is a chance for it to be accepted.

If possible, please advice how can I refer custom domain suffix (supplied with --dns-domain option) inside ContainerStarted method of watcher? For sake of simplicity I've hardcoded it into my POC snippet.

This functionality can also rely on some container labels, such as weave.works.dns-alias or something.

@bboreham
Copy link
Contributor

Hi @berlic and thanks for the suggestion!

I was pondering whether this might be harmful to someone who uses container names for some other purpose and doesn't expect them to resolve via DNS but, as discussed on Slack, Docker itself will actually resolve the container name if you query from the same host (not cross-host unless you use the plugin in "global scope"). So guess it's not dangerous for Weave to add the name.

I'm less clear about the domain - why wouldn't you want to use config.Domainname ?
Could query the Weave Net daemon for its domain via api.DNSDomain()

Lastly, note there is a --net-alias option that could be used (and implemented here) instead of labels. See #1975 (and also #1914)

@berlic
Copy link
Contributor Author

berlic commented Aug 15, 2017

@bboreham thanks for --net-alias tip, implemented that as well.

As for domain name, config.Domainname is rarely used and is usually empty – it can't be populated via docker CLI in late docker versions and via docker_container ansible module, see moby/moby#27067 issue and related PR moby/moby#20200 (which stopped splitting fqdn). So I think it is safe to add configured weave domain as suffix to container name, because we already know that this container is in "our" network.

@bboreham bboreham added this to the 2.7 milestone Aug 4, 2020
@bboreham
Copy link
Contributor

bboreham commented Aug 4, 2020

Closed by #3090

@bboreham bboreham closed this as completed Aug 4, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants