Consul Exec
Command: consul exec
The exec
command provides a mechanism for remote execution. For example,
this can be used to run the uptime
command across all machines providing
the web
service.
Remote execution works by specifying a job, which is stored in the KV store. Agents are informed about the new job using the event system, which propagates messages via the gossip protocol. As a result, delivery is best-effort, and there is no guarantee of execution.
While events are purely gossip driven, remote execution relies on the KV store
as a message broker. As a result, the exec
command will not be able to
properly function during a Consul outage.
Verbose output warning: use care to make sure that your command does not produce a large volume of output. Writes to the KV store for this output go through the Consul servers and the Raft consensus algorithm, so having a large number of nodes in the cluster flow a large amount of data through the KV store could make the cluster unavailable.
The table below shows the required ACLs in order to execute this command.
ACL Required | Scope |
---|---|
agent:read | local agent |
session:write | local agent |
key:write | "_rexec" prefix |
event:write | "_rexec" prefix |
In addition to the above, the policy associated with the agent token should have write
on "_rexec"
key prefix. This policy permits agents to read the exec
command and write its output back to the KV store.
Usage
Usage: consul exec [options] [-|command...]
The only required option is a command to execute. This is either given
as trailing arguments, or by specifying -
; STDIN will be read to
completion as a script to evaluate.
Command Options
-prefix
- Key prefix in the KV store to use for storing request data. Defaults to_rexec
.-node
- Regular expression to filter nodes which should evaluate the event.-service
- Regular expression to filter to only nodes with matching services.-shell
- Optional, use a shell to run the command. The default value is true.-tag
- Regular expression to filter to only nodes with a service that has a matching tag. This must be used with-service
. As an example, you may do-service mysql -tag secondary
.-wait
- Specifies the period of time in which no agent's respond before considering the job finished. This is basically the quiescent time required to assume completion. This period is not a hard deadline, and the command will wait longer depending on various heuristics.-wait-repl
- Period to wait after writing the job specification for replication. This is a heuristic value and enables agents to do a stale read of the job. Defaults to 200 msec.-verbose
- Enables verbose output.
API Options
-ca-file=<value>
- Path to a CA file to use for TLS when communicating with Consul. This can also be specified via theCONSUL_CACERT
environment variable.-ca-path=<value>
- Path to a directory of CA certificates to use for TLS when communicating with Consul. This can also be specified via theCONSUL_CAPATH
environment variable.-client-cert=<value>
- Path to a client cert file to use for TLS whenverify_incoming
is enabled. This can also be specified via theCONSUL_CLIENT_CERT
environment variable.-client-key=<value>
- Path to a client key file to use for TLS whenverify_incoming
is enabled. This can also be specified via theCONSUL_CLIENT_KEY
environment variable.-http-addr=<addr>
- Address of the Consul agent with the port. This can be an IP address or DNS address, but it must include the port. This can also be specified via theCONSUL_HTTP_ADDR
environment variable. In Consul 0.8 and later, the default value is http://127.0.0.1:8500, and https can optionally be used instead. The scheme can also be set to HTTPS by setting the environment variableCONSUL_HTTP_SSL=true
. This may be a unix domain socket usingunix:///path/to/socket
if the agent is configured to listen that way.-tls-server-name=<value>
- The server name to use as the SNI host when connecting via TLS. This can also be specified via theCONSUL_TLS_SERVER_NAME
environment variable.-token=<value>
- ACL token to use in the request. This can also be specified via theCONSUL_HTTP_TOKEN
environment variable. If unspecified, the query will default to the token of the Consul agent at the HTTP address.-token-file=<value>
- File containing the ACL token to use in the request instead of one specified via the-token
argument orCONSUL_HTTP_TOKEN
environment variable. This can also be specified via theCONSUL_HTTP_TOKEN_FILE
environment variable.
-datacenter=<name>
- Name of the datacenter to query. If unspecified, the query will default to the datacenter of the Consul agent at the HTTP address.-stale
- Permit any Consul server (non-leader) to respond to this request. This allows for lower latency and higher throughput, but can result in stale data. This option has no effect on non-read operations. The default value is false.