<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Rafael Pirolla on Medium]]></title>
        <description><![CDATA[Stories by Rafael Pirolla on Medium]]></description>
        <link>https://medium.com/@pirolla?source=rss-525e48529c03------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*LX6IvDH5fkAMhIc1.jpg</url>
            <title>Stories by Rafael Pirolla on Medium</title>
            <link>https://medium.com/@pirolla?source=rss-525e48529c03------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 10:15:33 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@pirolla/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Consul Connect + Envoy]]></title>
            <link>https://pirolla.medium.com/consul-connect-envoy-36ebee5f8f6b?source=rss-525e48529c03------2</link>
            <guid isPermaLink="false">https://medium.com/p/36ebee5f8f6b</guid>
            <category><![CDATA[connect]]></category>
            <category><![CDATA[envoy-proxy]]></category>
            <category><![CDATA[consul]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[service-mesh]]></category>
            <dc:creator><![CDATA[Rafael Pirolla]]></dc:creator>
            <pubDate>Thu, 04 Apr 2019 08:43:17 GMT</pubDate>
            <atom:updated>2022-06-23T08:22:14.696Z</atom:updated>
            <content:encoded><![CDATA[<h3>Connect + Envoy</h3><p><a href="https://www.consul.io/docs/guides/connect-envoy.html">Using Envoy with Connect - Consul by HashiCorp</a></p><p>This tutorial is a working example of consul connect using envoy to load balance traffic to a redis cluster. I feel it&#39;s a more real life scenario then Hashicorp&#39;s example and this one&#39;s working on Docker for Mac.</p><h4>Building docker image with needed binaries</h4><p>As we need to run envoy through consul the docker image should have both binaries available. Also, we will need a consul agent in the same container as redis — let&#39;s just make one image to rule them all.</p><p>We will use envoy 1.9.1 to be able to use redis filter with dynamic cluster. As a side note envoy 1.10 is not yet supported by consul (<a href="https://github.com/hashicorp/consul/issues/5323">#5323</a>) [20190403].</p><p>Create the Dockerfile:</p><pre>FROM consul<br>FROM envoyproxy/envoy-alpine:v1.9.1<br>FROM redis<br>COPY --from=0 /bin/consul /usr/local/bin/consul<br>COPY --from=1 /usr/local/bin/envoy /usr/local/bin/envoy</pre><pre>RUN set -ex; \<br> apt-get update; \<br> apt-get install -y --no-install-recommends dumb-init curl iproute2 gawk vim; \<br> rm -rf /var/lib/apt/lists/*</pre><pre>COPY docker-entrypoint.sh /usr/local/bin/</pre><pre>ENTRYPOINT [&quot;dumb-init&quot;, &quot;docker-entrypoint.sh&quot;]</pre><p>And the docker-entrypoint.sh:</p><pre>#!/bin/sh<br>set -e</pre><pre>redis-server &amp;</pre><pre># generate random id<br>UUID=$(cat /dev/urandom | tr -dc &#39;a-z0-9&#39; | fold -w 8 | head -n 1)</pre><pre>CONSUL_DATA_DIR=/consul/data<br>CONSUL_CONFIG_DIR=/consul/config<br>CONSUL_BIND_ADDRESS=$(ip -o -4 addr list eth0 | head -n1 | awk &#39;{print $4}&#39; | cut -d/ -f1)</pre><pre>mkdir -p ${CONSUL_DATA_DIR} ${CONSUL_CONFIG_DIR}</pre><pre># run a local agent<br>consul agent -bind=${CONSUL_BIND_ADDRESS} -grpc-port=8502 -data-dir=&quot;$CONSUL_DATA_DIR&quot; -config-dir=&quot;$CONSUL_CONFIG_DIR&quot; -join=consul-server &amp;<br>echo &quot;Ran consul&quot;<br>sleep 15s</pre><pre># create the service definition with the envoy config<br>cat &lt;&lt;EOF &gt;redis.json<br>{<br>  &quot;name&quot;: &quot;redis&quot;,<br>  &quot;id&quot;: &quot;${UUID}&quot;,<br>  &quot;address&quot;: &quot;127.0.0.1&quot;,<br>  &quot;port&quot;: 6379,<br>  &quot;connect&quot;: {<br>    &quot;sidecar_service&quot;: {<br>      &quot;port&quot;: 6000,<br>      &quot;proxy&quot;: {<br>        &quot;destination_service_name&quot;: &quot;redis&quot;,<br>        &quot;destination_service_id&quot;: &quot;${UUID}&quot;,<br>        &quot;local_service_address&quot;: &quot;127.0.0.1&quot;,<br>        &quot;local_service_port&quot;: 6379,<br>        &quot;config&quot;: {<br>          &quot;envoy_public_listener_json&quot;: &quot;<br>          {<br>            \&quot;<a href="http://twitter.com/type">@type</a>\&quot;: \&quot;type.googleapis.com/envoy.api.v2.Listener\&quot;,<br>            \&quot;name\&quot;: \&quot;public_listener:${CONSUL_BIND_ADDRESS}:6000\&quot;,<br>            \&quot;address\&quot;: {<br>              \&quot;socketAddress\&quot;: {<br>                \&quot;address\&quot;: \&quot;0.0.0.0\&quot;,<br>                \&quot;portValue\&quot;: 6000<br>              }<br>            },<br>            \&quot;filterChains\&quot;: [<br>              {<br>                \&quot;filters\&quot;: [<br>                  {<br>                    \&quot;name\&quot;: \&quot;envoy.redis_proxy\&quot;,<br>                    \&quot;config\&quot;: {<br>                      \&quot;stat_prefix\&quot;: \&quot;ingress_redis\&quot;,<br>                      \&quot;cluster\&quot;: \&quot;local_app\&quot;,<br>                      \&quot;settings\&quot;: {<br>                        \&quot;op_timeout\&quot;: \&quot;5s\&quot;<br>                      }<br>                    }<br>                  }<br>                ]<br>              }<br>            ]<br>          }&quot;<br>        }<br>      }<br>    }<br>  }<br>}<br>EOF</pre><pre># register the service<br># JSON strings cannot have line breaks so we just make it a one liner<br>tr -d &#39;\n&#39; &lt; redis.json | curl -s -X PUT -d @- <a href="http://localhost:8500/v1/agent/service/register">http://localhost:8500/v1/agent/service/register</a><br>echo &quot;Registered&quot;<br>sleep 10s</pre><pre># run consul connect<br>export CONSUL_HTTP_ADDR=&quot;<a href="http://localhost:8500/">http://localhost:8500</a>&quot;<br>export CONSUL_GRPC_ADDR=&quot;localhost:8502&quot;</pre><pre># we expect the ADMIN_PORT to be defined as docker --env<br>consul connect envoy -sidecar-for ${UUID} -admin-bind 0.0.0.0:${ADMIN_PORT} -- -l debug</pre><p>Build the image:</p><pre>$ docker build -t redis-envoy .</pre><h4>Start a consul server</h4><p>We need a bridge network to take advantage of DNS aliases:</p><pre>$ docker network create --driver bridge consul-net</pre><p>Start the consul server:</p><pre>$ docker run --rm -d -p 8500:8500 --name consul-server --network consul-net -e CONSUL_BIND_INTERFACE=eth0 consul agent -dev -client=0.0.0.0 -ui</pre><p>You can access <a href="http://localhost:8500/">http://localhost:8500</a> now.</p><h4>Starting the redis envoy cluster</h4><p>We will start three instances of redis but we could add and remove instances at will (given you know what you are doing).</p><p>There is a side note further below about redis cluster vs envoy redis cluster.</p><pre>$ docker run --rm -d --network consul-net -p 19001:19001 --env &quot;ADMIN_PORT=19001&quot; --name redis01 redis-envoy<br>$ docker run --rm -d --network consul-net -p 19002:19002 --env &quot;ADMIN_PORT=19002&quot; --name redis02 redis-envoy<br>$ docker run --rm -d --network consul-net -p 19003:19003 --env &quot;ADMIN_PORT=19003&quot; --name redis03 redis-envoy</pre><h4>Creating a client to envoy redis cluster</h4><p>We have the instances of redis available with a sidecar proxy running. How to access them through consul connect? We will create a client service definition and use redis-cli to connect to the local port that will communicate with the instances.</p><p>Let&#39;s start with a shell inside a redis-envoy container:</p><pre>$ docker run -p 19000:19000 --rm -it --network consul-net --entrypoint /bin/bash --name redis-client redis-envoy</pre><p>Create the dummy-client.json file:</p><pre>{<br>  &quot;name&quot;: &quot;dummy-client&quot;,<br>  &quot;id&quot;: &quot;dummy-client&quot;,<br>  &quot;connect&quot;: {<br>    &quot;sidecar_service&quot;: {<br>    &quot;port&quot;: 8282,<br>      &quot;proxy&quot;: {<br>        &quot;destination_service_name&quot;: &quot;dummy-client&quot;,<br>        &quot;upstreams&quot;: [<br>          {<br>            &quot;destination_name&quot;: &quot;redis&quot;,<br>            &quot;local_bind_address&quot;: &quot;127.0.0.1&quot;,<br>            &quot;local_bind_port&quot;: 8383,<br>            &quot;config&quot;: {<br>              &quot;envoy_cluster_json&quot;: &quot;<br>              {<br>                \&quot;<a href="http://twitter.com/type">@type</a>\&quot;: \&quot;type.googleapis.com/envoy.api.v2.Cluster\&quot;,<br>                \&quot;name\&quot;: \&quot;redis\&quot;,<br>                \&quot;type\&quot;: \&quot;EDS\&quot;,<br>                \&quot;lb_policy\&quot;: \&quot;MAGLEV\&quot;,<br>                \&quot;eds_cluster_config\&quot;: {<br>                  \&quot;eds_config\&quot;: {<br>                    \&quot;ads\&quot;: {}<br>                  }<br>                },<br>                \&quot;connect_timeout\&quot;: \&quot;5s\&quot;,<br>                \&quot;outlier_detection\&quot;: {<br>                  \&quot;consecutive_5xx\&quot;: 5,<br>                  \&quot;base_ejection_time\&quot;: \&quot;30s\&quot;<br>                }<br>              }&quot;,<br>              &quot;envoy_listener_json&quot;: &quot;<br>              {<br>                \&quot;<a href="http://twitter.com/type">@type</a>\&quot;: \&quot;type.googleapis.com/envoy.api.v2.Listener\&quot;,<br>                \&quot;name\&quot;: \&quot;redis:127.0.0.1:8383\&quot;,<br>                \&quot;address\&quot;: {<br>                  \&quot;socketAddress\&quot;: {<br>                    \&quot;address\&quot;: \&quot;127.0.0.1\&quot;,<br>                    \&quot;portValue\&quot;: 8383<br>                  }<br>                },<br>                \&quot;filterChains\&quot;: [<br>                  {<br>                    \&quot;filters\&quot;: [<br>                      {<br>                        \&quot;name\&quot;: \&quot;envoy.redis_proxy\&quot;,<br>                        \&quot;config\&quot;: {<br>                          \&quot;stat_prefix\&quot;: \&quot;egress_redis\&quot;,<br>                          \&quot;cluster\&quot;: \&quot;redis\&quot;,<br>                          \&quot;settings\&quot;: {<br>                            \&quot;op_timeout\&quot;: \&quot;5s\&quot;<br>                          }<br>                        }<br>                      }<br>                    ]<br>                  }<br>                ]<br>              }&quot;<br>            }<br>          }<br>        ]<br>      }<br>    }<br>  }<br>}</pre><p>Notice that I am using the maglev lb_type for the redis cluster (we could also use ring_hash) and redis filter for the upstream listener port.</p><p>Now we need:</p><ol><li>a local consul agent</li><li>register the service</li><li>run envoy through consul connect</li></ol><p>Here are the commands:</p><pre>CONSUL_DATA_DIR=/consul/data<br>CONSUL_CONFIG_DIR=/consul/config<br>CONSUL_BIND_ADDRESS=$(ip -o -4 addr list eth0 | head -n1 | awk &#39;{print $4}&#39; | cut -d/ -f1)</pre><pre>mkdir -p ${CONSUL_DATA_DIR} ${CONSUL_CONFIG_DIR}</pre><pre># run a local agent<br>consul agent -bind=${CONSUL_BIND_ADDRESS} -grpc-port=8502 -data-dir=&quot;$CONSUL_DATA_DIR&quot; -config-dir=&quot;$CONSUL_CONFIG_DIR&quot; -join=consul-server &amp;</pre><pre># register the service<br>tr -d &#39;\n&#39; &lt; dummy-client.json | curl -s -X PUT -d @- <a href="http://localhost:8500/v1/agent/service/register">http://localhost:8500/v1/agent/service/register</a></pre><pre># then finally run envoy<br>consul connect envoy -sidecar-for dummy-client -admin-bind 0.0.0.0:19000 &amp;</pre><p>We can now connect to the envoy redis cluster through the local upstream port that we defined in the service definition:</p><pre>$ redis-cli -p 8383<br>127.0.0.1:8383&gt; set bar foo<br>OK<br>127.0.0.1:8383&gt; get foo<br>&quot;bar&quot;<br>127.0.0.1:8383&gt; get bar<br>&quot;foo&quot;</pre><p>You can check envoy stats at <a href="http://localhost:19002/stats?usedonly">http://localhost:1900[0–3]/stats?usedonly</a>.</p><h3>Side notes</h3><h4>Envoy redis cluster vs redis cluster</h4><p>As per envoy documentation:</p><blockquote>Envoy can act as a Redis proxy, partitioning commands among instances in a cluster.</blockquote><p>This means that envoy itself will choose the redis server (available in the envoy cluster definition) to access using the lb_type hash of your choosing. You can use redis cluster (i.e. redis-cli — cluster create …) or envoy redis cluster; not both.</p><p>Here is a good article about it:</p><p><a href="http://dmitrypol.github.io/redis/2019/03/18/envoy-proxy.html">http://dmitrypol.github.io/redis/2019/03/18/envoy-proxy.html</a></p><h4>Consul JSON service files</h4><p>There are two ways to register services in consul:</p><ol><li>Through config file (JSON or HCL) and sending consul process a HUP</li><li>Through the agent/service API</li></ol><p>I used option 2 in this documentation.</p><p>The configuration files can have top level &quot;service&quot; or &quot;services&quot; definition. Through the API we cannot and therefore we need to specify one service per JSON file.</p><p>There are two kind of services: connect-proxy and service. You can define them separately or you can embed the connect-proxy definition inside the service.</p><p>Here is the separate version:</p><p>redis01.json</p><pre>{<br>  &quot;name&quot;: &quot;redis&quot;,<br>  &quot;id&quot;: &quot;redis01&quot;,<br>  &quot;port&quot;: 7001<br>}</pre><p>redis01-proxy.json</p><pre>{<br>  &quot;name&quot;: &quot;redis01-proxy&quot;,<br>  &quot;port&quot;: 6001,<br>  &quot;kind&quot;: &quot;connect-proxy&quot;,<br>  &quot;proxy&quot;: {<br>    &quot;destination_service_name&quot;: &quot;redis&quot;,<br>    &quot;destination_service_id&quot;: &quot;redis01&quot;,<br>    &quot;local_service_address&quot;: &quot;127.0.0.1&quot;,<br>    &quot;local_service_port&quot;: 7001<br>  }<br>}</pre><p>We can merge them together like this:</p><p>redis01.json</p><pre>{<br>  &quot;name&quot;: &quot;redis&quot;,<br>  &quot;id&quot;: &quot;redis01&quot;,<br>  &quot;port&quot;: 7001<br>  &quot;connect&quot;: {<br>    &quot;sidecar_service&quot;: {<br>      &quot;port&quot;: 6001,<br>      &quot;proxy&quot;: {<br>        &quot;destination_service_name&quot;: &quot;redis&quot;,<br>        &quot;destination_service_id&quot;: &quot;redis01&quot;,<br>        &quot;local_service_address&quot;: &quot;127.0.0.1&quot;,<br>        &quot;local_service_port&quot;: 7001<br>      }<br>    }<br>  }<br>}</pre><h4>Envoy Filters</h4><p>If you use the last service definition you will get only basic TCP routing and metrics from envoy. Since we are using redis we can take advantage of the redis filter from envoy — we need it for the cluster, of course.</p><p>As of today consul provides “escape hatches&quot; to inject envoy configurations. The last service definition with the redis filter in place would then become:</p><p>redis01-filter.json</p><pre>{<br>  &quot;name&quot;: &quot;redis&quot;,<br>  &quot;id&quot;: &quot;redis01&quot;,<br>  &quot;port&quot;: 7001,<br>  &quot;connect&quot;: {<br>    &quot;sidecar_service&quot;: {<br>      &quot;port&quot;: 6001,<br>      &quot;proxy&quot;: {<br>        &quot;destination_service_name&quot;: &quot;redis&quot;,<br>        &quot;destination_service_id&quot;: &quot;redis01&quot;,<br>        &quot;local_service_address&quot;: &quot;172.18.0.2&quot;,<br>        &quot;local_service_port&quot;: 7001<br>      },<br>      &quot;config&quot;: {<br>          &quot;envoy_public_listener_json&quot;: &quot;<br>          {<br>            \&quot;<a href="http://twitter.com/type">@type</a>\&quot;: \&quot;type.googleapis.com/envoy.api.v2.Listener\&quot;,<br>            \&quot;name\&quot;: \&quot;public_listener:0.0.0.0:6001\&quot;,<br>            \&quot;address\&quot;: {<br>              \&quot;socketAddress\&quot;: {<br>                \&quot;address\&quot;: \&quot;0.0.0.0\&quot;,<br>                \&quot;portValue\&quot;: 6001<br>              }<br>            },<br>            \&quot;filterChains\&quot;: [<br>              {<br>                \&quot;filters\&quot;: [<br>                  {<br>                    \&quot;name\&quot;: \&quot;envoy.redis_proxy\&quot;,<br>                    \&quot;config\&quot;: {<br>                      \&quot;stat_prefix\&quot;: \&quot;ingress_redis\&quot;,<br>                      \&quot;cluster\&quot;: \&quot;local_app\&quot;,<br>                      \&quot;settings\&quot;: {<br>                        \&quot;op_timeout\&quot;: \&quot;5s\&quot;<br>                      }<br>                    }<br>                  }<br>                ]<br>              }<br>            ]<br>          }&quot;<br>      }<br>    }<br>  }<br>}</pre><h4>Hyperkit</h4><p>To spin up a shell inside the hyperkit VM in MacOS:</p><p>docker run -it — rm — privileged — pid=host busybox nsenter -t1 -m -u -i -n</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=36ebee5f8f6b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>