<?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 marco cipri on Medium]]></title>
        <description><![CDATA[Stories by marco cipri on Medium]]></description>
        <link>https://medium.com/@marcocipri?source=rss-92bc4f20444c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*d0PNbI5XqVdrqK45xCFnfw.jpeg</url>
            <title>Stories by marco cipri on Medium</title>
            <link>https://medium.com/@marcocipri?source=rss-92bc4f20444c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 13:22:26 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@marcocipri/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[OpenAPI approach in a polyglot world]]></title>
            <link>https://medium.com/@marcocipri/openapi-approach-in-a-polyglot-world-577ad05bc112?source=rss-92bc4f20444c------2</link>
            <guid isPermaLink="false">https://medium.com/p/577ad05bc112</guid>
            <category><![CDATA[openapi-initiative]]></category>
            <category><![CDATA[restful-api]]></category>
            <category><![CDATA[microservices]]></category>
            <dc:creator><![CDATA[marco cipri]]></dc:creator>
            <pubDate>Mon, 12 Oct 2020 21:12:16 GMT</pubDate>
            <atom:updated>2020-10-12T21:12:16.587Z</atom:updated>
            <content:encoded><![CDATA[<p>In this article I show how I use the <a href="https://www.openapis.org/">OpenAPI</a> specs in microservices polyglot design.</p><p>Often <strong>microservices</strong> architectures are composed by a bunch of modules use the same interface playing different roles :sometime as client or server, sometime as client and server at the same time. In add each module can be developed in different language and technology and moreover the same interface can be used by test and load tools. In these scenarios would be useful put all interface specs in the same repository, this approach allows to all the modules to grab the last version of the interfaces at compile time and generating the appropriate mock code.</p><p>In case of <strong>RESTful</strong> communication between modules, I like to design and export the interfaces by the <strong>OpenAPI</strong> specification. This spec doesn’t care about implementation language on the contrary <strong>OpenAPI</strong> initiative and many independent organizations provide tools that automate the code generation based on <strong>OpenAPI</strong> specs. in particular I use the <a href="https://openapi-generator.tech/"><strong>OpenAPI Generator</strong></a><strong>.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/891/1*NRaVHSE0Hgav-M85IMYcbg.png" /></figure><p>hope you enjoy this approach… here the project of a very simple example (few line of code)</p><p><a href="https://github.com/marcocipri/RESTfulPolyglotExample">marcocipri/RESTfulPolyglotExample</a></p><p>To make it short, there are two sections, one for the <strong>Go</strong> language and one for <strong>Python</strong>. In each a <strong>makefile</strong> manages</p><ol><li>the downloading of a <strong>OpenAPI</strong> file from a common git repository (guess what? The pet example from the <strong>OpenAPI</strong> tutorial)</li><li>the mock code generation by <strong>OpenAPI Generator</strong></li><li>compilation and building for <strong>Go</strong>, package installation for <strong>Python</strong></li><li>run the client/server example</li></ol><p>Ingredients :</p><ul><li><strong>docker</strong> : v19.* . Generation of mock code by the OpenAPI specs is done by the docker distribution of <a href="https://openapi-generator.tech/">OpenAPI Generator</a></li><li><strong>python : </strong>I use Conda for the administration of virtual env</li><li><strong>go : </strong>amazing….</li><li><strong>soundtrack</strong> : <a href="https://www.youtube.com/watch?v=plL2VDAoThU">Steve Reich + Pat Metheny (</a>Electric Counterpoint<a href="https://www.youtube.com/watch?v=plL2VDAoThU">)</a></li></ul><p>Code is very basic but show the idea behind:</p><ol><li>design the interface by OpenAPI</li><li>each module which uses this interface has to download the specification from the interfaces repository then generate the client/server mocks.</li></ol><p>in this way design and implementation are decoupled and developers have the opportunity to choose the appropriate language or technology for each module.</p><p>Moreover every changing in the RESTfull interface during the development can be annotated in the interfaces repository and the impacts of this modifications on the modules are minimized since the mock code and the client / server code are managed as different packages and decoupled in the project structure.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=577ad05bc112" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[My home lab]]></title>
            <link>https://medium.com/@marcocipri/my-home-lab-a20b2f1904d0?source=rss-92bc4f20444c------2</link>
            <guid isPermaLink="false">https://medium.com/p/a20b2f1904d0</guid>
            <category><![CDATA[virtualbox]]></category>
            <category><![CDATA[docker-machine]]></category>
            <category><![CDATA[docker]]></category>
            <dc:creator><![CDATA[marco cipri]]></dc:creator>
            <pubDate>Sun, 14 Jun 2020 15:20:24 GMT</pubDate>
            <atom:updated>2020-06-14T15:23:01.070Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>The art of recycling old pieces of hardware giving it new opportunities and mainly : <strong>saving my money</strong>…….</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*x6D3p9uN2Z1u6KV7" /><figcaption>Photo by <a href="https://unsplash.com/@matthew_t_rader?utm_source=medium&amp;utm_medium=referral">Matthew T Rader</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>This post is the diary of how I made my Docker and Kubernetes playground at home using my old (and valiant) <a href="https://support.apple.com/kb/sp578?locale=it_IT"><strong>late 2009&#39; Mac Mini</strong></a>.<br>As everybody I need dive deeply on new paradigms, patterns and products, test new solutions and I need a lab where build, deploy, destroy and measure…. in other words to <strong>play</strong>.</p><p>My main goals (with short answers):</p><ul><li>different nodes in order to replicate networking issues (from the same host I can build many nodes and clusters by <a href="https://www.virtualbox.org/"><strong>VirtualBox</strong></a> machines)</li><li>docker and virtual machines by default (main node has <a href="https://docs.docker.com/machine/"><strong>docker-machine</strong></a> installed with <a href="https://www.virtualbox.org/">V<strong>irtualBox</strong></a> as device. The virtual machine are build on <strong>boot2docker</strong> image)</li><li>remote control (grated by <strong>ssh</strong> with <strong>key-based</strong> authentication)</li><li>ability of re-build from the ground all environments with a bit of automation (configurations and docker deploy by <a href="https://www.ansible.com/">A<strong>nsible</strong></a>)</li><li>all the playgrounds has to be cheap ( tested on the old <a href="https://support.apple.com/kb/sp578?locale=it_IT"><strong>Mac Mini</strong></a> but works on my second hand <strong>Intel NUC </strong>as well)</li></ul><p>Code on github</p><p><a href="https://github.com/marcocipri/myHomeLab">marcocipri/myHomeLab</a></p><p>ingredients :</p><ul><li><a href="https://support.apple.com/kb/sp578?locale=it_IT"><strong>late 2009&#39; Mac Mini</strong></a></li><li><a href="https://www.virtualbox.org/">V<strong>irtualBox</strong></a></li><li><a href="https://docs.docker.com/machine/"><strong>docker-machine</strong></a></li><li><a href="http://www.tinycorelinux.net/"><strong>Tiny Core Linux</strong></a> (the base of boot2docker)</li><li><a href="https://en.wikipedia.org/wiki/Billy_Joel"><strong>Billy Joel</strong></a> music</li></ul><p>In my narration a bee is a VM made by <a href="https://docs.docker.com/machine/"><strong>docker-machine</strong></a>, which means based on boot2docker image. See below….. and</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/815/1*N0wAIx1IJDZUKL6hcpO_TQ.png" /><figcaption><strong>the big picture</strong></figcaption></figure><p>My ancient Mac Mini doens’t support Docker natively, only virtualization, but in some way <a href="https://docs.docker.com/machine/"><strong>docker-machine</strong> </a>allows to run Docker images on boot2docker’s (Tiny Core Linux) VM. So I’ve decided for VirtualBox as hypervisor, OSX is the last version supported by apple. I image the host as an hive where create virtual machine (worker bees) could be possible by <a href="https://docs.docker.com/machine/"><strong>docker-machine</strong></a>. The VM base image is based on <strong>boot2docker </strong>and is fine for me because is light and restores the original status at any reboot… useful in order to burn from the ground the system.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*QfNtnbS3BnSc-WkJsrChMw.gif" /><figcaption><strong>approaching a hive</strong></figcaption></figure><p>When the system is out of control or return to the initial status is required<br>we have two options:</p><ul><li>restart the VM (by cli with <a href="https://docs.docker.com/machine/"><strong>docker-machine </strong></a><strong>restart &lt;VM name&gt;</strong> or <strong>reboot</strong> in a ssh session)</li><li>remove the VM by cli with <a href="https://docs.docker.com/machine/"><strong>docker-machine</strong></a><strong> rm &lt;VM name&gt;</strong> and create again</li></ul><p>with this procedure any modification on the VM will be lost (you know that boot2docker in on memory and just few objects are persistent).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*D-THpoEN4Lc52vsOpoi5xA.png" /><figcaption><strong>too many virtual machines (bees)….</strong></figcaption></figure><p>Once the VMs are restored some kind of automation is needed for the re-installation and re-configuration of the services on top of the VM. Adoption of <a href="https://www.ansible.com/"><strong>Ansible</strong></a> is a good answer but Python has to be installed on the boot2docker VM. Services will be distributed as Docker images which can run easily on boot2docker’s VMs. By the way boot2docker was made as a ‘batteries included’ solution ready to test Dockers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*jMloQCeir4SgZuMh_qFtNQ.png" /><figcaption><strong>feeding a bee</strong></figcaption></figure><p>As already said a bee is a VM made by <a href="https://docs.docker.com/machine/"><strong>docker-machine,</strong></a> which means based on boot2docker image and now is clear has to have some caracteristics:</p><ul><li>python on board (in order to run <a href="https://www.ansible.com/"><strong>Ansible</strong></a>)</li><li>courtesy users with custom environment properties</li></ul><p>at this point of the story we know that boot2docker has few persistence features</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d2ce69204b8695d186207b0471cdc010/href">https://medium.com/media/d2ce69204b8695d186207b0471cdc010/href</a></iframe><p>every time the VM will be restarted the script before will be run and :</p><ul><li>creates my custom user (let say bingousr)</li><li>sets the user password</li><li>put my key in order to grant the ssh remote access</li><li>add to custom user the privileges for the staff users</li><li>put the python install file in the /etc/profile.d directory</li></ul><p>the script below checks if python is installed then runs the installation. The location of the script (/etc/profile.d) ensure that the check will be run for each login.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ed05afd2b42593e8cf2318ec355de612/href">https://medium.com/media/ed05afd2b42593e8cf2318ec355de612/href</a></iframe><p>but now I have the access of the VM only from the main host (the hive), I’d like to access by ssh from a remote workstation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/259/1*mo88XmzaPO6CR7e1ylMvhA.jpeg" /><figcaption><strong>were are my bees?</strong></figcaption></figure><p>In order to access to the VMs a new network interface is needed, a <strong>bridge</strong> one.</p><p>The below script creates a VM by <a href="https://docs.docker.com/machine/"><strong>docker-machine,</strong></a> copies all the files, creates a new bridged interface linked to the the host network interface then shows the IP and MAC address. All by VBoxManage tool (the cli tool of Virtual Box)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6a42dba67377d8daff12f67f6f8c3a39/href">https://medium.com/media/6a42dba67377d8daff12f67f6f8c3a39/href</a></iframe><p>Access on this IP is granted by the public key loaded before.</p><p>Now I can install, delete and reinstall all by <a href="https://www.ansible.com/"><strong>Ansible</strong></a> scripts from my remote workstations, no agents but only ssh.</p><p>to run the bee creation, in other words a VM based on boot2docker, accessible by ssh and ready to run a docker, I need to invoke this command</p><blockquote>createBoot2dockerBee.sh &lt;VM’s name&gt; &lt;host network interface&gt;</blockquote><p>next the <a href="https://www.ansible.com/"><strong>Ansible</strong></a><strong> </strong>deployment</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a20b2f1904d0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Approaching go(lang) : my IDE definition]]></title>
            <link>https://medium.com/@marcocipri/approaching-go-lang-my-ide-definition-57c01ae90096?source=rss-92bc4f20444c------2</link>
            <guid isPermaLink="false">https://medium.com/p/57c01ae90096</guid>
            <category><![CDATA[vscode]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[go]]></category>
            <category><![CDATA[devcontainer]]></category>
            <category><![CDATA[ide]]></category>
            <dc:creator><![CDATA[marco cipri]]></dc:creator>
            <pubDate>Wed, 20 May 2020 21:25:06 GMT</pubDate>
            <atom:updated>2020-05-20T21:25:06.087Z</atom:updated>
            <content:encoded><![CDATA[<h3>Approaching go(lang) : my IDE definition</h3><p>I like the idea is that a project brings the whole development environment, in other words the projects has to be “battery included”.</p><p>I have worked with ‘VsCode’ for some time and I appreciate its flexibility but I also like it seems I am working with a slightly friendlier version of Vi or Emacs.</p><p>I need to implement a service with a lot of concurrency and parallelisms and since I wanted to do it for a while, I want to implement it in Go. The service will have a Rest interface and therefore I would like to explore the interoperability between Go and OpenApi .</p><p>I don’t want to install Go on my macbook, on the contrary, I don’t want to install any tools anymore and treat each development project as a unit in its own right … so I decided to use the development mode inside containers provided by VsCode.</p><p>First I had to define a docker that contains all the tools that I love to use and that make my life easier …. I certainly can’t give up my zsh or ‘oh my shell’ here, I took an example from the Microsoft portal dedicated to development in Go<br>I’ve extended, compiled and uploaded it to the docker hub.</p><p><a href="https://hub.docker.com/repository/docker/marcocipri/vscode-remote-dev-go">https://hub.docker.com/repository/docker/marcocipri/vscode-remote-dev-go</a></p><p>This image contains the main GoLang tools and is the base for the dev container. I preferred to insert only a few indications in the final Dockerfile which uses the image already loaded on dockerHub whit ‘<strong>golang image: 1’</strong> which loads the ‘<strong>go1.14.3’</strong>.</p><p>The development environment in this mode uses the environment of VsCode which lives in a Docker generated following the instructions in the <strong>Dockerfile</strong> present in the <br><strong>“.devcontainer” <br></strong>directory. This Dockerfile imports the ‘oh my shell’ suite, agnoster theme abd git, golang plugins.</p><blockquote># Install gocode-gomod<br>RUN export CHSH=no \<br>sh -c “$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)&quot; \<br>sed -i ‘s/robbyrussell/agnoster/g’ /home/vscode/.zshrc \<br>sed -i ‘s/(git)/(git go golang)/g’ /home/vscode/.zshrc</blockquote><p>In the same directory the file<br><strong>“devcontainer.json”<br></strong>indicates how the user and the IDE interacts with the newly created docker. Sets the shell</p><p><strong>“terminal.integrated.shell.linux”: “/bin/zsh”,</strong></p><p>the go tool path<br><strong>“go.gopath”: “/go”</strong></p><p>forwarding the port 9000 for external testing</p><p>at the end of the docker building, the project code is pulled in ‘src’ local dir as golang does.</p><p><strong>“postCreateCommand”: “go get github.com/marcocipri/go-openapi-go”</strong></p><p>Regenerating everything with a more updated version wouldn’t be a big problem.</p><p>below how works.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*q6q8BunpwCwXPqATWRXoJQ.png" /><figcaption>shift+command+p then select ‘Reopen in Container’</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*T5dRGgrYBNn9K2dY0xxLqQ.png" /><figcaption>a new VsCode is started and the Docker is compiled… see the progress</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VSg9LxJqPO6x4K9qGJjqVw.png" /><figcaption>see the go tools working!</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=57c01ae90096" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[my Python comfort suite]]></title>
            <link>https://medium.com/@marcocipri/my-python-comfort-suite-1b634ab751ae?source=rss-92bc4f20444c------2</link>
            <guid isPermaLink="false">https://medium.com/p/1b634ab751ae</guid>
            <category><![CDATA[vscode]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[terminal]]></category>
            <dc:creator><![CDATA[marco cipri]]></dc:creator>
            <pubDate>Tue, 02 Oct 2018 13:34:55 GMT</pubDate>
            <atom:updated>2018-10-02T19:38:05.719Z</atom:updated>
            <content:encoded><![CDATA[<h3>my Python confort suite</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*f2Eawcv42-iLmtFk" /><figcaption>Photo by <a href="https://unsplash.com/@thekidnamedhosea?utm_source=medium&amp;utm_medium=referral">Hosea Georgeson</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>I’ve spent much of the last decade avoiding Python but at the end I surrender…. Python met lot of my needs in term of quick prototyping, testing and systems management.<br>In add of this I still live with my laziness, source of another surrender.<br>These defeats lead me to looking for a comfortable, customizable and effective work environment.</p><p>In the last years I’ve worked with a lot of Python based framework/tool such Ansible or Locust, So build a Python environment where I like to dive become crucial.</p><p>Below a brief description, a collection of notes, written for personal use just to replicate and don’t loose what I did… once written, why don’t share this recipe?</p><h3>Ingredients</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*STp3N-JN3zPaaYdf" /><figcaption>“person baking infront of table” by <a href="https://unsplash.com/@elevatebeer?utm_source=medium&amp;utm_medium=referral">Elevate</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><ul><li><strong>Python</strong> : of course….</li><li><strong>Operative System</strong> :Currently my favorite desktop OS is OSX, for this reason any *nix environment could be the basement of the suite. Adoption of *nix os in this context made easy the usage of Python, term console and so on…</li><li><strong>console term</strong> : I’ve adopted <strong><em>oh-my-zsh</em></strong> over z-shell for the interactive functionality, integration with a lot of tool (has plugin for GIT, Python etc. etc.) in my humble opinion a great tool for CLI usage.</li><li><strong>ide</strong> : vs code</li><li><strong>environment management</strong> : conda</li></ul><h3>console term</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*kYrQ5Yh3tIEvfO0N" /><figcaption>“bunch of shell clams on shore” by <a href="https://unsplash.com/@mandyhenry1?utm_source=medium&amp;utm_medium=referral">Mandy Henry</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Once Python is installed it’s time to extend zsh so, let install <strong><em>oh-my-zsh …</em></strong> it’s easy</p><pre>sh -c “$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)&quot;</pre><p>easy customization… default setting already met my needs, the .zshrc has</p><pre>ZSH_THEME=”robbyrussell”</pre><p>set and provides Git/Python integration, shows working directory, Conda and colors… in the picture below a description of my favourites functionalities.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/888/1*yRbrzYUHTAkjnMXiZssAJw.png" /></figure><ol><li>current directory</li><li>current GIT’s branch</li><li>python environment (after activation)</li><li>GIT consistency (file not staged)</li></ol><p>zsh (<strong><em>oh-my-zsh</em></strong>) could be further customized but I’m currently default is enougth for me. add in zshrc</p><pre>plugins=(<br> git<br> docker<br> mvn<br> ng<br> node<br> npm<br> pip<br> python<br> vscode<br>)</pre><h3>ide</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*570m316FEoaO7-pd" /><figcaption>“handheld tool lot” by <a href="https://unsplash.com/@carlevarino?utm_source=medium&amp;utm_medium=referral">Cesar Carlevarino Aragon</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>in those days there’s a lot of options but I’d liked to find a tool that allows a command line usage also met several languages (java, python …. ) quite general purpouse (docker development, remote debugging, rest test ….). The story is that I’m tired to switch tool or environment for each task/language, I want a unique tool which supports all of these and …. guess what? Microsoft made it for me! to be honest I’m surprised myself when I realized Microsoft reached Opensource development in so elegant mode….. in the 90’s MS was seen as the dark side of development instead Linux was the Jedi sabre…. times has changed.</p><p>at the end of the story my option is <a href="https://code.visualstudio.com/"><strong>vscode</strong></a> which integrates python, java, editing facility, online correction, supports conda (python virtual env) , Git etc. etc.</p><p>allows extension by plugins, has a nice developer community.</p><p>on top of all…. is free.</p><p>regarding Python environment , I can switch on an environment created by <strong>virtualenv</strong> or his wrapper as <strong>conda </strong>with</p><pre>shift +command +p </pre><p>then select “Python : select interpreter”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AfsgqXbidHG6tIM_3FllxQ.png" /></figure><p>in ad has a lot of plugin downloadable for free from the main site.</p><p>Terminal is quite nice and integrate with zsh by an easy configuration.</p><p>type :</p><pre>⌘,</pre><p>for User Settings</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yPmnnUwbI8AZz1CbWnQsjQ.png" /></figure><h3>environment management</h3><p>In my case the need for a virtual environment was felt soon. To many libraries and to many tools… I was scared of the mix of different versions…..</p><p>At the beginning I started using virtualenv but at the end I landed on conda which wraps functionalities of virtualenv and adds plays as package management. once <strong>conda</strong> has been installed, these two lines activate it</p><pre>export PATH=”/Users/mcipri/anaconda3/bin:$PATH”<br>. /Users/mcipri/anaconda3/etc/profile.d/conda.sh</pre><p>creation of a virtual env is easy, for instance</p><pre>conda create — name ansible python=3.6.2</pre><p>and I can add Python module by pip or conda package manager.</p><p>switching of the environment :</p><pre>➜  ansible git:(master)<br>➜  ansible git:(master)<br>➜  ansible git:(master) conda env list<br># conda environments:<br>#<br>base                  *  /Users/mcipri/anaconda3<br>ansible                  /Users/mcipri/anaconda3/envs/ansible<br>aws                      /Users/mcipri/anaconda3/envs/aws<br>moka                     /Users/mcipri/anaconda3/envs/moka</pre><pre>➜  ansible git:(master) source activate ansible<br>(ansible) ➜  ansible git:(master)</pre><p>as said before, these few notes are intended as my personal reminder but, I hope could be useful for someone else.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1b634ab751ae" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Docker containers in swarm mode using Consul]]></title>
            <link>https://medium.com/@marcocipri/docker-containers-in-swarm-mode-using-consul-27ee1ac41d35?source=rss-92bc4f20444c------2</link>
            <guid isPermaLink="false">https://medium.com/p/27ee1ac41d35</guid>
            <category><![CDATA[swarm]]></category>
            <category><![CDATA[consul]]></category>
            <category><![CDATA[docker]]></category>
            <dc:creator><![CDATA[marco cipri]]></dc:creator>
            <pubDate>Fri, 04 Aug 2017 13:57:59 GMT</pubDate>
            <atom:updated>2017-08-07T13:30:34.173Z</atom:updated>
            <content:encoded><![CDATA[<h3>Brief intro :</h3><p>Since I read about <strong>microservices</strong> as <em>“</em><strong><em>a way of designing software applications as suites of independently deployable services</em></strong><em>” </em>I’m very intrigued by the docker and in the last six months I wondered what sorts out when we mix Consul and the last native swarm-mode.<br>Can we have a reliable system with less effort and less maintenance?<br>How could we have centralized key configuration and service discovery? Can the system self manages fault conditions and load variations? <br>I’ll try to show my point of view whit a practical (and implemented) solution for an extendible architecture. This post is intended to be the logbook of this journey.wor</p><p>hope you enjoy the recipe…</p><p>Ingredients :</p><ul><li><strong>docker</strong> : v1.12 the container swarm</li><li><strong>consul : </strong>service discovery, healt and Key/Value repository</li><li><strong>nodejs : </strong>fast and easy for demo microservices</li><li><strong>haproxy : </strong>software load balancer</li><li><strong>EnvConsul and Consultemplate :</strong></li><li><strong>soundtrack</strong> : <a href="https://en.wikipedia.org/wiki/Algiers_(band)">https://en.wikipedia.org/wiki/Algiers_(band)</a></li></ul><p>stirred, not shacked…</p><h3>Desiderata :</h3><p>what I want to have is an extensible framework, easy to scale then:</p><ul><li>centralize the services configurations</li><li>add new service workers dinamically at “run time”</li><li>have centralised health checks</li><li>automatic worker remove in case of fault</li><li>transparent load balancing</li></ul><h3>High level Description:</h3><p>Only for instance the service exposed by the system is a token generator (very basic and trivial implements a Access Token pattern). Token service could involves JWT but I like to write few node’s code lines and to show how we integrate service discover, Key/Value repository and fault management. For this round the token service is written in javascript and runs on <strong>node.js</strong>, <strong>microservice’s</strong> configuration is stored on a Consul (KV) cluster which acts also as dynamic discovery, DNS service and basic failover. The same service will be delivered by an appropriate number of independent swarms, in case is needed, a new swarm. Haproxy provides the load balanced entry points from outbound and send the requests to the swarms. Security matters are out of the scope of this post. For now, focus on reliability, balancing and adaptability…<strong>.</strong></p><p>below the system view from hight perspective :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-oLI6SywUWckO2ZMLePoLg.png" /></figure><h3>The Code :</h3><p>in order to follow the recipe you must install docker and virtualbox on your machine. All servers will be deployed locally.</p><p>Scripts are stored on github at : (<a href="https://github.com/marcocipri/docker-framework-I">https://github.com/marcocipri/docker-framework-I</a>)</p><p><a href="https://github.com/marcocipri/docker-framework-I">marcocipri/docker-framework-I</a></p><p>are docker-machine based therefore quite adjustable i . order to deploy on google, amazon etc. etc.</p><p>microservice code is on (<a href="https://github.com/marcocipri/tokenmanager">https://github.com/marcocipri/tokenmanager</a>)</p><p><a href="https://github.com/marcocipri/tokenmanager">marcocipri/tokenmanager</a></p><h3>The Core :</h3><p>this is the heart of the system. In this case I have choosed Consul, scripts provide 3 nodes but could be increased as needed. below a brief description of the usage of the script devoted to the creation of the core (see on git)</p><p><strong>create_core_vms.sh </strong>: creates the VMfore the Consul servers. For test the local virtual box is enough. in the script will be created just 3 very light VMs using doker-machine tool : core-vm1, core-vm2, core-vm3. Extends as you like. VMs are based on “boot2docker.iso” nad isn’t intented to be used on production enviroment. At the end of this script you can see</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/927/1*sLym2E5Q5CBXsQVfzq29kg.png" /></figure><p>and on Virtualbox</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3Iu962kTwJY6GXWsPvtR-Q.png" /></figure><p><strong>deploy_consul_srv.sh</strong> : Consul servers deploy. Consul cluster requires at least three nodes (2N+1) and is declared in bootstrap-expect parameter in accordance with the consensus protocol specs. Consul are deployed as dockers using the official image from hub.docker.com . Services are exposed on the default ports but the DNS services are exposed on the standard port (53) instead (8600). Now we have our living DNS. To archive the port binding the usage of the host’s network is required. At the end of the script you can see</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HazFtEgMfp9SwUR3uE-GLw.png" /></figure><p>and the consul user interface is available on each node on port 8500</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*h8LxMH0C9QgIQxjcdrpdbw.png" /></figure><p>all the three nodes are available and are shown as part of the cluster.</p><p><strong>set_consul_env.sh</strong> : sets the first KV entries on the Consul cluster. very trivial by curl command. Now the KV are visible on the Consul UI :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nwW3dilpYorjnYCAiVw8iA.png" /></figure><p>KY values could be updated by UI or rest API and plays in this architecture as centralized configuration. Next we see how the microservices read the KV by rest.</p><p>In the GIT repository are present delete and clean scripts, devoted to restore the environment.</p><h3>The SWARMs :</h3><p>the idea behind the swarm is deploy a bunch of servers which share a overlay network. Some of these servers play as Manager and the other as Worker. Managers receive the requests and redirect to the Worker in accordance with the Load Balance policy. Once the consumers throughput is not enough anymore, just deploy a new swarm with the same microservices deployed. let explore the scripts :</p><p><strong>create_swarm_vms.sh</strong> : create VMs, quite similar as what seen in the Core. when</p><blockquote>bash create_swarm_vms.sh a</blockquote><p>is invoked, all the VMs needed for the swarm “a” will be created. the script create just three VMs, one for the manager and two for the worker but there isn’t any limit in the number of manager and worker. Refine as you need. Now you can see something like :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/981/1*gpxi0cibNTdI4IHuDTcVeA.png" /></figure><p><strong>create_swarm.sh</strong> : create the swarm defining the Manager, the Workers and the overlay network</p><blockquote>bash create_swarm.sh a</blockquote><p>once the script ends we can inspect the status of the cluster following these instructions :</p><blockquote>docker-machine ssh swarm-a-mng-1</blockquote><p>connects to the manager node by a shell</p><blockquote>docker node ls</blockquote><p>ask to the swarm to show how is composed, you can see :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bcIT1tATuaIsQ4j8bp-gIQ.png" /></figure><p>swarm-a-mng-1 is the leader. Now we the swarm infrastructure… let deploy some services.</p><p><strong>deploy_consul_cli.sh</strong> : creates a consul client which interact with the cluster, resolves names, store KV and manages the service discovery. With this approach we can avoid the DNS management. After the invocation of</p><blockquote>bash deploy_consul_cli.sh a</blockquote><p>we have the cluster client only on the swarm managers, on the worker isn’t needed. See on the manager</p><blockquote>docker ps</blockquote><p>and see</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TCS5Xas6NLeV7dMVEcWNvg.png" /></figure><p>now run :</p><p>deploy_visualizer.sh : deploys a docker which shows the swarm status, is usefull just for test environment not for prodution porpouse</p><blockquote>bash deploy_visualizer.sh a</blockquote><p>on this url (update the ip address)</p><p><a href="http://192.168.99.103:5001/">http://192.168.99.103:5001/</a></p><p>is shown the swarm composition and the deployment status</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/752/1*wgqjn6QSDn4ehjmD7qNa4g.png" /></figure><p>a this point of the story we have only the visualizer, Now we can deploy the microservice :</p><p>deploy_token_manager.sh : deploys the microservice packaged as a docker, all the codes and the dockerfile are on git (<a href="https://github.com/marcocipri/tokenmanager">https://github.com/marcocipri/tokenmanager</a>). Run :</p><blockquote>bash deploy_token_manager.sh a</blockquote><p>and see on the swarmvisualizer what happen</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_CDUKCu7za21GMnx-550eQ.png" /></figure><p>two istances of tokenmanager are deployed as requested in the parameter</p><p>— replicas=2</p><p>briefly : the docker uses the consul client in the same host(service registration, KV service), exposes his service on port 8080 and run the service <strong>service.js . </strong>Let see what happen on the swarm. Access on the manager and run :</p><blockquote>docker@swarm-a-mng-1:~$ docker service logs token-manager</blockquote><p>Now you see what the microservice writes on console (docker logging is not the scope of this post).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9T5hu2rnsSeo_FPILFf52g.png" /></figure><p>by the rest Consul API the service retrieves the configuration stored on the Consul Cluster as seen before in the Core installation. Refresh frequency is part of the configuration itself. Next the service registers himself as <strong>token-manager</strong> service, always on the Consul Cluster as seen below :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-XR9GROq0DzzODrYalMkWg.png" /></figure><p>the Consul Cluster check the health of the node by a http GET on the <strong>/healt/</strong></p><p>this function can be evolved as needed, now returns only “OK”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4BXdrMb5io4xxRpCpOymig.png" /></figure><p>but what is the pourpouse of the service? Generate and Verify an authentication Token. The endpoint devoted to provide this service is on the Manager Host by an HTTP POST on:</p><p><a href="http://192.168.99.103:8080/generate">http://192.168.99.103:8080/generate</a></p><p>header :</p><p>Content-Type: application/json</p><p>for instance payload :</p><blockquote>{<br> “username”: “123456789012”,<br> “token”: “-”,<br> “millis”: “-”,<br> “gametype”: “othello”,<br> “reseller”: “12345”,<br> “clientversion”:”ver01&quot;<br> }</blockquote><p>see the response :</p><blockquote>{</blockquote><blockquote>“sessionID”: “aec816e4f83ef3dca658a11705c7a2c03b91ecbc8b0fc4e2468f086e0f4919b5cf4a98b4d35da2ff67b1207e51ae03cffa59a4176b915857b6d703ab906a76944f61458950d010e72ba797783d381e84f0f9ea7f75eaaabcb10b22fcce5d172f414293a824a5530df06db2b6556cf54b4d88a6e0982748419e2fc7cdc68957e9”,</blockquote><blockquote>“baker”: “c03798ef8511”</blockquote><blockquote>}</blockquote><p>the baker is the worker whom return the response, few hit and you can see change as expected. In this configuration the “baker” field assume only two values infact we have just two worker.</p><p>At the end what we have now? A balanced microservice, deployed as Docker, self subscribing as public Service and using a remote/centralized configuration.</p><h3>The Load Balancing :</h3><p>In my plan the default access point isn’t the swarm’s manager. For this reason a load balancer will be created in orfder to export an external access to the clients. This component is based on HAproxy and Cosul-Template. In details HAproxy provides the http service and Consul-Template update the balancing policy following the Consul Cluster service subscriptions, see below.<br>First create the VMs for the first loadbalancer (lb-01)</p><blockquote>bash create_load_balancer_vms.sh 01</blockquote><p>then deploy the consulclient with nale consul-lb-01</p><blockquote>bash deploy_consul.sh 01</blockquote><p>the consulclient subscribes itself o the consul cluster, automatically…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KmbsOoxRiEKreQwteHwX9g.png" /></figure><blockquote>bash deploy_haproxy.sh 01</blockquote><p>this image has installed haproxy and consul-template.<br><strong>Consul-template </strong>checks any change on <strong>tokenmanager</strong> service configuration ad shown in <strong>in.tpl</strong></p><blockquote>{{ range service “tokenmanager” }} server {{ .Name }} {{ .Address }}:{{ .Port }}{{ end }}</blockquote><p>when a new tokenmanager is subscribed (or unsubscribed for fault) the configuration file of <strong>HAproxy</strong> is rewritten and the http server restarted. This approach is easy and fast in case of basic http traffic. Rules to rewrite the configuration file are defined in <strong>haproxy.template</strong></p><p>what we have archived? Try to send a http post to the loadbalancer node on the port 1080 (as defined in the docker run), assuming that the loadbalancer</p><blockquote>curl -S -s -H “Content-Type: application/json” -X POST -d ‘{ “username”: “123456789012”, “token”: “-”, “millis”: “-”, “gametype”: “othello”,”reseller”: “12345”,”clientversion”:”ver01&quot;}’ “<a href="http://192.168.99.107:1080/generate">http://192.168.99.107:1080/generate</a>&quot;</blockquote><p>the baker (the worker node which generates the response) changes and is one of the available workers.</p><p>now we are able to add new swarm or new loadbalancer, the system manages this variations exposing the services mantaining the policies of load balancing and reliability.</p><h3>Play with the environment :</h3><p>lets try with add a new swarm.<br>In the current situation we have one swarm composed by one manager and one worker running this script (contains the same operations described before)</p><p>bash create_new_swarm.sh b</p><p>now we have a new swarm</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o4Tfdo9j6DyYvJtsmkdfIg.png" /></figure><p>new swarm has subscribed itself to the services available on consul cluster</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*laTs6ZBtDBGT2w5naVVLTw.png" /></figure><p>also as node</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*S4z0M-jHz6C24JxEzgf_ZQ.png" /></figure><p>assuming that, in my local installation the load balancer node has ip address 192.168.99.107 and the port of the service is 1080, the following script</p><p>bash check_the_baker.sh 192.168.99.107 1080</p><p>shows which baker asks…. now are four different ID.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wlP9FAVpEzp7QfnAIvs6BA.png" /></figure><p>gently stopping one swarm we have :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o_tsZvmFsiy4Z965qHGcFw.png" /></figure><p>and more brutally stopping one of the manager host by VirtualBox :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EY0rBv2UZ9wWObGpFTIHBg.png" /></figure><p>the systems takes 20 seconds to restore himself (see consul.hlc)</p><p>Have fun! :-)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=27ee1ac41d35" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>