github.com/outbrain/consul@v1.4.5/website/source/intro/getting-started/connect.html.md (about) 1 --- 2 layout: "intro" 3 page_title: "Consul Connect" 4 sidebar_current: "gettingstarted-connect" 5 description: |- 6 Connect is a feature of Consul that provides service-to-service connection authorization and encryption using mutual TLS. This ensures that all service communication in your datacenter is encrypted and that the rules of what services can communicate is centrally managed with Consul. 7 --- 8 9 # Connect 10 11 We've now registered our first service with Consul and we've shown how you 12 can use the HTTP API or DNS interface to query the address and directly connect 13 to that service. Consul also provides a feature called **Connect** for 14 automatically connecting via an encrypted TLS connection and authorizing 15 which services are allowed to connect to each other. 16 17 Applications do not need to be modified at all to use Connect. 18 [Sidecar proxies](/docs/connect/proxies.html) can be used 19 to automatically establish TLS connections for inbound and outbound connections 20 without being aware of Connect at all. Applications may also 21 [natively integrate with Connect](/docs/connect/native.html) 22 for optimal performance and security. 23 24 -> **Security note:** The getting started guide will show Connect features and 25 focus on ease of use with a dev-mode agent. We will _not setup_ Connect in a 26 production-recommended secure way. Please read the [Connect production 27 guide](/docs/guides/connect-production.html) to understand the tradeoffs. 28 29 ## Starting a Connect-unaware Service 30 31 Let's begin by starting a service that is unaware of Connect. To keep it simple, 32 let's just use `socat` to start a basic echo service. This service will accept 33 TCP connections and echo back any data sent to it. If `socat` isn't installed on 34 your machine, it should be easily available via a package manager. 35 36 ```sh 37 $ socat -v tcp-l:8181,fork exec:"/bin/cat" 38 ``` 39 40 You can verify it is working by using `nc` to connect directly to it. Once 41 connected, type some text and press enter. The text you typed should be 42 echoed back: 43 44 ``` 45 $ nc 127.0.0.1 8181 46 hello 47 hello 48 echo 49 echo 50 ``` 51 52 `socat` is a decades-old Unix utility and our process is configured to 53 only accept a basic TCP connection. It has no concept of encryption, the 54 TLS protocol, etc. This can be representative of an existing service in 55 your datacenter such as a database, backend web service, etc. 56 57 ## Registering the Service with Consul and Connect 58 59 Next, let's register the service with Consul. We'll do this by writing 60 a new service definition. This is the same as the previous step in the 61 getting started guide, except this time we'll also configure Connect. 62 63 ```sh 64 $ cat <<EOF | sudo tee /etc/consul.d/socat.json 65 { 66 "service": { 67 "name": "socat", 68 "port": 8181, 69 "connect": { "sidecar_service": {} } 70 } 71 } 72 EOF 73 ``` 74 75 After saving this, run `consul reload` or send a `SIGHUP` signal to Consul 76 so it reads the new configuration. 77 78 Notice the only difference is the line starting with `"connect"`. The existence 79 of this empty configuration notifies Consul to register a sidecar proxy for this 80 process. The proxy process represents that specific service. It accepts inbound 81 connections on a dynamically allocated port, verifies and authorizes the TLS 82 connection, and proxies back a standard TCP connection to the process. 83 84 The sidecar service registration here is just telling Consul that a proxy should 85 be running, Consul won't actually run a proxy process for you. 86 87 We need to start the proxy process in another terminal: 88 89 ```sh 90 $ consul connect proxy -sidecar-for socat 91 ==> Consul Connect proxy starting... 92 Configuration mode: Agent API 93 Sidecar for ID: socat 94 Proxy ID: socat-sidecar-proxy 95 96 ... 97 ``` 98 99 ## Connecting to the Service 100 101 Next, let's connect to the service. We'll first do this by using the `consul 102 connect proxy` command again directly. This time we use the command to configure 103 and run a local proxy that can represent a service. This is a useful tool for 104 development since it'll let you masquerade as any service (that you have 105 permissions for) and establish connections to other services via Connect. 106 107 The command below starts a proxy representing a service "web". We request 108 an upstream dependency of "socat" (the service we previously registered) 109 on port 9191. With this configuration, all TCP connections to 9191 will 110 perform service discovery for a Connect-capable "socat" endpoint and establish 111 a mutual TLS connection identifying as the service "web". 112 113 ```sh 114 $ consul connect proxy -service web -upstream socat:9191 115 ==> Consul Connect proxy starting... 116 Configuration mode: Flags 117 Service: web 118 Upstream: socat => :9191 119 Public listener: Disabled 120 121 ... 122 ``` 123 124 With that running, we can verify it works by establishing a connection: 125 126 ``` 127 $ nc 127.0.0.1 9191 128 hello 129 hello 130 ``` 131 132 **The connection between proxies is now encrypted and authorized.** 133 We're now communicating to the "socat" service via a TLS connection. 134 The local connections to/from the proxy are unencrypted, but in production 135 these will be loopback-only connections. Any traffic in and out of the 136 machine is always encrypted. 137 138 ## Registering a Dependent Service 139 140 We previously established a connection by directly running `consul connect 141 proxy` in developer mode. Realistically, services need to establish connections 142 to dependencies over Connect. Let's register a service "web" that registers 143 "socat" as an upstream dependency in it's sidecar registration: 144 145 ```sh 146 $ cat <<EOF | sudo tee /etc/consul.d/web.json 147 { 148 "service": { 149 "name": "web", 150 "port": 8080, 151 "connect": { 152 "sidecar_service": { 153 "proxy": { 154 "upstreams": [{ 155 "destination_name": "socat", 156 "local_bind_port": 9191 157 }] 158 } 159 } 160 } 161 } 162 } 163 EOF 164 ``` 165 166 This registers a sidecar proxy for the service "web" that 167 should listen on port 9191 to establish connections to "socat" as "web". The 168 "web" service should then use that local port to talk to socat rather than 169 directly attempting to connect. 170 171 With that file in place, use `consul reload` or SIGHUP to reload Consul. If the 172 proxy command from the previous section (with the inline upstream listener) is 173 still running, stop it with `Ctrl-C`. Now we can start the web proxy using the 174 configuration from the sidecar registration as we did for socat. 175 176 ```sh 177 $ consul connect proxy -sidecar-for web 178 ==> Consul Connect proxy starting... 179 Configuration mode: Agent API 180 Sidecar for ID: web 181 Proxy ID: web-sidecar-proxy 182 183 ==> Log data will now stream in as it occurs: 184 185 2018/10/09 12:34:20 [INFO] 127.0.0.1:9191->service:default/socat starting on 127.0.0.1:9191 186 2018/10/09 12:34:20 [INFO] Proxy loaded config and ready to serve 187 2018/10/09 12:34:20 [INFO] TLS Identity: spiffe://df34ef6b-5971-ee61-0790-ca8622c3c287.consul/ns/default/dc/dc1/svc/web 188 2018/10/09 12:34:20 [INFO] TLS Roots : [Consul CA 7] 189 ``` 190 191 Note in the first log line that the proxy discovered its configuration from the 192 local agent and setup a local listener on port 9191 that will proxy to the socat 193 service just as we configured in the sidecar registration. 194 195 You can also see the identity URL from the certificate it loaded from the agent 196 identifying it as the "web" service and the set of trusted root CAs it knows 197 about. 198 199 -> **Security note:** The Connect security model requires trusting 200 loopback connections when proxies are in use. To further secure this, 201 tools like network namespacing may be used. 202 203 We can verify it works by establishing a new connection: 204 205 ``` 206 $ nc 127.0.0.1 9191 207 hello 208 hello 209 ``` 210 211 ## Controlling Access with Intentions 212 213 Intentions are used to define which services may communicate. Our connections 214 above succeeded because in a development mode agent, the ACL system is "allow 215 all" by default. 216 217 Let's insert a rule to deny access from web to socat: 218 219 ```sh 220 $ consul intention create -deny web socat 221 Created: web => socat (deny) 222 ``` 223 224 With the proxy processes running that we setup previously, connection 225 attempts now fail: 226 227 ```sh 228 $ nc 127.0.0.1 9191 229 $ 230 ``` 231 232 Try deleting the intention and attempt the connection again. 233 234 ```sh 235 $ consul intention delete web socat 236 Intention deleted. 237 $ nc 127.0.0.1 9191 238 hello 239 hello 240 ``` 241 242 Intentions allow services to be segmented via a centralized control plane 243 (Consul). To learn more, read the reference documentation on 244 [intentions](/docs/connect/intentions.html). 245 246 Note that in the current release of Consul, changing intentions will not 247 affect existing connections. Therefore, you must establish a new connection 248 to see the effects of a changed intention. This will be addressed in the near 249 term in a future version of Consul. 250 251 ## Discover More Connect 252 253 This quick guide has given a taste of what Connect can do but there is much 254 more. Take a look at [getting started with 255 Connect](/docs/connect/index.html#getting-started-with-connect) for more guides 256 on setting up Connect with Envoy proxy, with Docker and in Kubernetes. 257 258 ## Next Steps 259 260 We've now configured a service on a single agent and used Connect for 261 automatic connection authorization and encryption. This is a great feature 262 highlight but let's explore the full value of Consul by [setting up our 263 first cluster](/intro/getting-started/join.html)!