Skip to content

Feature #630 basic service mesh support for Consul #664

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 11 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

[[constraint]]
name = "github.com/hashicorp/consul"
version = "1.0.7"
version = "1.5.1"

[[constraint]]
branch = "master"
Expand Down
8 changes: 8 additions & 0 deletions bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,17 @@ func (b *Bridge) newService(port ServicePort, isgroup bool) *Service {
service.ID = id
}

mesh := mapDefault(metadata, "mesh", "")
if mesh != "" {
service.Mesh, _ = strconv.ParseBool(mesh)
} else {
service.Mesh = b.config.ServiceMesh
}

delete(metadata, "id")
delete(metadata, "tags")
delete(metadata, "name")
delete(metadata, "mesh")
service.Attrs = metadata
service.TTL = b.config.RefreshTtl

Expand Down
2 changes: 2 additions & 0 deletions bridge/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Config struct {
RefreshInterval int
DeregisterCheck string
Cleanup bool
ServiceMesh bool
}

type Service struct {
Expand All @@ -39,6 +40,7 @@ type Service struct {
Tags []string
Attrs map[string]string
TTL int
Mesh bool

Origin ServicePort
}
Expand Down
9 changes: 7 additions & 2 deletions consul/consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ func init() {
bridge.Register(f, "consul-unix")
}

func (r *ConsulAdapter) interpolateService(script string, service *bridge.Service) string {
func (r *ConsulAdapter) interpolateService(script string, service *bridge.Service) []string {
withIp := strings.Replace(script, "$SERVICE_IP", service.IP, -1)
withPort := strings.Replace(withIp, "$SERVICE_PORT", strconv.Itoa(service.Port), -1)
return withPort
return strings.Split(withPort, " ")
}

type Factory struct{}
Expand Down Expand Up @@ -86,6 +86,11 @@ func (r *ConsulAdapter) Register(service *bridge.Service) error {
registration.Address = service.IP
registration.Check = r.buildCheck(service)
registration.Meta = service.Attrs
if service.Mesh == true {
registration.Connect = &consulapi.AgentServiceConnect{
SidecarService: &consulapi.AgentServiceRegistration{},
}
}
return r.client.Agent().ServiceRegister(registration)
}

Expand Down
18 changes: 18 additions & 0 deletions docs/user/backends.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,24 @@ If enabled this should be much longer than any expected recoverable outage.
SERVICE_CHECK_DEREGISTER_AFTER=10m
```

### Consul ServiceMesh Support

Consul can support ServiceMesh by integrating with proxies like Envoy, the most convenient way to enable this capability is creating a Service like [this](https://www.consul.io/docs/connect/registration/sidecar-service.html#minimal-example):

```json
{
"service": {
"name": "web",
"port": 8080,
"connect": { "sidecar_service": {} }
}
}
```

Then Consul will automatically generate a corresponding SidecarService with default configurations.

In Registrator, you may add the `"connect": { "sidecar_service": {} }` configuration by the `-service-mesh` flag. It's also overridable by `SERVICE_MESH`.

## Consul KV

consulkv://<address>:<port>/<prefix>
Expand Down
1 change: 1 addition & 0 deletions docs/user/run.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Option | Since | Description
`-ttl <seconds>` | | TTL for services. Default: 0, no expiry (supported backends only)
`-ttl-refresh <seconds>` | | Frequency service TTLs are refreshed (supported backends only)
`-useIpFromLabel <label>` | | Uses the IP address stored in the given label, which is assigned to a container, for registration with Consul
`-service-mesh` | | Enable basic ServiceMesh support in Consul

If the `-internal` option is used, Registrator will register the docker0
internal IP and port instead of the host mapped ones.
Expand Down
3 changes: 2 additions & 1 deletion docs/user/services.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ object into a particular registry.
Port int // port service is listening on
Tags []string // extra tags to classify service
Attrs map[string]string // extra attribute metadata
Mesh bool // Enable basic ServiceMesh support in Consul
}

## Container Overrides

The fields `Name`, `Tags`, `Attrs`, and `ID` can be overridden by user-defined
The fields `Name`, `Tags`, `Attrs`, `Mesh` and `ID` can be overridden by user-defined
container metadata. You can use environment variables or labels prefixed with
`SERVICE_` or `SERVICE_x_` to set values, where `x` is the internal exposed port.
For example `SERVICE_NAME=customerdb` and `SERVICE_80_NAME=api`.
Expand Down
2 changes: 2 additions & 0 deletions registrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var deregister = flag.String("deregister", "always", "Deregister exited services
var retryAttempts = flag.Int("retry-attempts", 0, "Max retry attempts to establish a connection with the backend. Use -1 for infinite retries")
var retryInterval = flag.Int("retry-interval", 2000, "Interval (in millisecond) between retry-attempts.")
var cleanup = flag.Bool("cleanup", false, "Remove dangling services")
var serviceMesh = flag.Bool("service-mesh", false, "Enable basic ServiceMesh support (for Consul only)")

func getopt(name, def string) string {
if env := os.Getenv(name); env != "" {
Expand Down Expand Up @@ -112,6 +113,7 @@ func main() {
RefreshInterval: *refreshInterval,
DeregisterCheck: *deregister,
Cleanup: *cleanup,
ServiceMesh: *serviceMesh,
})

assert(err)
Expand Down