docker override port, host, backend

reuse loadbalancer
This commit is contained in:
emile 2015-09-09 16:49:51 +02:00
parent 0af872e661
commit 730fbfe9c5
5 changed files with 76 additions and 45 deletions

View file

@ -5,8 +5,31 @@ import (
"bytes" "bytes"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
"log" "log"
"text/template"
"strings"
) )
var DockerFuncMap = template.FuncMap{
"getBackend": func(container docker.Container) string {
for key, value := range container.Config.Labels {
if (key == "træfik.backend") {
return value
}
}
return container.Config.Hostname
},
"getPort": func(container docker.Container) string {
for key, value := range container.Config.Labels {
if (key == "træfik.port") {
return value
}
}
for key, _ := range container.NetworkSettings.Ports {
return key.Port()
}
return ""
},
"getHost": getHost,
}
type DockerProvider struct { type DockerProvider struct {
Watch bool Watch bool
Endpoint string Endpoint string
@ -38,16 +61,22 @@ func (provider *DockerProvider) loadDockerConfig() *Configuration {
configuration := new(Configuration) configuration := new(Configuration)
containerList, _ := provider.dockerClient.ListContainers(docker.ListContainersOptions{}) containerList, _ := provider.dockerClient.ListContainers(docker.ListContainersOptions{})
containersInspected := []docker.Container{} containersInspected := []docker.Container{}
hosts := map[string][]docker.Container{}
for _, container := range containerList { for _, container := range containerList {
containerInspected, _ := provider.dockerClient.InspectContainer(container.ID) containerInspected, _ := provider.dockerClient.InspectContainer(container.ID)
containersInspected = append(containersInspected, *containerInspected) containersInspected = append(containersInspected, *containerInspected)
hosts[getHost(*containerInspected)] = append(hosts[getHost(*containerInspected)], *containerInspected)
} }
containers := struct {
templateObjects := struct {
Containers []docker.Container Containers []docker.Container
Hosts map[string][]docker.Container
}{ }{
containersInspected, containersInspected,
hosts,
} }
tmpl, err := gtf.New("docker.tmpl").ParseFiles("docker.tmpl") gtf.Inject(DockerFuncMap)
tmpl, err := template.New("docker.tmpl").Funcs(DockerFuncMap).ParseFiles("docker.tmpl")
if err != nil { if err != nil {
log.Println("Error reading file:", err) log.Println("Error reading file:", err)
return nil return nil
@ -55,7 +84,7 @@ func (provider *DockerProvider) loadDockerConfig() *Configuration {
var buffer bytes.Buffer var buffer bytes.Buffer
err = tmpl.Execute(&buffer, containers) err = tmpl.Execute(&buffer, templateObjects)
if err != nil { if err != nil {
log.Println("Error with docker template:", err) log.Println("Error with docker template:", err)
return nil return nil
@ -67,3 +96,12 @@ func (provider *DockerProvider) loadDockerConfig() *Configuration {
} }
return configuration return configuration
} }
func getHost(container docker.Container) string {
for key, value := range container.Config.Labels {
if (key == "træfik.host") {
return value
}
}
return strings.TrimPrefix(container.Name, "/")
}

View file

@ -1,25 +1,12 @@
[backends]{{range .Containers}} [backends]{{range .Containers}}
[backends.backend-{{.Config.Hostname}}] [backends.backend-{{getBackend .}}.servers.server-{{.Name | replace "/"}}]
[backends.backend-{{.Config.Hostname}}.servers.server-{{.Config.Hostname}}] url = "http://{{.NetworkSettings.IPAddress}}:{{getPort .}}"
{{/* Only one exposed port! */}}
{{if eq (len .Config.ExposedPorts) 1}}
{{$ip := .NetworkSettings.IPAddress}}
{{range $keyPort, $valuePort := .Config.ExposedPorts}}
url = "http://{{$ip}}:{{$keyPort.Port}}"
{{end}}
{{else}}
{{end}}
{{end}} {{end}}
[routes]{{range .Containers}} [routes]{{range $host, $containers := .Hosts}}
[routes.route-{{.Config.Hostname}}] [routes.route-{{$host}}]
backends = ["backend-{{.Config.Hostname}}"] backends = [{{range $container := $containers}}"backend-{{getBackend $container}}",{{end}}]
[routes.route-{{.Config.Hostname}}.rules.rule-{{.Config.Hostname}}-hostname] [routes.route-{{$host}}.rules.rule-host-{{$host}}]
category = "Host" category = "Host"
value = "{{.Config.Hostname}}.zenika.fr" value = "{{$host}}.zenika.fr"
[routes.route-{{.Config.Hostname}}.rules.rule-{{.Config.Hostname}}-name]
category = "Host"
value = "{{.Name | replace "/"}}.zenika.fr"
{{end}} {{end}}

View file

@ -1,7 +1,7 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<title>Ramify</title> <title>træfik</title>
<!-- Bootstrap --> <!-- Bootstrap -->
<link rel="stylesheet" href="/static/bootstrap/css/bootstrap.min.css"> <link rel="stylesheet" href="/static/bootstrap/css/bootstrap.min.css">
@ -18,7 +18,7 @@
<body> <body>
<nav class="navbar navbar-default" role="navigation"> <nav class="navbar navbar-default" role="navigation">
<div class="navbar-header"> <div class="navbar-header">
<a class="navbar-brand" href="/html/">Ramify</a> <a class="navbar-brand" href="/html/">træfik</a>
</div> </div>
<div class="collapse navbar-collapse navbar-ex1-collapse"> <div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav"> <ul class="nav navbar-nav">

View file

@ -40,9 +40,9 @@ func main() {
log.Println("Configuration receveived", configuration) log.Println("Configuration receveived", configuration)
if configuration == nil { if configuration == nil {
log.Println("Skipping empty configuration") log.Println("Skipping empty configuration")
} else if(reflect.DeepEqual(currentConfiguration, configuration)){ } else if (reflect.DeepEqual(currentConfiguration, configuration)) {
log.Println("Skipping same configuration") log.Println("Skipping same configuration")
} else{ } else {
currentConfiguration = configuration currentConfiguration = configuration
configurationRouter = LoadConfig(configuration) configurationRouter = LoadConfig(configuration)
srv.Stop(10 * time.Second) srv.Stop(10 * time.Second)
@ -52,15 +52,15 @@ func main() {
}() }()
configuration := LoadFileConfig() configuration := LoadFileConfig()
if(configuration.Docker != nil){ if (configuration.Docker != nil) {
providers = append(providers, configuration.Docker) providers = append(providers, configuration.Docker)
} }
if(configuration.File != nil){ if (configuration.File != nil) {
providers = append(providers, configuration.File) providers = append(providers, configuration.File)
} }
if(configuration.Web != nil){ if (configuration.Web != nil) {
providers = append(providers, configuration.Web) providers = append(providers, configuration.Web)
} }
@ -107,6 +107,7 @@ func main() {
func LoadConfig(configuration *Configuration) *mux.Router { func LoadConfig(configuration *Configuration) *mux.Router {
router := mux.NewRouter() router := mux.NewRouter()
backends := map[string]http.Handler{}
for routeName, route := range configuration.Routes { for routeName, route := range configuration.Routes {
log.Println("Creating route", routeName) log.Println("Creating route", routeName)
fwd, _ := forward.New() fwd, _ := forward.New()
@ -118,6 +119,7 @@ func LoadConfig(configuration *Configuration) *mux.Router {
newRoutes = append(newRoutes, newRoute) newRoutes = append(newRoutes, newRoute)
} }
for _, backendName := range route.Backends { for _, backendName := range route.Backends {
if (backends[backendName] ==nil) {
log.Println("Creating backend", backendName) log.Println("Creating backend", backendName)
lb, _ := roundrobin.New(fwd) lb, _ := roundrobin.New(fwd)
rb, _ := roundrobin.NewRebalancer(lb) rb, _ := roundrobin.NewRebalancer(lb)
@ -126,8 +128,12 @@ func LoadConfig(configuration *Configuration) *mux.Router {
url, _ := url.Parse(server.Url) url, _ := url.Parse(server.Url)
rb.UpsertServer(url) rb.UpsertServer(url)
} }
backends[backendName]=lb
}else {
log.Println("Reusing backend", backendName)
}
for _, route := range newRoutes { for _, route := range newRoutes {
route.Handler(handlers.CombinedLoggingHandler(os.Stdout, lb)) route.Handler(handlers.CombinedLoggingHandler(os.Stdout, backends[backendName]))
} }
} }
} }
@ -144,7 +150,7 @@ func Invoke(any interface{}, name string, args ...interface{}) []reflect.Value {
func LoadFileConfig() *FileConfiguration { func LoadFileConfig() *FileConfiguration {
configuration := new(FileConfiguration) configuration := new(FileConfiguration)
if _, err := toml.DecodeFile("ramify.toml", configuration); err != nil { if _, err := toml.DecodeFile("træfik.toml", configuration); err != nil {
log.Fatal("Error reading file:", err) log.Fatal("Error reading file:", err)
} }
return configuration return configuration

View file

@ -6,7 +6,7 @@ watch = true
address = ":8010" address = ":8010"
#[file] #[file]
#filename = "ramify.toml" #filename = "træfik.toml"
#watch = true #watch = true
[backends] [backends]