Consul Connect Redirect Traffic
Command: consul connect redirect-traffic
The connect redirect-traffic command is used to apply traffic redirection rules to enforce all traffic to go through the Envoy proxy when using Consul service mesh in the Transparent Proxy mode.
This command requires iptables
command line utility to be installed,
and as a result, this command can currently only run on linux.
The user running the command needs to have NET_ADMIN
capability.
By default, this command will apply rules to intercept and redirect all inbound and outbound TCP traffic to the Envoy's inbound and outbound ports accordingly.
When proxy-id
is specified, additional exclusion rules will be applied based on proxy's
configuration stored in the local Consul agent. This includes redirecting to the proxy's
inbound and outbound ports specified in the service registration.
Usage
Usage: consul connect redirect-traffic [options]
Command Options
-node-name
- The node name where the proxy service is registered. It requires proxy-id to be specified. This is needed if running in an environment without client agents.-consul-dns-ip
- The IP address of the Consul DNS resolver. If provided, DNS queries will be redirected to the provided IP address for name resolution.-consul-dns-port
- The port of the Consul DNS resolver. If provided, DNS queries will be redirected to the provided IP address for name resolution.-proxy-id
- The proxy service ID. This service ID must already be registered with the local agent.-proxy-inbound-port
- The inbound port that the proxy is listening on.-proxy-outbound-port
- The outbound port that the proxy is listening on. When not provided, 15001 is used by default.-proxy-uid
- The user ID of the proxy to exclude from traffic redirection.-exclude-inbound-port
- Inbound port to exclude from traffic redirection. May be provided multiple times.-exclude-outbound-cidr
- Outbound CIDR to exclude from traffic redirection. May be provided multiple times.-exclude-outbound-port
- Outbound port to exclude from traffic redirection. May be provided multiple times.-exclude-uid
- Additional user ID to exclude from traffic redirection. May be provided multiple times.-netns
- The Linux network namespace where traffic redirection rules should apply. This must be a path to the network namespace, e.g. /var/run/netns/foo.
Enterprise Options
-partition=<string>
- Enterprise Specifies the admin partition to query. If not provided, the partition is inferred from the request's ACL token, or defaults to thedefault
partition.
-namespace=<string>
- Specifies the namespace to query. If not provided, the namespace will be inferred from the request's ACL token, or will default to thedefault
namespace. Namespaces are a Consul Enterprise feature added in v1.7.0.
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.
Examples
Basic Rules
The default traffic redirection rules can be applied with:
$ consul connect redirect-traffic \
-proxy-uid 1234 \
-proxy-inbound-port 20000
Using Registered Proxy Configuration
To automatically apply rules based on proxy's service registration, use the following command:
$ consul connect redirect-traffic -proxy-uid 1234 -proxy-id web
This command assumes that the proxy service is registered with the local agent and that the local agent is reachable.