<?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 Suyash Mohan on Medium]]></title>
        <description><![CDATA[Stories by Suyash Mohan on Medium]]></description>
        <link>https://medium.com/@suyashmohan?source=rss-c5b92518ef24------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*qHMlF2FgEQtwzpxFcmBHaQ.jpeg</url>
            <title>Stories by Suyash Mohan on Medium</title>
            <link>https://medium.com/@suyashmohan?source=rss-c5b92518ef24------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 14:44:45 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@suyashmohan/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[Setup WSL kinda Linux Box on Macbook M1 with Apple Virtualization framework]]></title>
            <link>https://medium.com/@suyashmohan/setup-wsl-kinda-linux-box-on-macbook-m1-with-apple-virtualization-framework-ec2529f9797?source=rss-c5b92518ef24------2</link>
            <guid isPermaLink="false">https://medium.com/p/ec2529f9797</guid>
            <category><![CDATA[virtualization]]></category>
            <category><![CDATA[wsl]]></category>
            <category><![CDATA[macbook]]></category>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[apple-silicon]]></category>
            <dc:creator><![CDATA[Suyash Mohan]]></dc:creator>
            <pubDate>Fri, 03 Sep 2021 07:20:59 GMT</pubDate>
            <atom:updated>2021-09-03T07:29:29.586Z</atom:updated>
            <content:encoded><![CDATA[<h4>MacOS Big Sur allows to create Virtual Machines and run Linux-based operating systems. Let’s see how to do so.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nP_7XO0svIGw7I4XXnzHLQ.jpeg" /><figcaption><a href="https://unsplash.com/photos/YfyDNZSeGbg">https://unsplash.com/photos/YfyDNZSeGbg</a></figcaption></figure><p>The moment I realized that Apple’s MacOS Big Sur has a built-in Virtualization framework that can run Linux OS, I was right away intrigued to try it out. Being a backend and DevOps developer I have some serious love for Linux OSes. But at the same time, I do have other hobbies too, like Photography, for which I prefer Photoshop and Lightroom. At the same time, I also love the new MacBook Air M1 for its hardware. Therefore being able to run Linux VM on my MacBook Air M1 with nearly native speeds, gives me the best of both worlds.</p><p>There are some third-party paid applications like <a href="https://www.parallels.com/">Parallels</a> and <a href="https://www.vmware.com/asean/products/fusion.html">VMWare Fusion</a> that allow you to create any kind of Virtual Machine and they are quite good at that. There is also an open-source app named <a href="https://mac.getutm.app/">UTM</a> that uses Qemu to run Virtualised Linux instances on MacBook. Personally, I don’t need GUI, I just need a Linux terminal. So If I could set up a Linux box on MacBook like WSL (Windows Substem for Linux) on Windows 10, that will be really amazing. And actually, you can do that, with a bit of hacking around.</p><p>Apple allows to create Virtual Machines and run Linux-based operating systems but only through APIs. There is no stand-alone app. That’s not a big issue. There is an open-source tool named <a href="https://github.com/evansm7/vftool">vftool</a> that acts as a wrapper around these APIs so you can use it to run a Linux Virtual Machine. Bear in mind, this Virtualization framework doesn’t provide all the features that might be present on apps like Parallels. In fact, you won’t be able to run any Linux ISO right away. That’s where a bit of hacking comes around. You might also need some basic Linux knowledge. There is no GUI straight out of the box too. You can set up something like a VNC server though.</p><h4>Let’s start with Alpine Linux</h4><p>Alpine Linux is great for running virtual machines as it provides a very minimal system of Linux just to run inside Virtual Machine. Alpine Linux is also great for being used as a base for containers. I am using Alpine Linux because it is fairly easy, to begin with for use with Apple’s Virtualization framework.</p><p>First, you would have to build the vftool from the source code, so you will need Xcode. But you don’t need the full version. I used `<em>xcode-select — install</em>` to install the basic command-line tools to compile the applications. Once you have cloned the repo you can run the make command.</p><pre>git clone <a href="https://github.com/evansm7/vftool.git">https://github.com/evansm7/vftool.git</a><br>cd vftool<br>make</pre><p>You should now have vftool binary.</p><p>Next use something like Keka to extract the Alpine Linux ISO. I downloaded the aarch64 build under the Virtual category on Alpine Linux’s <a href="https://alpinelinux.org/downloads/">download page</a>.</p><p>This should give you `<em>vmlinuz-virt`</em> and `<em>initramfs-virt</em>` under the boot folder in extracted files. vmlinuz-virt is a gzip file so we need to extract it further. Simple rename it and add .gz extension. Now you can extract it again. This will give uncompressed vmlinuz-virt. No need to extract initramfs-virt, we will use it as it is.</p><p>Now we should have three files,</p><ol><li>Uncompressed kernel file aka vmlinuz-virt</li><li>Initial ram disk aka initramfs-virt</li><li>The original ISO alpine-virt-3.14.2-aarch64.iso</li></ol><p>Now you can simply run vftool like this</p><pre>vftool -k vmlinuz-virt -i initramfs-virt -d alpine-virt-3.14.2-aarch64.iso</pre><p>This will give you an output like this</p><pre>2021–09–03 12:49:33.808 vftool[3653:133705] vftool (v0.3 10/12/2020) starting<br>2021–09–03 12:49:33.809 vftool[3653:133705] +++ kernel at vmlinuz-virt, initrd at initramfs-virt, cmdline ‘console=hvc0’, 1 cpus, 512MB memory<br>2021–09–03 12:49:33.809 vftool[3653:133705] +++ fd 3 connected to /dev/ttys001<br>2021–09–03 12:49:33.809 vftool[3653:133705] +++ Waiting for connection to: /dev/ttys001</pre><p>The important point is that you should see something like Waiting for connection to: /dev/ttys001</p><p>Now in another terminal tab, run `<em>screen /dev/ttys001</em>` You might see different ttys number, but that is normal. The moment you will run the screen command, you can see alpine linux’s login screen. The default user is <em>root </em>without any password.</p><p>So here you have it now, you can do whatever you like with your Alpine Linux. But make sure to first run `<em>setup-alpine`</em> command to get working internet. Do note that everything is running in memory and all changes will be gone the moment VM is stopped. We will soon see how to have a permanent setup.</p><p>The vftool allows passing additional parameters to set how many CPUs to use and how much memory to allocate. Use `<em>-p</em>` for processors and `<em>-m</em>` memory. There is an additional parameter with `<em>-a</em>` that allows to set up parameters to pass to the kernel and initial ramdisk when booting.</p><h4>Let’s now move on to Debian</h4><p>Alpine Linux is great to get a feel and understand how the Virtualization framework works, but you might want to run something like Debian OS for some serious work.</p><p>Like I mentioned above, we can’t just take an ISO file and expect it to run like on VirtualBox or Parallels. Apple’s virtualization framework doesn’t allow GUI OS to run out of the box, so you can’t just run any Desktop distribution right away. Another issue is the virtualization framework uses Virtio as drivers for IO. So downloading cloud server images won’t work too out of the box. Luckily <a href="https://github.com/danielrfry">Dan Fry</a> on GitHub has a <a href="https://github.com/danielrfry/debian-vm-aarch64">repo</a> that has a build of Debian OS that comes with Virtio drivers. He also provides <a href="https://github.com/danielrfry/debian-vm-build">scripts</a> that can be used to build your own Debian images for running in Virtualization framework on Apple Silicon<a href="https://github.com/danielrfry/debian-vm-build.">.</a></p><p>From the repo, you can download vmlinuz, initrd and the rootfs tar file. Extract the rootfs tar.bz file to get something like debian-rootfs-aarch64.img</p><p>Now just run something like this</p><pre>vftool -k vmlinuz-4.19.160 -i initrd.img-4.19.160 -d debian-rootfs-aarch64.img -a &quot;console=hvc0 root=/dev/vda1&quot;</pre><p>And like the above steps, in a new terminal tab use screen to connect to the tty session. The default user is <em>root</em> and the password is <em>password</em>.</p><p>I usually like to install ssh server and kill screen sessions, then just use ssh to access my server. The image in the GitHub repo at the time of writing was Debian 10, but I was able to do a full update and upgrade it to Debian 11. You can also increase the number of Processors and Memory using the <em>-p</em> and <em>-m</em> parameters.</p><h4>Let’s try Ubuntu</h4><p>We tried Alpine and Debian, but what about Ubuntu. Ubuntu is used by many people and they might feel comfortable with Ubuntu instead. Ubuntu can work too, but again with some hacking. Unlike Debian, we can actually use Ubuntu’s cloud image directly instead of depending to build it for Apple Silicon’s Virtualization framework. But the steps are going to be slightly different.</p><p>Download the ubuntu cloud server root disk image from <a href="https://cloud-images.ubuntu.com/releases/focal/release/ubuntu-20.04-server-cloudimg-arm64.tar.gz">https://cloud-images.ubuntu.com/releases/focal/release/ubuntu-20.04-server-cloudimg-arm64.tar.gz</a> and extract it.</p><p>Then download the kernel file <a href="https://cloud-images.ubuntu.com/releases/focal/release/unpacked/ubuntu-20.04-server-cloudimg-arm64-vmlinuz-generic.">https://cloud-images.ubuntu.com/releases/focal/release/unpacked/ubuntu-20.04-server-cloudimg-arm64-vmlinuz-generic.</a> This is a gzip file, so add .gz and extract kernel again.</p><p>Now finally download the initrd file <a href="https://cloud-images.ubuntu.com/releases/focal/release/unpacked/ubuntu-20.04-server-cloudimg-arm64-initrd-generic">https://cloud-images.ubuntu.com/releases/focal/release/unpacked/ubuntu-20.04-server-cloudimg-arm64-initrd-generic</a></p><p>The root disk image doesn’t have much space so you will need to expand the file. I used qemu-img utility from qemu to expand the file. I installed qemu using `brew install qemu`. But many people might find it too much to install qemu just to get a utility to expand the disk. You can instead try the dd command</p><pre>dd if=/dev/zero of=vm/focal-server-cloudimg-arm64.img seek=20000000 obs=1024 count=0</pre><p>Personally, I haven’t used this command, but I have seen people using it.</p><p>Now let’s run the VM as follows</p><pre>./vftool -k vmlinuz -i initrd -d focal-server-cloudimg-arm64.img -m 2048 -a &quot;console=hvc0&quot;</pre><p>We need to set `<em>-m 2048`</em> to increase RAM to 2GB, the default 512MB might not be enough to run Ubuntu.</p><p>Once you use the screen command, you will be put into initramfs instead of being able to use ubuntu. Now you will have to follow these commands</p><pre>mkdir /mnt<br>mount /dev/vda /mnt<br>chroot /mnt</pre><pre>touch /etc/cloud/cloud-init.disabled</pre><pre>echo &#39;root:root&#39; | chpasswd</pre><pre>cat &lt;&lt;EOF &gt; /etc/netplan/01-dhcp.yaml<br>network:<br>  renderer: networkd<br>  ethernets:<br>    enp0s1:<br>      dhcp4: true<br>  version: 2<br>EOF</pre><pre>exit</pre><pre>umount /dev/vda</pre><p>Basically, we mounted the root disk, chroot into it and disabled cloud-init. We also set default user <em>root</em> with the <em>root</em> password and configure netplan to connect to the internet. Most people also set up ssh keys to make ssh work, but I believe we can do that later on. I took these steps from the discussion done here <a href="https://github.com/evansm7/vftool/issues/2">https://github.com/evansm7/vftool/issues/2</a> so for those steps the credit goes to people there.</p><p>Now stop the VM by simply sending <em>ctrl-c</em> on the terminal running vftool and again the command as</p><pre>./vftool -k vmlinuz -i initrd -d focal-server-cloudimg-arm64.img -m 2048 -a &quot;console=hvc0 root=/dev/vda&quot;</pre><p>This time we mentioned the <em>root</em> device parameter. This will make us log in to Ubuntu. You can use root user to log in. Once login is done, remember to run the `<em>resize2fs`</em> command to actually expand the disk for usage.</p><pre>resize2fs /dev/vda</pre><p>Now you can set up ssh if you prefer using that. Remember to do following to make ssh work</p><pre>echo “m1silicon” &gt;/etc/hostname<br>ssh-keygen -f /etc/ssh/ssh_host_rsa_key -N ‘’ -t rsa<br>ssh-keygen -f /etc/ssh/ssh_host_dsa_key -N ‘’ -t dsa<br>ssh-keygen -f /etc/ssh/ssh_host_ed25519_key -N ‘’ -t ed25519</pre><p>These are the steps that we skipped earlier.</p><p>There you have it, your Ubuntu Server running with near-native speed on your Macbook powered by an Apple Silicon M1 chip.</p><p>Hope you found this article helpful :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ec2529f9797" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[I Quit My Job and You Can Too]]></title>
            <link>https://medium.com/@suyashmohan/i-quit-my-job-and-you-can-too-27c3ae8afad0?source=rss-c5b92518ef24------2</link>
            <guid isPermaLink="false">https://medium.com/p/27c3ae8afad0</guid>
            <category><![CDATA[careers]]></category>
            <category><![CDATA[work]]></category>
            <category><![CDATA[quitting-a-job]]></category>
            <category><![CDATA[mwc-work]]></category>
            <category><![CDATA[advice-and-opinion]]></category>
            <dc:creator><![CDATA[Suyash Mohan]]></dc:creator>
            <pubDate>Sun, 01 Aug 2021 01:46:29 GMT</pubDate>
            <atom:updated>2021-08-01T10:20:31.806Z</atom:updated>
            <content:encoded><![CDATA[<h4>Some thoughts on our hustle culture and the need to keep working more</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rKd9c9Nu0NoBHohxO34Jyw.jpeg" /><figcaption><a href="https://unsplash.com/photos/k_T9Zj3SE8k">https://unsplash.com/photos/k_T9Zj3SE8k</a></figcaption></figure><p>Yesterday was the last day of my employment. I resigned from a well-paying job that I was about to complete for 4 years and that helped me live a comfortable life in Malaysia as an expatriate. But there was some part of me that was unsatisfied and unhappy. So I decided to quit.</p><p>Somehow there is such a big deal about leaving a job without having another job offer or even taking a break. No matter how good the pay is, if you are unhappy, it’s better to leave. Being stuck in a job you don’t love is like being stuck in a toxic relationship that should be ended as soon as possible.</p><p>Growing up, during school we would get summer breaks. Some of us might even be used to getting winter breaks. During college too, there were breaks between semesters. But then suddenly in your early 20s, you get into the job world and are expected to continuously work for the rest of your life. I just don’t get it. Why is it such a taboo against taking a break and just slowing down for a bit? Isn’t it better to free yourself from such a burden and focus on yourself, figuring out what would work for you in the long run?</p><p>You don’t have to be jobless for the rest of your life or have to start a business. But it’s okay to take a break from your job and find some other job after a while. Or even pursue a hobby to relax your mind. Travelling the world is another great thing that you can do. You have the knowledge and skills, you can again find another good job.</p><p>If you were able to reach this point in your life, why don’t you trust yourself that you will figure everything out and still reach higher goals in your life? Why is believing in yourself difficult for you? If you don’t feel confident in your skills, well, it is time to focus on building stronger skills. Once you build them, you can leave your job. In my opinion, you can still leave your job and strengthen your skills afterwards. You will have even more time, energy and motivation to sharpen them. A lot of time, you already have skills, you are just not confident about yourself. So being honest with yourself will help you in the longer run.</p><p>Okay, all this sounds nice, but what about finance? How is someone supposed to get bread on their plates? Again, in my opinion, the situation is not always as bad as we fear. You might already have some savings that can help you survive a few months without a job. In case you don’t have any savings, you can start working towards it. Save enough and then leave.</p><p>But don’t get stuck in the loop of saving enough and never feeling you have enough. We live in a world of the internet, there are far more ways to make money. You don’t need to just have a traditional 9 to 5 job. People are making money by writing blogs about their knowledge and experiences. People are making money by making videos on the internet. There is a big market for freelance jobs. You can always find a part-time job to make enough money to get you through the time you don’t feel like you need to have a full-time job.</p><p>In conclusion, I am not asking everyone to leave their jobs just because they can. But if you are unhappy and unsatisfied with your job and the thought of leaving your job keeps coming back to you, then just go for it. If for some reason you can’t then start working towards this plan. You don’t have to let society scare you from doing what you want to do. Sometimes things seem far scarier in our heads than they are. You have figured out your life until this day, you will figure out further.</p><p>This post is mostly full of my opinions, so I would completely understand if you disagree with me. In that case, I would love it if you leave a comment and let me know what are your thoughts about leaving your job. And if you agree with my opinions, then great, you can still leave your thoughts in the comment. If you have any interesting experiences regarding the topic of quitting the job, then do share your experiences in the comments. Your comments might help other people in making their decisions.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=27c3ae8afad0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using Apple Macbook M1 for Python & Django based website development]]></title>
            <link>https://medium.com/@suyashmohan/using-apple-macbook-m1-for-python-django-based-website-development-572bca04f5c3?source=rss-c5b92518ef24------2</link>
            <guid isPermaLink="false">https://medium.com/p/572bca04f5c3</guid>
            <category><![CDATA[django]]></category>
            <category><![CDATA[macbook]]></category>
            <category><![CDATA[m1-macbook-air]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[apple-m1-chip]]></category>
            <dc:creator><![CDATA[Suyash Mohan]]></dc:creator>
            <pubDate>Wed, 21 Jul 2021 07:03:24 GMT</pubDate>
            <atom:updated>2021-07-21T07:07:45.082Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1OtV9jVCvZ1TOaV5wpXffw.jpeg" /></figure><p>Recently I got myself a MacBook Air M1 8GB model. And I must say, I am pretty impressed. The performance is really good and the fact there is no fan amazes me. Plus I haven’t experienced any heating issue yet.</p><p>I am going to use MacBook Air M1 primarily to help in my web development projects. 8GB RAM might seem less and probably you want to get 16Gb RAM for heavy work or future proof it. But in my experience, I was able to run VS Code, Firefox with few tabs, PostgreSQL database, Insomnia Rest client and Django website running easily with this 8GB model.</p><p>Here is a screenshot of my memory while running above stated apps.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GqigELnN2kiok_KYxmQDqw.png" /></figure><p>I must say, it was not a smooth or easy ride running python and Django on M1 for the first time. I spent a half-day looking around for the solutions. Therefore I decided to write this post to share how you can make Python and Django work on your Macbook M1. There are few articles that talk about setting up both ARM and Intel versions of Homebrew and other things. But you don’t have to do so, as per my experience. It was not that difficult once I figured how to set up all.</p><p>The main issue I faced came from one of the libraries used in Django, <em>psycopg2</em>. This library tried to compile itself from sources and needs proper header files and libraries for PostgreSQL and OpenSSL. I initially installed PostgreSQL using <a href="https://postgresapp.com/">https://postgresapp.com</a> Although Postgresql does support ARM chip, the Postgres.app uses Intel Libraries and it was messing my flow as I was using ARM version of Python.</p><h3><strong>Getting Python and Django Dependencies</strong></h3><p>The easiest option I found was to simply use python installed through Xcode. You will need Xcode to compile libraries in the next steps anyway. XCode installed Python 3.8 ARM version which is good enough for me. Since I don’t want the whole Xcode, but rather just compiling support, I used following</p><pre>xcode-select --install</pre><p>This install all tools required to compile c/c++ code and also gave Python 3.8.</p><p>The next step was to install HomeBrew to help me install libpq. Just follow the <a href="https://brew.sh/">Homebrew</a> website to know how to install Homebrew. Installing libpq from sources not only creates an ARM version but also installs the required header and library files. This also installs OpenSSL dependencies which are also a requirement. So let’s install libpq from sources.</p><pre>brew install libpq --build-from-source</pre><p>Now you can create your virtual environment. In this post, I will just call it myenv.</p><pre>python3 -m venv myenv <br>source ./myvenv/bin/activate</pre><p>Once you have activated your virtual environment in the terminal, please upgrade your pip.</p><pre>pip install --upgrade pip</pre><p>Now just <em>`cd`</em> to your Django project and you can install the required dependencies. Here is where you need to set the correct path for PostgreSQL and OpenSSL header and library paths.</p><pre>export LDFLAGS=&quot;-L/opt/homebrew/opt/libpq/lib -L/opt/homebrew/opt/openssl@1.1/lib&quot;</pre><pre>pip install -r requirements</pre><p>The above command should help you install all the dependencies required to run the Django project. The <em>libpq</em> was used to build the correct version of <em>psycopg2</em>. You don’t have to use libpq also to create and manage your database, although it will install all PostgreSQL binaries. I still use <em>Postges.app</em> to manage the databases, since it makes it a little easier. Hopefully, they will update this app to support ARM in future. But I must also say I didn’t face any issue with the Intel version. Rosetta 2 works great. The only issue is if you try to compile any ARM library that needs the PostgreSQL development library and headers, it won’t work with the intel version installed by Postgres.app. For that, we can use libpq installed from the earlier step.</p><h3>Getting to run Geospatial support in Django (GDAL_LIBRARY)</h3><p>If you are using geospatial logic in your code which depends on gdal library, you are going to face another issue regarding GDAL_LIBRARY_PATH. To resolve this simply use brew to install gdal. This will install the ARM version of gdal.</p><pre>brew install geos gdal</pre><p>To make your project pick correct path for GDAL_LIBRARY_PATH, update your settings.py file by adding following:</p><pre># settings.py<br>GDAL_LIBRARY_PATH = os.getenv(‘GDAL_LIBRARY_PATH’)<br>GEOS_LIBRARY_PATH = os.getenv(‘GEOS_LIBRARY_PATH’)</pre><p>Now, you can go back to your terminal and export these environment variables as follows</p><pre>export GDAL_LIBRARY_PATH=/opt/homebrew/lib/libgdal.dylib<br>export GEOS_LIBRARY_PATH=/opt/homebrew/lib/libgeos_c.dylib</pre><p>To make your life easier, you can also add the above to lines in your <em>.zshrc</em>, <em>.profile</em> or <em>.bashrc</em> depending on whichever shell you are using.</p><p>Now you should be able to run your Django project.</p><pre>python3 manage.py shell</pre><p>Hope this post helped you with setting up Python and Django on your Apple Macbook M1 laptop. Do put a comment if this post helped you or if something was missing on this post.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=572bca04f5c3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Setting up PostgreSQL Database on Kubernetes]]></title>
            <link>https://medium.com/@suyashmohan/setting-up-postgresql-database-on-kubernetes-24a2a192e962?source=rss-c5b92518ef24------2</link>
            <guid isPermaLink="false">https://medium.com/p/24a2a192e962</guid>
            <category><![CDATA[postgres]]></category>
            <category><![CDATA[azure]]></category>
            <category><![CDATA[database]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[kubernetes]]></category>
            <dc:creator><![CDATA[Suyash Mohan]]></dc:creator>
            <pubDate>Fri, 31 Jan 2020 03:08:37 GMT</pubDate>
            <atom:updated>2020-01-31T03:08:37.338Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*m5ME6h64zeEpmP4D4ExNBw.jpeg" /></figure><p>Kubernetes is getting a lot of traction in the world of DevOps for hosting stateless microservices. But what about Stateful applications like Databases? It’s been a topic of debate when it comes to hosting databases on Kubernetes or even docker for production environments. Thankfully Kubernetes does have a special resource that can be used for setting applications like Databases. I am talking about Statefulsets.</p><p>StatefulSets are like deployment resources but they are special in the sense that Kubernetes maintains a persistent identifier for the pods and hence these pods are not interchangeable. This helps when running stateful applications.</p><p>To learn more about this, we will set up a Postgres database inside Kubernetes. Although we are using Postgres the concepts and steps are the same for any databases like MySQL, etc.</p><p>In order to set up this configuration, we will need to set up the following resources:</p><ol><li>Storage Class</li><li>Persistent Volume</li><li>Persistent Volume Claim</li><li>Stateful Set</li><li>Config Map</li><li>Service</li></ol><p>Since the data is persistent we don’t want to save our data inside the container’s storage. Instead, we want to mount external storage inside the Pod. In this way even if Pod dies, our data stays safe. Kubernetes has the concept of Persistent Volume and Persistent Volume Claim. To explain in simple terms, we need to claim a volume so that it belongs to us and we don’t end having two pods writing to the same storage. PersistentVolume defines these external storage volumes. Then PersistentVolumeClaims is a way to claim these storage volumes. A Persistent volume also needs to define what kind of Storage Class it needs. In Kubernetes, these storage volumes could be anything. It could be EBS in AWS or a local folder or any other block storage in other cloud providers. Hence we define a Storage Class with specific provisioner.</p><p>For this setup, I used microk8s. You can install it on Ubuntu by simply using snap</p><pre>$ sudo snap install microk8s --classic --edge</pre><p>The following code is also available at my <a href="https://github.com/suyashmohan/kubernetes_postgres_statefulset">Github Repo</a></p><p>Let’s define a Storage Class that uses the provisioner provided by microk8s. It is essentially local storage already set up inside micro8ks.</p><pre>kind: StorageClass<br>apiVersion: storage.k8s.io/v1<br>metadata:<br>  name: microk8s-hostpath<br>  annotations:<br>    storageclass.kubernetes.io/is-default-class: &quot;true&quot;<br>provisioner: microk8s.io/hostpath</pre><p>Now we need to define PersistentVolume that will use this Storage Class and following it we also define PersistenVolumeClaim to claim this volume</p><pre>kind: PersistentVolume<br>apiVersion: v1<br>metadata:<br>  name: postgres-pv<br>  labels:<br>    app: postgres<br>    type: local<br>spec:<br>  storageClassName: microk8s-hostpath<br>  capacity:<br>    storage: 5Gi<br>  accessModes:<br>    - ReadWriteOnce<br>  hostPath:<br>    path: &quot;/var/data&quot;<br><br>---<br><br>kind: PersistentVolumeClaim<br>apiVersion: v1<br>metadata:<br>  name: postgres-pv-claim<br>  labels:<br>    app: postgres<br>spec:<br>  storageClassName: microk8s-hostpath<br>  capacity:<br>  accessModes:<br>    - ReadWriteOnce<br>  resources:<br>    requests:<br>      storage: 5Gi</pre><p>Since we are using a local disk provisioner we need to define where on host node our data will be saved. Here we choose /var/data/. Another important parameter is accessMode. Here we choose ReadWriteOnce. This will make sure only one pod can write at a time. So, no two pods end up with this volume for writing. We can also define what should be the size of this volume, which we choose 5Gb.</p><p>Next, we will setup ConfigMap. They are just an easy way to provide configuration for our setup. The advantage is we can get the values like username and password by different means for example Environment Variables making sure we don’t end up saving passwords in our code. To keep this example simple we have hardcoded the values inside ConfigMap.</p><pre>apiVersion: v1<br>kind: ConfigMap<br>metadata:<br>  name: postgres-configuration<br>  labels:<br>    app: postgres<br>data:<br>  POSTGRES_DB: awesomedb<br>  POSTGRES_USER: amazinguser<br>  POSTGRES_PASSWORD: perfectpassword</pre><p>Now we have our Configs and Storage Volume, we can define our StatefulSet that will use these,</p><pre>apiVersion: apps/v1<br>kind: StatefulSet<br>metadata:<br>  name: postgres-statefulset<br>  labels:<br>    app: postgres<br>spec:<br>  serviceName: &quot;postgres&quot;<br>  replicas: 1<br>  selector:<br>    matchLabels:<br>      app: postgres<br>  template:<br>    metadata:<br>      labels:<br>        app: postgres<br>    spec:<br>      containers:<br>      - name: postgres<br>        image: postgres:12<br>        envFrom:<br>        - configMapRef:<br>            name: postgres-configuration<br>        ports:<br>        - containerPort: 5432<br>          name: postgresdb<br>        volumeMounts:<br>        - name: pv-data<br>          mountPath: /var/lib/postgresql/data<br>      volumes:<br>      - name: pv-data<br>        persistentVolumeClaim:<br>          claimName: postgres-pv-claim</pre><p>The definition for Stateful Set is pretty much the same as Deployments. Here we have added two additional things. One we defined to load environment variables inside pod from the config map we defined earlier. Second the most important change, we defined our volume that will map to /var/lib/PostgreSQL/data inside our pod. This volume is defined using the persistent volume claim we used earlier.</p><p>Lastly, we can define a service resource to expose our database.</p><pre>apiVersion: v1<br>kind: Service<br>metadata:<br>  name: postgres-service<br>  labels:<br>    app: postgres<br>spec:<br>  ports:<br>  - port: 5432<br>    name: postgres<br>  type: NodePort <br>  selector:<br>    app: postgres</pre><p>To test these files, simply apply the files. Best to apply ConfigMap and Storage YAMLs first.</p><pre>$ sudo microk8s.kubectl apply -f file.yml</pre><p>You can check if everything is okay by using</p><p>If persistent volume claim gets stuck waiting, the stateful set will also get stuck as it can’t get its storage. So make sure both StorageClass and PersistentVolume are created properly.</p><p>If you are using Minikube, you might want to change the provisioner in StorageClass. Similarly, you can change it to EBS, etc for production setup.</p><p>This was an essential setup of all resources needed to host something like PostgreSQL databases. The same steps can be followed and used with MySQL, MongoDB or any other stateful application. If you have any doubts or thought do share it through the comments.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=24a2a192e962" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Nginx Ingress setup for kubernetes]]></title>
            <link>https://medium.com/@suyashmohan/nginx-ingress-setup-for-kubernetes-c95c71367967?source=rss-c5b92518ef24------2</link>
            <guid isPermaLink="false">https://medium.com/p/c95c71367967</guid>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[ingress]]></category>
            <category><![CDATA[minikube]]></category>
            <category><![CDATA[containers]]></category>
            <category><![CDATA[nginx]]></category>
            <dc:creator><![CDATA[Suyash Mohan]]></dc:creator>
            <pubDate>Wed, 22 Jan 2020 15:42:23 GMT</pubDate>
            <atom:updated>2020-01-22T15:45:29.181Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bYL46jvuTzhBeoswADSHiw.jpeg" /></figure><p>Recently I decided to give Kubernetes a try. I was avoiding it just for the reason of writing several long YAML files. I know I am a lazy guy. But once you get used to writing Kubernetes code, you discover the power of it. You do have to write a docker-compose file in the world of Docker but I have found writing YAML for Kubernetes more tedious but it might be worth it once you understand what more you can do with Kubernetes.</p><p>Like everyone the first thing I learned in Kubernetes is to create a deployment that serves a website and created a service around it. The first question that came in my mind afterward is how I can host 2 websites and route the traffic to them based on hostnames. Traditionally you would install something like NGINX and configure it to serve different websites or forward the requests to some other service. We can still use Nginx to route our request in Kubernetes but in a different way, by using Ingress.</p><p>In this article, we will have a look at Nginx Ingress Controller. Using this we can easily divert traffic based on hostnames inside our Kubernetes cluster essentially allowing us to host multiple projects on a single cluster. Isn’t it cool? Let me know in the comments if I am overselling it.</p><p>This post is also available on my blog:<br><a href="http://farzicoder.com/Nginx-Ingress-setup-for-kubernetes/">http://farzicoder.com/Nginx-Ingress-setup-for-kubernetes/</a></p><p>To test it out we will need to install and enable Nginx Ingress Controller. We will be using Minikiube to test on our local machine.</p><pre>$ minikube addons enable ingress</pre><p>Before we set up our Ingress controller we will need to set up deployments with corresponding services. Let’s Create a sample deployment and a service.</p><pre>apiVersion: apps/v1<br>kind: Deployment<br>metadata:<br>  name: first-deployment<br>spec:<br>  replicas: 3<br>  selector:<br>    matchLabels:<br>      app: first-deployment<br>  template:<br>    metadata:<br>      labels:<br>        app: first-deployment<br>    spec:<br>      containers:<br>      - name: hello-kubernetes<br>        image: paulbouwer/hello-kubernetes:1.5<br>        ports:<br>          - containerPort: 8080<br>        env:<br>        - name: MESSAGE<br>          value: Hello from first Deployment<br>---<br>apiVersion: v1<br>kind: Service<br>metadata:<br>  name: first-deployment<br>spec:<br>  type: ClusterIP<br>  ports:<br>  - port: 80<br>    targetPort: 8080<br>  selector:<br>    app: first-deployment</pre><p>Similarly, let’s create one more deployment just by replacing the word `first` in the above text with the word `second`. Being lazy sometimes does save some time. If you are too lazy to type that also, then you can find the code in my repo <a href="https://github.com/suyashmohan/simple_kubernetes_ingress_sample">https://github.com/suyashmohan/simple_kubernetes_ingress_sample</a></p><p>In case you are new to Kubernetes, you can deploy above YAML by using</p><pre>kubectl apply -f first-deployment.yml<br>kubectl apply -f second-deployement.yml</pre><p>You can check if everything is running fine by using</p><pre>$ kubectl get pods<br>$ kubectl get deployments<br>$ kubectl get services</pre><p>If everything is looking fine let’s continue and setup our Ingress controllers. If you are stuck at any error, you can comment below and I will try to help you out.</p><p>Setting up Ingress is pretty easily</p><pre>apiVersion: networking.k8s.io/v1beta1<br>kind: Ingress<br>metadata:<br>  name: test-ingress<br>  annotations:<br>    nginx.ingress.kubernetes.io/rewrite-target: /$1<br>spec:<br>  rules:<br>  - host: first.info<br>    http:<br>      paths:<br>      - path: /<br>        backend:<br>          serviceName: first-deployment<br>          servicePort: 8080<br>  - host: second.info<br>    http:<br>      paths:<br>      - path: /<br>        backend:<br>          serviceName: second-deployment<br>          servicePort: 8080</pre><p>Here we have defined a resource with kind of Ingress. The specifications for this resource includes rules which defines how traffic should be diverted to different services based on host. For e.g. if a request comes for first.info with root path, we will forward the request to first-deployment at port 8080. Same goes for second.info</p><p>Apply the above ingress resource</p><pre>$ kubectl apply -f ingress.yml</pre><p>Get the address for Ingress controller as following</p><pre>$ kubectl get ingress<br>NAME HOSTS ADDRESS PORTS AGE<br>test-ingress first.info,second.info 192.168.39.98 80 57s</pre><p>One advantage of Ingress is that it exposes the controller, so you don’t have to expose the services to the world. Testing this setup is also pretty easy.</p><p>We can either set up these hosts in /etc/host/ pointing to local address or we can just Host header in curl to test it out.</p><pre>$ curl -H ‘Host:first.info’ <a href="http://192.168.39.98/">http://192.168.39.98</a><br>$ curl -H ‘Host:second.info’ <a href="http://192.168.39.98/">http://192.168.39.98</a></pre><p>That’s it we have set up 2 services that get traffic based on Hostnames using Nginx Ingress Controller. I hope this article was helpful. Please share your thoughts and doubts using the comments below.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c95c71367967" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[util.promisify() in Node.js v8]]></title>
            <link>https://medium.com/@suyashmohan/util-promisify-in-node-js-v8-d07ef4ea8c53?source=rss-c5b92518ef24------2</link>
            <guid isPermaLink="false">https://medium.com/p/d07ef4ea8c53</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[Suyash Mohan]]></dc:creator>
            <pubDate>Sat, 17 Jun 2017 19:02:50 GMT</pubDate>
            <atom:updated>2017-06-17T19:03:43.165Z</atom:updated>
            <content:encoded><![CDATA[<p>If you have ever worked with Javascript you must have experienced callbacks. Javascript is known for its inbuilt asynchronous philosophy. From standard inbuilt modules to other frameworks and libraries, callbacks are everywhere. That’s how asynchronous programming is done in Javascript. But there is a dark side of Callbacks. It’s called <a href="http://callbackhell.com/">Callback Hells</a>.</p><blockquote>Originally posted on my blog <a href="http://farzicoder.com/util-promisify-in-Node-js-v8/">farzicoder.com</a></blockquote><p>The solution to callback hell is <a href="https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promises</a>. Instead of returning a value synchronously or passing result through a callback, we can return a promise that we are going to return some value or error in future.<br>To use promises there are a number of good libraries like <a href="https://github.com/petkaantonov/bluebird/">Bluebird</a> and <a href="https://github.com/kriskowal/q">Q</a>. But inbuilt modules and lot of other libraries don’t utilize Promises. Although these libraries are able to convert traditional callback methods into promises, we have an official way to promisify callback methods in Node v8. It is util.promisify. According to Node.js’s <a href="https://nodejs.org/dist/latest-v8.x/docs/api/util.html#util_util_promisify_original">documentation</a>:</p><p>util.promisify <em>takes a function following the common Node.js callback style, i.e. taking a (err, value) =&gt; … callback as the last argument, and returns a version that returns promises.</em></p><p>Take a look at traditional example:</p><pre>const fs = require(&#39;fs&#39;);</pre><pre>fs.readFile(&#39;./index.js&#39;, &#39;utf8&#39;, (err, text) =&gt; {<br>    if (err) {<br>        console.log(&#39;Error&#39;, err);<br>    } else {<br>        console.log(text);<br>    }<br>});</pre><p>Now this example can be written with Promises as following:</p><pre>const fs = require(&#39;fs&#39;);<br>const util = require(&#39;util&#39;);</pre><pre>const readFile = util.promisify(fs.readFile);</pre><pre>readFile(&#39;./index.js&#39;, &#39;utf8&#39;)<br>    .then((text) =&gt; {<br>        console.log(text);<br>    })<br>    .catch((err) =&gt; {<br>        console.log(&#39;Error&#39;, err);<br>    });</pre><p>Here we used util.promisify to convert fs.readFile into a promise based method. Now instead of taking a callback, the readFile method gives as a Promise. Promise has two methods, then and catch. then is used to get the result if the function call was successful. Whereas catch is used to catch the error if occurred. The advantage of Promise is that we can chain asynchronous calls without falling into callback hell.</p><h3>Async &amp; Await</h3><p>This is the most fun part I like. Promises can become a lot easier to deal with when combined with async &amp; await keywords. Even chaining of promises can look nasty when it gets bigger. Async &amp; Await make it look cleaner. Here is the above example with Async &amp; Await:</p><pre>const fs = require(&#39;fs&#39;);<br>const util = require(&#39;util&#39;);</pre><pre>const readFile = util.promisify(fs.readFile);</pre><pre>async function doFile() {<br>    try {<br>        const text = await readFile(&#39;./index.js&#39;, &#39;utf8&#39;);<br>        console.log(text);<br>    } catch (err) {<br>        console.log(&#39;Error&#39;, err);<br>    }<br>}</pre><pre>doFile();</pre><p>I hope you liked this post. Since I am just starting up with this blog, If you have any doubt or advice, please let me know through the comments.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d07ef4ea8c53" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>