{"id":2321,"date":"2017-05-22T05:50:45","date_gmt":"2017-05-22T05:50:45","guid":{"rendered":"https:\/\/intelligentbee.com\/blog\/?p=2321"},"modified":"2024-11-28T14:03:07","modified_gmt":"2024-11-28T14:03:07","slug":"emulating-redis-failover-docker","status":"publish","type":"post","link":"https:\/\/intelligentbee.com\/blog\/emulating-redis-failover-docker\/","title":{"rendered":"Emulate a Redis Failover with Docker"},"content":{"rendered":"<p>Reading the Redis documentation can be a bit confusing without the hands-on experience. You could in theory create multiple processes of the Redis Server on your machine and configure each of them in part, but what if you could do it in a few commands? Not only that but emulate the network they\u2019re connected to as well.<\/p>\n<p>I\u2019ve been looking into this and there\u2019s a few examples out there on Web, the best one I could find was this one: <a href=\"https:\/\/github.com\/AliyunContainerService\/redis-cluster\">https:\/\/github.com\/AliyunContainerService\/redis-cluster<\/a><\/p>\n<p>So, starting from that example, I\u2019ve tried to do the next best thing, which is to create a single docker-compose.yml file for all of it. Removing the need to build each image, just to do a docker-compose up and scale as needed.<\/p>\n<p>Here\u2019s what I got:<\/p>\n<pre class=\"lang:yaml decode:true\" title=\"docker-compose.yml file\">master:\r\n  image: redis\r\nslave:\r\n  image: redis\r\n  command: redis-server --slaveof master 6379\r\n  links:\r\n    - master\r\nsentinel:\r\n  image: redis\r\n  command: &gt;\r\n    bash -c \"echo 'port 26379' &gt; sentinel.conf &amp;&amp;\r\n    echo 'dir \/tmp' &gt;&gt; sentinel.conf &amp;&amp;\r\n    echo 'sentinel monitor master master 6379 2' &gt;&gt; sentinel.conf &amp;&amp;\r\n    echo 'sentinel down-after-milliseconds master 5000' &gt;&gt; sentinel.conf &amp;&amp;\r\n    echo 'sentinel parallel-syncs master 1' &gt;&gt; sentinel.conf &amp;&amp;\r\n    echo 'sentinel failover-timeout master 5000' &gt;&gt; sentinel.conf &amp;&amp;\r\n    cat sentinel.conf &amp;&amp;\r\n    redis-server sentinel.conf --sentinel\"\r\n  links:\r\n    - master\r\n    - slave\r\n<\/pre>\n<p>Basically, after saving this into a docker-compose.yml file and running docker-compose up in that folder you\u2019ll get this:<\/p>\n<p><img decoding=\"async\" class=\"size-medium wp-image-2322 aligncenter\" src=\"https:\/\/intelligentbee.com\/blog\/wp-content\/uploads\/2017\/05\/initial-300x73.png\" alt=\"\" width=\"300\" height=\"73\" srcset=\"https:\/\/intelligentbee.com\/blog\/wp-content\/uploads\/2017\/05\/initial-300x73.png 300w, https:\/\/intelligentbee.com\/blog\/wp-content\/uploads\/2017\/05\/initial.png 331w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>You can now scale as needed. For example, by running:<\/p>\n<pre class=\"lang:default decode:true\">docker-compose scale slave=2 sentinel=3<\/pre>\n<p>You\u2019ll end up with:<\/p>\n<p><img decoding=\"async\" class=\"size-medium wp-image-2323 aligncenter\" src=\"https:\/\/intelligentbee.com\/blog\/wp-content\/uploads\/2017\/05\/scale-300x255.png\" alt=\"\" width=\"300\" height=\"255\" srcset=\"https:\/\/intelligentbee.com\/blog\/wp-content\/uploads\/2017\/05\/scale-300x255.png 300w, https:\/\/intelligentbee.com\/blog\/wp-content\/uploads\/2017\/05\/scale.png 331w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>To initiale a failover, you\u2019ll need to take the master out of the picture, you can do that with:<\/p>\n<pre class=\"lang:default decode:true\">docker-compose pause master<\/pre>\n<p>You can now observe the communication between the sentinels and slaves. After the down-after-milliseconds and failover timeout passes, one of the slaves will be selected for promotion.<\/p>\n<p><img decoding=\"async\" class=\"size-medium wp-image-2324 aligncenter\" src=\"https:\/\/intelligentbee.com\/blog\/wp-content\/uploads\/2017\/05\/promotion-300x248.png\" alt=\"\" width=\"300\" height=\"248\" srcset=\"https:\/\/intelligentbee.com\/blog\/wp-content\/uploads\/2017\/05\/promotion-300x248.png 300w, https:\/\/intelligentbee.com\/blog\/wp-content\/uploads\/2017\/05\/promotion.png 356w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>After the sentinels agree on the selection, the slave will become the new master.<\/p>\n<p><img decoding=\"async\" class=\"size-medium wp-image-2327 aligncenter\" src=\"https:\/\/intelligentbee.com\/blog\/wp-content\/uploads\/2017\/05\/promoted-300x242.png\" alt=\"\" width=\"300\" height=\"242\" srcset=\"https:\/\/intelligentbee.com\/blog\/wp-content\/uploads\/2017\/05\/promoted-300x242.png 300w, https:\/\/intelligentbee.com\/blog\/wp-content\/uploads\/2017\/05\/promoted.png 348w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>You can now unpause the old master by doing this:<\/p>\n<pre class=\"lang:default decode:true\">docker-compose unpause master<\/pre>\n<p>The old master will now become a slave of the new master and perform a sync.<br \/>\n<img decoding=\"async\" class=\"size-medium wp-image-2328 aligncenter\" src=\"https:\/\/intelligentbee.com\/blog\/wp-content\/uploads\/2017\/05\/new-slave-300x255.png\" alt=\"\" width=\"300\" height=\"255\" srcset=\"https:\/\/intelligentbee.com\/blog\/wp-content\/uploads\/2017\/05\/new-slave-300x255.png 300w, https:\/\/intelligentbee.com\/blog\/wp-content\/uploads\/2017\/05\/new-slave.png 331w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>That\u2019s about it. As an exercise you could try setting up a cluster starting from this and observe failovers there.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Reading the Redis documentation can be a bit confusing without the hands-on experience. You could in theory create multiple processes [&hellip;]<\/p>\n","protected":false},"author":28,"featured_media":2331,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[73,75,86],"tags":[122,137,206,224],"yst_prominent_words":[1325,1334,1333,1332,1331,1330,1329,1328,1327,1326,413,1324,1323,1322,1321,1320,1037,1013,798,432],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts\/2321"}],"collection":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/comments?post=2321"}],"version-history":[{"count":2,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts\/2321\/revisions"}],"predecessor-version":[{"id":132582,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts\/2321\/revisions\/132582"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/media\/2331"}],"wp:attachment":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/media?parent=2321"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/categories?post=2321"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/tags?post=2321"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/yst_prominent_words?post=2321"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}