{"id":7745,"date":"2018-10-04T08:46:04","date_gmt":"2018-10-04T00:46:04","guid":{"rendered":"http:\/\/rmohan.com\/?p=7745"},"modified":"2018-10-04T08:46:10","modified_gmt":"2018-10-04T00:46:10","slug":"how-to-fix-redis-cli-error-connection-reset-by-peer","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=7745","title":{"rendered":"How to Fix Redis CLI Error Connection Reset by Peer"},"content":{"rendered":"<h3>Overview<\/h3>\n<p>Recently we faced an issue with an\u00a0<strong>AWS ElastiCache Redis<\/strong>\u00a0instance when trying to test the connections from\u00a0<strong>EC2<\/strong>\u00a0<strong>Instance<\/strong>\u00a0using\u00a0<strong>Redis CLI<\/strong>, we faced the following error<\/p>\n<p>$ .\/redis-cli -c -h <span class=\"hljs-keyword\">my<\/span>-redis-server -p <span class=\"hljs-number\">6379<\/span> <span class=\"hljs-keyword\">my<\/span>-redis-server:<span class=\"hljs-number\">6379<\/span>&gt; set a <span class=\"hljs-string\">&#8220;hello&#8221;<\/span> Error: Connection <span class=\"hljs-keyword\">reset<\/span> by peer<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h3>Problem<\/h3>\n<p>On investigation, we found that the\u00a0<strong>ElastiCache Redis<\/strong>\u00a0Instance is using\u00a0<strong>Encryption in-transit<\/strong>\u00a0and\u00a0<strong>Encryption at-rest<\/strong>\u00a0and by design,\u00a0<strong>the Redis CLI is not compatible with the encryption.<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"http:\/\/rmohan.com\/wp-content\/uploads\/2018\/10\/redis-encryption.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7746\" src=\"http:\/\/rmohan.com\/wp-content\/uploads\/2018\/10\/redis-encryption.png\" alt=\"\" width=\"2774\" height=\"1026\" srcset=\"https:\/\/mohan.sg\/wp-content\/uploads\/2018\/10\/redis-encryption.png 2774w, https:\/\/mohan.sg\/wp-content\/uploads\/2018\/10\/redis-encryption-300x111.png 300w, https:\/\/mohan.sg\/wp-content\/uploads\/2018\/10\/redis-encryption-768x284.png 768w, https:\/\/mohan.sg\/wp-content\/uploads\/2018\/10\/redis-encryption-1024x379.png 1024w, https:\/\/mohan.sg\/wp-content\/uploads\/2018\/10\/redis-encryption-150x55.png 150w, https:\/\/mohan.sg\/wp-content\/uploads\/2018\/10\/redis-encryption-400x148.png 400w\" sizes=\"(max-width: 2774px) 100vw, 2774px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h3>Solution<\/h3>\n<p>The solution to test the connectivity and to use the\u00a0<strong>Redis CL<\/strong>I with ElastiCache In-Transit encryption,\u00a0 we needed to configure \u2018<a href=\"https:\/\/www.stunnel.org\/\" target=\"_blank\" rel=\"noopener\">stunnel<\/a>\u2019<\/p>\n<blockquote><p>Stunnel is a proxy designed to add TLS encryption functionality to existing clients and servers without any changes in the programs\u2019 code<\/p><\/blockquote>\n<p>With stunnel\u00a0client would create a SSL tunnel to the Redis nodes and use redis-cli to connect through the tunnel to access data from encrypted redis nodes.<\/p>\n<p>Here is how to setup everything, we are using Amazon Linux in this example but same steps should work on Redhat Linux<\/p>\n<p>1. Install stunnel<\/p>\n<pre><code class=\"hljs shell\"><span class=\"hljs-meta\">\r\n$<\/span><span class=\"bash\">\u00a0sudo yum install stunnel\u00a0 -y<\/span>\r\n<\/code><\/pre>\n<p>2. Configure SSL tunnel for redis-cli<\/p>\n<pre><code class=\"hljs shell\"><span class=\"hljs-meta\">\r\n$<\/span><span class=\"bash\"> sudo vi \/etc\/stunnel\/redis-cli.conf<\/span>\r\n<\/code> Set the following properties in redis-cli.conf file <code class=\"hljs ini\">\r\n<span class=\"hljs-attr\">fips<\/span> = <span class=\"hljs-literal\">no<\/span>\r\n<span class=\"hljs-attr\">setuid<\/span> = root\r\n<span class=\"hljs-attr\">setgid<\/span> = root\r\n<span class=\"hljs-attr\">pid<\/span> = \/var\/run\/stunnel.pid\r\n<span class=\"hljs-attr\">debug<\/span> = <span class=\"hljs-number\">7<\/span>\r\n<span class=\"hljs-attr\">options<\/span> = NO_SSLv2\r\n<span class=\"hljs-attr\">options<\/span> = NO_SSLv3\r\n<span class=\"hljs-section\">[redis-cli]<\/span>\r\n<span class=\"hljs-attr\">client<\/span> = <span class=\"hljs-literal\">yes<\/span>\r\n<span class=\"hljs-attr\">accept<\/span> = <span class=\"hljs-number\">127.0<\/span>.<span class=\"hljs-number\">0.1<\/span>:<span class=\"hljs-number\">6379<\/span>\r\n<span class=\"hljs-attr\">connect<\/span> = my-redis-server:<span class=\"hljs-number\">6379<\/span>\r\n<\/code><\/pre>\n<p>3. Start Stunnel<\/p>\n<pre><code class=\"hljs shell\"><span class=\"hljs-meta\">\r\n$<\/span><span class=\"bash\"> sudo stunnel \/etc\/stunnel\/redis-cli.conf<\/span>\r\n<\/code><\/pre>\n<p>4.\u00a0Verify the tunnel is running<\/p>\n<pre><code class=\"hljs shell\"><span class=\"hljs-meta\">\r\n$<\/span><span class=\"bash\"> sudo netstat -tulnp | grep -i stunnel<\/span>\r\n<\/code> You might see following output from the above command <code class=\"hljs css\">\r\n<span class=\"hljs-selector-tag\">tcp<\/span> 0 0 127<span class=\"hljs-selector-class\">.0<\/span><span class=\"hljs-selector-class\">.0<\/span><span class=\"hljs-selector-class\">.1<\/span><span class=\"hljs-selector-pseudo\">:6379<\/span> 0<span class=\"hljs-selector-class\">.0<\/span><span class=\"hljs-selector-class\">.0<\/span><span class=\"hljs-selector-class\">.0<\/span>:* <span class=\"hljs-selector-tag\">LISTEN<\/span> 1314 <span class=\"hljs-selector-tag\">stunnel<\/span>\r\n<\/code><\/pre>\n<p>5. Last is to connect to Redis cluster using Redis CLI using SSL tunnel (Yes it is connecting using localhost tunnel)<\/p>\n<pre><code class=\"hljs\">\r\nredis-cli -h localhost -p 6379\r\n<\/code><\/pre>\n<p>Note: To install Redis CLI on Linux check\u00a0<a href=\"https:\/\/docs.aws.amazon.com\/AmazonElastiCache\/latest\/red-ug\/GettingStarted.ConnectToCacheNode.html\" target=\"_blank\" rel=\"noopener\">this AWS documentation<\/a><\/p>\n<p>6. Run few Redis commands to see if it works<\/p>\n<pre><code class=\"hljs http\">\r\n\r\n<span class=\"makefile\">$ .\/redis-cli -h localhost -p 6379\r\n<span class=\"hljs-section\">localhost:6379&gt; set a hello<\/span>\r\nOK\r\n<span class=\"hljs-section\">localhost:6379&gt; get a<\/span>\r\n<span class=\"hljs-string\">\"hello\"<\/span>\r\n<span class=\"hljs-section\">localhost:6379&gt;<\/span>\r\n\r\n<\/span><\/code><\/pre>\n<p>Hope you find this post useful, please leave a comment and let us know what topics you would like to see.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Overview <\/p>\n<p>Recently we faced an issue with an AWS ElastiCache Redis instance when trying to test the connections from EC2 Instance using Redis CLI, we faced the following error<\/p>\n<p>$ .\/redis-cli -c -h my-redis-server -p 6379 my-redis-server:6379&gt; set a &#8220;hello&#8221; Error: Connection reset by peer<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p> Problem <\/p>\n<p>On investigation, we found that the ElastiCache [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7745"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7745"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7745\/revisions"}],"predecessor-version":[{"id":7747,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7745\/revisions\/7747"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7745"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7745"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7745"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}