github.com/outbrain/consul@v1.4.5/website/source/docs/guides/connect-envoy.md (about) 1 --- 2 layout: "docs" 3 page_title: "Using Envoy with Connect" 4 sidebar_current: "docs-guides-connect-envoy" 5 description: |- 6 This guide walks though getting started running Envoy as a Connect Proxy. 7 --- 8 9 # Using Connect with Envoy Proxy 10 11 Consul Connect has first class support for using 12 [Envoy](https://www.envoyproxy.io) as a proxy. This guide will walk through a 13 working example on a local development machine that shows the moving parts. 14 15 For reference documentation on how the integration works and is configured, 16 please see [Envoy](/docs/connect/proxies/envoy.html). 17 18 ## Setup Overview 19 20 This guide will describe how to setup a development-mode Consul server and two 21 Envoy proxies on a single machine using [Docker](https://www.docker.com/). The 22 aim is to demonstrate a minimal working setup and the moving parts involved. 23 24 We choose to run in Docker since Envoy is only distributed as a Docker image so 25 it's the quickest way to get a demo running. The same commands used here will 26 work in just the same way outside of Docker if you build an Envoy binary 27 yourself. 28 29 We'll start all containers using Docker's `host` network mode which is not a 30 realistic simulation of a production setup, but makes the following steps much 31 simpler. 32 33 We should end up with five containers running: 34 35 1. The Consul agent 36 2. An example TCP `echo` service as a destination 37 3. An Envoy sidecar proxy for the `echo` service 38 4. An Envoy sidecar proxy for the `client` service 39 5. An example `client` service (netcat) 40 41 ## Building an Envoy Image 42 43 Starting Envoy requires a bootstrap configuration file that points Envoy to the 44 local agent for discovering the rest of it's configuration. The Consul binary 45 includes the [`consul connect envoy` command](/docs/commands/connect/envoy.html) 46 which can generate the bootstrap configuration for Envoy and optionally run it 47 directly. 48 49 Envoy's official Docker image can be used with Connect directly however it 50 requires some additional steps to generate bootstrap configuration and inject it 51 into the container. 52 53 Instead, we'll use Docker multi-stage builds (added in version 17.05) to make a 54 local image that has both `envoy` and `consul` binaries. 55 56 We'll create a local Docker image to use that contains both binaries. First 57 create a `Dockerfile` containing the following: 58 59 ```sh 60 FROM consul:latest 61 FROM envoyproxy/envoy:v1.8.0 62 COPY --from=0 /bin/consul /bin/consul 63 ENTRYPOINT ["dumb-init", "consul", "connect", "envoy"] 64 ``` 65 66 This takes the Consul binary from the latest release image and copies it into a 67 new image based on the official Envoy image. 68 69 This can be built locally with: 70 71 ```sh 72 docker build -t consul-envoy . 73 ``` 74 75 We will use the `consul-envoy` image we just made to configure and run Envoy 76 processes later. 77 78 ## Consul Agent Setup 79 80 Next we need a Consul agent. We'll work with a single Consul agent in `-dev` 81 mode for simplicity. 82 83 -> **Note:** `-dev` mode enables the gRPC server on port 8502 by default. For a 84 production agent you'll need to [explicitly configure the gRPC 85 port](/docs/agent/options.html#grpc_port). 86 87 In order to start a proxy instance, a [proxy service 88 definition](/docs/connect/proxies.html) must exist on the local agent. We'll 89 create one using the [sidecar service 90 registration](/docs/connect/proxies/sidecar-service.html) syntax. 91 92 Create a config file called `envoy_demo.hcl` containing the following service 93 definitions. 94 95 ```hcl 96 services { 97 name = "client" 98 port = 8080 99 connect { 100 sidecar_service { 101 proxy { 102 upstreams { 103 destination_name = "echo" 104 local_bind_port = 9191 105 } 106 } 107 } 108 } 109 } 110 services { 111 name = "echo" 112 port = 9090 113 connect { 114 sidecar_service {} 115 } 116 } 117 ``` 118 119 The Consul agent container can now be started with that configuration. 120 121 ```sh 122 $ docker run --rm -d -v$(pwd)/envoy_demo.hcl:/etc/consul/envoy_demo.hcl \ 123 --network host --name consul-agent consul:latest \ 124 agent -dev -config-file /etc/consul/envoy_demo.hcl 125 1c90f7fcc83f5390332d7a4fdda2f1bf74cf62762de9ea2f67cd5a09c0573641 126 ``` 127 128 Running with `-d` like this puts the container into the background so we can 129 continue in the same terminal. Log output can be seen using the name we gave. 130 131 ```sh 132 docker logs -f consul-agent 133 ``` 134 135 Note that the Consul agent has registered two services `client` and `echo`, but 136 also registered two proxies `client-sidecar-proxy` and `echo-sidecar-proxy`. 137 Next we'll need to run those services and proxies. 138 139 ## Running the Echo Service 140 141 Next we'll run the `echo` service. We can use an existing tcp echo utility image 142 for this. 143 144 Start the echo service on port 9090 as registered before. 145 146 ```sh 147 $ docker run -d --network host abrarov/tcp-echo --port 9090 148 1a0b0c569016d00aadc4fc2b2954209b32b510966083f2a9e17d3afc6d185d87 149 ``` 150 151 ## Running the Proxies 152 153 We can now run "sidecar" proxy instances. 154 155 ```sh 156 $ docker run --rm -d --network host --name echo-proxy \ 157 consul-envoy -sidecar-for echo 158 3f213a3cf9b7583a194dd0507a31e0188a03fc1b6e165b7f9336b0b1bb2baccb 159 $ docker run --rm -d --network host --name client-proxy \ 160 consul-envoy -sidecar-for client -admin-bind localhost:19001 161 d8399b54ee0c1f67d729bc4c8b6e624e86d63d2d9225935971bcb4534233012b 162 ``` 163 164 The `-admin-bind` flag on the second proxy command is needed because both 165 proxies are running on the host network and so can't bind to the same port for 166 their admin API (which cannot be disabled). 167 168 Again we can see the output using docker logs. To see more verbose information 169 from Envoy you can add `-- -l debug` to the end of the commands above. This 170 passes the `-l` (log level) option directly through to Envoy. With debug level 171 logs you should see the config being delivered to the proxy in the output. 172 173 The [`consul connect envoy` command](/docs/commands/connect/envoy.html) here is 174 connecting to the local agent, getting the proxy configuration from the proxy 175 service registration and generating the required Envoy bootstrap configuration 176 before `exec`ing the envoy binary directly to run it with the generated 177 configuration. 178 179 Envoy uses the bootstrap configuration to connect to the local agent directly 180 via gRPC and use it's xDS protocol to retrieve the actual configuration for 181 listeners, TLS certificates, upstream service instances and so on. The xDS API 182 allows the Envoy instance to watch for any changes so certificate rotations or 183 changes to the upstream service instances are immediately sent to the proxy. 184 185 ## Running the Client 186 187 Finally, we can see the connectivity by running a dummy "client" service. Rather 188 than run a full service that itself can listen, we'll simulate the service with 189 a simple netcat process that will only talk to the `client-sidecar-proxy` Envoy 190 instance. 191 192 Recall that we configured the `client` sidecar with one declared "upstream" 193 dependency (the `echo` service). In that declaration we also requested that the 194 `echo` service should be exposed to the client on local port 9191. 195 196 This configuration causes the `client-sidecar-proxy` to start a TCP proxy 197 listening on `localhost:9191` and proxying to the `echo` service. Importantly, 198 the listener will use the correct `client` service mTLS certificate to authorize 199 the connection. It discovers the IP addresses of instances of the echo service 200 via Consul service discovery. 201 202 We can now see this working if we run netcat. 203 204 ```sh 205 $ docker run -ti --rm --network host gophernet/netcat localhost 9191 206 Hello World! 207 Hello World! 208 ^C 209 ``` 210 211 ## Testing Authorization 212 213 To demonstrate that Connect is controlling authorization for the echo service, 214 we can add an explicit deny rule. 215 216 ```sh 217 $ docker run -ti --rm --network host consul:latest intention create -deny client echo 218 Created: client => echo (deny) 219 ``` 220 221 Now, new connections will be denied. Depending on a few factors, netcat may not 222 see the connection being closed but will not get a response from the service. 223 224 ```sh 225 $ docker run -ti --rm --network host gophernet/netcat localhost 9191 226 Hello? 227 Anyone there? 228 ^C 229 ``` 230 231 -> **Note:** Envoy will not currently re-authenticate already established TCP 232 connections so if you still have the netcat terminal open from before, that will 233 still be able to communicate with "echo". _New_ connections should be denied 234 though. 235 236 Removing the intention restores connectivity. 237 238 ``` 239 $ docker run -ti --rm --network host consul:latest intention delete client echo 240 Intention deleted. 241 $ docker run -ti --rm --network host gophernet/netcat localhost 9191 242 Hello? 243 Hello? 244 ^C 245 ``` 246 247 ## Summary 248 249 In this guide we walked through getting a minimal working example of two plain 250 TCP processes communicating over mTLS using Envoy sidecars configured by 251 Connect. 252 253 For more details on how the Envoy integration works, please see the [Envoy 254 reference documentation](/docs/connect/proxies/envoy.html). 255 256 To see how to get Consul Connect working in different environments like 257 Kubernetes see the [Connect Getting 258 Started](/docs/connect/index.html#getting-started-with-connect) overview.