Registering containers as separate nodes in consul
Is there a way for containerpilot to register a container as its own node in consul? Currently, containerpilot registers a service against a particular node in consul (given a clustered setting). If that consul node becomes unavailable, we lose service health visibility until that consul node is restored.
+1 For me this occurs when Triton containers are removed or fail. I will also see multiple containers on the same node within the consul UI.
Few days ago, I made some comtainer that include both the containerpilot and consul.
If you want register the container as a consul node, please try it.
In the dockerfile, entrypoint should contain some shell script that include running consul node for containerpilot's argument.
And the consul will execute itself as a foreground, so you can't execute some apllication what you want. So, you can use nohup for consul.
@jgillis01 someone at a asked me about this offline recently and I'm going to admit I'm a little unclear as to what we're doing wrong here. But obviously the behavior you're describing isn't ideal. We're currently using Agent.ServiceRegister with the Consul API client, which corresponds to /v1/service/agent/register.
Should we be using /v1/catalog/register (or in other words Catalog.Register)? The Consul documentation suggests otherwise but maybe I'm misreading it.
Note: it is usually preferrable instead to use the agent endpoints for registration as they are simpler and perform anti-entropy.
Do you have a minimal test case I could use to exercise the bad behavior so we can verify this?
@tgross,
The documentation seems to suggest that the agent endpoint is used to interact with a consul agent running locally on the node:
The Agent endpoints are used to interact with the local Consul agent.
I agree the catalog endpoint should not be used to register services due to the health check restrictions:
If the Check key is provided, a health check will also be registered. Note: this register API manipulates the health check entry in the Catalog, but it does not setup the script, TTL, or HTTP check to monitor the node's health. To truly enable a new health check, the check must either be provided in agent configuration or set via the agent endpoint.
The following steps can be used to reproduce the behavior I am experiencing:
- Create a consul cluster with 4 nodes (-bootstrap-expect 3).
- Run a service container with a containerpilot configuration pointing to the first consul node.
- Stop the first consul node.
Navigating to the consul ui on one of the other consul nodes, I notice the first consul node state is failing the serf health check, and the service health check still reports the last known health of the service. At this point, changes to the service container state are not seen by consul since containerpilot is trying to communicate (report health check results) with a non-existent consul node.
@jgillis01 I want to dive into this but I'll be on the road the next couple days so it might take until later this week to get back to you. In the meantime, are you deploying on Triton or on another platform?
@tgross I am deploying on Triton
@tgross - I think I had harassed you at ContainerSummit Vegas about this. tl;dr It's a SPOF that we would like to stomp out. We are in Joyent PC as well as our own SDC install on our gear. We've a team of people ready to work the issue, I just want to make sure we are working together in the right direction.
@zadunn @jgillis01 how are you discovering Consul? Is it via a Docker link or CNS name? If ContainerPilot is connecting to the Consul cluster via CNS service name (I'm playing with apps connected to consul.svc.d42e7882-89d2-459e-bc0a-e9af0bca409c.us-sw-1.cns.joyent.com now), it will connect to any available Consul instance.
Doesn't that eliminate the SPOF issues?
@misterbisson right now we're injecting the ip of the consul node as an env var into all the containerpilot containers. From a code perspective, we could easily switch that to injecting a CNS name. I don't really understand networking protocols in any detail, so @zdunn or @jgillis01 will need to answer the latter question for you. Would containerpilot connecting to any instance mean that the service information containerpilot registered on one node got "consensus'd" to the others? Or does it mean that if the node stuff is registered on fails, it won't have a hard time finding another? And if the latter, is containerpilot set up to know what to do in that situation?
@yarmiganosca ah, yes. Linking by IP number or Docker links can be problematic. Take a look at this alternative that uses Triton Container Name Service (CNS):
We give Consul a label that names the CNS service, triton.cns.services=consul. Here it is in the Compose file:
https://github.com/autopilotpattern/wordpress/blob/master/docker-compose.yml#L80
We can then set an environment var that identifies the Consul service by DNS. This is the pattern for our public cloud: CONSUL=consul.svc.${TRITON_ACCOUNT}.${TRITON_DC}.cns.joyent.com (that name points at the private interface). Here's where we generate it:
https://github.com/autopilotpattern/wordpress/blob/master/setup.sh#L176
Learn more about Triton CNS:
https://www.joyent.com/blog/introducing-triton-container-name-service
@misterbisson we're not using Docker Networking links. We're just injecting the env var into the container at runtime and interpolating it into the containerpilot config file. As to my questions about the HA properties of your solution, do you have any further information?
@misterbisson So our issue isn't so much of one at boot can we speak to consul. It's the fact that the services that are created by container pilot are pnned to a single consul node. So if that CONSUL node goes away, our service "fails". Which in the autopilot pattern is going to kick off a bunch of changes.
This is a lot different than the standard consul-template method of registering as a node in the cluster, then registering services onto yourself. With that pattern we are part of the cluster, and a consul server node failing doesn't cause a freak out (insert appropriate .wav here).
So we have two concerns:
- boot time, which using DNS/CNS helps with a lot.
- run time, which I am not sure we have a good answer for at this point. If we lose the consul node that we are registered on our service will be marked as unhealthy and removed from other services configurations.
EDIT: I think the obvious behaviour is to register a node per container and 1 service per port the container exposes. That way things will behave as expected, if the node goes down, all the associated services go down. If a consul node goes down, nothing should happen as the node information should be replicated. On a different note, you could also use Consul itself to resolve. This is what I've been doing, by setting CONSUL=https://consul.service.consul:8501 (When using a remote consul, I'd recommend using TLS and a token for authorization). This works once the consul cluster has quorum.
On reading the Consul documentation carefully, Consul expects to have local agents in each server/container/vm. I'm not sure there is a way to express the behaviour we want without running a consul agent in each container (which I did in my vault-containerpilot docker image). See: https://www.consul.io/docs/agent/basics.html
CONSUL AGENT
The Consul agent is the core process of Consul. The agent maintains membership information, registers services, runs checks, responds to queries, and more. The agent must run on every node that is part of a Consul cluster.
Any agent may run in one of two modes: client or server. A server node takes on the additional responsibility of being part of the consensus quorum. These nodes take part in Raft and provide strong consistency and availability in the case of failure. The higher burden on the server nodes means that usually they should be run on dedicated instances -- they are more resource intensive than a client node. Client nodes make up the majority of the cluster, and they are very lightweight as they interface with the server nodes for most operations and maintain very little state of their own.
What about registering all services in all Consul nodes? It is very hacky but will provide the desired resiliency.
I've had a chance to dig into this now and @zadunn has described the problem well at this point:
It's the fact that the services that are created by container pilot are pnned to a single consul node.
And I think @mterron has hit the root of the problem on the head:
Consul expects to have local agents in each server/container/vm. I'm not sure there is a way to express the behaviour we want without running a consul agent in each container
Although the data about the service is replicated via the raft to all nodes, Consul includes data about what agent you've registered with. So we need to come up with a workaround for that. I suspect but haven't proved yet that we can do this by using the catalog API directly. I'll be attempting this today and maybe try to reach out to some folks at Hashicorp to see what they would suggest, given that this is a non-standard topology for them.
@mterron wrote:
What about registering all services in all Consul nodes?
In the environment where this is a problem, we typically are going to be using CNS/DNS to reach the Consul cluster, so we probably don't have a good way to enumerate all the nodes.
@jgillis01 is trying an experiment using containerpilot to start consul agent then have containerpilot register to it locally. The dirty part is that we are starting consul agent and whatever else we want with supervisord. It works but again it feels dirty as hell.
We would also have issue of managing of all the parts needed for consul agent. Anyway, I think the issue is clear now!
Yeah another similar approach could be if ContainerPilot was acting as a Consul client node instead of as a HTTP client (this distinction is a little weird but check out the docs for details). We'd get the same effect, but we'd have special-cased Consul behaviors to an extent I'm not comfortable with for portability's sake. I'm currently digging into catalog API and seeing whether we can live without anti-entropy in this use case.
Edit: we wouldn't lose anti-entropy entirely, as the agents would still run it periodically depending on cluster size.
@tgross @zadunn I can confirm that having containerpilot start a supervisord process which manages a consul agent and service shows the desired behavior. From there you just point containerpilot to the agent running on the local node. Telemetry and health checks still work the same. If the service is terminated (not SIGTERM), supervisord will restart it.
Ok, good to hear that there's a workaround that works for you. Lots of folks don't want to run multi-process containers w/ a supervisor, though, so I'm still going to see if we can come up with a workaround inside the ContainerPilot.